# std::vector\<juce::AudioProcessorValueTreeState\> presets;

**URL:** <https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579>\
**Category:** Audio Plugins\
**Created:** [October 31, 2022, 5:20pm UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579 "2022-10-31T17:20:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sluijten](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sluijten/32/12213_2.png) [@sluijten](https://forum.juce.com/u/sluijten)\
**Post date:** [October 31, 2022, 5:20pm UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/1 "2022-10-31T17:20:57Z")

</div>

Is it workable to use a vector of AudioProcessorValueTreeState to store presets?  
If so, how would you read/write all presets as one xml document, to and from a file?

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [October 31, 2022, 5:21pm UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/2 "2022-10-31T17:21:56Z")

</div>

No. There is meant to be one AudioProcessorValueTreeState.

The state data itself is a ValueTree; you could have a `std::vector<ValueTree>`.

---

<div class="post-metadata">

**Author:** ![sluijten](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sluijten/32/12213_2.png) [@sluijten](https://forum.juce.com/u/sluijten)\
**Post date:** [October 31, 2022, 5:36pm UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/3 "2022-10-31T17:36:31Z")

</div>

OK, that makes sense.  
So you could store plugin presets in a `std::vector<juce::ValueTreeState>`, and then update the AudioProcessorValueTreeState according to the selected preset?  
If so, the second question remains: how would you read/write a `std::vector<juce::ValueTreeState>` as one xml document, to and from a file?  
Guess I am looking for an xml merge and split…

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [October 31, 2022, 5:58pm UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/4 "2022-10-31T17:58:17Z")

</div>

I would make one “parent” ValueTree and append each preset as a child of it.

For example:

```cpp
std::vector<juce::ValueTree> presets;

void savePresets (juce::File destFile)
{
    juce::ValueTree presetData { "MyPluginPresets" };

    for (const auto& preset : presets)
        presetData.appendChild (preset, nullptr);

    const auto xml = presetData.createXml();

    xml->writeTo (destFile);
}

```

and to load them do the inverse, load a single ValueTree from the file, then loop through all its child trees.

---

<div class="post-metadata">

**Author:** ![Fandusss](https://avatars.discourse-cdn.com/v4/letter/f/a4c791/32.png) [@Fandusss](https://forum.juce.com/u/Fandusss)\
**Post date:** [November 1, 2022, 4:16am UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/5 "2022-11-01T04:16:28Z")

</div>

This is worth watching as a primer: [Preset Manager in JUCE || Akash Murthy - YouTube](https://youtu.be/YwAtWuGA4Cg)

---

<div class="post-metadata">

**Author:** ![jellebakker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jellebakker/32/17053_2.png) [@jellebakker](https://forum.juce.com/u/jellebakker)\
**Post date:** [November 1, 2022, 7:43am UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/6 "2022-11-01T07:43:47Z")

</div>

If one ValueTree represents one preset, it could make sense to store each preset as an individual file on disk, instead of in one large xml file.

You can use [ValueTree::toString](https://docs.juce.com/master/classValueTree.html#aaefd76c438284755ed1fc0cc77257984) and [ValueTree::fromXml](https://docs.juce.com/master/classValueTree.html#aee2ead4129fe7622ab69614da6294969) to serialize the tree and then use the juce [File](https://docs.juce.com/master/classFile.html) class to write and read to files on disk.  
To update the current preset, use [AudioProcessorValueTreeState::replaceState](https://docs.juce.com/master/classAudioProcessorValueTreeState.html#afabb748808e24bdc2fa06dba23bf6bb3).

You could indeed create a vector of ValueTree’s representing all presets, however in some cases creating a vector of Files is more suitable. In that case converting to ValueTree would happen at the moment when a user selects the preset. If you have thousands of presets, this way you don’t block the GUI while creating all the ValueTree’s.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 1, 2022, 8:36am UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/7 "2022-11-01T08:36:59Z")

</div>

The AudioProcessorValueTreeState has a public `state` ValueTree that represents the state of all parameters and whatever you add to it.  
It is trivial to store that state in a savePreset method and to assign it balc in a loadPreset Method.  
How you organise those presets is up to you, could be one file or multiple files. Just be aware that a ValueTree can only be child to one ValueTree, so you need to use createCopy():

```auto
void savePreset()
{
    auto preset = apvts.state.createCopy();
    presets.appendChild (preset, nullptr);
}

void loadPreset (int index)
{
    auto preset = presets.getChild (index).createCopy();
    apvts.replaceState (preset);
}

```

---

<div class="post-metadata">

**Author:** ![sluijten](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sluijten/32/12213_2.png) [@sluijten](https://forum.juce.com/u/sluijten)\
**Post date:** [November 1, 2022, 9:03am UTC](https://forum.juce.com/t/std-vector-juce-audioprocessorvaluetreestate-presets/53579/8 "2022-11-01T09:03:38Z")

</div>

All good input, thanks! 👍 👍 👍
