Include a header file required for build on mac 10.4
[supercollider.git] / Help / Control / OSC_communication.html
blob5b1dd50cbe650d0dbb8c1e32638d0bf540c3874f
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.54">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 9.0px Monaco; color: #842620}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
17 p.p8 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 9.0px Monaco; color: #842620; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #842620}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #1229a9}
23 p.p14 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
24 span.s1 {color: #1229a9}
25 span.s2 {color: #000000}
26 span.s3 {color: #606060}
27 span.s4 {color: #0c16f9}
28 span.s5 {font: 9.0px Monaco; color: #606060}
29 span.s6 {color: #842620}
30 span.s7 {color: #4d6b29}
31 span.Apple-tab-span {white-space:pre}
32 </style>
33 </head>
34 <body>
35 <p class="p1"><b>OSC_communication</b></p>
36 <p class="p2"><br></p>
37 <p class="p3">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 <a href="NetAddr.html"><span class="s1">NetAddr</span></a> of the target application and creating an <a href="OSCresponderNode.html"><span class="s1">OSCresponderNode</span></a> to listen to another application. The underlying protocol of OSC is either UDP or TCP.</p>
38 <p class="p2"><br></p>
39 <p class="p2"><br></p>
40 <p class="p2"><br></p>
41 <p class="p4"><b>Sending OSC to another application</b></p>
42 <p class="p3">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:</p>
43 <p class="p5"><br></p>
44 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span>b = </span><span class="s1">NetAddr</span><span class="s2">.new(</span><span class="s3">"127.0.0.1"</span><span class="s2">, 7771); </span>// create the NetAddr</p>
45 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span>b.sendMsg(</span><span class="s3">"/hello"</span><span class="s2">, </span><span class="s3">"there"</span><span class="s2">);</span>// send the application the message "hello" with the parameter "there"</p>
46 <p class="p2"><br></p>
47 <p class="p2"><br></p>
48 <p class="p4"><b>Receiving OSC from another application</b></p>
49 <p class="p2"><br></p>
50 <p class="p3">To listen to another application, that application needs to send a message to the port SuperCollider is listening on, <b>normally this is 57120, but it can change.</b> The current port can be retrieved with</p>
51 <p class="p5"><span class="Apple-tab-span"> </span></p>
52 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s1">NetAddr</span><span class="s2">.langPort; </span>// retrieve the current port SC is listening to</p>
53 <p class="p2"><br></p>
54 <p class="p3">Or you can retrieve both the IP and the port with:</p>
55 <p class="p2"><br></p>
56 <p class="p6"><span class="s1"><span class="Apple-tab-span"> </span>NetAddr</span><span class="s2">.localAddr; </span>// retrieve the current IP and port</p>
57 <p class="p2"><br></p>
58 <p class="p3">To listen to incoming messages, an <a href="OSCresponderNode.html"><span class="s1">OSCresponderNode</span></a> needs to be created in SuperCollider. If the sending application <b>has a fixed port it sends message from</b>, you can set the OSCresponderNode to listen only to messages coming from that IP and port:</p>
59 <p class="p7"><br></p>
60 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span>n = </span><span class="s1">NetAddr</span><span class="s2">.new(</span><span class="s3">"127.0.0.1"</span><span class="s2">, 7771); </span>// create the NetAddr</p>
61 <p class="p6"><span class="Apple-tab-span"> </span>// create the OSCresponderNode</p>
62 <p class="p8"><span class="Apple-tab-span"> </span>o = <span class="s1">OSCresponderNode</span>.new(n, <span class="s3">"/goodbye"</span>, { <span class="s1">arg</span> time, resp, msg; [time,resp].postln; } ).add;<span class="Apple-converted-space"> </span></p>
63 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span>o.remove; </span>// remove the OSCresponderNode when you are done.</p>
64 <p class="p2"><br></p>
65 <p class="p3">Why <a href="OSCresponderNode.html"><span class="s4">OSCresponderNode</span></a> rather than <a href="OSCresponder.html"><span class="s4">OSCresponder</span></a>? If you create two OSCresponders with the same message name -- e.g. <span class="s5">"/goodbye"</span> above -- the second OSCresponder will overwrite the first. You can have only one OSCresponder per message name at the same time. OSCresponderNodes do not have that restriction -- many OSCresponderNodes with the same name can coexist. (This also means you have to keep track of your OSCresponderNodes to remove them when they are no longer needed.) See also <a href="OSCpathResponder.html"><span class="s4">OSCpathResponder</span></a>.</p>
66 <p class="p2"><br></p>
67 <p class="p2"><br></p>
68 <p class="p4"><b>Receiving from an application that is sending from a variable port</b></p>
69 <p class="p2"><br></p>
70 <p class="p3">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 OSCresponderNode needs to be set up, so that it listens to messages coming from anywhere:</p>
71 <p class="p2"><br></p>
72 <p class="p8"><span class="Apple-tab-span"> </span>o = <span class="s1">OSCresponderNode</span>.new(<span class="s1">nil</span>, <span class="s3">"/goodbye"</span>, { <span class="s1">arg</span> time, resp, msg; [time,resp].postln; } ).add; <span class="s6">// create the OSCresponderNode</span></p>
73 <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span>o.remove; </span>// remove the OSCresponderNode when you are done.</p>
74 <p class="p9"><br></p>
75 <p class="p9"><br></p>
76 <p class="p9"><br></p>
77 <p class="p9"><br></p>
78 <p class="p4"><b>Testing incoming traffic</b></p>
79 <p class="p3">All incoming OSC messages call the message recvOSCmessage, or recvOSCbundle in <a href="../Core/Kernel/Main.html"><span class="s1">Main</span></a>.<span class="Apple-converted-space"> </span></p>
80 <p class="p3">To see the incoming traffic, one may set a function called <b>recvOSCfunc</b> in Main:</p>
81 <p class="p10"><br></p>
82 <p class="p11">// post all incoming traffic except the server status messages</p>
83 <p class="p12">(</p>
84 <p class="p13">thisProcess<span class="s2">.recvOSCfunc = { </span>|time, addr, msg|<span class="s2"><span class="Apple-converted-space"> </span></span></p>
85 <p class="p12"><span class="Apple-tab-span"> </span>if(msg[0] != <span class="s7">'/status.reply'</span>) {</p>
86 <p class="p14"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>"time: % sender: %\nmessage: %\n"<span class="s2">.postf(time, addr, msg);<span class="Apple-converted-space"> </span></span></p>
87 <p class="p12"><span class="Apple-tab-span"> </span>} <span class="Apple-converted-space"> </span></p>
88 <p class="p12">}</p>
89 <p class="p12">);</p>
90 <p class="p5"><br></p>
91 <p class="p11">// stop posting.</p>
92 <p class="p12"><span class="s1">thisProcess</span>.recvOSCfunc = <span class="s1">nil</span>;</p>
93 <p class="p7"><br></p>
94 </body>
95 </html>