1 title:: OSC Communication
2 categories:: External Control>OSC
3 summary:: OSC network communication
4 related:: Classes/NetAddr, Classes/OSCFunc
6 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.
8 section::Sending OSC to another application
10 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:
12 b = NetAddr.new("127.0.0.1", 7771); // create the NetAddr
13 b.sendMsg("/hello", "there"); // send the application the message "hello" with the parameter "there"
16 section::Receiving OSC from another application
18 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
20 NetAddr.langPort; // retrieve the current port SC is listening to
22 Or you can retrieve both the IP and the port with:
24 NetAddr.localAddr // retrieve the current IP and port
27 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.
29 thisProcess.openUDPPort(1121); // attempt to open 1121
30 thisProcess.customPorts; // list all open custom ports
33 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:
35 n = NetAddr.new("127.0.0.1", 7771); // create the NetAddr
36 // create the OSCresponderNode
37 o = OSCFunc({ arg msg, time, addr, recvPort; [msg, time, addr, recvPort].postln; }, '/goodbye', n);
38 o.free; // remove the OSCFunc when you are done.
40 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. ::
42 section::Receiving from an application that is sending from a variable port
44 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.
46 o = OSCFunc({ arg msg, time, addr, recvPort; [msg, time, addr, recvPort].postln; }, '/goodbye'); // create the OSCresponderNode
47 o.free; // remove the OSCresponderNode when you are done.
50 section::Testing incoming traffic
52 OSCFunc has a convenience method, link::Classes/OSCFunc#*trace:: which posts all incoming OSC messages:
54 OSCFunc.trace(true); // Turn posting on
55 OSCFunc.trace(false); // Turn posting off
58 section::Custom OSC message processing
60 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#-addOSCRecvFunc:: and link::Classes/Main#-removeOSCRecvFunc:: to avoid overwriting any other functions that may have been added by class code.
62 // this example is basically like OSCFunc.trace but filters out
63 // /status.reply messages
65 f = { |msg, time, addr|
66 if(msg[0] != '/status.reply') {
67 "time: % sender: %\nmessage: %\n".postf(time, addr, msg);
70 thisProcess.addOSCRecvFunc(f);
74 thisProcess.removeOSCRecvFunc(f);