File: recursive_phrasing.schelp

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (596 lines) | stat: -rw-r--r-- 11,877 bytes parent folder | download | duplicates (2)
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
title:: recursive_phrasing
summary:: Recursive phrases and granular composite sounds
categories:: Libraries>JITLib>Tutorials
related:: Overviews/JITLib, Classes/Pdef, Classes/PlazyEnvirN

Pdef can be used as a global storage for event patterns. Here a way is provided by which these definitions can be used as an instrument that consists of several events (a emphasis::phrase::), such as a cloud of short grains. Furthermore, this scheme can be applied recursively, so that structures like a cloud of clouds can be constructed.

When the event type code::\phrase:: is passed in, the event looks for a pattern in code::Pdef.all:: if it can find a definition.
list::
## if it finds one it plays this pattern in the context of the outer pattern's event.
## f there is no definition to be found there, it uses a link::Classes/SynthDef:: with this name, if present.
::

When passing a emphasis::function:: to Pdef it creates a PlazyEnvirN internally. Its function is evaluated in the context of the input event (see link::Classes/PlazyEnvirN::) which should return a pattern or a stream. Note that this doesn't allow the usual access of the outer environment from within the function.

code::
(
s.boot;

SynthDef(\pgrain,
	{ arg out = 0, freq=800, sustain=0.001, amp=0.5, pan = 0;
		var window;
		window = Env.sine(sustain, amp * AmpCompA.kr(freq));
		Out.ar(out,
			Pan2.ar(
				SinOsc.ar(freq),
				pan
			) * EnvGen.ar(window, doneAction:2)
		)
	}
).add;

SynthDef(\noiseGrain,
	{ arg out = 0, freq=800, sustain=0.001, amp=0.5, pan = 0;
		var window;
		window = Env.perc(0.002, sustain, amp * AmpCompA.kr(freq));
		Out.ar(out,
			Pan2.ar(
				Ringz.ar(PinkNoise.ar(0.1), freq, 2.6),
				pan
			) * EnvGen.ar(window, doneAction:2)
		)
	}
).add;



Pdef(\sweep, { arg sustain=1, n=8, freq=440, ratio=0.1;
	Pbind(
		\instrument, \pgrain,
		\dur, sustain.value / n,
		\freq, Pseq((1..n)) * ratio + 1 * freq.value // freq is a function, has to be evaluated
	)
});
Pdef(\sweep2, { arg sustain=1, n=8, freq=440, ratio=0.1;
	Pbind(
		\instrument, \noiseGrain,
		\dur, sustain.value / n, // sustain is also a function, has to be evaluated
		\freq, Pseq((1..n).scramble) * ratio + 1 * freq.value,
		\recursionLevel, 2
	)
});
Pdef(\sweep3, { arg freq=440;
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\freq, Pfunc({ rrand(0.8, 1.3) }) * freq.value,
		\dur, 0.3,
		\legato, 1.3,
		\n, 5
	)
});
)




// the pattern that is found in Pdef.all (or your own defined library) is truncated in time
// using the sustain provided by the outer pattern.
(
Pbind(
	\type, \phrase, // phrase event from global library
	\instrument, \sweep,
	\n, 15,
	\degree, Pseq([0, 4, 6, 3], inf),
	\sustain, Pseq([1.3, 0.2, 0.4],inf)
).play
)

// multichannel expansion is propagated into the subpatterns
(
Pbind(
	\type, \phrase, // phrase event from global library
	\instrument, \sweep,
	\n, 15,
	\degree, Pseq([0, 0, 6, 3], inf) + Prand([0, [0, 3], [0, 5], [0, 15]], inf),
	\ratio, Prand([ 0.1, 0.1, [0.1, -0.1] ], inf)
).play
)

// various instruments and synthdefs can be used on the same level
(
Pbind(
	\type, \phrase,
	\instrument, Pseq([\sweep, \default, \sweep2, \sweep3, \pgrain, \pgrain], inf),
	\degree, Pseq([0, 3, 2], inf),
	\dur, Pseq([1, 0.5], inf) * 0.7,
	\n, Pseq([4, 6, 25, 10], inf),
	\ratio, Prand([0.03, 0.1, 0.4, -0.1],inf) + Pseq([0, 0, [0, 0.02]], inf),
	\legato, Pseq(#[0.5, 1, 0.5, 0.1, 0.1],inf)
).play;
)


//////// of course also a patten can be used directly in a Pdef

(
Pdef(\sweep,
	Pbind(
		\instrument, Pseq([\pgrain, \noiseGrain],inf),
		\dur, Pseq([1, 2, 1, 3, 1, 4, 1, 5]) * 0.05,
		\legato, Prand([0.5, 0.5, 3],inf)
	)
)
)

// play directly, emebdded in stream (see Pdef.help)

Pn(Pdef(\sweep), 2).play;
Pdef(\sweep).fork; // play without changing player state (see Pdef.help)


// play within a pattern
(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\degree, Pseq([0, 1b, 4, 2, 3, 1b], inf),
	\pan, Pfunc(#{ 1.0.rand2 })
).play
)



//////// recursion examples //////////

// the given pattern can be recursively applied to itself
// resulting in selfsimilar sound structures, like lindenmeyer systems (see also Prewrite)
// special care is taken so that no infinite loops can happen.
// just like with non recursive phrasing, new values override old values,
// any values that are not provided within the pattern definition
// are passed in from the outer event.



(
Pdef(\sweep, { arg dur=1, n=4, freq=440, ratio=0.3;
	Pbind(
		\instrument, \pgrain,
		\dur, dur.value / n,	// now dur is dependant on outer dur.
		\freq, Pseries(1, 1, inf) * ratio + 1 * freq.value % 17000
	)
});
)


// no recursion
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, Pseq((0..5),inf)
	).play;
)

