-
Notifications
You must be signed in to change notification settings - Fork 1
/
SCML.scd
715 lines (609 loc) · 30.2 KB
/
SCML.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//************************ SCML (SuperCollider Machine Listening) ************************
//SEE THE README FOR OPERATING INSTRUCTIONS
( //#1 ======INITIALIZE SC SERVER ======
//if server fails to start and error encountered: try, try again...it's stubborn sometimes...
o = Server.local.options;
o.sampleRate = 44100;
o.blockSize = 512;
o.inDevice = nil; //use system default audio input (change value to manually configure input)
o.outDevice = nil; //use system default audio output (change value to manually configure output)
s.latency = 0.05; //0.2 is SC default
s.boot;
)
ServerOptions.devices; //evaluate this to see what audio input/output devices are available (if you want to customize your audio I/O configuration)
( //#2 ======SETUP======
//OSC In/Out ----------
~oscOut_weki = NetAddr("127.0.0.1", 6448); //<---OSC OUTPUT HOST & PORT CONFIG SETTINGS (currently set to default Wekinator input port)
~oscIn_weki_1 = {|numIn, defKey, port = 12000| //<---OSC INPUT CONFIG SETTINGS (currently set to default Wekinator output port)
var outBus;
outBus = Bus.control(s, numIn);
OSCdef.new(key: defKey.asSymbol, func: { |msg, time, addr, recvPort|
var outArray = Array.newClear(msg.size - 1); //(# of Weki outputs - 1) b/c msg[0] is msg name
for (1, msg.size - 1, {|i| outArray[i - 1] = msg[i];} ); //collect OSC msg vals into an array
outBus.setn(outArray);
//outBus.setnSynchronous(outArray); //would this yield better timing accuracy???
}, path: '/wek/outputs', recvPort: port);
outBus; //return bus for receiving all msg vals from Weki OSC output
};
//BUSSES-----------
~bus_input1 = Bus.audio(s, 1);
~bus_loudness = Bus.control(s, 1);
~bus_rms = Bus.control(s, 1);
~bus_mfcc = Bus.control(s, 13);
~bus_pitch = Bus.control(s, 2);
~bus_pitch2 = Bus.control(s, 3);
~bus_specCent = Bus.control(s, 1);
~bus_specFlat = Bus.control(s, 1);
~bus_onsets = Bus.control(s, 1);
~bus_dryIn1Amp = Bus.control(s, 1); //dry input level bus
~bus_vocAmp = Bus.control(s, 1); //voc proc synth sum level bus
~bus_wislAmp = Bus.control(s, 1); //whistle proc synth sum level bus
~bus_vocwislMix = Bus.control(s, 1); //mix balance between vocal FX synths and whistle FX synths
~bus_accompAmp = Bus.control(s, 1); //level of summed, mixed accomp synths
~bus_verbMix = Bus.control(s, 1);
~bus_verbRoom = Bus.control(s, 1);
~bus_vocSynthSum = Bus.audio(s, 2);
~bus_wislSynthSum = Bus.audio(s, 2);
~bus_dryIn1Amp.set(0.3);
~bus_accompAmp.set(1.0);
~bus_vocAmp.set(0.6); //two voc accomp synths, adjusting levels by ear and monitoring out meter
~bus_wislAmp.set(1.0);
~bus_vocwislMix.set(0.0); //0 = voc accomp only, 1 = whistle accomp only. Set via control routine
~bus_verbMix.set(0.3);
~bus_verbRoom.set(0.4);
//SYNTH DEFS ----------
SynthDef.new(\monAudioInput, {
arg inChannel = 0, amp = 1, outBus;
var sig = SoundIn.ar(bus: inChannel, mul: amp); //defaults to first channel of system audio input
OffsetOut.ar(outBus, sig);
}).add;
SynthDef.new(\Loudness, {
//arg input, buffer, outBus;
arg input, outBus;
var sigIn, fft, loudness;
sigIn = In.ar(input);
//fft = FFT(buffer, sigIn, wintype: 1);
fft = FFT(LocalBuf(1024), sigIn, wintype: 1); //using LocalBuf per HelpFile recommendation, 1024 frame size recommended for samp rates 44100 and 48000 and 2048 for rates 88200 and 96000
loudness = Loudness.kr(fft);
//loudness.poll(trig: 10, label: "Sones");
Out.kr(outBus, loudness);
}).add;
SynthDef.new(\RMS, {
arg input, numSamp = 40, outBus;
var sigIn, rms;
sigIn = In.ar(input);
rms = (RunningSum.kr(sigIn.squared, numSamp) / numSamp).sqrt;
//rms.poll(trig: 10, label: "RMS");
Out.kr(outBus, rms);
}).add;
SynthDef.new(\Pitch, {
arg input, ampThresh = 0.04, median = 7, minFreq = 60, maxFreq = 4000, outBus;
var sigIn, freq, hasFreq;
sigIn = In.ar(input);
# freq, hasFreq = Pitch.kr(sigIn, ampThreshold: ampThresh, median: median, minFreq: minFreq, maxFreq: maxFreq);
//freq = Lag.kr(freq.cpsmidi.round(1).midicps, 0.05);
Out.kr(outBus, [freq, hasFreq]);
//freq.poll(trig: 10, label: "Freq");
//hasFreq.poll(trig: 10, label: "HasFreq");
}).add;
SynthDef.new(\Pitch2, {
arg input, ampThresh = 0.04, median = 7, minFreq = 60, maxFreq = 4000, outBus;
var sigIn, freq, hasFreq, midinote;
sigIn = In.ar(input);
# freq, hasFreq = Pitch.kr(sigIn, ampThreshold: ampThresh, median: median, minFreq: minFreq, maxFreq: maxFreq);
//midinote = Lag.kr(freq.cpsmidi.round(1), 0.05);
midinote = freq.cpsmidi.round(1);
Out.kr(outBus, [freq, hasFreq, midinote]);
//freq.poll(trig: 10, label: "Freq");
//hasFreq.poll(trig: 10, label: "HasFreq");
}).add;
SynthDef.new(\MFCC, {
//arg input, buffer, outBus;
arg input, outBus;
var sigIn, fft, array;
sigIn = In.ar(input);
//fft = FFT(buffer, sigIn, wintype: 1);
fft = FFT(LocalBuf(1024), sigIn, wintype: 1);
array = MFCC.kr(fft); //outputs 13 coefficients by default
Out.kr(outBus, array);
//array.poll(trig: 10, label: "MFCCs");
}).add;
SynthDef.new(\specCent, {
arg input, outBus;
var sigIn, fft, centroid;
sigIn = In.ar(input);
fft = FFT(LocalBuf(2048), sigIn, wintype: 1);
centroid = SpecCentroid.kr(fft);
Out.kr(outBus, centroid);
}).add;
SynthDef.new(\specFlat, {
arg input, outBus;
var sigIn, fft, flatness, flatdb, flatdbScaled;
sigIn = In.ar(input);
fft = FFT(LocalBuf(2048), sigIn, wintype: 1);
flatness = SpecFlatness.kr(fft);
//flatdb = 10 * flat.log; //convert flatness to decibels
//flatdbScaled = LinLin.kr(flatdb, -45, -1.6, 0, 1).max(-10); // Rescale db roughly to 0...1
Out.kr(outBus, flatness);
}).add;
SynthDef.new(\onsets, {
arg input, outBus, threshold = 0.5, trigtime = 0.1, odtype = \rcomplex, reltime = 1.0, floor = 0.1;
var sigIn, fft, onsets, trigger;
sigIn = In.ar(input);
fft = FFT(LocalBuf(1024), sigIn, wintype: 1);
onsets = Onsets.kr(fft, threshold, odtype, reltime, floor);
trigger = Trig1.kr(onsets, trigtime); //if onset detected, send output 1 for trigtime seconds to control bus
//trigger.poll(10, label: 'trig'); //uncomment this to monitor trigger val constantly
Out.kr(outBus, trigger);
}).add;
//--- Synth Defs for Auto-Harmonizer-Tuner example instrument ---
SynthDef.new(\mono2stereo, { //pass mono input to stereo output
|in, amp = 1, pan = 0.0, out|
var sig;
sig = In.ar(in) * amp;
//sig = InFeedback.ar(in) * amp;
sig = Pan2.ar(sig, pan);
//OffsetOut.ar(out, sig);
Out.ar(out, sig);
} ).add;
SynthDef.new(\reverb2x2, {|outBus, mix = 0.25, room = 0.15, damp = 0.2, amp = 1.0|
var sigIn, sigOut;
sigIn = In.ar(outBus, 2);
sigOut = FreeVerb2.ar(sigIn[0], sigIn[1], mix, room, damp, amp);
ReplaceOut.ar(outBus, sigOut);
} ).add;
~wekiSyn_voc1 = {//|oscInBus, audioIn, audioOut, interval = 5.0, pan = 0.0, /*scaleBuf,*/ amp = 1.0, mix = 1.0| //FUNCTION ARGS AREN'T BEING TRANSFERED TO SYNTHDEF ARGS !!!!!
|oscInBus, audioIn, audioOut|
var numCh, inMsg, sigIn, sigOut, returnSynth, pitchRatio, pitchDisp, panMod;
numCh = oscInBus.numChannels;
returnSynth = SynthDef(\vocHarm_weki1, {|interval = 5.0, pan = 0.0, amp = 0.5, mix = 1.0|
sigIn = In.ar(audioIn, 1);
inMsg = In.kr(oscInBus, numCh);
pitchRatio = interval.asFloat.round(0.01).midiratio; //set pitch transposition (semi-tone intervals)
//pitchDisp = LinLin.kr(inMsg[1], 0, 1, 0, 0.12).round(0.01); //scale weki out1 for pitchDisp val
sigOut = SelectX.ar(mix, [sigIn, PitchShift.ar(sigIn, 0.1, pitchRatio, 0, 0.002)]);
//sigOut = SelectX.ar(mix, [sigIn, PitchShift.ar(sigIn, 0.1, pitchRatio, pitchDisp, 0.002)]);
//panMod = LinLin.kr(inMsg[1], 0, 1, -0.35, 0.35).round(0.01); //consider
//pan = Clip.kr(pan.asFloat + panMod, -0.9, 0.9); //consider
sigOut = Pan2.ar(sigOut, pan.asFloat); //?? yes?
Out.ar(audioOut, sigOut * amp);
}).play(addAction: \addToTail);
returnSynth;
};
~wekiSyn_wisl1 = {//|oscInBus, audioIn, audioOut, scaleBuf, amp = 1.0, mix = 1.0|
|oscInBus, audioIn, audioOut, scaleBuf| //add arg for initial interval scale spread of weki output1?? (12 == (-12, 12) | 5 == (-5, 5) | etc...)
var numCh, inMsg, sigIn, sigOut, returnSynth, pitchRatio, pitchDisp;
numCh = oscInBus.numChannels;
returnSynth = SynthDef(\wislHarm_weki1, {|amp = 1.0, mix = 1.0| //can't use scaleBuf as arg for this synthDef :(
sigIn = In.ar(audioIn, 1);
inMsg = In.kr(oscInBus, numCh);
pitchRatio = DegreeToKey.kr(scaleBuf.bufnum, LinLin.kr(inMsg[1], 0, 1, 5, -5).round(1)).midiratio; //convert Weki out 1 into a transposition ratio from mapped scale degrees
//pitchRatio.poll(2);
//pitchDisp = LinLin.kr(inMsg[1], 0, 1, 0, 0.12).round(0.01); //scale weki out1 for pitchDisp val
sigOut = SelectX.ar(mix, [sigIn, PitchShift.ar(sigIn, 0.1, pitchRatio, 0, 0.002)]); //no pDisp
//sigOut = SelectX.ar(mix, [sigIn, PitchShift.ar(sigIn, 0.1, pitchRatio, pitchDisp, 0.002)]); //pDisp
sigOut = Pan2.ar(sigOut, 0);
//sigOut = Pan2.ar(sigOut, LinLin.kr(inMsg[1], 0, 1, -0.35, 0.35)); //mod pan w/ weki out??
Out.ar(audioOut, sigOut * amp);
}).play(addAction: \addToTail);
returnSynth;
};
SynthDef.new(\XFadeStereo, {
|in1, in2, out, amountControlBus, amp = 1.0|
var inArray, mixSignal, sig;
inArray = [In.ar(in1, 2), In.ar(in2, 2)];
//inArray = [InFeedback.ar(in1), InFeedback.ar(in2)];
mixSignal = SelectX.ar(amountControlBus, inArray);
sig = mixSignal * amp;
//OffsetOut.ar(out, sig); //should I use this instead?
Out.ar(out, sig);
} ).add;
SynthDef(\LinInterp_num, {//imported from SonicMirror
arg startVal, stopVal, duration, mult = 1, out;
Out.kr(out, Line.kr(startVal, stopVal, duration, mul: mult, doneAction: 2));
} ).add;
SynthDef.new(\subMix, {//imported from SonicMirror
|in, amp = 1, out|
var sig;
sig = In.ar(in) * amp;
//sig = InFeedback.ar(in) * amp;
//OffsetOut.ar(out, sig);
Out.ar(out, sig);
} ).add;
//FUNCS------------------------
~sumFunc_xin = {arg busArray; //bus val concat function for variable size arg array of feat busses
var numCh = 0, busCol, outArray, outBus;
busArray.do({|item| numCh = numCh + item.numChannels}); //calculate total # bus channels
outBus = Bus.control(s, numCh);
busCol = Array.newClear(busArray.size);
~sumSyn_xin = SynthDef(\sumSyn_xin, {
busArray.do({|item, i| busCol[i] = In.kr(item, item.numChannels); }); //input bus vals to var array
busCol.do({|item| //concat all bus vals into single out array
if (item.numChannels == 1,
{outArray = outArray ++ [item]},
{outArray = outArray ++ (item.numChannels.collect( {|i| item[i]} ))}
);
});
Out.kr(outBus, outArray);
}).play(addAction: \addToTail);
outBus; //return new bus w/ concat values of all input busses in arg array
};
~getSend_busSum = {|bus, netAdr, oscMsg, post = 0|
var numCh = bus.numChannels; //needed for message posting...
bus.get( {arg val; { //<---changing to getSynchronous prevents OSC output for some reason...?
netAdr.sendMsg(*[oscMsg] ++ val); //transmit feature vals via OSC
if (post == 1, { //monitor feature values in SC post window
switch (bus,
~busSum_ldns_mfcc, {
("Loudness:" + val[0].round(0.0001)).postln;
("MFCC:" + val[1..13].round(0.0001)).postln;
("-------").postln;
},
~busSum_ldns_sFlat_sCent, {
("Loudness:" + val[0].round(0.0001)).postln;
("Flatness:" + val[1].round(0.0001)).postln;
("Centroid:" + val[2].round(0.0001)).postln;
("-------").postln;
},
~busSum_ldns_pitch, {
("Loudness:" + val[0].round(0.0001)).postln;
("Pitch-freq:" + val[1].round(0.01)).postln;
("Pitch-hasFreq?:" + val[2]).postln;
("Pitch-MIDInote:" + val[3]).postln;
("-------").postln;
},
~busSum_ldns_sFlat_onset, {
("Loudness:" + val[0].round(0.0001)).postln;
("Flatness:" + val[1].round(0.0001)).postln;
("Onset Detected:" + val[2]).postln;
("-------").postln;
},
{//all other busses (cases)
if (numCh > 1,
{
numCh.do({arg item; ("Feature_" ++ (item + 1) ++ ":" + val[item].round(0.0001)).postln;});
("-------").postln;
},
{("Feature_1:" + val.round(0.0001)).postln;} //else
);
}
);
} );
}.defer; } );
};
~streamFeatures = {
|bus, netAdr, oscMsg, rate = 0.04, post = 0|
Routine ( {
~getSend_busSum.value(bus, netAdr, oscMsg, post);
rate.yieldAndReset;
} );
};
//===========ACCOMP SYNTH AND FX CONTROL ROUTINE===========
~flag_wekSyn_prevIn = 1; //langside flag storing last input from WekiOut1, 1 == voc, 2 == whistle
~flag_wekSyn_prevIn_change = false; //flag for debouncing sudden changes due to input noise
~flag_wekSyn_prevVow = 1; //flag for storing last input from WekiOut3, 1 == ah, 2 == eh, 3 == ee, 4 == oh, 5 == oo
~flag_wekSyn_prevVow_change = false; //flag for debouncing sudden changes due to input noise
~yieldTime_wsc1 = 0.1;
~debounceTime = 0.3; //(in seconds) increase or reduce this value for less or more jittery parameter change response, respectively (higher value causes more delay in parameter changes after detected changes in sound, but may yield more accurate performance...the accuracy of trained Wekinator models is also a factor)
~routine_wekiSynthControl1 = {|oscInBus, rate = 0.1| //<---new version w/ debounce for vowel detection
Routine ({
oscInBus.get({ |msg|
if (msg[0] == 1, { //"voc detected"
if (~flag_wekSyn_prevIn == 1, { //sound source has not changed since prev bus.get
var x;
~flag_wekSyn_prevIn_change = false; //reset debounce flag in case it was previously triggered
//"msg[0] = 1 - NO CHANGE".postln;
x = case //MONITOR CHANGES IN msg[2] <<<<<<<<
{msg[2] == 1} {//if msg[2] == 1 - 'AH' DETECTED
if (~flag_wekSyn_prevVow == 1, { //msg[2] has not changed since prev bus.get
~flag_wekSyn_prevVow_change = false; //reset flag if it was triggered by noise
~yieldTime_wsc1 = rate.asFloat; //<---
} , { //else if ~flag_prevVow != 1, maybe we're changing vowels
if (~flag_wekSyn_prevVow_change == false, { //DEBOUNCE
~flag_wekSyn_prevVow_change = true;
//"msg[2] = 1 | prev msg[2] = ? - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime;
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
~flag_wekSyn_prevVow_change = false;
~flag_wekSyn_prevVow = 1; //set flag to remember last state before resetting routine
//"msg[2] = 1 | prev msg[2] = ? - STATE CHANGE CONFIRMED".postln;
"SINGING DETECTED: AH | msg[2] = 1".postln;
~vocA1.set(\interval, -5, \pan, -0.75);
~vocA2.set(\interval, 4.25, \pan, 0.75);
~yieldTime_wsc1 = rate.asFloat; //<---
} );
} );
}
{msg[2] == 2} {//if msg[2] == 2 - 'EH' DETECTED
if (~flag_wekSyn_prevVow == 2, { //msg[2] has not changed since prev bus.get
~flag_wekSyn_prevVow_change = false; //reset flag if it was triggered by noise
~yieldTime_wsc1 = rate.asFloat; //<---
} , { //else if ~flag_prevVow != 2, maybe we're changing vowels
if (~flag_wekSyn_prevVow_change == false, { //DEBOUNCE
~flag_wekSyn_prevVow_change = true;
//"msg[2] = 2 | prev msg[2] = ? - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime;
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
~flag_wekSyn_prevVow_change = false;
~flag_wekSyn_prevVow = 2; //set flag to remember last state before resetting routine
//"msg[2] = 2 | prev msg[2] = ? - STATE CHANGE CONFIRMED".postln;
"SINGING DETECTED: EH | msg[2] = 2".postln;
~vocA1.set(\interval, -3, \pan, -0.6);
~vocA2.set(\interval, 5, \pan, 0.6);
~yieldTime_wsc1 = rate.asFloat; //<---
} );
} );
}
{msg[2] == 3} {//if msg[2] == 3 - 'EE'
if (~flag_wekSyn_prevVow == 3, { //msg[2] has not changed since prev bus.get
~flag_wekSyn_prevVow_change = false; //reset flag if it was triggered by noise
~yieldTime_wsc1 = rate.asFloat; //<---
} , { //else if ~flag_prevVow != 3, maybe we're changing vowels
if (~flag_wekSyn_prevVow_change == false, { //DEBOUNCE
~flag_wekSyn_prevVow_change = true;
//"msg[2] = 3 | prev msg[2] = ? - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime;
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
~flag_wekSyn_prevVow_change = false;
~flag_wekSyn_prevVow = 3; //set flag to remember last state before resetting routine
//"msg[2] = 3 | prev msg[2] = ? - STATE CHANGE CONFIRMED".postln;
"SINGING DETECTED: EE | msg[2] = 3".postln;
~vocA1.set(\interval, -4, \pan, -0.45);
~vocA2.set(\interval, 10, \pan, 0.45);
~yieldTime_wsc1 = rate.asFloat; //<---
} );
} );
}
{msg[2] == 4} {//if msg[2] == 4 - 'OH'
if (~flag_wekSyn_prevVow == 4, { //msg[2] has not changed since prev bus.get
~flag_wekSyn_prevVow_change = false; //reset flag if it was triggered by noise
~yieldTime_wsc1 = rate.asFloat; //<---
} , { //else if ~flag_prevVow != 4, maybe we're changing vowels
if (~flag_wekSyn_prevVow_change == false, { //DEBOUNCE
~flag_wekSyn_prevVow_change = true;
//"msg[2] = 4 | prev msg[2] = ? - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime;
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
~flag_wekSyn_prevVow_change = false;
~flag_wekSyn_prevVow = 4; //set flag to remember last state before resetting routine
//"msg[2] = 4 | prev msg[2] = ? - STATE CHANGE CONFIRMED".postln;
"SINGING DETECTED: OH | msg[2] = 4".postln;
~vocA1.set(\interval, -3, \pan, -0.6);
~vocA2.set(\interval, 7, \pan, 0.6);
~yieldTime_wsc1 = rate.asFloat; //<---
} );
} );
}
{msg[2] == 5} {//if msg[2] == 5 'OO'
if (~flag_wekSyn_prevVow == 5, { //msg[2] has not changed since prev bus.get
~flag_wekSyn_prevVow_change = false; //reset flag if it was triggered by noise
~yieldTime_wsc1 = rate.asFloat; //<---
} , { //else if ~flag_prevVow != 5, maybe we're changing vowels
if (~flag_wekSyn_prevVow_change == false, { //DEBOUNCE
~flag_wekSyn_prevVow_change = true;
//"msg[2] = 5 | prev msg[2] = ? - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime;
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
~flag_wekSyn_prevVow_change = false;
~flag_wekSyn_prevVow = 5; //set flag to remember last state before resetting routine
//"msg[2] = 5 | prev msg[2] = ? - STATE CHANGE CONFIRMED".postln;
"SINGING DETECTED: OO | msg[2] = 5".postln;
~vocA1.set(\interval, -4, \pan, -0.25);
~vocA2.set(\interval, 5, \pan, 0.25);
~yieldTime_wsc1 = rate.asFloat; //<---
} );
} );
};
//~yieldTime_wsc1 = rate.asFloat; //<---
} , { //else ~flag_wekSyn_prevIn == 2, we're changing from "2 - whistling" to "1 - voc"
//CHECK TO SEE IF TIME THRESHOLD SURPASSED BEFORE CONFIRMING STATE CHANGE
if (~flag_wekSyn_prevIn_change == false, {
~flag_wekSyn_prevIn_change = true;
//"msg[0] = 1 | prev msg[0] = 2 - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime; //"debounce" time
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
var interpSynth, interpVerbRoom, interpVerbMix;
~flag_wekSyn_prevIn_change = false;
//XFADE ~mixAmount.bus from whistle to voc processing synth busses (1.0 - 0.0)
interpSynth = Synth(\LinInterp_num, [\startVal, 1, \stopVal, 0, \duration, 0.3, \out, ~bus_vocwislMix]);
interpVerbRoom = Synth(\LinInterp_num, [\startVal, 0.7, \stopVal, 0.4, \duration, 0.3, \out, ~bus_verbRoom]);
interpVerbMix = Synth(\LinInterp_num, [\startVal, 0.6, \stopVal, 0.3, \duration, 0.3, \out, ~bus_verbMix]);
~flag_wekSyn_prevIn = 1; //set flag to remember last state before resetting routine
//"msg[0] = 1 | prev msg[0] = 1 - STATE CHANGE CONFIRMED".postln;
"SINGING DETECTED--AUTO-HARMONIZE MODE ACTIVE".postln;
~yieldTime_wsc1 = rate.asFloat;
//~yieldTime_wsc1 = 0.31; //precautionary yield time to allow LinInterp synth to finish before resuming routine...thought this helped avoid audio clicks, but don't think it does & it adds latency....
} );
} );
} , { //ELSE======= if msg[0] == 2 "whistling detected"
if (~flag_wekSyn_prevIn == 2, { //sound source has not changed since prev bus.get
~flag_wekSyn_prevIn_change = false; //reset debounce flag
//"msg[0] = 2 - NO CHANGE".postln;
~yieldTime_wsc1 = rate.asFloat; //<----
} , { //else flag must have changed recently from "1 - voc" to "2 - whistling"
//CHECK TO SEE IF TIME THRESHOLD SURPASSED BEFORE CONFIRMING STATE CHANGE
if (~flag_wekSyn_prevIn_change == false, {
~flag_wekSyn_prevIn_change = true;
//"msg[0] = 2 | prev msg[0] = 1 - STATE CHANGE TEST".postln;
~yieldTime_wsc1 = ~debounceTime; //"debounce" time <---
}, { //else if ~flag_stateChange == true, state has changed and remained same long enough to rule out noise, and we can proceed with triggered changes
var interpSynth, interpVerbRoom, interpVerbMix;
~flag_wekSyn_prevIn_change = false;
//XFADE ~mixAmount.bus from voc to whistle processing synth busses (0.0 - 1.0)
interpSynth = Synth(\LinInterp_num, [\startVal, 0, \stopVal, 1, \duration, 0.3, \out, ~bus_vocwislMix]);
interpVerbRoom = Synth(\LinInterp_num, [\startVal, 0.4, \stopVal, 0.7, \duration, 0.3, \out, ~bus_verbRoom]);
interpVerbMix = Synth(\LinInterp_num, [\startVal, 0.3, \stopVal, 0.6, \duration, 0.3, \out, ~bus_verbMix]);
~flag_wekSyn_prevIn = 2; //set flag to remember last state before resetting routine
//"msg[0] = 2 | prev msg[0] = 2 - STATE CHANGE CONFIRMED".postln;
"WHISTLING DETECTED--AUTO-TUNE MODE ACTIVE".postln;
~yieldTime_wsc1 = rate.asFloat; //<---
//~yieldTime_wsc1 = 0.31; //precautionary yield time to allow LinInterp synth to finish before resuming routine...thought this helped avoid audio clicks, but don't think it does & it adds latency....
} );
} );
} );
});
//"Out of .get, before slight .yield".postln;
0.02.yield; //<-----ANY OTHER WAY W/OUT INDUCING LATENCY!??????
// ("yieldTime_wsc1 = " ++ ~yieldTime_wsc1).postln;
~yieldTime_wsc1.yieldAndReset;
});
};
~initAutoHT = Routine({ //===initialize supporting synths and OSC In/Out functions for SC-Wekinator Auto-Harmonizer-Tuner example instrument===
~scale1 = Scale.majorPentatonic.degrees; //[0, 2, 4, 7, 9]
~scaleBuf = Buffer.alloc(s, ~scale1.size, 1, {|b| b.setnMsg(0, ~scale1) } );
i = Synth(\monAudioInput, [\outBus, ~bus_input1]);
l = Synth(\Loudness, [\input, ~bus_input1, \outBus, ~bus_loudness]).moveAfter(i);
m = Synth(\MFCC, [\input, ~bus_input1, \outBus, ~bus_mfcc]).moveAfter(i);
p = Synth(\Pitch, [\input, ~bus_input1, \outBus, ~bus_pitch]).moveAfter(i); //outputs two values (freq)
//"AUDIO IN & FEATURE EXTRACTOR SYNTHS ACTIVE...".postln;
//0.15.wait;
//TRANSMIT FEATURES
~busSum_xin = ~sumFunc_xin.value([~bus_loudness, ~bus_mfcc, ~bus_pitch]); //collect loudness, mfcc, and pitch features to output via OSC to Wekinator
~transmit = ~streamFeatures.value(~busSum_xin, ~oscOut_weki, "/wek/inputs", 0.08);
SystemClock.play(~transmit);
//"TRANSMITTING FEATURES (OSC OUT)...".postln;
//0.15.wait;
//RECEIVE WEKI OSC OUTPUT
~bus_oscIn_1 = ~oscIn_weki_1.value(numIn: 4, defKey: "oscDef1", port: 12000); //open port to receive OSC output from Wekinator and send to a control bus
//"OSC-IN Port Ready...".postln;
0.15.wait; //voodoo wait... not necessary, but including this seems to prevent audio clicks
~dryIn1 = Synth(\mono2stereo, [\in, ~bus_input1, \amp, ~bus_dryIn1Amp.asMap, \out, 0]).moveAfter(i); //pipe dry mono input to stereo output
~wisA1 = ~wekiSyn_wisl1.value(oscInBus: ~bus_oscIn_1, audioIn: ~bus_input1, audioOut: ~bus_wislSynthSum, scaleBuf: ~scaleBuf).moveAfter(~dryIn1); //whistle harmonizer 1
~vocA1 = ~wekiSyn_voc1.value(oscInBus: ~bus_oscIn_1, audioIn: ~bus_input1, audioOut: ~bus_vocSynthSum).moveAfter(~dryIn1); //voc harmonizer 1
~vocA2 = ~wekiSyn_voc1.value(oscInBus: ~bus_oscIn_1, audioIn: ~bus_input1, audioOut: ~bus_vocSynthSum).moveAfter(~dryIn1); //voc harmonizer 2
//"AutoHarmonizer and AutoTune Synths Active...".postln;
0.15.wait; //necessary wait
~dryIn1.set(\amp, ~bus_dryIn1Amp.asMap);
~wisA1.set(\amp, ~bus_wislAmp.asMap);
~vocA1.set(\amp, ~bus_vocAmp.asMap);
~vocA2.set(\amp, ~bus_vocAmp.asMap);
//"Configure Synths...".postln;
0.15.wait; //voodoo wait... not necessary, but including this seems to prevent audio clicks
~accomp_mix = Synth(\XFadeStereo, [\in1, ~bus_vocSynthSum, \in2, ~bus_wislSynthSum, \out, 0, \amountControlBus, ~bus_vocwislMix.asMap, \amp, ~bus_accompAmp.asMap], addAction: \addToTail);
~fx_reverb = Synth(\reverb2x2, [\outBus, 0, \mix, ~bus_verbMix.asMap, \room, ~bus_verbRoom.asMap], addAction: \addToTail); //stereo reverb processing sum output
//--Activate OSC input streaming--
~wekiSynthControl1 = ~routine_wekiSynthControl1.value(oscInBus: ~bus_oscIn_1, rate: 0.1); //create routine to get latest values from OSC input control bus
SystemClock.play(~wekiSynthControl1); //start OSC input bus monitoring routine
//"OSC Input Streaming...".postln;
"--AUTOHARMTUNE ACTIVE--".postln;
});
)
// #3 ====== ACTIVATE WEKINATOR INSTRUMENT EXAMPLE (Auto-harmonize & Auto-tune) ======
SystemClock.play(~initAutoHT);
// #4 ==== STOP & CLEANUP WEKINATOR INSTRUMENT EXAMPLE (Auto-harmonize & Auto-tune) ====
(
~initAutoHT.stop;
~initAutoHT.reset;
~transmit.stop;
~transmit.free;
~sumSyn_xin.free;
~busSum_xin.free;
~scaleBuf.free;
i.free;
l.free;
m.free;
p.free;
~dryIn1.free;
~wisA1.free;
~vocA1.free;
~vocA2.free;
~accomp_mix.free;
~fx_reverb.free;
~bus_oscIn_1.free;
~wekiSynthControl1.stop;
~wekiSynthControl1.free;
OSCdef(\oscDef1).free;
)
///////////////////////////// --- CUSTOM CONFIGURATION --- ///////////////////////////////////
(// B. ====== ACTIVATE AUDIO INPUT AND FEATURE EXTRACTOR SYNTHS ======
i = Synth(\monAudioInput, [\outBus, ~bus_input1]); //stream your system's first audio input channel to an SC audio bus (NOTE: THIS SYNTH MUST BE ACTIVE BEFORE ENABLING ANY OTHER FEATURE EXTRACTOR SYNTH BELOW)
//LOUDNESS (volume - perceptual measure in Sones) ------
l = Synth(\Loudness, [\input, ~bus_input1, \outBus, ~bus_loudness]).moveAfter(i);
//RMS (volume) ------
r = Synth(\RMS, [\input, ~bus_input1, \numSamp, 40, \outBus, ~bus_rms]).moveAfter(i);
//MFCC
m = Synth(\MFCC, [\input, ~bus_input1, \outBus, ~bus_mfcc]).moveAfter(i);
//PITCH TRACKERS
p = Synth(\Pitch, [\input, ~bus_input1, \outBus, ~bus_pitch]).moveAfter(i); //outputs two values (freq, hasPitch)
~p2 = Synth(\Pitch2, [\input, ~bus_input1, \outBus, ~bus_pitch2]).moveAfter(i); //3 output vals (freq, hasPitch, MIDInote)
//SPECTRAL CENTROID
c = Synth(\specCent, [\input, ~bus_input1, \outBus, ~bus_specCent]).moveAfter(i);
//SPECTRAL FLATNESS
f = Synth(\specFlat, [\input, ~bus_input1, \outBus, ~bus_specFlat]).moveAfter(i);
//ONSETS
n = Synth(\onsets, [\input, ~bus_input1, \outBus, ~bus_onsets, \threshold, 0.7]).moveAfter(i);
//n.set(\threshold, 0.5); //uncomment evaluate to change onset detection threshold (between 0 - 1)
)
// ====== OUTPUT AUDIO FEATURES VIA OSC ======
// Evaluate one chunk below at a time to output different feature sets via OSC.
// Evaluate the corresponding STOP chunk before attempting to transmit alternate feature combinations.
// NOTE: You may need to edit the "netAdr" and "oscMsg" args suited to your needs if you're trying to output OSC messages to something other than the Wekinator application, which is currently the default configuration of this code.
// Default OSC output (~oscOut_weki): IP - 127.0.0.1, Port # 6448
(//TRANSMIT LOUDNESS AND MFCCs - (14 features)
~busSum_ldns_mfcc = ~sumFunc_xin.value( [~bus_loudness, ~bus_mfcc] );
~transmit = ~streamFeatures.value(bus: ~busSum_ldns_mfcc, netAdr: ~oscOut_weki, oscMsg: "/wek/inputs", rate: 0.1, post: 1); //rate arg adjusts rate OSC messages are output (0.04 or about 25 times/sec is default)
SystemClock.play(~transmit);
)
( //STOP transmitting (NOTE: evaluate this chunk before transmitting other feature sets)
~transmit.stop;
~sumSyn_xin.free;
~busSum_ldns_mfcc.free;
)
(//TRANSMIT LOUDNESS, SPECTRAL FLATNESS, AND SPECTRAL CENTROID - (3 features)
~busSum_ldns_sFlat_sCent = ~sumFunc_xin.value( [~bus_loudness, ~bus_specFlat, ~bus_specCent] );
~transmit = ~streamFeatures.value(bus: ~busSum_ldns_sFlat_sCent, netAdr: ~oscOut_weki, oscMsg: "/wek/inputs", rate: 0.1, post: 1);
SystemClock.play(~transmit);
)
( //STOP transmitting
~transmit.stop;
~sumSyn_xin.free;
~busSum_ldns_sFlat_sCent.free;
)
(//TRANSMIT LOUDNESS AND PITCH
//~busSum_ldns_pitch = ~sumFunc_xin.value( [~bus_loudness, ~bus_pitch] ); //3 features [loudness, freq, hasFreq]
~busSum_ldns_pitch = ~sumFunc_xin.value( [~bus_loudness, ~bus_pitch2] ); //4 features [loudness, freq, hasFreq, MIDI note]
~transmit = ~streamFeatures.value(bus: ~busSum_ldns_pitch, netAdr: ~oscOut_weki, oscMsg: "/wek/inputs", rate: 0.1, post: 1);
SystemClock.play(~transmit);
)
( //STOP transmitting
~transmit.stop;
~sumSyn_xin.free;
~busSum_ldns_pitch.free;
)
(//TRANSMIT LOUDNESS, SPECTRAL FLATNESS, ONSETS - (3 features)
~busSum_ldns_sFlat_onset = ~sumFunc_xin.value( [~bus_loudness, ~bus_specFlat, ~bus_onsets] );
~transmit = ~streamFeatures.value(bus: ~busSum_ldns_sFlat_onset, netAdr: ~oscOut_weki, oscMsg: "/wek/inputs", rate: 0.1, post: 1);
~transmit.play;
)
(//STOP transmitting
~transmit.stop;
~sumSyn_xin.free;
~busSum_ldns_sFlat_onset.free;
)
(//TRANSMIT FEATURES (MISC FEATURE SET) - Loudness, Flatness, Pitch
~busSum_xin = ~sumFunc_xin.value([~bus_loudness, ~bus_specFlat, ~bus_pitch]); //input desired feature busses in array (separate w/ comma)
~transmit = ~streamFeatures.value(bus: ~busSum_xin, netAdr: ~oscOut_weki, oscMsg: "/wek/inputs", rate: 0.1, post: 1);
SystemClock.play(~transmit);
)
( //STOP transmitting
~transmit.stop;
~sumSyn_xin.free;
~busSum_xin.free;
)
// ======CLEANUP======
//evaluate below code before quitting SuperCollider
(
s.freeAll; //free all active synths on server
~bus_input1.free;
~bus_loudness.free;
~bus_rms.free;
~bus_mfcc.free;
~bus_pitch.free;
~bus_pitch2.free;
~bus_specCent.free;
~bus_specFlat.free;
~bus_onsets.free;
~oscOut_weki.disconnect;
OSCdef.freeAll;
)
s.quit; //shutdown the SC server