File: BasicOpsUGen.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 (496 lines) | stat: -rw-r--r-- 12,904 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// These Unit Generators are instantiated by math operations on UGens

BasicOpUGen : UGen {
	var <operator;

//	writeName { arg file;
//		var name, opname;
//		name = this.class.name.asString;
//		opname = operator.asString;
//		file.putInt8(name.size + opname.size + 1);
//		file.putString(name);
//		file.putInt8(0);
//		file.putString(opname);
//	}
	operator_ { arg op;
		operator = op;
		specialIndex = operator.specialIndex;
		if(specialIndex < 0) {
			Error("Operator '%' applied to a UGen is not supported in scsynth".format(operator)).throw
		}
	}

	argNamesInputsOffset { ^2 }
	argNameForInputAt { arg i;
		var method = this.class.class.findMethod('new');
		if(method.isNil or: {method.argNames.isNil},{ ^nil });
		^method.argNames.at(i + this.argNamesInputsOffset)
	}
	dumpArgs {
		" ARGS:".postln;
		("   operator:" + operator).postln;
		inputs.do({ arg in,ini;
			("   " ++ (this.argNameForInputAt(ini) ? ini.asString)++":" + in + in.class).postln
		});
	}

	dumpName {
		^synthIndex.asString ++ "_" ++ this.operator
	}
}

UnaryOpUGen : BasicOpUGen {

	*new { arg selector, a;
		^this.multiNew('audio', selector, a)
	}

	init { arg theOperator, theInput;
		this.operator = theOperator;
		rate = theInput.rate;
		inputs = theInput.asArray;
	}

	optimizeGraph {
		super.performDeadCodeElimination
	}
}

