File: ProxyInterfaces.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (423 lines) | stat: -rw-r--r-- 10,121 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// lightweight objects that insulate different ways of playing/stopping.
// the bundle that is passed in is a MixedBundle


AbstractPlayControl {

	var <source, <>channelOffset;
	var <paused=false;

	classvar <>buildMethods, <>proxyControlClasses; // see wrapForNodeProxy for methods

	*new { | source, channelOffset = 0 |
		^super.newCopyArgs(source, channelOffset);
	}

	build { ^true }
	pause { this.stop }
	resume { this.start }
	nodeID { ^nil }

	readyForPlay { ^true }
	distributable { ^false } // shared proxy support

	loadToBundle {}
	spawnToBundle {} // only active in synthcontrols


	playToBundle { | bundle, args |
		bundle.addOnSendMessage(this, \play); //no latency (latency is in stream already)
		^nil //return a nil object instead of a synth
	}

	stopToBundle { | bundle |
		bundle.addOnSendMessage(this, \stop);
	}

	freeToBundle {}

	set {}
	controlNames { ^nil }

	play { this.subclassResponsibility(thisMethod) }
	stop { this.subclassResponsibility(thisMethod) }


	wakeUpParentsToBundle {}
	addParent { "wrong object in NodeProxy.buildControl".error } // for now.
	parents { ^nil }
	store {}

	copy {
		^this.class.new.copyState(this)
	}

	copyState { |control|
		source = control.source; // source is not copied, assumed to be stateless
		channelOffset = control.channelOffset;
		paused = control.paused;
	}


}


// The stream is a pause stream. This is meant for running in the proxy to control inner properties so it is stopped when removed.


StreamControl : AbstractPlayControl {

	var stream, <clock;

	playToBundle { | bundle |
		// no latency (latency is in stream already)
		if(paused.not) { bundle.addOnSendMessage(this, \play) }
		^nil // return a nil object instead of a synth
	}

	build { | proxy, orderIndex = 0 |
		clock = proxy.clock;
		paused = proxy.paused;
		stream = source.buildForProxy(proxy, channelOffset, orderIndex);
		^true;
	}

	pause { stream.pause; paused=true }
	resume { | clock, quant = 1.0 |
		stream.resume(clock, quant);
		paused = false;
	}

	readyForPlay { ^stream.notNil }

	play {
		if(stream.isPlaying.not) {
			stream.play(clock, false, 0.0)
		}
	}
	stop { stream.stop }

	copyState { |control|
		// stream can't be copied.
		// control has to be rebuilt (see: NodeProxy:copyState)
		super.copyState(control);
		clock = control.clock;
	}

	copyRequiresRebuild {
		^true
	}

	controlNames {
		^if(source.isNumber or: { source.isArray }) {
			// this special key (#) allows to set the source from a slider
			ControlName('#', defaultValue:source)
		}
	}

}


PatternControl : StreamControl {

	var fadeTime, <array;

	playStream { | str |
		var dt = fadeTime.value ? 0.0;
		if(dt <= 0.02) {
			str.play(clock, false, 0.0)
		} {
			str.xplay(dt / clock.beatDur, clock, false, 0.0)
		}
	}

	stopStreams { | streams, dt |

		dt = (dt ? fadeTime).value;
		if(dt <= 0.02) {
			streams.do { arg item; item.stop  }
		} {
			dt = dt / clock.beatDur;
			streams.do { arg item; item.xstop(dt) };
			// make sure it is stopped, in case next is never called
			SystemClock.sched(dt, { streams.do(_.stop) });
		}
	}

	build { | proxy, orderIndex = 0 |
		fadeTime = { proxy.fadeTime }; // needed for pattern xfade
		stream = source.buildForProxy(proxy, channelOffset, orderIndex);
		clock = proxy.clock ? TempoClock.default;
		paused = proxy.paused;
		^stream.notNil
	}

	stopToBundle { | bundle, dt |
		var streams;
		streams = array.copy;
		array = nil;
		bundle.onSend({ this.stopStreams(streams, dt) });
	}

	pause {
		array.do(_.pause);
		paused = true;
	}

	resume { | clock, quant = 1.0 |
		array.do(_.resume(clock, quant));
		paused = false;
	}

	playToBundle { | bundle, args, proxy, addAction = 1 |
		var str, event;
		if(paused.not and: { stream.isPlaying.not })
		{
			str = source.buildForProxy(proxy, channelOffset);
			if(args.notNil) {
				event = str.event;
				args.value.pairsDo { arg key, val; event[key] = val }
			};
			array = array.add(str);
			// no latency (latency is in stream already)
			bundle.onSend {
				this.playStream(str)
			}
		}
		^nil // return a nil object instead of a synth
	}

	stop {
		this.stopStreams(array.copy);
		array = nil;
	}

}



