-
Notifications
You must be signed in to change notification settings - Fork 1
/
06. machine listening and more GUIs.scd
334 lines (239 loc) · 11.1 KB
/
06. machine listening and more GUIs.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
//--------------------------------------------------
// Server-language communication; Machine listening;
//--------------------------------------------------
//sound examples first!
// Server.killAll // you're getting "Exception in World_OpenUDP: unable to bind udp socket" error
//--------------------------------------------------
// Sending information from the Synth to the Lang
//--------------------------------------------------
//there is SendTrig and SendReply
//SendTrig - send single value, identified by an ID
//SendReply - sends array of values, identified by an ID and custom address/name
//looking into all incoming OSC messages
OSCFunc.trace(true);
OSCFunc.trace(false); //turn off
//--------
// SendTrig
//--------
s.boot;
s.options.sampleRate = 48000;
//amplitude < 0.8; //possible synth trigger
(
s.waitForBoot({
~synth = CtkSynthDef(\sendTrigTest, {|id = 0|
var trig, value;
trig = Impulse.kr(2);
// trig.poll(10, \myImpulse);
// trig = Dust.kr(2);//try that as well - random
value = TRand.kr(0, 1, trig); //getting values on trig...
SendTrig.kr(trig, id, value);
});
// register to receive this message
~resp = OSCdef(\defName, { arg msg, time;
[time, msg].postln;
"id: ".post; msg[2].postln;
"value: ".post; msg[3].postln;
},'/tr'); //responding to '/tr' message '/tr' \otherSymbols
//note OSCdef not OSCFunc; OSCdef will OVERWRITE any previous responders associated with \defName symbol, so it's less problematic if you forget to free it before running the code again
});
);
~synth.note(0, 10).play; //play for 10 seconds, see posting
~synth.note(0, 10).id_(123).play; //play for 10 seconds, different ID
~resp.free; //important! use cmdPeriod or window.onClose to evaluate that;
//--------
// SendReply
//--------
//can send multiple values!
(
var replyName;
replyName = '/myReply'; //you can't change that when you call the synth, but you can have various synth definition with various names if desired; ID will sill probably be useful; not it needs to start with a forward slash
~synth = CtkSynthDef(\sendReplyTest,{|id = 0|
var trig, values;
trig = Impulse.kr(2);
// trig = Dust.kr(2);//try that as well
values = [TRand.kr(0, 1, trig), TRand.kr(0, 1, trig), TRand.kr(0, 1, trig), TRand.kr(0, 1, trig)]; //getting and array of random values on trig...
SendReply.kr(trig, replyName, values, id);
});
// register to receive this message
~resp2 = OSCdef(replyName, { arg msg, time; //we can safely assume we want only one responder per replyName, so instead of creating a special name for it, we can use the reply sybol for it
// [time, msg].postln;
"id: ".post; msg[2].postln;
"values: ".post; msg[3..].round(0.001).postln; //syntactical shortcut for "copy from 3rd member till the end of an array"
}, replyName); //responding to '/tr' message
)
~synth.note(0, 10).play; //play for 10 seconds, see posting
~synth.note(0, 10).id_(123).play; //play for 10 seconds, different ID
~resp2.free; //important! if you don't do that, you'll run into trouble! use cmdPeriod or window.onClose to evaluate that
//use ctkcontrol
//--------------------------------------------------
// Extracting musical features
//--------------------------------------------------
s.quit;
s.options.numOutputBusChannels = 16;
s.options.numInputBusChannels = 16;
s.boot
s.meter;
~inputNumber = 0; //offset into input bus
//-------
//onsets
//-------
(
var replyName;
~inputNumber = 0; //offset into input bus
replyName = '/myReply';
~synth = CtkSynthDef(\onsetsTest,{|in = 0, threshold = 0.5, id = 0|
var trig, values, inSig;
var localbuf, chain;
inSig = In.ar(in, 1);
localbuf = LocalBuf(512);
chain = FFT(localbuf, inSig);
trig = Onsets.kr(chain, threshold); //here we detect onsets
values = 0; //we don't have anything to send - yet
SendReply.kr(trig, replyName, values, id);
});
// register to receive this message
~resp = OSCdef(replyName, { arg msg, time; //we can safely assume we want only one responder per replyName, so instead of creating a special name for it, we can use the reply sybol for it
[time, msg].postln;
"id: ".post; msg[2].postln;
"values: ".post; msg[3..].postln; //syntactical shortcut for "copy from 3rd member till the end of an array"
}, replyName); //responding to '/tr' message
)
~synth.note(0, 10).in_(s.options.numOutputBusChannels + ~inputNumber).threshold_(0.5).play; //play for 10 seconds, see posting
~resp.free; //important! if you don't do that, you'll run into trouble! use cmdPeriod or window.onClose to evaluate that
//---------
//pitch and amplitude
//---------
(
var replyName;
~inputNumber = 0; //offset into input bus
replyName = '/myReply';
~synth = CtkSynthDef(\pitchAmpTest,{|in = 0, threshold = 0.93, id = 0, att = 0.01, rel = 0.01|
var trig, freq, hasFreq, amplitude, inSig;
// var localbuf, chain;
inSig = In.ar(in, 1);
trig = Impulse.kr(10); //let's repor continuosly 10 times per second
#freq, hasFreq = Tartini.kr(inSig, threshold);
amplitude = Amplitude.kr(inSig, att, rel); //also see Lag ugens for smoothing
// values = 0; //we don't have anything to send - yet
SendReply.kr(trig, replyName, [freq, hasFreq, amplitude], id);
});
// register to receive this message
~resp = OSCdef(replyName, { arg msg, time; //we can safely assume we want only one responder per replyName, so instead of creating a special name for it, we can use the reply sybol for it
// [time, msg].postln;
// "id: ".post; msg[2].postln;
// "values: ".post; msg[3..].postln; //syntactical shortcut for "copy from 3rd member till the end of an array"
postf("freq: %, hasFreq: %, amplitude (dB): %\n", msg[3].round(0.01), msg[4].round(0.01), msg[5].ampdb.round(0.01)/*convert amp to dB*/); //see String helpfile for postf explanation; also note newline symbol at the end (\n); values are rounded for posting
}, replyName); //responding to '/tr' message
)
~synth.note(0, 10).in_(s.options.numOutputBusChannels + ~inputNumber).play; //play for 10 seconds, see posting
~resp.free; //important! if you don't do that, you'll run into trouble! use cmdPeriod or window.onClose to evaluate that
//---------
//spectral flatness and centroid
//---------
(
var replyName;
~inputNumber = 0; //offset into input bus
replyName = '/myReply';
~synth = CtkSynthDef(\spectrTest,{|in = 0, threshold = 0.93, id = 0, att = 0.01, rel = 0.01|
var trig, flatness, centroid, inSig;
var localbuf, chain;
inSig = In.ar(in, 1);
localbuf = LocalBuf(512);
chain = FFT(localbuf, inSig);
trig = Impulse.kr(10); //let's repor continuosly 10 times per second
centroid = SpecCentroid.kr(chain);
flatness = SpecFlatness.kr(chain);
// values = 0; //we don't have anything to send - yet
SendReply.kr(trig, replyName, [centroid, flatness], id);
});
// register to receive this message
~resp = OSCdef(replyName, { arg msg, time; //we can safely assume we want only one responder per replyName, so instead of creating a special name for it, we can use the reply sybol for it
// [time, msg].postln;
// "id: ".post; msg[2].postln;
// "values: ".post; msg[3..].postln; //syntactical shortcut for "copy from 3rd member till the end of an array"
postf("spectral centroid: %, flatness: %\n", msg[3].round(0.01), msg[4].round(0.001)); //see String helpfile for postf explanation; also note newline symbol at the end (\n); values are rounded for posting
}, replyName); //responding to '/tr' message
)
~synth.note(0, 10).in_(s.options.numOutputBusChannels + ~inputNumber).play; //play for 10 seconds, see posting
~resp.free; //important! if you don't do that, you'll run into trouble! use cmdPeriod or window.onClose to evaluate that
//---------
//musical example: onsets trigger synth, amplitude and pitch
//---------
(
var replyName, lastNote, playerSynth;
~inputNumber = 0; //offset into input bus
replyName = '/myReply';
~synth = CtkSynthDef(\spectrTest,{|in = 0, threshold = 0.93, id = 0, att = 0.01, rel = 0.01, onsetThreshold = 0.5, freqOut = 0, ampOut = 0, hasFreqThreshold = 0.8|
var trig, freq, hasFreq, amplitude, inSig;
var localbuf, chain;
inSig = In.ar(in, 1);
localbuf = LocalBuf(256);
chain = FFT(localbuf, inSig);
// trig = Impulse.kr(10); //let's repor continuosly 10 times per second
trig = Onsets.kr(chain, onsetThreshold); //here we detect onsets
#freq, hasFreq = Tartini.kr(inSig, threshold);
amplitude = Amplitude.kr(inSig, att, rel); //also see Lag ugens for smoothing
// amplitude = (hasFreq > hasFreqThreshold) * amplitude;
// hasFreq.poll(2, \hasFreq);
// values = 0; //we don't have anything to send - yet
SendReply.kr(trig, replyName, freq, id); //send freq at the time of onset
//output for continuous signals
Out.kr(freqOut, freq);
Out.kr(ampOut, amplitude);
});
playerSynth = CtkSynthDef(\sawPlayer, {|out = 0, amp = 1, freq = 400, freqMul = 1, ampLagU = 0.01, ampLagD = 1, pan = 0, gate = 1, att = 0.1, rel = 1|
var sig, env;
env = EnvGen.kr(Env([0,1,0], [att, rel], \sin, 1), gate);
sig = Saw.ar(freq * freqMul, amp.lag3ud(ampLagU, ampLagD)); //see Lag3UD help
Out.ar(out, Pan2.ar(sig, pan) * env);
});
~pitchBus = CtkControl.play;
~ampBus = CtkControl.play;
// register to receive this message
~resp = OSCdef(replyName, { arg msg, time;
var freq;
// [time, msg].postln;
// "id: ".post; msg[2].postln;
"values: ".post; msg[3..].postln; //syntactical shortcut for "copy from 3rd member till the end of an array"
freq = msg[3];
// postf("spectral centroid: %, flatness: %\n", msg[3].round(0.01), msg[4].round(0.001)); //see String helpfile for postf explanation; also note newline symbol at the end (\n); values are rounded for posting
lastNote.free;
lastNote = playerSynth.note(0, 10).out_(0).amp_(~ampBus).freq_(~pitchBus).play; //free after 10s anyway
// lastNote = playerSynth.note(0, 20).out_(0).amp_(~ampBus).freq_(~pitchBus).ampLagD_(5).freqMul_(3.2).play; //try me
// lastNote = playerSynth.note(0, 20).out_(0).amp_(~ampBus).freq_(freq).ampLagD_(5).freqMul_(3.2).play; //try me
}, replyName); //responding to '/tr' message
)
~synth.note(0, 10).in_(s.options.numOutputBusChannels + ~inputNumber).freqOut_(~pitchBus).ampOut_(~ampBus).rel_(2).play; //play for 10 seconds, see posting
[~resp, ~pitchBus, ~ampBus].do(_.free); //free everything
//other ugens to explore (see helpfiles):
//SpecPcile
//Loudness
//---------
//musical example: onsets trigger harmonizer
//---------
(
var replyName, lastNote, playerSynth;
~inputNumber = 0; //offset into input bus
replyName = '/myReply';
~synth = CtkSynthDef(\spectrHarm,{|in = 0, threshold = 0.93, id = 0, att = 0.01, rel = 0.01, onsetThreshold = 0.5, freqOut = 0, ampOut = 0, out = 0|
var trig, freq, hasFreq, amplitude, inSig;
var localbuf, chain;
var outSig;
inSig = In.ar(in, 1);
// inSig = A2K.kr(inSig);
// inSig = K2A.ar(inSig);
localbuf = LocalBuf(512);
chain = FFT(localbuf, inSig);
trig = Onsets.kr(chain, onsetThreshold); //here we detect onsets
outSig = 3.collect({
PitchShift.ar(inSig, 0.2, TRand.kr(0, 7, trig).round(1).midiratio)
}).sum;
// values = 0; //we don't have anything to send - yet
SendReply.kr(trig, replyName, freq, id); //send freq at the time of onset
Out.ar(out, Pan2.ar(outSig));
//output for continuous signals
// Out.kr(freqOut, freq);
// Out.kr(ampOut, amplitude);
});
)
~synth.note(0, 10).in_(s.options.numOutputBusChannels + ~inputNumber).out_(0).play; //play for 10 seconds, see posting