BinaryOpUGen : BasicOpUGen {
	*new { arg selector, a, b;
		^this.multiNew('audio', selector, a, b)
	}

	determineRate { arg a, b;
		if (a.rate == \demand, { ^\demand });
		if (b.rate == \demand, { ^\demand });
		if (a.rate == \audio, { ^\audio });
		if (b.rate == \audio, { ^\audio });
		if (a.rate == \control, { ^\control });
		if (b.rate == \control, { ^\control });
		^\scalar
	}
	*new1 { arg rate, selector, a, b;

		// eliminate degenerate cases
		if (selector == '*', {
			if (a == 0.0, { ^0.0 });
			if (b == 0.0, { ^0.0 });
			if (a == 1.0, { ^b });
			if (a == -1.0, { ^b.neg });
			if (b == 1.0, { ^a });
			if (b == -1.0, { ^a.neg });
		},{
		if (selector == '+', {
			if (a == 0.0, { ^b });
			if (b == 0.0, { ^a });
		},{
		if (selector == '-', {
			if (a == 0.0, { ^b.neg });
			if (b == 0.0, { ^a });
		},{
		if (selector == '/', {
			if (b == 1.0, { ^a });
			if (b == -1.0, { ^a.neg });
		})})})});

		^super.new1(rate, selector, a, b)
	}

	init { arg theOperator, a, b;
		this.operator = theOperator;
		rate = this.determineRate(a, b);
		inputs = [a, b];
	}

	optimizeGraph {
		//this.constantFolding;


		if (super.performDeadCodeElimination) {
			^this
		};

		if (operator == '+') {
			this.optimizeAdd;
			^this;
		};

		if (operator == '-') {
			this.optimizeSub
		};
	}

	optimizeAdd {
		var optimizedUGen;

		// create a Sum3 if possible.
		optimizedUGen = this.optimizeToSum3;

		// create a Sum4 if possible.
		if (optimizedUGen.isNil) {
			optimizedUGen = this.optimizeToSum4
		};

		// create a MulAdd if possible.
		if (optimizedUGen.isNil) {
			optimizedUGen = this.optimizeToMulAdd
		};

		// optimize negative additions
		if (optimizedUGen.isNil) {
			optimizedUGen = this.optimizeAddNeg
		};

		if (optimizedUGen.notNil) {
			synthDef.replaceUGen(this, optimizedUGen);
			optimizedUGen.optimizeGraph
		};
	}

	// 'this' = old ugen being replaced
	// replacement = this's replacement
	// deletedUnit = auxiliary unit being removed, not replaced
	optimizeUpdateDescendants { |replacement, deletedUnit|
		var replaceFunc = { |ugen|
			var desc = ugen.tryPerform(\descendants);
			desc.add(replacement).remove(this);
			desc.remove(deletedUnit);
		};
		replacement.inputs.do { |in|
			replaceFunc.value(in);
			if(in.isKindOf(OutputProxy)) {
				replaceFunc.value(in.source);
			};
		};
	}

	optimizeAddNeg {
		var a, b, replacement;
		#a, b = inputs;
		if(a === b) { ^nil };  // non optimizable edge case.

		if (b.isKindOf(UnaryOpUGen) and: { b.operator == 'neg' and: { b.descendants.size == 1 } }) {
			// a + b.neg -> a - b
			buildSynthDef.removeUGen(b);
			replacement = a - b.inputs[0];
			// this is the first time the dependants logic appears. It's repeated below.
			// We will remove 'this' from the synthdef, and replace it with 'replacement'.
			// 'replacement' should then have all the same descendants as 'this'.
			replacement.descendants = descendants;
			// drop 'this' and 'b' from all of replacement's inputs' descendant lists
			// so that future optimizations decide correctly
			this.optimizeUpdateDescendants(replacement, b);
			^replacement
		};

		if (a.isKindOf(UnaryOpUGen) and: { a.operator == 'neg' and: { a.descendants.size == 1 } }) {
			// a.neg + b -> b - a
			buildSynthDef.removeUGen(a);
			replacement = b - a.inputs[0];
			replacement.descendants = descendants;
			this.optimizeUpdateDescendants(replacement, a);
			^replacement
		};
		^nil
	}

	optimizeToMulAdd {
		var a, b, replacement;
		#a, b = inputs;
		if(a === b) { ^nil };

		if (a.isKindOf(BinaryOpUGen) and: { a.operator == '*'
			and: { a.descendants.size == 1 }})
		{
			if (MulAdd.canBeMulAdd(a.inputs[0], a.inputs[1], b)) {
				buildSynthDef.removeUGen(a);
				replacement = MulAdd.new(a.inputs[0], a.inputs[1], b).descendants_(descendants);
				this.optimizeUpdateDescendants(replacement, a);
				^replacement
			};

			if (MulAdd.canBeMulAdd(a.inputs[1], a.inputs[0], b)) {
				buildSynthDef.removeUGen(a);
				replacement = MulAdd.new(a.inputs[1], a.inputs[0], b).descendants_(descendants);
				this.optimizeUpdateDescendants(replacement, a);
				^replacement
			};
		};

		if (b.isKindOf(BinaryOpUGen) and: { b.operator == '*'
			and: { b.descendants.size == 1 }})
		{
			if (MulAdd.canBeMulAdd(b.inputs[0], b.inputs[1], a)) {
				buildSynthDef.removeUGen(b);
				replacement = MulAdd.new(b.inputs[0], b.inputs[1], a).descendants_(descendants);
				this.optimizeUpdateDescendants(replacement, b);
				^replacement
			};

			if (MulAdd.canBeMulAdd(b.inputs[1], b.inputs[0], a)) {
				buildSynthDef.removeUGen(b);
				replacement = MulAdd.new(b.inputs[1], b.inputs[0], a).descendants_(descendants);
				this.optimizeUpdateDescendants(replacement, b);
				^replacement
			};
		};
		^nil
	}

	optimizeToSum3 {
		var a, b, replacement;
		#a, b = inputs;
		if(a.rate == \demand or: { b.rate == \demand }) { ^nil };

		if (a.isKindOf(BinaryOpUGen) and: { a.operator == '+'
			and: { a.descendants.size == 1 }}) {
			buildSynthDef.removeUGen(a);
			if(a === b) {
				replacement = Sum4(a.inputs[0], a.inputs[0], a.inputs[1], a.inputs[1])
				.descendants_(descendants);
			} {
				replacement = Sum3(a.inputs[0], a.inputs[1], b).descendants_(descendants);
			};
			this.optimizeUpdateDescendants(replacement, a);
			^replacement;
		};

		if (b.isKindOf(BinaryOpUGen) and: { b.operator == '+'
			and: { b.descendants.size == 1 }}) {
			buildSynthDef.removeUGen(b);
			// we do not need the a === b check here
			// if a === b, then the 'a' branch above must have handled it
			replacement = Sum3(b.inputs[0], b.inputs[1], a).descendants_(descendants);
			this.optimizeUpdateDescendants(replacement, b);
			^replacement;
		};
		^nil
	}

	optimizeToSum4 {
		var a, b, replacement;
		#a, b = inputs;
		if(a === b) { ^nil };
		if(a.rate == \demand or: { b.rate == \demand }) { ^nil };

		if (a.isKindOf(Sum3) and: { a.descendants.size == 1 }) {
			buildSynthDef.removeUGen(a);
			replacement = Sum4(a.inputs[0], a.inputs[1], a.inputs[2], b).descendants_(descendants);
			this.optimizeUpdateDescendants(replacement, a);
			^replacement;
		};

		if (b.isKindOf(Sum3) and: { b.descendants.size == 1 }) {
			buildSynthDef.removeUGen(b);
			replacement = Sum4(b.inputs[0], b.inputs[1], b.inputs[2], a).descendants_(descendants);
			this.optimizeUpdateDescendants(replacement, b);
			^replacement;
		};
		^nil
	}

	optimizeSub {
		var a, b, replacement;
		#a, b = inputs;

		if (b.isKindOf(UnaryOpUGen) and: { b.operator == 'neg' and: { b.descendants.size == 1 } }) {
			// a - b.neg -> a + b
			buildSynthDef.removeUGen(b);

			replacement = BinaryOpUGen('+', a, b.inputs[0]);
			replacement.descendants = descendants;
			this.optimizeUpdateDescendants(replacement, b);

			synthDef.replaceUGen(this, replacement);
			replacement.optimizeGraph  // not called from optimizeAdd; no need to return ugen here
		};
		^nil
	}

	constantFolding {
		var a, b, aa, bb, cc, dd, temp, ac_ops, value;

		// associative & commutative operators
		ac_ops = #['+','*','min','max','&&','||'];

		if (ac_ops.includes(operator).not) { ^this };

		#a, b = inputs;
		if (a.isKindOf(BinaryOpUGen) and: { operator == a.operator
			and: { b.isKindOf(BinaryOpUGen) and: { operator == b.operator } }}) {
			#aa, bb = a.inputs;
			#cc, dd = b.inputs;
			if (aa.isKindOf(SimpleNumber)) {
				if (cc.isKindOf(SimpleNumber)) {
					b.inputs[0] = bb;
					this.inputs[0] = aa.perform(operator, cc);
					synthDef.removeUGen(a);
				}{
				if (dd.isKindOf(SimpleNumber)) {
					b.inputs[1] = bb;
					this.inputs[0] = aa.perform(operator, dd);
					synthDef.removeUGen(a);
				}}
			}{
			if (bb.isKindOf(SimpleNumber)) {
				if (cc.isKindOf(SimpleNumber)) {
					b.inputs[0] = aa;
					this.inputs[0] = bb.perform(operator, cc);
					synthDef.removeUGen(a);
				}{
				if (dd.isKindOf(SimpleNumber)) {
					b.inputs[1] = aa;
					this.inputs[0] = bb.perform(operator, dd);
					synthDef.removeUGen(a);
				}}
			}};

		};
		#a, b = inputs;
		if (a.isKindOf(BinaryOpUGen) and: { operator == a.operator }) {
			#aa, bb = a.inputs;
			if (b.isKindOf(SimpleNumber)) {
				if (aa.isKindOf(SimpleNumber)) {
					buildSynthDef.removeUGen(a);
					this.inputs[0] = aa.perform(operator, b);
					this.inputs[1] = bb;
					^this
				};
				if (bb.isKindOf(SimpleNumber)) {
					buildSynthDef.removeUGen(a);
					this.inputs[0] = bb.perform(operator, b);
					this.inputs[1] = aa;
					^this
				};
			};
			// percolate constants upward so that a subsequent folding may occur
			if (aa.isKindOf(SimpleNumber)) {
				this.inputs[1] = aa;
				a.inputs[0] = b;
			}{
			if (bb.isKindOf(SimpleNumber)) {
				this.inputs[1] = bb;
				a.inputs[1] = b;
			}};
		};
		#a, b = inputs;
		if (b.isKindOf(BinaryOpUGen) and: { operator == b.operator }) {
			#cc, dd = b.inputs;
			if (a.isKindOf(SimpleNumber)) {
				if (cc.isKindOf(SimpleNumber)) {
					buildSynthDef.removeUGen(b);
					this.inputs[0] = a.perform(operator, cc);
					this.inputs[1] = dd;
					^this
				};
				if (dd.isKindOf(SimpleNumber)) {
					buildSynthDef.removeUGen(b);
					this.inputs[0] = a.perform(operator, dd);
					this.inputs[1] = cc;
					^this
				};
			};
			// percolate constants upward so that a subsequent folding may occur
			if (cc.isKindOf(SimpleNumber)) {
				this.inputs[0] = cc;
				b.inputs[0] = a;
			}{
			if (dd.isKindOf(SimpleNumber)) {
				this.inputs[0] = dd;
				b.inputs[1] = a;
			}};
		};
		#a, b = inputs;
		if (a.isKindOf(SimpleNumber) and: { b.isKindOf(SimpleNumber) }) {
			synthDef.replaceUGen(this, a.perform(operator, b));
			synthDef.removeUGen(this);
		};
	}
}

