How to use AudioProcessorValueTreeState addParameterListener?


How does one use AudioProcessorValueTreeState::addParameterListener ? The only two bits of code I can find on this forum use something like:

mState->addParameterListener(id, this);

But my compiler objects to the ‘this’ pointer as it points to my PluginProcessor, not an AudioProcessorValueTreeState::Listener. But how to create an instance of AudioProcessorValueTreeState::Listener to point at when it’s a pure virtual class?


Your AudioProcessor class needs to inherit from AudioProcessorValueTreeState::Listener and implement its parameterChanged() method. You can then register your AudioProcessor as a listener to the AudioProcessorValueTreeState using the code you posted and the parameterChanged function will be called when a parameter is changed.


Ed, thank you for your answer. I’ve got it working now.


Correction: almost working! I’ve realised that parameterChanged() doesn’t seem to be called when my plug-in is first initialised - I have to manually move the appropriate GUI controls. Parameter values are correctly saved and loaded by the host so shouldn’t parameterChanged() be called when I set the AudioProcessorValueTreeState in setStateInformation()?

parameters->state = ValueTree::fromXml (*xmlState);


The setStateInformation() method will be called when you load a new state but it won’t necessarily be called when you initially load your plugin. You should really do the initial parameter setup in your constructor - what are you doing in your listener callback that you need to happen when the plugin is loaded?



Sorry Ed, I explained that wrong. I mean that parameterChanged() isn’t called when setStateInformation() is called. So the parameters are set correctly and the GUI controls set to the right positions but the variables I’m calculating in parameterChanged() aren’t set, so I assume parameterChanged() isn’t called. It is called when I move the GUI controls manually.


Just tested this out in my own project by setting an AudioProcessorValueTreeState object to a new ValueTree in the setStateInformation() method and parameterChanged() was called as expected. Can you put a breakpoint in the parameterChanged() method to see when it is called? Also, are you registering your AudioProcessor class as a listener to the AudioProcessorValueTreeState object before the setStateInformation() method is called (i.e. in the constructor of your AudioProcessor class)?



Yes, I’ve got it worked out now. ParameterChanged() isn’t called if the parameter values restored in setStateInfromation() are the same as the default values set in addAndCreateParameter(), which makes 100% sense. It’s just that I’d assumed that it would be called when the default values were set initially.

So it’s working now. Thanks for helping Ed.