Possible timing issues with OSC

I am sending OSC messages from Supercollider to Processing. I want to use the OSC messages to populate two arrays storing x and y coordinates for certain shapes. My idea was to send three messages: an index, an x position, and a y position. The index would indicated where to populate the x and y in the array.

My ideas was to use global variables (x,y,index) that the OSC message would change. I can, however, see a flaw with this idea. If two OSC messages are sent at the same time it could cause a conflict. In addition, something feels wrong about my whole approach. I’m not sure if using global variables is the correct approach.

Does anyone have insight or experience into this? Thank you in advance for any help!

I’ll preface this by saying that I know nothing about OSC.

The three messages idea seems bad – is there any guarantee that the messages will arrive in the same order as they were dispatched (does your code handle this possibility?).

Could you pack the index, x and y into a single (32 bit?) integer so as to deal with everything in one message? If the index, x and y ranges can’t be represented as fields that fit into an integer frame then format the message as a BLOB and deliver all of the data in one block.

Hello @dom,

ChatGPT SuperCollider code I used:

(
// Setup OSC target and vars
~target = NetAddr("127.0.0.1", 12000);  // Your Processing port
~index = 0;
~toggle = 0;

~sendRandomData = {
    var x = rrand(0.0, 1.0);
    var y = rrand(0.0, 1.0);
    ~target.sendMsg("/data", x, y, ~index, ~toggle);
    ("Sent: x=%, y=%, index=%, toggle=% at %").format(x, y, ~index, ~toggle, thisThread.clock.seconds).postln;

    ~index = ~index + 1;
    ~toggle = 1 - ~toggle;
};

// Define a fresh sender task but DO NOT start it yet
~sender = Task({
    loop {
        ~sendRandomData.();
        2.0.wait;
    }
});
)

I used the oscP5broadcastClient example to receive the data.

You should be able to receive the data and populate different arrays according to the msg number (toggle in this case).

ChatGPT Processing oscEvent() used with minimal example:

void oscEvent(OscMessage msg) {
  if (msg.checkAddrPattern("/data")) {
    if (msg.checkTypetag("ffii")) {  // expecting 2 floats, 2 ints
      float x = msg.get(0).floatValue();
      float y = msg.get(1).floatValue();
      int index = msg.get(2).intValue();
      int toggle = msg.get(3).intValue();
      println("Received: x=" + x + ", y=" + y + ", index=" + index + ", toggle=" + toggle);
    } else {
      println("Unexpected typetag: " + msg.typetag());
    }
  }
}

You can process each one as it is received; I just added the toggle as an example of two different data sets.

I have very little knowledge of either of OSC so this was just a quick exploration.

Cool beans! I may use this in future!

:)