title:: OSC Communication
categories:: External Control>OSC
summary:: OSC network communication
related:: Classes/NetAddr, Classes/OSCFunc

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 link::Classes/NetAddr:: of the target application and creating an link::Classes/OSCFunc:: to listen to another application. The underlying protocol of OSC is either UDP or TCP.

section::Sending OSC to another application

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:
code::
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"
::

section::Receiving OSC from another application

To listen to another application, that application needs to send a message to the port SuperCollider is listening on. Normally the default port is 57120, but it could be something different if that port was already bound when SC started. The current default port can be retrieved with
code::
NetAddr.langPort;	// retrieve the current port SC is listening to
::
Or you can retrieve both the IP and the port with:
code::
NetAddr.localAddr	// retrieve the current IP and port
::

You can open additional ports using link::Classes/Main#-openUDPPort::. This will return a link::Classes/Boolean:: indicating whether SC succeeded in opening the new port. Or you can just pass a custom port as the strong::recvPort:: argument to link::Classes/OSCFunc:: and it will open it automatically if not already open.
code::
thisProcess.openUDPPort(1121); // attempt to open 1121
thisProcess.customPorts; // list all open custom ports
::

To listen to incoming messages, an link::Classes/OSCFunc:: needs to be created in SuperCollider. If the sending application strong::has a fixed port it sends message from::, you can set the OSCFunc to listen only to messages coming from that IP and port:
code::
n = NetAddr.new("127.0.0.1", 7771);	// create the NetAddr
// create the OSCresponderNode
o = OSCFunc({ arg msg, time, addr, recvPort; [msg, time, addr, recvPort].postln; }, '/goodbye', n);
o.free;	// remove the OSCFunc when you are done.
::
note:: The port 7771 above is the port the other application is sending strong::from::, not the port SC is receiving on. See the strong::recvPort:: argument in link::Classes/OSCFunc#*new:: if you want to receive on another port than NetAddr.langPort. ::

section::Receiving from an application that is 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 OSCFunc needs to be set up, so that it listens to messages coming from anywhere. You do this by passing nil as the srcID argument.
code::
o = OSCFunc({ arg msg, time, addr, recvPort; [msg, time, addr, recvPort].postln; }, '/goodbye'); // create the OSCresponderNode
o.free;	// remove the OSCresponderNode when you are done.
::

section::Testing incoming traffic

OSCFunc has a convenience method, link::Classes/OSCFunc#*trace:: which posts all incoming OSC messages:
code::
OSCFunc.trace(true); // Turn posting on
OSCFunc.trace(false); // Turn posting off
::

section::Custom OSC message processing

All incoming OSC messages call the message recvOSCmessage, or recvOSCbundle in link::Classes/Main::. If needed, one can add a custom link::Classes/Function:: or other object to Main's recvOSCFunc variable. Although one can do this directly using the corresponding setter, it is better to use the link::Classes/Main#-addOSCFunc:: and link::Classes/Main#-removeOSCFunc:: to avoid overwriting any other functions that may have been added by class code.
code::
// this example is basically like OSCFunc.trace
(
f = { |time, addr, msg|
	if(msg[0] != '/status.reply') {
		"time: % sender: %\nmessage: %\n".postf(time, addr, msg);
	}
};
thisProcess.addOSCFunc(f);
);

// stop posting.
thisProcess.removeOSCFunc(f);
::
