Store string in AudioProcessorValueTreeState class


#1

Hi All!
Is there a way to store in the parameters of AudioProcessorValueTreeState a String parameter?
In the tutorial I see only

parameters.createAndAddParameter("gain", "Gain", String(), NormalisableRange<float> (0.0, 1.0), 1.0, nullptr, nullptr);

#2

Parameters need to be numerical but you can use a range of floats with an integer interval as an index into an array of strings. This is an extension of the technique to convert 0.0 to “Normal” and 1.0 to “Inverted” in the tutorial.

For example:

    const StringArray shapes ({ "Sine", "Saw", "Tri", "Square" });
    auto shapeToText = [=](float value) { return shapes[(int) (value + 0.5)]; };
    auto textToShape = [=](const String& text) { return (float) shapes.indexOf (text); };
    
    parameters.createAndAddParameter ("shapes", "Shape", String(),
                                      NormalisableRange<float> (0.0f, shapes.size() - 1.0f, 1.0f), 0.0f,
                                      shapeToText, textToShape);

You can then use the same list of strings in a ComboBox and use the AudioProcessorValueTreeState::ComboBoxAttachment class to connect your parameter and the ComboBox. You just need to make sure that the first item has an item ID of 1. Assuming you have a combo box called myShapeComoboBox in your UI you can do something like this to initialise its items.

myShapeComoboBox.addItemList ({ "Sine", "Saw", "Tri", "Square" }, 1);

#3

I need only to store a single string for each preset…no attachments.
Do I use the same technique?
EDIT: I also need to change the string value during plugin execution


#4

It sounds like you just want to store some text in your plugin state rather than needing a parameter. You can add child ValueTree objects to the state member within the AudioProcessorValueTreeState object. Then you can store pretty much anything you like, it just won’t be automatable by the host (but the host will store and retrieve the state on save/load).

Here’s a similar question on storing envelope points: