-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabrics.ck
583 lines (449 loc) · 11.6 KB
/
fabrics.ck
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
// ********************************** //
// Fabrics [fabrics_lap.ck] v. 3.0
// Apr 08 - Apr 09
// Updated, Sept. 2015
//
// A Musical Instrument for Creating Textures and Sequences
//
// by Scott Smallwood
//
//
"224.0.0.1" => string serverName;
1 => int call;
// channels
dac.channels() => int chanOut;
// tuning system magic numbers
6 => int voices;
8 => int partials;
60 => float baseFundamental;
5 => int bases;
1.5 => float baseFactor;
5 => int baseStep;
// init reverb mix
.01 => float verb;
// size of freq ridge (deviation)
0 => float devi;
// time between freq mods
30 => int ridgeSize;
// sequence on/off
0 => int space;
// pulse on/off
0 => int goPulse;
4::second => dur beat;
if (me.args() > 0)
{
Std.atof(me.arg(0)) => baseFundamental;
Std.atoi(me.arg(1)) => bases;
Std.atof(me.arg(2)) => baseFactor;
Std.atoi(me.arg(3)) => baseStep;
}
// total pitches
(bases - 1) * baseStep => int stepMax;
// main pitch table
float step[stepMax];
// random number seed
Std.rand2f(.01,.09) => float seed;
seed::second => now;
// trigger for new texture
1 => int bang;
// **** AUDIO CHAIN
// oscillator array
SinOsc s[voices][partials];
// main gain
Gain mainOut[chanOut];
// envelopes per channels
ADSR e[chanOut];
// reverberators per channels
JCRev mainRev[chanOut];
// submaster gains voice
Gain sub[voices];
// pans per voice
Pan2 p[voices];
// stereo chain
if (chanOut == 2)
{
// main bus setup
for (0 => int i; i < chanOut; i++) {
mainOut[i] => e[i] => mainRev[i] => dac.chan(i);
0.0 => mainOut[i].gain;
verb => mainRev[i].mix;
e[i].set(50::ms, 0::ms, 1, 50::ms);
}
// voice assignments and panning
for (0 => int i; i < voices; i++) {
// spread voices evenly across field
(((1. / (voices - 1)) * i) * 2) - 1 => p[i].pan;
sub[i] => p[i].left => mainOut[0];
sub[i] => p[i].right => mainOut[1];
1 => sub[i].gain;
}
}
// multi-channel chain
else
{
// main bus setup
for (0 => int i; i < chanOut; i++) {
sub[i] => mainOut[i] => e[i] => mainRev[i] => dac.chan(i);
e[i].set(500::ms, 0::ms, 1, 500::ms);
0.0 => mainOut[i].gain;
1 => sub[i].gain;
verb => mainRev[i].mix;
}
}
float partialgain_current[partials];
float partialgain_target[partials];
for (0 => int i; i < voices; i++)
for (0 => int j; j < partials; j++){
0.1 => s[i][j].gain => partialgain_target[j];
partialgain_target[j] => partialgain_current[j];
}
// controller boxes
int control[99];
// control events
Event fabON, fabOFF, screenFresh;
// main volume knob
0.0 => float volMain;
1::second => now;
// **** MODULES
spork ~ screenPanel();
spork ~ pitchFab();
spork ~ keys();
spork ~ tweaker();
spork ~ modulator();
for (0 => int partial; partial < 8; partial++)
spork ~ gainEnv(partial);
spork ~ netComX();
spork ~ netComR();
1::second => now;
fabON.signal(); //turn on on startup
1::day => now; //hang out
// ********* FUNCTIONS ********* FUNCTIONS ********* FUNCTIONS ********* FUNCTIONS
// ####
// ####
// **** CREATE PITCH FABRIC
fun void pitchFab()
{
while (true)
{
fabON => now; // start switch
// create all voices
for (0 => int i; i < voices; i++)
spork ~ buildWave(i, (1. / voices));
for (0 => int i; i < chanOut; i++)
1 => e[i].keyOn;
e[0].attackTime() => now;
fabOFF => now; //stop switch
for (0 => int i; i < chanOut; i++)
1 => e[i].keyOff;
e[0].releaseTime() => now;
}
}
// ####
// ####
fun void buildWave(int wavNum, float baseGain)
{
// ## POPULATE ARRAY OF PITCHES (step) **
baseFundamental * Std.rand2(1,3) => float base;
0 => int k;
for (1 => int i; i < bases; i++)
{
for (0 => int j; j < baseStep; j++)
{
base + (j * (((base * baseFactor) - base) / baseStep)) => step[k];
k++;
}
base * baseFactor => base;
}
// ## Build Additive Waveforms
// * partials - number of components
// * wavNum - this waveform (current)
// * baseGain - the gain threshold of this stage
Std.rand2(0, stepMax / 2) => int startStep; // pick any freq from first half
(stepMax - startStep) / partials => int partInv; // interval size btw partials
// assign partial freqs, gain, and assign to channel
for (0 => int i; i < partials; i++)
{
s[wavNum][i] =< sub[wavNum];
s[wavNum][i] => sub[wavNum];
step[startStep + (partInv * i)] => s[wavNum][i].freq;
(1. / (i + 1)) * baseGain => s[wavNum][i].gain;
}
}
// ####
// ####
// modulates freq of all of the oscs a tiny bit, randomized
fun void modulator()
{
while (true)
{
for (0 => int j; j < voices; j ++)
{
for (0 => int k; k < partials; k++)
s[j][k].freq() + Std.rand2f(-devi * (k + 1), devi * (k + 1))
=> s[j][k].freq;
}
ridgeSize * Std.rand2f(0.9, 1.1)::ms => now;
screenFresh.signal();
}
}
// ####
// ####
fun void keys ()
{
// **** KEYBOARD SETUP
Hid kb;
HidMsg msg;
if( !kb.openKeyboard( 0 ) ) me.exit();
// key numbers
[53, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 45, 46,
20, 26, 8, 21, 23, 28, 24, 12, 18, 19, 47, 48, 49,
4, 22, 7, 9, 10, 11, 13, 14, 15, 51, 52,
29, 27, 6, 25, 5, 17, 16, 54, 55, 56,
44]
@=> int key[];
while( true )
{
kb => now;
while( kb.recv(msg) )
{
screenFresh.signal(); //trigger for screen refresh
for (0 => int i; i < key.cap(); i++)
{
if ((msg.which == key[i]) && msg.isButtonDown())
1 => control[i];
if (msg.isButtonUp())
0 => control[i];
}
}
}
}
// ####
// ####
fun void tweaker()
{
0.001 => float slew;
while (true)
{
// volume controls
(control[13] * -.2) * slew + volMain => volMain;
(control[14] * -.1) * slew + volMain => volMain;
(control[15] * -.05) * slew + volMain => volMain;
(control[16] * -.01) * slew + volMain => volMain;
(control[19] * .01) * slew + volMain => volMain;
(control[20] * .05) * slew + volMain => volMain;
(control[21] * .1) * slew + volMain => volMain;
(control[22] * .2) * slew + volMain => volMain;
// limiter
if (volMain <= 0)
0 => volMain;
if (volMain >= .999)
.999 => volMain;
// assign volume to all channel gains
for (0 => int i; i < chanOut; i++)
volMain => mainOut[i].gain;
// freq deviation factor
(control[45] * .005) + devi => devi;
(control[44] * -.005) + devi => devi;
if (devi >= 1) 1 => devi;
if (devi <= 0) 0 => devi;
0 => control[44] => control [45];
if (control[38]) fabON.signal();
if (control[37]) fabOFF.signal();
if (control[32]) 1 => goPulse;
if (control[29]) 0 => goPulse;
1::ms => now;
}
}
// ####
// ####
fun void gainEnv(int j)
{
while (true) {
Std.rand2f(.0001,.0003) => float bump;
Std.rand2(10,3000) => int rampTime;
if (control[27 + j])
{
for (0 => int t; t < rampTime / 2; t++)
{
for (0 => int i; i < voices; i++)
{
s[i][j].gain() + bump => s[i][j].gain;
if (s[i][j].gain() >= .5) .5 => s[i][j].gain;
}
1::ms => now;
}
for (0 => int t; t < rampTime / 2; t++)
{
for (0 => int i; i < voices; i++)
{
s[i][j].gain() - bump => s[i][j].gain;
if (s[i][j].gain() <= .01) .01 => s[i][j].gain;
}
1::ms => now;
}
}
1::ms => now;
}
}
// ####
// ####
fun void screenPanel()
{
while (true) {
for (0 => int i; i < 40; i++)
<<< " ", " " >>>;
<<< " !!! F A B R I C S !!!", " " >>>;
<<< " ++++++++++++++++++++++++++++++++++++++++++++++++++++", " " >>>;
<<< " ", " " >>>;
<<< " ", chanOut, " channels">>>;
<<< " ", partials, " partials per ", voices, " voices.">>>;
<<< " ", baseFundamental, " Hz., ", bases, " bases.">>>;
<<< " Base ratio of ", baseFactor, ". ", baseStep, " scale members.">>>;
<<< " ", " " >>>;
<<< " Voices and their first four partials:", " ">>>;
<<< " ", " " >>>;
for (0 => int v; v < voices; v++)
<<<"{", s[v][0].freq(), "} ","{", s[v][1].freq(), "} ","{", s[v][2].freq(), "} ","{", s[v][3].freq(), "} ">>>;
<<< " ", " " >>>;
<<< " Partial Gains for All Voices (0-7):", " ">>>;
<<< " ", " " >>>;
<<<"{ ", s[0][0].gain(), " }", "{ ", s[0][1].gain(), " }", "{ ", s[0][2].gain(), " }", "{ ", s[0][3].gain(), " }">>>;
<<<"{ ", s[0][4].gain(), " }", "{ ", s[0][5].gain(), " }", "{ ", s[0][6].gain(), " }", "{ ", s[0][7].gain(), " }">>>;
<<< " ", " " >>>;
<<< " ++++++++++++++++++++++++++++++++++++++++++++++++++++", " " >>>;
<<< " ", " " >>>;
<<< " --- -- - + ++ +++ (volume)", " " >>>;
<<< " [q][w][e][r] [u][i][o][p] ", " {", volMain, "}" >>>;
<<< " ", " " >>>;
<<< " partials 0 1 2 3 4 5 6 7 (timbral bumps)", " " >>>;
<<< " [s][d][f][g][h][j][k][l] ", " ">>>;
<<< " ", " " >>>;
<<< " on/off drift", " ">>>;
<<< " - + - + ", " " >>>;
<<< " [z][x] [,][.] ", " {", devi, "}" >>>;
<<< " ", " " >>>;
<<< " This Hostname: ", Std.getenv("NETNAME"), " " >>>;
<<< " ++++++++++++++++++++++++++++++++++++++++++++++++++++", " " >>>;
<<< " ", " " >>>;
50::ms => now;
}
}
// ####
// ####
fun void netComX()
{
//OSC stuff
OscSend xmit;
xmit.setHost(serverName, 6449);
<<<"comx">>>;
// handshake introduction (machine USER.local here)
// until answered
while (call)
{
//send ping
xmit.startMsg("ping", "s");
Std.getenv("NETNAME") => xmit.addString;
1::second => now;
}
}
// ####
// ####
fun void netComR()
{
OscRecv recv;
6448 => recv.port;
recv.listen();
// respond to blob messages - types, parameter one, paramter 2
recv.event ("doit, i i f") @=> OscEvent blob;
while (true)
{
blob => now;
while (blob.nextMsg() != 0)
{
blob.getInt() => int job;
blob.getInt() => int p1;
blob.getFloat() => float p2;
// value of 1 shuts off sound
if (job == 1) 0 => volMain;
// value of 2 - frequency skips
if (job == 2)
{
for (0 => int cnt; cnt < p1; cnt++)
{
float tmp[voices][partials];
for (0 => int i; i < voices; i++)
{
for (0 => int j; j < partials; j++)
{
s[i][j].freq() => tmp[i][j];
step[Std.rand2(0, stepMax - 1)] => s[i][j].freq;
}
}
p2 * (1 + Std.rand2f(0, .1))::ms => now;
for (0 => int i; i < voices; i++)
for (0 => int j; j < partials; j++)
tmp[i][j] => s[i][j].freq;
p2 * (1 + Std.rand2f(0, .1))::ms => now;
}
}
// value of 3 - ridge size change
if (job == 3)
{
p1 => ridgeSize;
}
// value of 4 - freq deviation change
if (job == 4)
{
p2 => devi;
}
// value of 4 - freq deviation change
if (job == 5)
{
p2 => volMain;
}
}
}
}
// #### for testing below
// ####
fun void freqShow() //feedback for oscillators freqs
{
while (true) {
for (0 => int i; i < voices; i++)
for (0 => int j; j < partials; j++)
<<<"Voice ", i, " - Partial ", j, " - Frequency ", s[i][j].freq()>>>;
1000::ms => now;
}
}
// ####
// ####
fun void gainShow() //feedback for gains
{
while (true)
{
for (0 => int i; i < chanOut; i++)
<<<"Main Vol ", i, " - ", mainOut[i].gain()>>>;
1000::ms => now;
}
}
fun void freqTest()
{
4::second => now;
for (0 => int cnt; cnt < Std.rand2(6, 12); cnt++)
{
Std.rand2(10,40)::ms => now;
float tmp[voices][partials];
for (0 => int i; i < voices; i++)
{
for (0 => int j; j < partials; j++)
{
s[i][j].freq() => tmp[i][j];
step[Std.rand2(0, stepMax - 1)] => s[i][j].freq;
}
}
Std.rand2(10,50)::ms => now;
for (0 => int i; i < voices; i++)
for (0 => int j; j < partials; j++)
tmp[i][j] => s[i][j].freq;
}
}