MulAdd : UGen {
	*new { arg in, mul = 1.0, add = 0.0;
		var args =  [in, mul, add].asUGenInput(this);
		var rate = args.rate;
		^this.multiNewList([rate] ++ args)
	}
	*new1 { arg rate, in, mul, add;
		var minus, nomul, noadd;

		// eliminate degenerate cases
		if (mul == 0.0, { ^add });
		minus = mul == -1.0;
		nomul = mul == 1.0;
		noadd = add == 0.0;
		if (nomul && noadd, { ^in });
		if (minus && noadd, { ^in.neg });
		if (noadd, { ^in * mul });
		if (minus, { ^add - in });
		if (nomul, { ^in + add });

		if (this.canBeMulAdd(in, mul, add)) {
			^super.new1(rate, in, mul, add)
		};
		if (this.canBeMulAdd(mul, in, add)) {
			^super.new1(rate, mul, in, add)
		};
		^( (in * mul) + add)
	}
	init { arg in, mul, add;
		inputs = [in, mul, add];
		rate = inputs.rate;
	}

	*canBeMulAdd { arg in, mul, add;
		// see if these inputs satisfy the constraints of a MulAdd ugen.
		if (in.rate == \audio, { ^true });
		if (in.rate == \control
			and: { mul.rate == \control || { mul.rate == \scalar }}
			and: { add.rate == \control || { add.rate == \scalar }},
		{
			^true
		});
		^false
	}
}