SynthControl : AbstractPlayControl {

	var <server, <>nodeID;
	var <canReleaseSynth=false, <canFreeSynth=false, <hasFadeTimeControl=false;
	var prevBundle;


	loadToBundle {} // assumes that SynthDef is loaded in the server

	asDefName { ^source }

	distributable { ^canReleaseSynth } // n_free not implemented in shared node proxy

	build { | proxy, orderIndex | 	// assumes audio rate proxy if not initialized
		var rate, desc;
		desc = this.synthDesc;
		if(desc.notNil) {
			canFreeSynth = desc.canFreeSynth;
			canReleaseSynth = desc.hasGate && canFreeSynth;
			hasFadeTimeControl = desc.controls.any { |x| x.name === \fadeTime };
		};
		if(proxy.isNeutral) { rate = \audio };
		^proxy.initBus(rate)
	}

	spawnToBundle { | bundle, extraArgs, target, addAction = 0 | // assumes self freeing
		var targetID = target.asTarget.nodeID;
		bundle.addCancel({ [9, this.asDefName, -1, addAction, targetID] ++ extraArgs.value });
	}

	playToBundle { | bundle, extraArgs, target, addAction = 1 |
		var group;
		server = target.server;
		group = target.asTarget;
		nodeID = server.nextNodeID;
		bundle.addCancel({ [9, this.asDefName, nodeID, addAction, group.nodeID] ++ extraArgs.value });
		if(paused) { bundle.addCancel(["/n_run", nodeID, 0]) };
		prevBundle = bundle;
		^nodeID
	}

	stopToBundle { | bundle, fadeTime |
		if(nodeID.notNil) {

			if(canReleaseSynth) {
				if(hasFadeTimeControl) {
					bundle.addAll([['/error', -1], [15, nodeID, \fadeTime, fadeTime, \gate, 0], ['/error', -2]])
				} {
					bundle.addAll([['/error', -1], [15, nodeID, \gate, -1.0 - fadeTime.abs], ['/error', -2]])
				}
			} {
				if(canFreeSynth.not) { //"/n_free"
					bundle.addAll([['/error', -1], [11, nodeID], ['/error', -2]]);
				}
				// otherwise it is self freeing by some inner mechanism.
			};
			nodeID = nil;
			this.prCancelPrevBundle;
		}
	}

	freeToBundle {
		this.prCancelPrevBundle
	}

	set { | ... args |
		server.sendBundle(server.latency, ["/n_set", nodeID] ++ args.asOSCArgArray);
	}

	pause { | clock, quant = 1 |
		this.run(clock, quant, false);
	}

	resume { | clock, quant = 1 |
		this.run(clock, quant, true);
	}

	run { | clock, quant, flag = true |
		if(nodeID.notNil) {
			(clock ? SystemClock).play({
				server.sendMsg("/n_run", nodeID, flag.binaryValue);
				paused = flag.not;
				nil;
			}, quant)
		} { paused = flag.not; }
	}

	synthDesc {
		var dict = SynthDescLib.global.synthDescs;
		^if(dict.notNil) { dict.at(this.asDefName.asSymbol) } { nil };
	}

	controlNames {
		var desc = this.synthDesc;
		^if(desc.notNil) { desc.controls } { nil }
	}

	synthDefPath { ^SynthDef.synthDefDir ++ this.asDefName ++ ".scsyndef" }

	store { SynthDescLib.global.read(this.synthDefPath) }

	copyState { |control|
		super.copyState(control);
		server = control.server;
		canReleaseSynth = control.canReleaseSynth;
		canFreeSynth = control.canFreeSynth;
	}

	// private

	prCancelPrevBundle {
		prevBundle !? { prevBundle.cancel };
		prevBundle = nil;
	}

}