// no recursion, with legato > 1.0 and varying notes
// note how subpatterns are truncated to note length
// provided by outer pattern (in this case determined by legato)
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, Pseq((0..5),inf),
		\legato, Pseq([1.2, 2.8, 0.3], inf)
	).play;
)

// to block the proliferation of \legato into the phrase, set \transparency to 0
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\transparency, 0,
		\degree, Pseq((0..5),inf),
		\legato, Pseq([1.2, 2.8, 0.3], inf)
	).play;
)


// recursion over one level
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, Pseq([0, 1, 2, 3], inf),
		\recursionLevel, 1
	).play
)

// recursion over one level: legato is recursively applied
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, Pseq([0, 1, 2, 3], inf),
		\legato, Pseq([0.5, 1, 1.5, 1.8], inf),
		\recursionLevel, 1
	).play
)

// to block the proliferation of properties such as \legato and \degree
// into the phrase, set \transparency to 0.
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, Pseq([0, 1, 2, 3], inf),
		\legato, Pseq([0.5, 1, 1.5, 1.8], inf),
		\recursionLevel, 1,
		\transparency, 0
	).play
)

// recursion over 3 levels: legato is recursively applied
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, 1,
		\legato, Pseq([0.5, 1, 1.3], inf),
		\recursionLevel, 3,
		\n, 3
	).play
)

// to block the proliferation of \legato into the phrase, set \transparency to a level at which
// to stop passing the value.
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, 1,
		\legato, Pseq([0.5, 1, 2.3], inf),
		\recursionLevel, 3,
		\n, 3,
		\transparency, Pstutter(8, Prand([0, 1, 2], inf))
	).play
)



// to modify this recursion, assign values explicitly:
(
Pdef(\sweep, { arg dur=1, n=4, ratio=0.5, freq=440;
	var legato;
	freq = freq.value;
	legato = freq % 200 / 200 * 3 + 0.2;
	Pbind(
		\instrument, \pgrain,
		\dur, dur.value / n,
		\legato, legato,
		\freq, Pseq((1..n) * ratio + 1 * freq)
	)
});
)

// recursion over one level: degree is assigned to each phrase,
// because freq is calculated internally and overrides degree on the second level
(
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\degree, Pseq((0..10),inf),
		\recursionLevel, 1
	).play
)



// recursion over two levels
(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\degree, 0,
	\recursionLevel, 2
).play
)

// recursion over three levels with variable number of grains
(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\degree, -5,
	\n, Pseq([1, 2, 3],inf),
	\recursionLevel, 3
).play
)


// "zoom" in
TempoClock.default.tempo = 0.2;
TempoClock.default.tempo = 1.0;


// recursion over variable levels
(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\n, Prand([2, 7, 3], inf),
	\degree, -5,
	\recursionLevel, Prand([0, 1, 2],inf)
).play
)



// replace the frequency based pattern with a degree based pattern
(
Pdef(\sweep, { arg sustain=1, n=8, degree=0, ratio=1;
	Pbind(
		\instrument, \pgrain,
		\dur, sustain.value / n,
		\degree, Pseq((1..n)) * ratio + 1 + degree.value
	)
});
)


// drunken master
(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\n, Prand([2, 4, 3, 8], inf),
	\degree, Pseq([-5, 0, -2], inf),
	\legato, Pseq([1.4, 0.5, 2], inf),
	\scale, #[0, 2, 5, 7, 10],
	\recursionLevel, Prand([0, 1, 2],inf)
).play
)


(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\synthDef, Prand([\pgrain, \default, \noiseGrain],inf),
	\n, Prand([2, 4, 3, 8], inf),
	\degree, Pseq([-5, 0, -2], inf),
	\recursionLevel, Prand([0, 1],inf)
).play
)


