Cabbage Logo
Back to Cabbage Site

Ideas for saving generic data in DAW sessions

Following from this discussion, I’ve started working on a system for saving generic data whenever a plugin is saved as part of a DAW session. We declare as JSON widget:

<Cabbage>
json channel("jsonData")
form ...
</Cabbage>

That channel’s data will be written to the plugin state memory whenever a session is saved. To make it easy to write and write to that string I’ve started writing a few utility opcodes. Here is an example of how they work.

instr WriteJSON
    //overwrite existing data
    iRes writeJSONToChannel 0, "jsonData", "{ \"happy\": true, \"pi\": 3.141}"

    if iRes == -1 then
        prints "Couldn't write JSON data"
    endif
endin

instr UpdateJSON
    //updated existing data
    iRes writeJSONToChannel 1, "jsonData", {{
        { 
        "happy": true, "pi": 1.141, "test": 12
        }
    }}

    if iRes == -1 then
        prints "Couldn't write JSON data"
    endif

endin

instr ShowJSON
    prints chnget:S("jsonData")
endin

instr GetJSONFloatValue
    kVal getJSONFloatValue "jsonData", "pi"
    printk2 kVal
endin

I just wonder if the opcodes names are a little long? Note these opcodes will only be accessible in Cabbage. They’ll be baked into the code-base. I will add string, and array getters too. I’m just putting it out there now so we can decide on opcodes names and other use case I may have overlooked. I prefer to use of camelCase naming because a) it differs from the majority of Csound opcodes, which helps make it clear that these don’t belong to Csound, and b) I find them easier to read
:wink:

Let me know what you think. .

It might be cleaner to avoid the need to declare of a json widget altogether. And simply let Cabbage communicate to the opcodes directly. That would also avoid any potential problems with maximum strings size for Csound channels. I shall look into this a little and see if it’s workable. The idea being that we wouldn’t need to pass a channel to the json opcodes. They would keep track of an internal JSON string instead.

Let me introduce a new set of Cabbage opcodes that will let users save generic data across DAW sessions.


Basic read/write opcodes
These opcodes perform basic reading and writing of ‘flat’ JSON object.


iRes writeStateData iMode, SJSONData

Will write JSON data to the plugins internal state if it saved in a DAW session. It lets you overwrite the existing data, or update it.

SData readStateData
Returns a string that containing all current state data


Value reading/writing
These opcodes let you read write and access values from the JSON data structure. While writeStateData (see above) lets you write chunks of JSON data in one go, it also requires you to construct your JSON data in Csound which is a little cumbersome. These opcodes are probably better for filling the data structure.


iRes setStateValue SKeyName, SData
kRes setStateValue SKeyName, kData
iRes setStateValue SKeyName, iData
iRes setStateValue SKeyName, SData[]
kRes setStateValue SKeyName, kData[]

iRes will return -1 one if there were any errors.


iVal getStateValue SKeyName
kVale getStateValue SKeyName
SVal getStateValue SKeyName
iVal[] getStateValue SKeyName
kVal[] getStateValue SKeyName
SVal[] getStateValue SKeyName


There are available in the latest beta build. Note they are only useful if saving session data within a DAW. I’ve also added a simple JSONOpcodes.csd file to the Misc examples folder.