cmake build system: visiblity support for clang
[supercollider.git] / SCClassLibrary / Common / Control / OSCBundle.sc
blob6b5570262bb6633eab233a0c3a7e0c08fe46d82b
1 OSCBundle {
2         var <messages, <preparationMessages;
4         add { arg msg; messages = messages.add(msg) }
5         addAll { arg mgs; messages = messages.addAll(mgs) }
6         addPrepare { arg msg; preparationMessages = preparationMessages.add(msg) }
8         // the sound starts at: (next beat + latency) when the preparation is finished
9         // the client side task starts at: preparationTime + next beat
10         // next beat is 0 if no clock is passed in.
11         // eventstreams e.g. take into account the latency internally
13         schedSend { arg server, clock, quant;
14                 server = server ?? { Server.default };
15                 this.doPrepare(server, {
16                         if(quant.isNil or: { clock.isNil }) {
17                                 this.prSend(server, server.latency)
18                         } {
19                                 clock.schedAbs(quant.nextTimeOnGrid(clock),  {
20                                         this.prSend(server, server.latency);
21                                 });
22                         };
23                 });
24         }
26         send { arg server, time, timeOfRequest;
27                 server = server ?? { Server.default };
28                 timeOfRequest = timeOfRequest ?? {Main.elapsedTime};
29                 this.doPrepare(server, { this.prSend(server, time,timeOfRequest) })
30         }
31         // see atTime helpfile
32         sendAtTime { arg server, atTime, timeOfRequest; // offset by preparation
33                 server = server ?? { Server.default };
34                 atTime.schedBundle(this,server,timeOfRequest ?? { Main.elapsedTime});
35         }
37         doPrepare { arg server, onComplete;
38                 if(preparationMessages.isNil) { ^onComplete.value };
40                 Routine.run {
41                         server.sync(Condition.new, preparationMessages);
42                         onComplete.value;
43                 };
44         }
47         // private //
49         prSend { arg server, delta,timeOfRequest;
50                 if(messages.notNil, {
51                         server.listSendBundle(delta ?? { server.latency }, messages);
52                 })
53         }
58 MixedBundle : OSCBundle {
60         var <sendFunctions;     // functions to evaluate on send
61         var <functions;                 // functions to evaluate when the bundle is scheduled on the server
64         onSend { arg func;
65                 sendFunctions = sendFunctions.add(func);
66         }
67         addFunction { arg func;
68                 functions = functions.add(func);
69         }
70         sched { arg time=0.0, func;
71                 sendFunctions = sendFunctions.add({ SystemClock.sched(time, { func.value; nil }) });
72         }
74         addMessage { arg receiver, selector, args;
75                 functions = functions.add( Message(receiver,selector,args) )
76         }
77         addOnSendMessage { arg receiver, selector, args;
78                 sendFunctions = sendFunctions.add( Message(receiver,selector,args) )
79         }
82         doFunctions {
83                 functions.do({ arg item; item.value });
84         }
85         doSendFunctions {
86                 sendFunctions.do({ arg item; item.value });
87         }
89         // private //
91         prSend { arg server, delta, timeOfRequest;
92                 if(delta.isNil,{
93                         delta = server.latency;
94                 },{
95                         if(timeOfRequest.notNil,{
96                                 delta = (timeOfRequest + delta) - Main.elapsedTime;
97                                 if(delta.isNegative,{
98                                         ("bundle sent late:" + delta).warn;
99                                         delta = 0.0;// just send it now
100                                 });
101                         });
102                 });
103                 if(functions.notNil) {
104                         SystemClock.sched(delta ? 0.0, {
105                                 this.doFunctions;
106                         });
107                 };
108                 if(messages.notNil) {
109                         server.listSendBundle(delta, messages);
110                 };
111                 this.doSendFunctions;
112         }