# Cross Platform presets - any ideas?

**URL:** <https://forum.juce.com/t/cross-platform-presets-any-ideas/6449>\
**Category:** Audio Plugins\
**Created:** [February 21, 2011, 2:34am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449 "2011-02-21T02:34:42Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![andrewj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/andrewj/32/13428_2.png) [@andrewj](https://forum.juce.com/u/andrewj)\
**Post date:** [March 3, 2011, 9:31pm UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/21 "2011-03-03T21:31:53Z")

</div>

No worries Sean. This is still just the skeleton of the plug, so there’s not a lot of state information - but you should get the idea OK:

`void HyperdriveAudioProcessor::deSerialiseStateInformation(ScopedPointer<XmlElement> xmlState)
{
	// make sure that it's actually our type of XML object..
    if (xmlState->hasTagName ("MYPLUGINSETTINGS"))
    {
        // ok, now pull out our parameters..
        lastUIWidth = xmlState->getIntAttribute ("uiWidth", lastUIWidth);
        lastUIHeight = xmlState->getIntAttribute ("uiHeight", lastUIHeight);
		XmlElement* xmlChild[HYPERDRIVE_numPrograms];
		for (int i = 0; i<HYPERDRIVE_numPrograms; ++i) {
			xmlChild[i] = xmlState->getChildByName ("ProgramState_" + String(i+1));
			if (xmlChild[i]!=0)
			{
				programName[i] = xmlChild[i]->getStringAttribute ("ProgramName", programName[i]);
				drive[i] = (float) xmlChild[i]->getDoubleAttribute ("Drive", drive[i]);
				attack[i] = (float) xmlChild[i]->getDoubleAttribute ("Attack", attack[i]);
				release[i] = (float) xmlChild[i]->getDoubleAttribute ("Release", release[i]);
			}
		}
    }
}
void HyperdriveAudioProcessor::deSerialiseStateInformationAndNotify(ScopedPointer<XmlElement> xmlState)
{
	deSerialiseStateInformation(xmlState);
	// Notify host of changes to parameters
	sendParamChangeMessageToListeners (driveParam, param2Float(driveParam, drive[currentProgram]));
	sendParamChangeMessageToListeners (attackParam, param2Float(attackParam, attack[currentProgram]));
	sendParamChangeMessageToListeners (releaseParam, param2Float(releaseParam, release[currentProgram]));
}`

---

<div class="post-metadata">

**Author:** ![valhalladsp](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/valhalladsp/32/71_2.png) [@valhalladsp](https://forum.juce.com/u/valhalladsp)\
**Post date:** [March 7, 2011, 7:17pm UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/22 "2011-03-07T19:17:43Z")

</div>

Here’s my current preset code on the AudioProcessor side of things, as used in my upcoming plugin. I call this from a TextButton in my Editor code. Feel free to make suggestions about C++ correctness (seriously, I’m crappy at the ++ of C++).

[code]//==============================================================================  
void ValhallaRoom::getStateInformation (MemoryBlock& destData)  
{   
// create an outer XML element…  
XmlElement xmlState (T(“ValhallaRoom”));  
saveStateToXML(xmlState);

```
// then use this helper function to stuff it into the binary blob and return it..
copyXmlToBinary (xmlState, destData);

```

}

void ValhallaRoom::setStateInformation (const void\* data, int sizeInBytes)  
{  
// use this helper function to get the XML from this binary blob…  
XmlElement\* const xmlState = getXmlFromBinary (data, sizeInBytes);

```
if (xmlState != 0)
{	
	loadStateFromXML(xmlState);
    delete xmlState;
}

```

}

void ValhallaRoom::setCurrentPresetName(String& newName)  
{  
presetName = newName;  
}

String ValhallaRoom::getCurrentPresetName()  
{  
return presetName;  
}

void ValhallaRoom::saveStateToXML(XmlElement& xmlState)  
{  
// add some attributes to it…  
xmlState.setAttribute (T(“pluginVersion”), 1);  
xmlState.setAttribute(T(“presetName”), presetName);  
for(int j=0;j\<kNumberOfParameters;j++)  
xmlState.setAttribute(getParameterName(j), getParameter(j));  
}

void ValhallaRoom::loadStateFromXML(XmlElement\* xmlState)  
{  
// check that it’s the right type of xml…  
if (xmlState-\>hasTagName (T(“ValhallaRoom”)))  
{  
presetName = xmlState-\>getStringAttribute(T(“presetName”));  
// ok, now pull out our parameters…  
for(int j=0;j\<kNumberOfParameters;j++)  
setParameter(j, (float) xmlState-\>getDoubleAttribute (getParameterName(j), getParameter(j)));

```
	sendChangeMessage();
}

```

}

void ValhallaRoom::getStateAsText (String& destStr)  
{  
// create an outer XML element.  
XmlElement xml (T(“ValhallaRoom”));  
saveStateToXML(xml);   
destStr = xml.createDocument (String::empty, true, false);  
}

void ValhallaRoom::setStateFromText (const String& stateStr)  
{  
XmlDocument doc(stateStr);  
XmlElement\* xmlState = doc.getDocumentElement();  
if (xmlState)  
{  
loadStateFromXML(xmlState);  
delete xmlState;  
}  
}

bool ValhallaRoom::saveStateToFile(File newFile)  
{  
// create an outer XML element…  
XmlElement xmlState (T(“ValhallaRoom”));  
saveStateToXML(xmlState);  
return (xmlState.writeToFile(newFile, T(""), “UTF-8”, 60));  
}

void ValhallaRoom::setStateFromFile(File newFile)  
{  
XmlElement\* xmlState = XmlDocument::parse (newFile);

```
if (xmlState != 0 && xmlState->hasTagName ("ValhallaRoom"))
{
	loadStateFromXML(xmlState);
	delete xmlState;
}

```

}  
[/code]

---

<div class="post-metadata">

**Author:** ![bdawson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bdawson/32/14755_2.png) [@bdawson](https://forum.juce.com/u/bdawson)\
**Post date:** [November 17, 2015, 5:31am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/23 "2015-11-17T05:31:31Z")

</div>

Bringing this thread back to life!&nbsp; ![devil](http://www.juce.com/sites/all/libraries/ckeditor/plugins/smiley/images/devil_smile.gif "devil")

&nbsp;

Would storing preset information as XML still be the best approach for cross-platform&nbsp;presets using the JUCE4 framework?&nbsp;

I'm working on a VST/VST3/AU Audio plug-in and moving into the preset mechanism phase.

Any insight regarding this topic would be most appreciated.

&nbsp;

Additionally, thank you Andrew J for your snippets of code (four years later) and other contributors to this thread; I'll be experimenting with xml techniques tomorrow and I have found this thread most helpful.

&nbsp;

Cheers,  
B  
&nbsp;

---

<div class="post-metadata">

**Author:** ![andrewj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/andrewj/32/13428_2.png) [@andrewj](https://forum.juce.com/u/andrewj)\
**Post date:** [November 17, 2015, 5:54am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/24 "2015-11-17T05:54:07Z")

</div>

No worries, but 4 years later and I still never finished that plugin! (have built up a decent library of other code though)

---

<div class="post-metadata">

**Author:** ![bdawson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bdawson/32/14755_2.png) [@bdawson](https://forum.juce.com/u/bdawson)\
**Post date:** [November 17, 2015, 5:42pm UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/25 "2015-11-17T17:42:45Z")

</div>

Andrew,

the code that you have completed within the past four years:&nbsp;have any of these programs included a preset system?

If so, was the approach XML-based similar to this thread?

&nbsp;

Cheers,  
B

---

<div class="post-metadata">

**Author:** ![andrewj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/andrewj/32/13428_2.png) [@andrewj](https://forum.juce.com/u/andrewj)\
**Post date:** [November 17, 2015, 10:15pm UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/26 "2015-11-17T22:15:54Z")

</div>

No I haven't.

Did you know&nbsp;that JUCE 4 has new audio parameter classes and something to hold a plugin state in a ValueTree (which has XML methods)? Take a look at&nbsp;AudioProcessorValueTreeState...

---

<div class="post-metadata">

**Author:** ![bdawson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bdawson/32/14755_2.png) [@bdawson](https://forum.juce.com/u/bdawson)\
**Post date:** [November 18, 2015, 4:34am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/27 "2015-11-18T04:34:42Z")

</div>

I did not know. After reading the documentation, it seems very useful and simplifies parameter callback for sure! For my current project, I do have a system in place -- however I will keep the&nbsp;AudioProcessorValueTreeState in mind for future projects.&nbsp;

The&nbsp;AudioProcessorValueTreeState doesn't seem to address importing and exporting parameter settings as xml either, which is my current hurdle (particularly importing). I will make a seperate post in that regard.

&nbsp;

Cheers,  
B

---

<div class="post-metadata">

**Author:** ![bdawson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bdawson/32/14755_2.png) [@bdawson](https://forum.juce.com/u/bdawson)\
**Post date:** [November 18, 2015, 4:54am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/28 "2015-11-18T04:54:36Z")

</div>

```
 
```

Attached below&nbsp;is a super-shortened version of Andrew J's deSerialiseStateInformation function. The 'If' statement above is when my plug-in seems to crash.&nbsp;The above functions are called in the stateStateInformation and getStateInformation respectively.&nbsp;  
  
Perhaps I am not addressing the XML document correctly? Perhaps I am not saving the XML document correctly?&nbsp;Perhaps there's a bug?  
Any thoughts would be _very&nbsp;_helpful.&nbsp;

Cheers,  
B

```
void HyperdriveAudioProcessor::deSerialiseStateInformation(ScopedPointer<XmlElement> xmlState) 
{ 
     // make sure that it's actually our type of XML object.. 
     if (xmlState->hasTagName ("DSPMODULESETTINGS")) /* The program is crashing when it reaches this line. */ 
     { 
          // something happens here 
     } 
}
```

```
XmlElement HyperdriveAudioProcessor::serialiseStateInformation()
{
    XmlElement xml ("DSPMODULESETTINGS");
    
    for (int param = 0; param < NumParams; ++param)
    {
        String thisAttr = parameterMap.GetParameterName (param);
        double value    = getLinearParameter (param);
        xml.setAttribute (thisAttr, value);
    }
    
    return xml;
}
```

When my 'Preset Load' button is pressed, this is the code block that executes:

```
        String fyleSpec = ("/Users/bDawson/presets.xml");
        File fyle(fyleSpec);
        
        if (fyle.existsAsFile())
        {
            XmlDocument xmlDoc(fyle);
            deSerialiseStateInformation(xmlDoc.getDocumentElement());   // CRASHING
        }
     
        uiDirty = true;
```

My saved xml file, which I am attempting to import to the program to set my parameters.

```
<?xml version="1.0" encoding="UTF-8"?>
<DSPMODULESETTINGS Depth="0" Dry/Wet="0" ComboBox="0" Preset Save="1" Preset Load="0"/>
```

---

<div class="post-metadata">

**Author:** ![andrewj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/andrewj/32/13428_2.png) [@andrewj](https://forum.juce.com/u/andrewj)\
**Post date:** [November 18, 2015, 6:03am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/29 "2015-11-18T06:03:48Z")

</div>

You can get the state as a ValueTree object and thus has methods to convert to XML

---

<div class="post-metadata">

**Author:** ![andrewj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/andrewj/32/13428_2.png) [@andrewj](https://forum.juce.com/u/andrewj)\
**Post date:** [November 18, 2015, 6:17am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/30 "2015-11-18T06:17:05Z")

</div>

I can't remember the rules for valid XML attribute names off the top of my head, but the slash in dry/wet and the space in preset load & preset save might be a problem.&nbsp;

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [November 18, 2015, 6:33am UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/31 "2015-11-18T06:33:38Z")

</div>

If a pointer might be nullptr (like here due to the failing xml parsing) you should check before using it :

```auto
 if (xmlState != nullptr && xmlState->hasTagName ("DSPMODULESETTINGS"))
```

---

<div class="post-metadata">

**Author:** ![bdawson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bdawson/32/14755_2.png) [@bdawson](https://forum.juce.com/u/bdawson)\
**Post date:** [November 18, 2015, 5:15pm UTC](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449/32 "2015-11-18T17:15:25Z")

</div>

fellow jucers,

Your insight has helped solve my current crashing issue. Yesterday's frusteration solved this morning in an instance (naturally).&nbsp;

&nbsp;

So here's what changed:

```
<?xml version="1.0" encoding="UTF-8"?> <DSPMODULESETTINGS Depth="0" Dry/Wet="0" ComboBox="0" Preset Save="1" Preset Load="0"/>
```

Now looks like this:

```
<?xml version="1.0" encoding="UTF-8"?> <DSPMODULESETTINGS Depth="0" DryWet="0" ComboBox="0" PresetSave="1" PresetLoad="0"/>
```

&nbsp;

The '/' character and spaces that crept their way into my xml document was causing the crashes. Now everything parses as expected.

&nbsp;

Thank you!  
B

[Previous page](https://forum.juce.com/t/cross-platform-presets-any-ideas/6449.md?page=1)
