Hello!
I have a plugin that uses AudioProcessorValueTreeState to manage its state, with a bunch of sliders in the UI.
I’d like to add something like an “INIT” button to reset all sliders to their initial position; each parameter has a default value which is set it in createParameterLayout(), and can be easily recalled by double-clicking the sliders).
But how can I achieve the same result from my code?
Is there any built-in mechanism to do this? I took a look at the docs but couldn’t find what I was looking for.
I tried to manually access my parameters and then call something like
From the documentation of AudioProcessorParameter::setValue:
If you want to set the value of a parameter internally, e.g. from your
editor component, then don’t call this directly - instead, use the
setValueNotifyingHost() method, which will also send a message to
the host telling it about the change. If the message isn’t sent, the host
won’t be able to automate your parameters properly.
For the records, after doing this,the next problem was that getDefaultValue is also private so it can’t be accessed; I found some useful tips on this thread and managed to get it working.
How about this approach? In short: convert the APVTS to XML, manipulate the XML so all current values are set to their default values, then replace the APVTS with the modified XML.
// in this example, we have named the Processor's AudioProcessorValueTreeState object as "parameters"
ValueTree state = parameters.copyState(); // grab a copy of the current parameters Value Tree
std::unique_ptr<XmlElement> tempXml (state.createXml()); // convert parameters Value Tree to an XML object
// iterate through each "PARAM" element in XML, and overwrite values with their defaults
forEachXmlChildElementWithTagName (*tempXml, child, "PARAM")
{
float defaultValue = parameters.getParameter(child->getStringAttribute("id"))->getDefaultValue();
child->setAttribute("value", defaultValue);
}
parameters.replaceState (ValueTree::fromXml (*tempXml));