Saving and loading array of vectors?

Hello! I’m working on a module that records mouse movements. So far, it looks like this:

xy-front-panel

Recording starts when the user clicks in the square area and continues as the user drags around the mouse. Recording stops on mouse-up.

Everything is working great so far except for saving and loading of the recordings. I’m planning on saving those as x,y pairs, and my code for writing to the json file looks like this:

json_t *dataToJson() override
{
    json_t *root = json_object();
    json_t *recording_memory_json_array = json_array();

    for(Vec position : recording_memory)
    {
        json_t *xy_vec = json_array();
        json_array_append_new(xy_vec, json_real(position.x));
        json_array_append_new(xy_vec, json_real(position.y));

        json_array_append_new(recording_memory_json_array, xy_vec);
    }

    json_object_set(root, "recording_memory_data", recording_memory_json_array);
    json_decref(recording_memory_json_array);

Question: Where is this json saved? I’d like to take a peek at the saved file to see if the json that I’m outputting looks correct.

Quick note: I also intend to only save data after recording has been completed. I intend to use a simple flag for that.

It’s stored inside the patch—inside your whatever-patch-name.vcv file.

1 Like

You can take a look at my ARENA module which has pretty much mouse interaction stuff going on. Including mouse movement recording and visualization.

2 Likes

Thanks Dale and Ben,

It looks like I’m on the right path:


  {
      "id": 141,
      "plugin": "voxglitch",
      "version": "1.0.0",
      "model": "xy",
      "params": [],
      "rightModuleId": 142,
      "data": {
        "recording_memory_data": [
          [
            21.8129921,
            209.287399
          ],
          [
            21.8129921,
            209.287399
          ],
          [
            21.8129921,
            209.287399
          ],
          [
            21.8129921,
            209.287399
          ],
         ...

That output is exactly what I was looking for! I’m going to work on loading next. Ben, thanks for posting your code. I’ll refer to it if I get stuck. :dancing_women:

You don’t get to choose when dataTo/FromJson() is called. It is called when Rack needs to serialize the patch or module preset. You can of course serialize empty array or a nonexistent "recording_memory_data" property if for some reason you want to load an empty state when unserializing from the serialized data.

Thanks Andrew. I ended up allowing Rack to save my data as it feel fit. In other words, I didn’t end up adding any flag. I just put the code in dataToJson() and let VCV handle it.

Here’s my code for saving and loading the vector of x,y vectors. recording_memory is defined in the module class as vector<Vec> recording_memory;

json_t *dataToJson() override
{
    json_t *root = json_object();

    json_t *recording_memory_json_array = json_array();

    for(Vec position : recording_memory)
    {
        json_t *xy_vec = json_array();
        json_array_append_new(xy_vec, json_real(position.x));
        json_array_append_new(xy_vec, json_real(position.y));

        json_array_append_new(recording_memory_json_array, xy_vec);
    }

    json_object_set(root, "recording_memory_data", recording_memory_json_array);
    json_decref(recording_memory_json_array);

    return root;
}

void dataFromJson(json_t *root) override
{
    json_t *recording_memory_data = json_object_get(root, "recording_memory_data");

    if(recording_memory_data)
    {
	recording_memory.clear();
	size_t i;
	json_t *json_array_pair_xy;

	json_array_foreach(recording_memory_data, i, json_array_pair_xy)
        {
            float x = json_real_value(json_array_get(json_array_pair_xy, 0));
            float y = json_real_value(json_array_get(json_array_pair_xy, 1));

	    recording_memory.push_back(Vec(x,y));
	}
    }
}