// use a different parent event in the inner pattern
(
e = Event.default;
e.use { ~sustain = { 2.0.exprand(0.05) } };
Pdef(\sweep, { arg sustain=1, n=8, degree=0, ratio=1;
	Pbind(
		\parent, e, // replace by some other event
		\instrument, \pgrain,
		\dur, sustain.value / n,
		\degree, Pseq((1..n)) * ratio + 1 + degree.value
	)
});
)


(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\n, Prand([2, 4, 3, 8], inf),
	\degree, Pseq([-5, 0, -2], inf),
	\recursionLevel, Prand([0, 1],inf)
).play
)


// pass in a pattern from outside

(
Pdef(\sweep, { arg sustain=1, n=8, degree=0, ratio=1;
	n = n.value;
	Pbind(
		\instrument, \pgrain,
		\dur, sustain.value / n,
		\degree, Pseq([ 1, 2, 3, 4, 5 ] * ratio + 1 + degree.value) )
});
)


(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\n, { Pshuf([2, 4, 3, 8, 16, 32], inf) }, // use a function to insulate from embedInStream
	\degree, Pseq([-5, 0, -2], inf),
	\recursionLevel, Prand([0, 1],inf)
).play
)




// recursion inside the pattern definition

(
Pdef(\sweep2, { arg sustain=1, n=2, degree=0, ratio=1;
	Pbind(
		\type, \phrase,
		\instrument, \sweep,
		\dur, sustain.value / n,
		\degree, Pseq((1..5).scramble * ratio + 1 + degree.value),
		\recursionLevel, 2
	)
});
)

(
Pbind(
	\type, \phrase,
	\instrument, \sweep2,
	\n, 3,
	\degree, Pseq([-5, 0, -2], inf)
).play
)


// instruments do not crossfade while they play (to make phrasing more efficient).


(
Pbind(
	\type, \phrase,
	\instrument, \sweep,
	\n, 3,
	\degree, Pseq([0, 2b, 3, 4], inf),
	\dur, 2,
	\legato, 2
).play
)

// change pattern definition while playing:
(
Pdef(\sweep,
	Pbind(
		\instrument, \pgrain,
		\dur, exprand(0.01, 0.1),
		\legato, rrand(0.01, 2.0),
		\octave, rrand(5, 7)
	)
)
)


// koch "snowflake"
(
Pdef(\koch, { arg dur=1, freq=440;
	Pbind(
		\dur, dur.value / 3,
		\freq, freq.value * Pseq([1, 1.2, 1])
	)
});
)

(
	Pbind(
		\type, \phrase,
		\instrument, \koch,
		\synthDef, \pgrain,
		\dur, 9,
		\recursionLevel, 2,
		\legato, 1.1
	).play
)

(
	Pbind(
		\type, \phrase,
		\instrument, \koch,
		\synthDef, \pgrain,
		\dur, 9,
		\recursionLevel, 4,
		\legato, 1.1
	).play
)

(
Pdef(\koch, { arg dur=1, degree=0;
	Pbind(
		\dur, dur.value / 3,
		\degree, degree + Pseq([0, 2, 0])
	)
});
)





// soundfile example


(
SynthDef(\play_from_to, { arg out, bufnum, from=0.0, to=1.0, sustain=1.0;
	var env;
	env = EnvGen.ar(Env.linen(0.01, sustain, 0.01), 1, doneAction:2);
	Out.ar(out,
		BufRd.ar(1, bufnum,
			Line.ar(from, to, sustain) * BufFrames.kr(bufnum)
		) * env
	)


}).add;
)

b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav")

(
Pdef(\poch, { arg sustain=1.0, from=0.0, to=1.0, n=3;
		var step;
		sustain = sustain.value;
		step = (to - from) / n;
		Pbind(
			\instrument, \play_from_to,
			\from, Pseries(from, step, n),
			\to, Pseries(from, step, n) + step,
			\legato, 1.0,
			\dur, sustain / n
		)
})
)

// this plays it straight
(
Pbind(
	\type, \phrase,
	\instrument, \poch,
	\recursionLevel, 3,
	\from, 0,
	\to, 1,
	\dur, 3,
	\bufnum, b
).play
)

// now turn round every middle part of every middle part
(
Pdef(\poch, { arg sustain=1.0, from=0.0, to=1.0, n=3;
	var step, f, t, i;
	sustain = sustain.value;
	step = (to - from) / n;
	f = Array.series(n, from, step) +.t [0.0, step];
	i = n div: 2;
	f[i] = f[i].reverse;
	Pbind(
		\instrument, \play_from_to,
		[\from, \to], Pseq(f),
		\legato, 1.0,
		\dur, sustain / n
	)
})
)


// varying recursion
(
Pbind(
	\type, \phrase,
	\instrument, \poch,
	\recursionLevel, Prand([0, 1, 2, 3], inf),
	\from, 0,
	\to, Prand([-1, 1], inf),
	\dur, 3,
	\n, Prand([1, 2, 3], inf),
	\bufnum, b,
	\amp, 0.2
).play
)
::