SynthDefControl : SynthControl {

	var <synthDef, <parents;
	var <bytes;

	readyForPlay { ^synthDef.notNil }

	build { | proxy, orderIndex = 0 |
		var ok, rate, numChannels, outerDefControl, outerBuildProxy, controlNames;

		outerDefControl = NodeProxy.buildProxyControl;
		outerBuildProxy = NodeProxy.buildProxy;
		NodeProxy.buildProxyControl = this;
		NodeProxy.buildProxy = proxy;
		synthDef = source.buildForProxy(proxy, channelOffset, orderIndex);
		NodeProxy.buildProxyControl = outerDefControl;
		outerBuildProxy = outerBuildProxy;

		rate = synthDef.rate;
		numChannels = synthDef.numChannels;
		ok = proxy.initBus(rate, numChannels);

		if(ok) {
			paused = proxy.paused;
			canReleaseSynth = synthDef.canReleaseSynth;
			canFreeSynth = synthDef.canFreeSynth;
			controlNames = synthDef.allControlNames;
			hasFadeTimeControl = controlNames.notNil and: {
				controlNames.any { |x| x.name === \fadeTime }
			};
		} {
			synthDef = nil;
			"synth def couldn't be built".warn;
		}
	}

	loadToBundle { | bundle, server |
		var size, path;

		// cache rendered synth def, so it can be copied if necessary (see: copyData)
		// We need to keep the bytes here, because some other instance may have deleted it from the server
		// (see: freeToBundle)
		// the resulting synthDef will have the same name, because the name is encoded in the data (bytes).
		bytes = bytes ?? { synthDef.asBytes }; // here the work for sclang is done.
		size = bytes.size;
		size = size - (size bitAnd: 3) + 84; // 4 + 4 + 16 + 16 // appx path length size + overhead
		if(server.options.protocol === \tcp or: { size < 16383}) {
			// full size: 65535, but does not work.
			bundle.addPrepare([5, bytes]); // "/d_recv"
		} {
			// bridge exceeding bundle size by writing to disk
			if(server.isLocal.not) {
				Error("SynthDef too large (" ++ size
					++ " bytes) to be sent to remote server via udp").throw;
			};
			path = this.synthDefPath;
			this.writeSynthDefFile(path, bytes);
			bundle.addPrepare([6, path]); // "/d_load"
		};
		prevBundle = bundle;
	}

	freeToBundle { | bundle, proxy |
		if(synthDef.notNil) { bundle.add([53, synthDef.name]) }; // "/d_free"
		parents.do { |x| x.removeChild(proxy) };
		bytes = parents = nil;
		this.prCancelPrevBundle;
	}

	writeSynthDefFile { | path, bytes |
		var file = File(path, "w");
		protect { file.putAll(bytes) } { file.close }
	}

	asDefName { ^synthDef.name }

	wakeUpParentsToBundle { | bundle, checkedAlready |
		parents.do { | proxy |
			proxy.wakeUpToBundle(bundle, checkedAlready)
		}
	}

	addParent { | proxy |
		if(parents.isNil) { parents = IdentitySet.new };
		if(parents.includes(proxy).not) {
			parents.add(proxy);
			proxy.addChild(NodeProxy.buildProxy);
		}
	}

	controlNames { ^synthDef.allControlNames }

	copyState { |control|
		super.copyState(control);
		synthDef = control.synthDef;
		parents = control.parents.copy;
		bytes = control.bytes; // copy cached data
	}

	findSpecFor { |controlName|
		^synthDef.findSpecFor(controlName)
	}
	specs {
		^synthDef.specs
	}

}