Sum3 : UGen {
	*new { arg in0, in1, in2;
		^this.multiNew(nil, in0, in1, in2)
	}

	*new1 { arg dummyRate, in0, in1, in2;
		var argArray, rate, sortedArgs;
		if (in2 == 0.0) { ^(in0 + in1) };
		if (in1 == 0.0) { ^(in0 + in2) };
		if (in0 == 0.0) { ^(in1 + in2) };

		argArray = [in0, in1, in2];
		rate = argArray.rate;
		sortedArgs = argArray.sort {|a b| a.rate < b.rate};

		^super.new1(rate, *sortedArgs)
	}
}

Sum4 : UGen {
	*new { arg in0, in1, in2, in3;
		^this.multiNew(nil, in0, in1, in2, in3)
	}

	*new1 { arg dummyRate, in0, in1, in2, in3;
		var argArray, rate, sortedArgs;

		if (in0 == 0.0) { ^Sum3.new1(nil, in1, in2, in3) };
		if (in1 == 0.0) { ^Sum3.new1(nil, in0, in2, in3) };
		if (in2 == 0.0) { ^Sum3.new1(nil, in0, in1, in3) };
		if (in3 == 0.0) { ^Sum3.new1(nil, in0, in1, in2) };

		argArray = [in0, in1, in2, in3];
		rate = argArray.rate;
		sortedArgs = argArray.sort {|a b| a.rate < b.rate};

		^super.new1(rate, *sortedArgs)
	}
}