Plugin state with ValueTree


#1

Hi !

Im trying to store [ValueTree] content (contains only simple data) in a plugin state. Having hard time here as there is no data to be recalled after all .

Trying to search the forum ,but cant find any clue so far .

 

Here is the code im testing at the moment .

 

ValueTree temp("DataOfSomeSort");
const Identifier key = "cell0";
void Processor::getStateInformation(MemoryBlock& destData) {
    temp.setProperty(key, 199, nullptr);
    ScopedPointer<XmlElement> e = temp.createXml();
    copyXmlToBinary(*e.get(), destData);
    DBG(temp.getProperty(key).toString());
}
void Processor::setStateInformation(const void* data, int sizeInBytes) {
    ScopedPointer<XmlElement> xml(getXmlFromBinary(data, sizeInBytes));
    if(xml == nullptr) return;
    temp.fromXml(*xml.get());
    if(temp.hasProperty(key))
        DBG(temp.getProperty(key).toString());
    else
        DBG("has no property of a given name");
    DBG("num childs" << temp.getNumChildren()); // yields 0
    DBG("num props" << temp.getNumProperties()); // yields 0
}

 

Could someone share some hints about the proper way of serialising and storing [ValueTree] in a plugin state ?


#2

getNumChildren is returning 0 because you have not added a child node to the tree.

Have you tried adding a child then setting the property on the child node?

Use XmlElement->writeToFile to check if you're actually creating something. 

I get your frustration with the ValueTree - it took me a while, and some trial and error, to really get to grips with it. 

I think a tutorial on best practice for use of the ValueTree would be a valuable addition. 

 


#3

Hi Mark !

I have still a lot of sympathy for [ValueTree], however frustration may come really soon  :D

yeah ive tried (writeToFile) and data is definitelly there ...

actually ive spoted the mistake few minutes ago.

 

i should assign whats returned (fromXml)

temp = ValueTree::fromXml(*xml);

not


temp.fromXml(*xml);

... this way it works as expected, just with one additional symptom ... all is turned into a [Strings], so i think i need different approach ,as rebuilding [ValueTree] node by node myself is the last thing i would go for if i have to ... 

 

 


#4

The best way to store a ValueTree for this kind of purpose is as binary, with writeToStream()

BTW I'd really recommend using AudioProcessorValueTreeState instead - we did a lot of hard work in there for you already!


#5

hah ! good to know Jules 

... are there any resources i can learn from about [AudioProcessorValueTreeState] ?


#6

Don't think we have an example for it yet, you'll just need to read the docs and look at the class.


#7

yes i will , thank you 


#8

ah ok ,its a class that manages parameters state ...

well my [ValueTree] is a data structure that holds various datas about many aspects of the plugin behaviour, most of them are not related to [GUI] directly , and its not about to be exposed as a plugin parameter interface for a daw ... the data is interpreted in the process while performing and distributed conditionally. 


#9

It work nicely with data streams 


ValueTree temp("DataOfSomeSort");
const Identifier key = "cell0";
void Processor::getStateInformation (MemoryBlock& destData){
    temp.setProperty(key, 199, nullptr);
    MemoryOutputStream ms;
    temp.writeToStream(ms);
    destData = ms.getMemoryBlock();
}
void Processor::setStateInformation (const void* data, int sizeInBytes){
    if(sizeInBytes == 0) return;
    MemoryInputStream is(data,sizeInBytes,true);
    temp = ValueTree::readFromStream(is);
    DBG(temp.getProperty(key).toString());
}