[JUCE API] Keeping the State of Plugin While DAW Reopen

SangHoon You·2025년 4월 2일

JUCE API

목록 보기
4/17

If you a composer and all of your plugin disappear when you reopen your DAW , you would be really ambrassed. In order to deal with them, JUCE Framework provide us specific function.

class EulerDelayAudioProcessor  : public juce::AudioProcessor
{
public:
  void getStateInformation (juce::MemoryBlock& destData) override;
  void setStateInformation (const void* data, int sizeInBytes) override;
}

As you’d expect, get() saves the plugin information when the DAW is closed, and set() set up the plugin parameters when the DAW is restarted.


void EulerDelayAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{    

    juce::ValueTree statePlugin(JucePlugin_Name);
    
    // Parameter
    const juce::ValueTree stateParameter = mApvts.copyState();
    
    if(statePlugin.isValid())
    {
        statePlugin.appendChild(stateParameter, nullptr);
    }
    
    //Preset
    const juce::ValueTree statePreset = mPresetManager.getState();
    if(statePreset.isValid())
    {
        statePlugin.appendChild(statePreset, nullptr);
    }
}

statePlugin
-- stateParameter
-- statePreset

Parameter and PresetName are appended as a child to statePlugin. ID of statePlugin is set to the Plugin's Name.
Then, two ValueTrees with the IDs 'MyParameter' and 'Preset' are appended to statePlugin using the .appendChild() function. And getState() is a method I created, not part of JUCE API.


void EulerDelayAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{    
    std::unique_ptr<juce::XmlElement> xmlState = statePlugin.createXml();
    if (xmlState != nullptr)
    {
        copyXmlToBinary(*xmlState, destData);
        
    }
}

Now, final ValueTree, statePlugin is converted into an XML file. juce::ValueTree class has a createXML() method that convert ValueTree into XML file. If you just pass the XML file as a pointer to copyXmlToBinary(*xmlState, destData), it will save the data.


void EulerDelayAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
    std::unique_ptr<juce::XmlElement> xmlPlugin = getXmlFromBinary(data, sizeInBytes);
    if(xmlPlugin==nullptr)
    {
        return;
    }
}

First, to get an information of plugin setup when you reopen the DAW, you can use getXmlFromBinary(data, sizeInBytes) function to get it as an XML file.


void EulerDelayAudioProcessor::setStateInformation 
{
  const juce::ValueTree statePlugin = juce::ValueTree::fromXml(*xmlPlugin);
    
    if(statePlugin.isValid() == false || statePlugin.getType().toString()!= JucePlugin_Name)
    {
        return;
    }
    
    const juce::ValueTree statePreset = statePlugin.getChildWithName(mPresetManager.getIdState());
    if(statePreset.isValid())
    {
        mPresetManager.setByState(statePreset);
    }
    
    const juce::ValueTree stateParameter = statePlugin.getChildWithName(mApvts.state.getType());
    if(stateParameter.isValid())
    {
        mApvts.replaceState(stateParameter);
    }
}

It converts XML file into an ValueTree. And you need to distinguish the childs by their own ID(juce::Identifier). Important thing is that there's getChildWithName() function in class ValueTree, and the 'name' doesn't mean a juce::String, it's actually a juce::Identifier.


This process can protect your Plugin from loosing parameter and preset information when you reopen the DAW

profile
Audio Plugin Developer

0개의 댓글