-
Notifications
You must be signed in to change notification settings - Fork 2
/
3.Counting_otherBases.scd
554 lines (482 loc) · 14.1 KB
/
3.Counting_otherBases.scd
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
/*
- An implementation in SuperCollider of Tom Johnson's techniques, as described in the "Self-Similar Melodies" book -
by Darien Brito (2015)
http://www.darienbrito.com
This work is licensed under Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International Licence (CC BY-NC-SA 4.0)
To view a copy of this license go to:
http://creativecommons.org/licenses/by-nc-sa/4.0/
*/
/*_______________________________________________________________________
Counting in other bases
_________________________________________________________________________*/
/*_______________________
Constants
_________________________*/
//Test sound:
(
SynthDef(\sine, {|freq = 440, amp = 0.1, rel = 0.8|
var sig = SinOsc.ar(freq, 0, amp);
var env = EnvGen.kr(Env.perc(releaseTime: rel),doneAction:2);
sig = sig*env;
Out.ar(0, sig!2)
}).add;
// Parameters
~scale = Scale.minor;
~amp = Pfunc({ |ev| if(ev.degree < 1) { 0.1 } { ev.degree.reciprocal * 0.25}} );
)
/*___________________________________
Counting in other bases
_____________________________________*/
// A function to convert to any integer base N if N > 1
(~intBase = { | n , base|
var output = List.new;
if (base > 1) {
while {n != 0} {
r = n % base; // remainder
n = (n / base).floor; //quotient
output.add(r.asString);
};
output = output.reverse; //swap it
output = output.join("");
output.asInt;
} {"Input base must be bigger than 1".warn}
})
(
// Structure
var limit = 8;
var interval = 3;
var dur = 0.15;
var base = 8; // The radix
// Base derived serie
var dec = Array.series(limit, 0, interval);
var baseSeq = limit.collect{ |i| ~intBase.value(i, base)};
var seq = [dec,baseSeq].lace;
~sequence = List.new;
p = Pbindef(\differentBase1,
\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seq,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur
).quant_(0).play;
)
p.play;
p.stop;
~sequence.postln;
/*_______________________________________
Interlacing sequences with a base number
_________________________________________*/
a = 0 1 2 3 4 0 1 2 3 4 (...)
b = 0 0 0 0 0 1 1 1 1 1 (...)
r = 0 0 1 0 2 0 3 0 4 0 0 1 1 1 2 1 3 1 4 1 (...)
(
// Structure
var limit = 5;
var interval = 5;
var dur = 0.15;
var base = limit;
var mode = 'rightLeft'; //change to leftRight to flip the interlacing
// Make series
var totalSize = (limit* base);
var serA = Array.series(limit, 0, interval);
var serB = Array.series(limit, 0, interval);
var out1 = Pseq(serA,inf).asStream;
var out2 = Pstutter(limit, Pseq(serB,inf)).asStream;
var seq;
serA = serA ! base;
serA = serA.flatten;
serB = out2.nextN(totalSize);
// Two modes of reading
switch(mode, 'rightLeft',{seq = [serA,serB].lace}, 'leftRight', {seq = [serB, serA].lace });
~sequence = List.new;
p = Pbindef(\differentBase2,
\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seq,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
//\octave, Pseq([4,5,6,4],inf),
\dur, dur
).quant_(0).play;
)
p.play;
p.stop;
~sequence.postln;
/*________________
Base-Accumulative
__________________*/
a = 0 0 0 0 0 01 01 01 (...)
b = 0 01 012 0123 01234 0 01 012 (...)
r = 0 0 0 01 0 012 0 0123 0 01234 01 0 01 01 01 012 (...)
(
// Structure
var limit = 5;
var interval = 1;
var dur = 0.2;
var base = limit; // Set to limit for symmetry, but can be other values.
var mode = 'leftRight';
// Make series
var ser = Array.series(limit, 0, interval);
var sequenceA = Pn(Pser(ser,Pseries(1,1).asStream),inf).asStream;
var sequenceB = Pn(Pser(ser,Pseries(1,1).asStream),inf).asStream;
var returnN = ~numberItems.value(ser.size);
var seq;
sequenceA = limit.collect{|i| sequenceA.nextN(i+1) ! base};
sequenceB = limit.collect{|i| sequenceB.nextN(i+1) } ! base;
sequenceA = sequenceA.flatten;
sequenceB = sequenceB.flatten;
// Set mode
switch(mode, 'rightLeft',{seq = [sequenceA,sequenceB].lace}, 'leftRight', {seq = [sequenceA, sequenceB].lace });
seq = seq.flatten;
~sequence = List.new;
p = Pbindef(\differentBase3,
\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seq,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur,
\rel, 0.5 //Shorter to hear better
).quant_(0).play;
)
p.play;
p.stop;
~sequence.postln;
/*___________________
Interlaced stuttered
_____________________*/
1 2 3 4
1 4 2 4 3 4 4 4
1 4 4 2 4 4 3 4 4 4 4 4
1 4 4 4 2 4 4 4 3 4 4 4 4 4 4 4
(...)
(
// Structure
var limit = 12;
var interval = 1;
var dur = 0.15;
var restsCount = 3;
var tempo = 120;
// Create sequence
var seq = Array.series(limit, 0, interval);
var lastItem = seq.last;
var reps = 1 + (limit * (limit-1));
var engine,players;
var seqContainer = List.new;
seq.removeAt(seq.size-1);
engine = Pseq([Pseq(seq,1),Pn(lastItem, reps )],inf).asStream.nextN(limit**2) ! limit;
seqContainer = engine.collect{ |array, i|
var slice = array.size - (i * limit);
array = slice.collect{|i| array[i]}
};
seqContainer = seqContainer.collect{ |array, i|
var size = array.size;
var globalOffset = \ ! ((i*16) + i); //Rests for voice entrances
var rests = \ ! size; //
var new = [array,rests].lace;
new = new.replace([\], \ ! restsCount);
new = globalOffset++new
};
players = limit.collect{|i|
Pbind(\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seqContainer[i],inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur,
\stretch, 60/tempo //pulse
)
};
p = Ppar(players).play
)
/*___________________
Interlaced inverted
_____________________*/
1 8 1,2 8,7 1,2,3 8,7,6 1,2,3,4 8,7,6,5
// Function to get accumulative size
(
~getAccumulativeSize = {|n|
var amount = n * ((n/2) + (1/2));
amount
})
//Test
a.nextN(~getAccumulativeSize.value(8));
(
// Structure
var limit = 8;
var interval = 1;
var dur = 0.15;
var barDistance = 8; //In rests
var stepDiference = 0;
// Create sequence
var seq = Array.series(limit, 0, interval);
var inverted, rests, offset, final;
seq = Pn(Pser(seq,Pseries(1,1).asStream),inf).asStream;
seq = limit.collect{|i| seq.nextN(i+1)};
inverted = (limit-1) - seq;
offset = \ ! barDistance;
stepDiference = \ ! stepDiference;
rests = (inverted.size).collect{|i| \ ! (((limit-1) - i ))++offset };
seq = [seq,rests].lace.flatten;
inverted = [rests, inverted].lace.flatten;
inverted = stepDiference++inverted; //The distance between sequences
~sequence = List.new;
p = Ppar([
Pbind(\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seq,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur
),
Pbind(\instrument, \sine,
\scale, ~scale,
\degree, Pseq(inverted,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur
)]
).play;
)
p.stop;
p.play;
~sequence.postln;
/*_______________________________
Calculating sequences by radixes
_________________________________*/
Numbers with step 4 and base 5 //(Tom Jhonson's' example in the book) goes as follows:
4 8 12 16 20 24 28 (...)
//equals, in base 5 to:
4 13 22 31 40 44 103 (...)
// He adds 0's to compensate for a regular stream of 4 note sequences:
0004 0013 0022 0031 0040 0044 0103
Here two simple functions that give us the right sequences with or without compensation:
/////////////////////////////////////////////////////////////
// Function to get a radix sequence without 0' compensation
/////////////////////////////////////////////////////////////
(
~seqRadixSimple = {|size, step, base|
var sequence = List.new;
var transform;
size.do{ |i|
// Note: the .base method is an addition I've made to the class Integer in the standard SC distribution
transform = ( i + 1 * step ).base(base).asString;
transform.do{|item| sequence.add(item)};
};
sequence
}
)
~seqRadixSimple.value(2, 1, 2, 4)
//////////////////////////////////////////////////////////////////////////////
// Function to get a radix sequence with 0's compensation for a N note stream
//////////////////////////////////////////////////////////////////////////////
(
~seqRadix = {|size, step, base, compensation|
var sequence = List.new;
var transform;
size.do{ |i|
var tempItem;
transform = ( i + 1 * step).base(base).asString;
tempItem = ((transform.size) - compensation).abs;
tempItem = 0 ! tempItem;
sequence.add(tempItem++transform)
};
sequence = sequence.flatten;
sequence = sequence.collect{|item| if(class(item) == Integer) {item} {item.digit} } // Need to convert chars back to ints
}
)
~seqRadix.value(20, 4, 5, 4)
/*_______________________________
Sequence radix N
_________________________________*/
(
// Structure
var limit = 30;
var interval = 1;
var dur = 0.15;
var base = 8; //Play with
var step = 2; //these to get nice sequences
var compensation = 4; //To make N note sequences compensated with 0's
// Make sequence
var seq = ~seqRadix.value(limit, step, base, compensation);
seq = seq * interval;
~sequence = List.new;
p = Pbindef(\radixSeq1,
\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seq,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur
).quant_(0).play;
)
p.play;
p.stop;
~sequence.postln;
/*_______________________________
Other ordering radix N
_________________________________*/
0033 0132 0231 0330 1023
1122 2212 2113 2020 1321 ...
(
// Structure
var limit = 30;
var interval = 1;
var dur = 0.15;
var base = 4; //Play with
var step = 15; //these to get nice sequences
var compensation = 4; //To make N note sequences compensated with 0's
// Get sequence
var seq = ~seqRadix.value(limit, step, base, compensation);
seq = seq * interval;
~sequence = List.new;
p = Pbindef(\radixSeq,
\instrument, \sine,
\scale, ~scale,
\degree, Pseq(seq,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur
).quant_(0).play;
//seq.postln;
)
~sequence.postln
/*_________________________________________________________________
Binary sequences with arbitrary initial positions for each number
___________________________________________________________________*/
1 = [ 4, , , , , 4, , , , , 4, , , ]
2 = [ , 6, , , , , 6, , , , , 6, , ]
3 = [ , , 8, , , , , 8, , , , , 8, ]
4 = [ , , , 4, , , , , 4, , , , , 4]
(
// Structure
var limit = 4;
var interval = 1;
var dur = 0.15;
var voices = 7;
var positions = [1,2,5,3,7,8,3]; //Starting positions for each voice in order;
var flip = true;
var base = 2;
var step = 1; //This remains at 1 so it's always a binary state
var compensation = 4; //To make N note sequences compensated with 0's
// Make sequence
var masterSeq = ~seqRadix.value(limit, step, base, compensation);
var arbitrary = ();
var restCount = Pn(\).asStream.nextN(voices-1);
restCount = restCount ! masterSeq.size;
if (flip) {masterSeq = (masterSeq - 1).abs}; //Flip interval direction
masterSeq = [masterSeq, restCount].lace;
masterSeq = masterSeq.flatten;
masterSeq = masterSeq * interval;
voices.do{|i|
var offset = \ ! i;
var currentVoice = offset ++ (masterSeq+positions[i]) ;
currentVoice.postln;
arbitrary.put(i,
Pbind(\instrument, \sine,
\scale, ~scale,
\degree, Pseq(currentVoice,inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur)
);
};
Ppar(arbitrary.asList).play
)
/*____________________________
Encoding Base 2 number series
______________________________*/
I II III IV V
binary signs occurrences position position2
0000 + 0 0 0
0001 - 1 1 1
0010 - 1 -1 2
0011 + 2 0 1
0100 - 1 1 -4
binary: the binary representation of the number (8 bits, here only 4 shown)
signs: '+' if the number of 1s is even '-' if odd
occurrences: shows the total number of 1s in each binary number
position: odd columns of the binary number equal to -1, even ones to +1 ones; shows the sum
position: columns (counting from last) go in series doubling their value and interchanging signs (1,-2,4,-8,etc...)
// Modified to produce radix series in binary arrays using the .asBinaryDigits method
(
~seqRadixCluster = {|size, step|
var sequence = List.new;
var transform;
size.do{ |i|
var tempItem;
transform = (i * step).asBinaryDigits;
sequence.add(transform)
};
sequence
})
//Test it:
x = ~seqRadixCluster.value(16,1)
(
var limit = 255;
var interval = 1;
var dur = 0.15;
var offset = 0;
// Master sequence
var masterSeq = ~seqRadixCluster.value(limit+1, 1);
var columns = ();
var signsList = List.new;
var occurencesOfOne = List.new;
var positionVal = List.new;
var positionVal2 = List.new;
var counter = 0;
//Occurences
masterSeq.do{|item|
var occurence = item.occurrencesOf(1);
if(occurence.even) {signsList.add('+')}
{signsList.add('-')};
//Occurences of 1
occurencesOfOne.add(occurence);
} ;
//Positions value assignment 1 (in binary, 8 bits, the biggest possible value is 255)
masterSeq.do{|item|
var counter = 0;
if(item[item.size-1] == 1) {counter = counter + 1};
if(item[item.size-3] == 1) {counter = counter + 1};
if(item[item.size-5] == 1) {counter = counter + 1};
if(item[item.size-7] == 1) {counter = counter + 1};
if(item[item.size-2] == 1) {counter = counter - 1};
if(item[item.size-4] == 1) {counter = counter - 1};
if(item[item.size-6] == 1) {counter = counter - 1};
if(item[item.size-8] == 1) {counter = counter - 1};
positionVal.add(counter);
};
//Positions value assignment 2
masterSeq.do{|item|
var counter = 0;
if(item[item.size-1] == 1) {counter = counter - 1};
if(item[item.size-2] == 1) {counter = counter + 2};
if(item[item.size-3] == 1) {counter = counter - 4};
if(item[item.size-4] == 1) {counter = counter + 8};
if(item[item.size-5] == 1) {counter = counter - 16};
if(item[item.size-6] == 1) {counter = counter + 32};
if(item[item.size-7] == 1) {counter = counter - 64};
if(item[item.size-8] == 1) {counter = counter + 128};
positionVal2.add(counter);
};
columns.put(\sequence, masterSeq * interval + offset);
columns.put(\evenOnes, signsList);
columns.put(\occOfOne, occurencesOfOne * interval + offset);
columns.put(\position, positionVal * interval + offset);
columns.put(\position2, positionVal2 * interval + offset);
//Now we can use some of this series to sequence:
p = Pbindef(\columns,
\instrument, \sine,
\scale, ~scale,
\degree, Pseq(columns[\position],inf),
//\degree, Pseq(columns[\position2],inf),
\amp, ~amp,
\recorder, Pfunc({ |ev| ~sequence.add(ev.degree) }),
\dur, dur
).quant_(0).play;
)
p.play;
p.stop;
~sequence.postln;