«CloudGenMiniExtraSynthDefs» by bateslewis

on 04 Feb'13 12:27 in

original example using PMOsc instead

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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
(
//original changed to FM

// CloudGenMini is based on CloudGenerator, a granular synthesis program
// by Curtis Roads and John Alexander.
// This partial miniature version was implemented by Alberto de Campo, 2007.




	// figure 8.23 - some granular synthdefs and tests
(
	// a gabor (approx. gaussian-shaped) grain
SynthDef(\sinGab, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = FSinOsc.ar(freq);
	var env = EnvGen.ar(Env.sine(sustain, amp * AmpComp.ir(freq) * 0.5), doneAction: 2);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// wider, quasi-gaussian envelope, with a hold time in the middle.
SynthDef(\sinWide, { |out, amp=0.1, freq=440, sustain=0.01, pan, width=0.5|
	var holdT = sustain * width;
	var fadeT = 1 - width * sustain * 0.5;
	var snd = FSinOsc.ar(freq);
	var env = EnvGen.ar(Env([0, 1, 1, 0], [fadeT, holdT, fadeT], \sin),
		levelScale: amp * AmpComp.ir(freq) * 0.5,
		doneAction: 2);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// a simple percussive envelope
SynthDef(\sinPerc, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = FSinOsc.ar(freq);
	var env = EnvGen.ar(
		Env.perc(0.1, 0.9, amp * AmpComp.ir(freq) * 0.5),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// a reversed  percussive envelope
SynthDef(\sinPercRev, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = FSinOsc.ar(freq);
	var env = EnvGen.ar(
		Env.perc(0.9, 0.1, amp * AmpComp.ir(freq) * 0.5, 4),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

SynthDef(\noiseGab, { |out, amp=0.2, freq=440, sustain=0.01, pan, rq=0.1|
	var snd = BPF.ar(GrayNoise.ar, freq, rq, 3);
	var env = EnvGen.ar(Env.sine(sustain, amp * 0.5), doneAction: 2);
	OffsetOut.ar(out,
		Pan2.ar(snd * env, pan, amp)
	);
}, \ir ! 6).add;

SynthDef(\noiseWide, { |out, amp=0.2, freq=440, sustain=0.01, pan, rq=0.1, width=0.5|
	var holdT = sustain * width;
	var fadeT = 1 - width * sustain * 0.5;
	var snd = BPF.ar(GrayNoise.ar, freq, rq, 3);
	var env = EnvGen.ar(Env([0, 1, 1, 0], [fadeT, holdT, fadeT], \sin),
		levelScale: amp * 0.5, doneAction: 2);
	OffsetOut.ar(out,
		Pan2.ar(snd * env, pan, amp)
	);
}, \ir ! 6).add;

		// a noise band grain with percussive envelope
SynthDef(\noisePerc, { |out, amp=0.2, freq=440, sustain=0.01, pan, rq=0.1|
	var snd = BPF.ar(GrayNoise.ar, freq, rq, 3);
	var env = EnvGen.ar(Env.perc(0.1, 0.9, amp * 0.5), timeScale: sustain, doneAction: 2);
	OffsetOut.ar(out,
		Pan2.ar(snd * env, pan, amp)
	);
}, \ir ! 6).add;

SynthDef(\noisePrev, { |out, amp=0.2, freq=440, sustain=0.01, pan, rq=0.1|
	var snd = BPF.ar(GrayNoise.ar, freq, rq, 3);
	var env = EnvGen.ar(
	    Env.perc(0.9, 0.1, amp * 0.5, 4),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out,
		Pan2.ar(snd * env, pan, amp)
	);
}, \ir ! 6).add;



SynthDef(\sqGab, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = Pulse.ar(freq, 0.5);
	var env = EnvGen.ar(Env.sine(sustain, amp * 0.5), doneAction: 2);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// wider, quasi-gaussian envelope, with a hold time in the middle.
SynthDef(\sqWide, { |out, amp=0.1, freq=440, sustain=0.01, pan, width=0.5|
	var holdT = sustain * width;
	var fadeT = 1 - width * sustain * 0.5;
	var snd = Pulse.ar(freq, 0.5);
	var env = EnvGen.ar(Env([0, 1, 1, 0], [fadeT, holdT, fadeT], \sin),
		levelScale: amp * 0.5,
		doneAction: 2);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// a simple percussive envelope
SynthDef(\sqPerc, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = Pulse.ar(freq, 0.5);
	var env = EnvGen.ar(
		Env.perc(0.1, 0.9, amp * 0.5),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// a reversed  percussive envelope
SynthDef(\sqPercRev, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = Pulse.ar(freq, 0.5);
	var env = EnvGen.ar(
		Env.perc(0.9, 0.1, amp * 0.5, 4),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

SynthDef(\triGab, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = DPW3Tri.ar(freq);
	var env = EnvGen.ar(Env.sine(sustain, amp * 0.5), doneAction: 2);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// wider, quasi-gaussian envelope, with a hold time in the middle.
SynthDef(\triWide, { |out, amp=0.1, freq=440, sustain=0.01, pan, width=0.5|
	var holdT = sustain * width;
	var fadeT = 1 - width * sustain * 0.5;
	var snd = DPW3Tri.ar(freq);
	var env = EnvGen.ar(Env([0, 1, 1, 0], [fadeT, holdT, fadeT], \sin),
		levelScale: amp * 0.5,
		doneAction: 2);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// a simple percussive envelope
SynthDef(\triPerc, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = DPW3Tri.ar(freq);
	var env = EnvGen.ar(
		Env.perc(0.1, 0.9, amp * 0.5),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

			// a reversed  percussive envelope
SynthDef(\triPercRev, { |out, amp=0.1, freq=440, sustain=0.01, pan|
	var snd = DPW3Tri.ar(freq);
	var env = EnvGen.ar(
		Env.perc(0.9, 0.1, amp * 0.5, 4),
			timeScale: sustain, doneAction: 2
		);
	OffsetOut.ar(out, Pan2.ar(snd * env, pan));
}, \ir ! 5).add;

/*
	// tests for the synthdefs:
Synth(\gab1st);
Synth(\gabWide);
Synth(\percSin);
Synth(\percSinRev);

Synth(\percSin, [\amp, 0.2, \sustain, 0.1]);
Synth(\percNoise, [\amp, 0.2, \sustain, 0.1]);

Synth(\gab1st, [\out, 0, \amp, 0.2, \freq, 2000, \sustain, 0.05, \pan, 0.5] );

Synth(\sawGab1st, [\amp, 0.2, \sustain, 0.1]);
Synth(\sawGabWide);
Synth(\sawPerc);
Synth(\sawPercRev);
*/
);





	// figure 8.24 - global setup and a player Tdef for the cloud.
(
q = q ? ();

	// some globals
q.paramRNames = [\freqRange, \durRange, \densRange, \ampRange, \panRange];
q.paramNames = [\freq, \grDur, \dens, \amp, \pan];
q.syndefNames = [\sinGab, \sinWide, \sinPerc, \sinPercRev,
	             \noiseGab, \noiseWide, \noisePerc, \noisePrev,
                 \sqGab, \sqWide, \sqPerc, \sqPercRev,
                 \triGab, \triWide, \triPerc, \triPercRev];

	// specs for some parameters
Spec.add(\xfadeTime, [0.001, 1000, \exp]);
Spec.add(\ring, [0.03, 30, \exp]);
Spec.add(\grDur, [0.0001, 1, \exp]);
Spec.add(\dens, [1, 1000, \exp]);

	// make an empty tdef that plays it,
	// and put the cloud parameter ranges in the tdef's environment
Tdef(\cloud0)
	.set(
	\synName, \sinGab,
	\vol, 0.25,
	\current, (
		freqRange: [200, 2000],
		ampRange: [0.1, 1],
		durRange: [0.001, 0.01],
		densRange: [1, 1000],
		panRange: [-1.0, 1.0]
	)
);

		// make the tdef that plays the cloud of sound particles here,
		// based on parameter range settings.
Tdef(\cloud0, { |e|

	loop {
		s.sendBundle(s.latency, [
			"/s_new", e.synName ? \sinGab,
			-1, 0, 0,
			\freq, 	exprand(e.current.freqRange[0], e.current.freqRange[1]),
			\amp,	exprand(e.current.ampRange[0], e.current.ampRange[1]) * e.vol,
			\sustain,	exprand(e.current.durRange[0], e.current.durRange[1]),
			\pan, 	rrand(e.current.panRange[0], e.current.panRange[1])
		]);
		exprand(e.current.densRange[0].reciprocal, e.current.densRange[1].reciprocal).wait;
	}
}).quant_(0);
);




/*
	// figure  8.25	-  tests for the cloud

Tdef(\cloud0).play;

	// try changing various things from outside the loop.
	// change its playing settings

Tdef(\cloud0).envir.current.put('densRange', [ 50, 200 ]); // dense, async
Tdef(\cloud0).envir.current.put('densRange', [ 1, 10 ]);  // sparse, async
Tdef(\cloud0).envir.current.put('densRange', [ 30, 30 ]); // synchronous

	// for faster access, call the tdef's envir d
d = Tdef(\cloud0).envir;
d.current.put('freqRange', [ 800, 1200 ]);
d.current.put('durRange', [ 0.02, 0.02 ]);

d.current.put('ampRange', [ 0.1, 0.1 ]);

d.current.put('panRange', [ 1.0, 1.0 ]);
d.current.put('panRange', [ -1.0, 1.0 ]);

d.current.put('densRange', [ 30, 60 ]);
d.synName = \percSin;
d.synName = \gab1st;
d.synName = \gabWide;
d.synName = \percSinRev;
d.synName = \percNoise;
d.synName = \percSinRev;
d.synName = \gab1st;
d.current.put('durRange', [ 0.001, 0.08 ]);


*/




	// figure 8.26 - making random settings, and 8 random presets to switch between
(
	// make the Tdef's envir a global variable for easier experimenting
d = Tdef(\cloud0).envir;
	// a pseudo-method to make random settings, kept in the Tdef's environment
		// randomize could also do limited variation on existing setting.
d.randSet = { |d|
	var randSet = ();
	q.paramRNames.do { |pName, i|
		randSet.put(pName,
			q.paramNames[i].asSpec.map([1.0.rand, 1.0.rand].sort)
		);
	};
	randSet;
};

/* 	test randSet:
d.current = d.randSet;
*/

// make 8 sets of parameter range settings:
d.setNames = (1..8).collect { |i| ("set" ++ i).asSymbol };
d.setNames.do { |key| d[key] = d.randSet; }

/*	test switching to the random presets
d.current = d.set1.copy;	// copy to avoid writing into a stored setting when it is current.
d.current = d.set3.copy;
d.current = d.set8.copy;
*/
);




	// ex. 8.27 - crossfading between different settings with a taskproxy

(
	// and some parameters for controlling the fade
d.stopAfterFade = false;
d.xfadeTime = 3;

d.morphtask = TaskProxy({
	var startSet = d[\current], endSet = d[\target];
	var stepsPerSec = 20;
	var numSteps = d.xfadeTime * stepsPerSec;
	var blendVal, morphSettings;

	if (d.target.notNil) {
		(numSteps).do { |i|
		//	["numSteps", i].postln;
			blendVal = (i + 1) / numSteps;
			morphSettings = endSet.collect({ |val, key|
				(startSet[key] ? val).blend(val, blendVal)
			});
			d.current_(morphSettings);
			(1/stepsPerSec).wait;
		};
		d.current_(d.target.copy);
		"morph done.".postln;
		if (d.stopAfterFade) { Tdef(\cloud0).stop; };
	};
}).quant_(0);		// no quantization so the task starts immediately

/* test morphing
(
Tdef(\cloud0).play;
d.target = d.set6.copy;
d.morphtask.play;
)
Tdef(\cloud0).stop;

	// playing a a finite cloud with tendency mask:
(
Tdef(\cloud0).play;		// begin playing
d.stopAfterFade = true; 	// end cloud when crossfade ends
d.xfadeTime = 10; 			// set fade time
d.target = d.set8.copy;		// and target
d.morphtask.play;			// and start crossfade.
)
*/

	// put fading into its own method, with optional stop.
d.fadeTo = { |d, start, end, time, autoStop|
	d.current = d[start] ? d.current;
	d.target = d[end];
	d.xfadeTime = time ? d.xfadeTime;
	if (autoStop.notNil) { d.stopAfterFade = autoStop };
	d.morphtask.stop.play;
};

/* 	// tests fadeTo:
Tdef(\cloud0).play;
d.fadeTo(\current, \set2, 20);
d.fadeTo(\current, \set6, 10);
d.fadeTo(\current, \set5, 3, true);

Tdef(\cloud0).play;
d.fadeTo(\current, \set1, 3, false);
*/
);



	// figure 8.28 is an image, the CloudGenMini GUI //




	// figure 8.29 - a lightweight graphical user interface for CloudGenMini
(
q.makeCloudGui = { |q, tdef, posPoint|
	var w, ezRangers, fdBox;
	var setMinis, skipjack;

	posPoint = posPoint ? 400@400;	// where to put the gui window

	w = Window.new("CloudGenMiniMetro",
		Rect.fromPoints(posPoint, (posPoint + (400@300)))).front;
	w.view.decorator_(FlowLayout(w.bounds.copy.moveTo(0, 0)));

	w.view.decorator.nextLine;

		// a just in time - gui for the Tdef
	z = TdefGui(tdef, parent: w);

	fdBox = EZNumber.new(w, 78@18, \Fade, ControlSpec(0.01, 100.0, \exp, 0.01, 3, nil),
		{ |nbx| tdef.envir.xfadeTime = nbx.value },
		 tdef.envir.xfadeTime, false, 35);

	~recordButton = Button(w, 51@19);
    ~recordButton.states_([["rec", Color.black, Color.white],["rec", Color.white, Color.red]]);
    ~recordButton.action = {|view|
	                        if (view.value==1) {s.record} {s.stopRecording}};
    ~recordButton.value=0;

	w.view.decorator.nextLine;


		// the range sliders display the current values
	ezRangers = ();

	q.paramRNames.do { |name, i|
		ezRangers.put(name,
		EZRanger(w, 400@20, name, q.paramNames[i],
			{ |sl| tdef.envir.current[name] = sl.value; },
			tdef.envir.current[name], labelWidth: 70, numberWidth: 50, unitWidth: 10)
			.round_([0.1, 0.00001, 0.0001, 0.0001, 0.01][i])
		);
	};




	Button.new(w, 126@20).states_([[\randomize]])
		.action_({
			tdef.envir.target_(d.randSet);
			tdef.envir.morphtask.stop.play;
		});



	Button.new(w, 126@20).states_([[\continuous], [\fadeStops]])
		.value_(tdef.envir.stopAfterFade.binaryValue)
		.action_({ |btn|
			tdef.set(\stopAfterFade, btn.value == 1)
		});

	Button.new(w, 126@20).states_([[\skipWatching], [\skipWaiting]])
		.action_({ |btn|
			[ { skipjack.play }, { skipjack.stop }][btn.value].value
		});

	w.view.decorator.nextLine;



			// skipjack is a task that survives cmd-period:
			// used here for lazy-updating the control views.
	skipjack = SkipJack({
		q.paramRNames.do { |name| ezRangers[name].value_(tdef.envir.current[name]) };
		fdBox.value_(tdef.envir.xfadeTime);

		// mark last settings that were used by color?
		// a separate color when changed?

	}, 0.5, { w.isClosed }, name: tdef.key);

	w.view.decorator.nextLine;

	// make a new layoutView for the 8 presets;
	// put button to switch to that preset,
	// a button to save current settings to that place,
	// and a miniview of the settings as a visual reminder in it.

		// make 8 setButtons
	tdef.envir.setNames.do { |setname, i|
		var minisliders, setMinis;
		var zone = CompositeView.new(w, Rect(0,0,45, 84));
		zone.decorator = FlowLayout(zone.bounds, 0@0, 5@0);
		zone.background_(Color.white);

		Button.new(zone, Rect(0,0,45,20)).states_([[setname]])
			.action_({
				// just switch: // tdef.envir.current.putAll(d[setname] ? ())
				tdef.envir.target = tdef.envir[setname];
				tdef.envir.morphtask.stop.play;
			});

		Button.new(zone, Rect(0,0,45,20))
			.states_([["save" ++ (i + 1)]])
			.action_({
				d[setname] = tdef.envir.current.copy;
				setMinis.value;
			});

		minisliders = q.paramRNames.collect { |paramRname|
			RangeSlider.new(zone, 45@8).enabled_(false);
		};
		setMinis = {
			q.paramRNames.do { |paramRname, i|
				var paramName = q.paramNames[i];
				var myrange = d[setname][paramRname];
				var unmapped = paramName.asSpec.unmap(myrange);
				minisliders[i].lo_(unmapped[0]).hi_(unmapped[1]);
			}
		};
		setMinis.value;
	};

/* 	Some extras:
	a volume slider for simple mixing,
	a popup menu for switching syndefnames;
	a button to stop/start the skipjack for refreshing,
	so one can use numberboxes to enter values.
*/


	EZSlider(w, 225@20, "vol", \amp, { |sl|tdef.set(\vol, sl.value) },
		0.6, false, 20, 36);

	StaticText.new(w, 60@20).string_("synthdef:").align_(\right);
	PopUpMenu.new(w, Rect(0,0,95,20))
		.items_([\sinGab, \sinWide, \sinPerc, \sinPercRev,
		         \noiseGab, \noiseWide, \noisePerc, \noisePrev,
	             \sqGab, \sqWide, \sqPerc, \sqPercRev,
	             \triGab, \triWide, \triPerc, \triPercRev])
		.action_({ |pop| tdef.envir.synName = pop.items[pop.value] });


};
q.makeCloudGui(Tdef(\cloud0))
);

)
descendants
«Re: CloudGenMiniExtraSynthDefs» by S P (private)
full graph
raw 15429 chars (focus & ctrl+a+c to copy)
comments
bateslewis user 05 Feb'13 18:40

this is not FM - just extended choice of synthDefs. Have posted FM2 version now...

rzzr user 11 Nov'15 09:31

Dependency: https://github.com/supercollider/sc3-plugins