How to set AudioProcessorValueTreeState parameter value

I’m trying to understand how to set the value of parameters manually.

In my example I just have 1 boolean “mute” parameter.

I’ve successfully hooked up my button to a valueTreeState with a ButtonAttachment, but I can’t figure out how I’m supposed to set the value of my parameter directly/manually. And if I use btnMute.setToggleState(true, false) the value tree doesn’t pick up the change.


//Create our value tree & a reference to our value
juce::AudioProcessorValueTreeState parameters;
std::atomic<float>* muteParameterValue = nullptr;

Processor constructor:

//Create our parameter and assign our parameter reference
parameters.createAndAddParameter (std::make_unique <juce::AudioParameterBool> ("mute", "Mute", 0));
muteParameterValue = parameters.getRawParameterValue("mute");

Process block:

//Get our boolean
bool mute = *muteParameterValue > 0.5f;


//Create our button and button attachment
juce::ImageButton btnMute;
juce::ScopedPointer<juce::AudioProcessorValueTreeState::ButtonAttachment> btnMuteAttachment;

Editor constructor:

//Add to component and hook up
btnMuteAttachment = new juce::AudioProcessorValueTreeState::ButtonAttachment(audioProcessor.parameters, "mute", btnMute);

Thanks a lot for any advice!

You put false as the second parameter there. That tells the button to not notify the host, and thus it will not broadcast to the parameter, either. You should pass true there.

Better yet (in my opinion), set the parameter itself use a sequence like this to both set the parameter and notify the host (and let the attachment notify the button):

AudioProcessorParameterWithID* pParam = parameters.getParameter ( "mute"  ); 
pParam->setValueNotifyingHost( paramValue );

(One reason I prefer this is because the editor - and thus the button - might not exist, but you may still want to set the parameter.)

1 Like

Just adding, the second parameter was changed to an enum ages ago. It should read juce::sendNotificationSync now. Seems like your compiler is way too forgiving, if you didn’t get at least a warning about an implicit bool to enum conversion.

But @HowardAntares hint is spot on to do such things in the processor and not from the GUI code.

1 Like

Fantastic! Thanks very much!

What are the purpose of those begin and end gesture methods? Something to do with thread safety?

Oops yep you’re right xcode does warn about it being depreciated. I dipped my toes in juce ages ago and now I’m dusting off my old projects… Thanks for pointing it out!

I’m also getting a warning for that ScopedPointer above but updating it to std::unique_ptr I got a No viable overloaded '=' at the assigment in the constructor, so I left it for now…

You can’t assign a pointer to a unique_ptr. Use btnMuteAttachment.reset (new juce::AudioProcessorValueTreeState::ButtonAttachment (audioProcessor.parameters, "mute", btnMute)) or btnMuteAttachment = std::make_unique<juce::AudioProcessorValueTreeState::ButtonAttachment> (audioProcessor.parameters, "mute", btnMute).

1 Like

Those are for the DAW’s parameter automation, to allow it to know when a parameter change is about to occur, and when the change has completed.

When moving sliders, for example, the attachment calls beginGesture() when the knob/slider starts moving, and calls endGesture() when it stops moving and the mouse is released. If you write automation for that parameter, you will then see the ramp of that change.

1 Like