1 // lightweight objects that insulate different ways of playing/stopping.
2 // the bundle that is passed in is a MixedBundle
7 var <source, <>channelOffset;
10 classvar <>buildMethods, <>proxyControlClasses; // see wrapForNodeProxy for methods
12 *new { | source, channelOffset = 0 |
13 ^super.newCopyArgs(source, channelOffset);
21 readyForPlay { ^true }
22 distributable { ^false } // shared proxy support
25 spawnToBundle {} // only active in synthcontrols
28 playToBundle { | bundle, args |
29 bundle.addOnSendMessage(this, \play); //no latency (latency is in stream already)
30 ^nil //return a nil object instead of a synth
33 stopToBundle { | bundle |
34 bundle.addOnSendMessage(this, \stop);
42 play { this.subclassResponsibility(thisMethod) }
43 stop { this.subclassResponsibility(thisMethod) }
46 wakeUpParentsToBundle {}
47 addParent { "wrong object in NodeProxy.buildControl".error } // for now.
55 // The stream is a pause stream. This is meant for running in the proxy to control inner properties so it is stopped when removed.
58 StreamControl : AbstractPlayControl {
62 playToBundle { | bundle |
63 // no latency (latency is in stream already)
64 if(paused.not) { bundle.addOnSendMessage(this, \play) }
65 ^nil // return a nil object instead of a synth
68 build { | proxy, orderIndex = 0 |
70 paused = proxy.paused;
71 stream = source.buildForProxy(proxy, channelOffset, orderIndex);
75 pause { stream.pause; paused=true }
76 resume { | clock, quant = 1.0 |
77 stream.resume(clock, quant);
81 readyForPlay { ^stream.notNil }
84 if(stream.isPlaying.not) {
85 stream.play(clock, false, 0.0)
93 PatternControl : StreamControl {
100 array = array.add(str);
102 str.play(clock, false, 0.0)
104 str.xplay(dt / clock.beatDur, clock, false, 0.0)
108 stopStreams { | streams, dt |
110 dt = (dt ? fadeTime).value;
112 streams.do { arg item; item.stop }
114 dt = dt / clock.beatDur;
115 streams.do { arg item; item.xstop(dt) };
116 // make sure it is stopped, in case next is never called
117 SystemClock.sched(dt, { streams.do(_.stop) });
121 build { | proxy, orderIndex = 0 |
122 fadeTime = { proxy.fadeTime }; // needed for pattern xfade
123 stream = source.buildForProxy(proxy, channelOffset, orderIndex); clock = proxy.clock ? TempoClock.default;
124 paused = proxy.paused;
128 stopToBundle { | bundle, dt |
130 streams = array.copy;
132 bundle.onSend({ this.stopStreams(streams, dt) });
140 resume { | clock, quant = 1.0 |
141 array.do(_.resume(clock, quant));
145 playToBundle { | bundle, args, proxy, addAction = 1 |
146 if(paused.not and: { stream.isPlaying.not })
148 // no latency (latency is in stream already)
151 str = source.buildForProxy(proxy, channelOffset);
154 args.pairsDo { arg key, val; event[key] = val }
159 ^nil // return a nil object instead of a synth
163 this.stopStreams(array.copy);
171 SynthControl : AbstractPlayControl {
173 var <server, <>nodeID;
174 var <canReleaseSynth=false, <canFreeSynth=false;
176 loadToBundle {} // assumes that SynthDef is loaded in the server
178 asDefName { ^source }
180 distributable { ^canReleaseSynth } // n_free not implemented in shared node proxy
182 build { | proxy | // assumes audio rate proxy if not initialized
184 desc = this.synthDesc;
186 canFreeSynth = desc.canFreeSynth;
187 canReleaseSynth = desc.hasGate && canFreeSynth;
189 if(proxy.isNeutral) { rate = \audio };
190 ^proxy.initBus(rate, proxy.numChannels ? 2)
193 spawnToBundle { | bundle, extraArgs, target, addAction = 0 | // assumes self freeing
194 var synthMsg, msg, targetID = target.asTarget.nodeID;
195 if(extraArgs.notNil and: { extraArgs.any { |x| x.size > 1 } }) {
196 synthMsg = [9, this.asDefName, -1, addAction, targetID];
197 bundle.add(synthMsg);
198 synthMsg = ["/n_setn", -1];
199 extraArgs.pairsDo { |key, val|
200 synthMsg = synthMsg.add(key);
201 synthMsg = synthMsg.add(val.size.max(1));
202 synthMsg = synthMsg.addAll(val);
204 bundle.add(synthMsg);
207 synthMsg = [9, this.asDefName, -1, addAction, targetID] ++ extraArgs;
208 bundle.add(synthMsg);
212 playToBundle { | bundle, extraArgs, target, addAction = 1 |
214 server = target.server;
215 group = target.asTarget;
216 nodeID = server.nextNodeID;
217 if(extraArgs.notNil and: { extraArgs.any { |x| x.size > 1 } }) {
218 synthMsg = [9, this.asDefName, nodeID, addAction, group.nodeID];
219 bundle.add(synthMsg);
220 synthMsg = ["/n_setn", -1];
221 extraArgs.pairsDo { |key, val|
222 synthMsg = synthMsg.add(key);
223 synthMsg = synthMsg.add(val.size.max(1));
224 synthMsg = synthMsg.addAll(val);
226 bundle.add(synthMsg);
228 bundle.add([9, this.asDefName, nodeID, addAction, group.nodeID]++extraArgs)
230 if(paused) { bundle.add(["/n_run", nodeID, 0]) };
234 stopToBundle { | bundle, fadeTime |
236 if(canReleaseSynth) {
237 bundle.addAll([['/error', -1], [15, nodeID, \gate, 0.0, \fadeTime, fadeTime], ['/error', -2]]);
239 if(canFreeSynth.not) { //"/n_free"
240 bundle.addAll([['/error', -1], [11, nodeID], ['/error', -2]]);
242 // otherwise it is self freeing by some inner mechanism.
249 server.sendBundle(server.latency, ["/n_set", nodeID] ++ args);
252 pause { | clock, quant = 1 |
253 this.run(clock, quant, false);
256 resume { | clock, quant = 1 |
257 this.run(clock, quant, true);
260 run { | clock, quant, flag = true |
262 (clock ? SystemClock).play({
263 server.sendMsg("/n_run", nodeID, flag.binaryValue);
267 } { paused = flag.not; }
271 var dict = SynthDescLib.global.synthDescs;
272 ^if(dict.notNil) { dict.at(this.asDefName.asSymbol) } { nil };
276 var desc = this.synthDesc;
277 ^if(desc.notNil) { desc.controls } { nil }
280 synthDefPath { ^SynthDef.synthDefDir ++ this.asDefName ++ ".scsyndef" }
282 store { SynthDescLib.global.read(this.synthDefPath) }
287 SynthDefControl : SynthControl {
289 var <synthDef, <parents;
291 readyForPlay { ^synthDef.notNil }
293 build { | proxy, orderIndex = 0 |
294 var ok, rate, numChannels;
296 NodeProxy.buildProxyControl = this;
297 synthDef = source.buildForProxy(proxy, channelOffset, orderIndex);
298 NodeProxy.buildProxyControl = nil;
300 rate = synthDef.rate ?? { if(proxy.rate !== \control) { \audio } { \control } };
301 numChannels = synthDef.numChannels ? proxy.numChannels ? 2;
302 ok = proxy.initBus(rate, numChannels);
304 if(ok and: { synthDef.notNil}) {
305 paused = proxy.paused;
306 canReleaseSynth = synthDef.canReleaseSynth;
307 canFreeSynth = synthDef.canFreeSynth;
313 loadToBundle { | bundle, server |
314 var bytes, size, path;
316 bytes = synthDef.asBytes;
318 size = size - (size bitAnd: 3) + 84; // 4 + 4 + 16 + 16 // appx path length size + overhead
319 if(server.options.protocol === \tcp or: { size < 16383}) {
320 // full size: 65535, but does not work.
321 bundle.addPrepare([5, bytes]); // "/d_recv"
323 // bridge exceeding bundle size by writing to disk
324 if(server.isLocal.not) {
325 Error("SynthDef too large (" ++ size
326 ++ " bytes) to be sent to remote server via udp").throw;
328 path = this.synthDefPath;
329 this.writeSynthDefFile(path, bytes);
330 bundle.addPrepare([6, path]); // "/d_load"
334 freeToBundle { | bundle |
335 if(synthDef.notNil) { bundle.addPrepare([53, synthDef.name]) } // "/d_free"
338 writeSynthDefFile { | path, bytes |
339 var file = File(path, "w");
340 protect { file.putAll(bytes) } { file.close }
343 asDefName { ^synthDef.name }
345 wakeUpParentsToBundle { | bundle, checkedAlready |
346 parents.do { | proxy |
347 proxy.wakeUpToBundle(bundle, checkedAlready)
351 addParent { | proxy |
352 if(parents.isNil) { parents = IdentitySet.new };
356 controlNames { ^synthDef.allControlNames }