Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VolumeAndPan: Made vol/pan parameters undoable #165

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ VolumeAndPanPlugin::VolumeAndPanPlugin (PluginCreationInfo info, bool isMasterVo
addAutomatableParameter (panParam = new PanAutomatableParameter ("pan", TRANS("Pan"), *this, { -1.0f, 1.0f }));
}

volParam->attachToCurrentValue (volume);
panParam->attachToCurrentValue (pan);
volParam->setParameter (volume, juce::dontSendNotification);
panParam->setParameter (pan, juce::dontSendNotification);
}

VolumeAndPanPlugin::VolumeAndPanPlugin (Edit& ed, const juce::ValueTree& v, bool isMaster)
Expand All @@ -134,9 +134,6 @@ VolumeAndPanPlugin::VolumeAndPanPlugin (Edit& ed, const juce::ValueTree& v, bool
VolumeAndPanPlugin::~VolumeAndPanPlugin()
{
notifyListenersOfDeletion();

volParam->detachFromCurrentValue();
panParam->detachFromCurrentValue();
}

juce::ValueTree VolumeAndPanPlugin::create()
Expand Down Expand Up @@ -256,15 +253,15 @@ void VolumeAndPanPlugin::setVolumeDb (float vol)

void VolumeAndPanPlugin::setSliderPos (float newV)
{
volParam->setParameter (juce::jlimit (0.0f, 1.0f, newV), juce::sendNotification);
volume = juce::jlimit (0.0f, 1.0f, newV);
}

void VolumeAndPanPlugin::setPan (float p)
{
if (p >= -0.005f && p <= 0.005f)
p = 0.0f;

panParam->setParameter (juce::jlimit (-1.0f, 1.0f, p), juce::sendNotification);
pan = juce::jlimit (-1.0f, 1.0f, p);
}

PanLaw VolumeAndPanPlugin::getPanLaw() const noexcept
Expand Down Expand Up @@ -310,9 +307,27 @@ void VolumeAndPanPlugin::restorePluginStateFromValueTree (const juce::ValueTree&
copyPropertiesToNullTerminatedCachedValues (v, cvsFloat);
copyPropertiesToNullTerminatedCachedValues (v, cvsInt);
copyPropertiesToNullTerminatedCachedValues (v, cvsBool);
}

for (auto p : getAutomatableParameters())
p->updateFromAttachedValue();
void VolumeAndPanPlugin::valueTreePropertyChanged (juce::ValueTree& v, const juce::Identifier& i)
{
Plugin::valueTreePropertyChanged (v, i);

if (v == state)
{
if (i == IDs::volume)
{
volume.forceUpdateOfCachedValue();
volParam->setParameter (juce::jlimit (0.0f, 1.0f, static_cast<float> (volume)),
juce::sendNotification);
}
else if (i == IDs::pan)
{
pan.forceUpdateOfCachedValue();
panParam->setParameter (juce::jlimit (-1.0f, 1.0f, static_cast<float> (pan)),
juce::sendNotification);
}
}
}

}} // namespace tracktion { inline namespace engine
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ class VolumeAndPanPlugin : public Plugin
void restorePluginStateFromValueTree (const juce::ValueTree&) override;

//==============================================================================
juce::CachedValue<float> volume, pan;
juce::CachedValue<bool> applyToMidi, ignoreVca, polarity;
juce::CachedValue<int> panLaw;

// NB the units used here are slider position
AutomatableParameter::Ptr volParam, panParam;

private:
juce::CachedValue<float> volume, pan;

float lastGainL = 0.0f, lastGainR = 0.0f, lastGainS = 0.0f, lastVolumeBeforeMute = 0.0f;

juce::CriticalSection vcaTrackLock;
Expand All @@ -81,6 +82,8 @@ class VolumeAndPanPlugin : public Plugin

void refreshVCATrack();

void valueTreePropertyChanged (juce::ValueTree&, const juce::Identifier&) override;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VolumeAndPanPlugin)
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
,--. ,--. ,--. ,--.
,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2018
'-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
| | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
`---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com

Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
*/

#if TRACKTION_UNIT_TESTS && ENGINE_UNIT_TESTS_VOLPANPLUGIN

namespace tracktion { inline namespace engine
{

//==============================================================================
//==============================================================================
class VolPanTests : public juce::UnitTest
{
public:
VolPanTests()
: juce::UnitTest ("VolumeAndPan Plugin", "tracktion_engine")
{
}

void runTest() override
{
runUndoTests();
}

private:
void runUndoTests()
{
auto& engine = *Engine::getEngines()[0];
juce::ValueTree editState;

beginTest ("Undo/redo");
{
auto edit = Edit::createSingleTrackEdit (engine);
auto& um = edit->getUndoManager();
um.setMaxNumberOfStoredUnits (30000, 30); // Ensure this isn't the default "single transaction"
um.clearUndoHistory();

auto p = getAudioTracks (*edit)[0]->getVolumePlugin();

// Starting new undo transaction
um.beginNewTransaction();
expect (! um.canUndo());
expect (! um.canRedo());

expectWithinAbsoluteError (p->getVolumeDb(), 0.0f, 0.001f);
p->setVolumeDb (-60.0f);
expectWithinAbsoluteError (p->getVolumeDb(), -60.0f, 0.001f);
expect (um.canUndo());

um.beginNewTransaction();
expectWithinAbsoluteError (p->getPan(), 0.0f, 0.001f);
p->setPan (1.0f);
expectWithinAbsoluteError (p->getPan(), 1.0f, 0.001f);
expect (um.canUndo());

um.undo();
expect (um.canUndo());
expect (um.canRedo());

expectWithinAbsoluteError (p->getVolumeDb(), -60.0f, 0.001f);
expectWithinAbsoluteError (p->getPan(), 0.0f, 0.001f);

um.undo();
expect (! um.canUndo());
expect (um.canRedo());

expectWithinAbsoluteError (p->getVolumeDb(), 0.0f, 0.001f);
expectWithinAbsoluteError (p->getPan(), 0.0f, 0.001f);

um.redo();
expect (um.canRedo());
expectWithinAbsoluteError (p->getVolumeDb(), -60.0f, 0.001f);
expectWithinAbsoluteError (p->getPan(), 0.0f, 0.001f);

um.redo();
expectWithinAbsoluteError (p->getVolumeDb(), -60.0f, 0.001f);
expectWithinAbsoluteError (p->getPan(), 1.0f, 0.001f);

edit->flushState();
editState = edit->state;
}

beginTest ("Load saved state");
{
Edit edit ({ engine, editState, ProjectItemID::createNewID (0) });
auto p = getAudioTracks (edit)[0]->getVolumePlugin();
expectWithinAbsoluteError (p->getVolumeDb(), -60.0f, 0.001f);
expectWithinAbsoluteError (p->getPan(), 1.0f, 0.001f);
}
}
};

static VolPanTests volPanTests;


}} // namespace tracktion { inline namespace engine

#endif
1 change: 1 addition & 0 deletions modules/tracktion_engine/tracktion_engine_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ using namespace std::literals;
#include "plugins/internal/tracktion_TextPlugin.cpp"
#include "plugins/internal/tracktion_VCA.cpp"
#include "plugins/internal/tracktion_VolumeAndPan.cpp"
#include "plugins/internal/tracktion_VolumeAndPan.test.cpp"
#include "plugins/internal/tracktion_InternalPlugins.test.cpp"

#include "plugins/effects/tracktion_Chorus.cpp"
Expand Down