OSC_communication

OSC communication between programs is often done to send messages from one application to another, possibly with the applications running on different computers. In SuperCollider this communication is done by creating a NetAddr of the target application and creating an OSCresponder to listen to another application. The underlying protocol of OSC is either UDP or TCP.

Setting up an OSC connection

To establish communication to another application, you need to know on which port that application is listening. For example if an application is listening on port 7771, we can create a NetAddr and send it a message:

b = NetAddr.new("127.0.0.1", 7771); // create the NetAddr

b.sendMsg("/hello", "there");// send the application the message "hello" with the parameter "there"

To listen to another application, that application needs to send SuperCollider a message to the port SuperCollider is listening on, normally this is 57120, but it can change. The current port can be retrieved with

NetAddr.langPort; // retrieve the current port SC is listening to

Or you can retrieve both the IP and the port with:

NetAddr.localAddr; // retrieve the current IP and port

To listen to incoming message an OSCresponder needs to be created in SuperCollider. If the sending application has a fixed port it sends message from, you can set the OSCresponder to listen only to messages coming from that IP and port:

n = NetAddr.new("127.0.0.1", 7771); // create the NetAddr

o = OSCresponder.new(n, "/goodbye", { arg time, resp, msg; [time,resp].postln; } ).add; // create the OSCresponder

o.remove; // remove the OSCresponder when you are done.

Connecting to applications sending from a variable port

Some applications (notably Pd and Max) do not send messages from a fixed port, but instead use a different port each time they send out a message. In that case the OSCresponder needs to be set up, so that it listens to messages coming from anywhere:

o = OSCresponder.new(nil, "/goodbye", { arg time, resp, msg; [time,resp].postln; } ).add; // create the OSCresponder

o.remove; // remove the OSCresponder when you are done.