/
ixi_SC_tutorial_07.sc~
474 lines (351 loc) · 11.7 KB
/
ixi_SC_tutorial_07.sc~
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
// =====================================================================
// - SuperCollider Basics -
// =====================================================================
// Tutorial 07 - Envelopes
// =====================================================================
// - ixi audio tutorial - www.ixi-audio.net
// =====================================================================
/*
---------------------------------------------------------------
Copyright (c) 2005-2008, ixi audio.
This work is licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 2.0 England & Wales License.
http://creativecommons.org/licenses/by-nc-sa/2.0/uk/
---------------------------------------------------------------
*/
// ========== Contents of this tutorial ==========
// 1) Envelope generator
// 2) Envelope types
// 3) Triggers and gates
// 4) MIDI keyboard example
// 1) ========= Envelope generator ==========
// To create an envelope, we need an envelope generator
// The envelope generator can calculate various types of envelopes
// it contains the envelope and performs the doneActions (what
// happens when the envelope ends)
EnvGen.ar(envelope, gate, levelScale, levelBias, timeScale, doneAction)
doneActions:
0 do nothing when the envelope has ended.
1 pause the synth running, it is still resident.
2 remove the synth and deallocate it.
3 remove and deallocate both this synth and the preceeding node.
4 remove and deallocate both this synth and the following node.
5 remove and deallocate this synth
and if the preceeding node is a group then do g_freeAll on it, else n_free it.
6 remove and deallocate this synth
and if the following node is a group then do g_freeAll on it, else n_free it.
7 remove and deallocate this synth and all preceeding nodes in this group
8 remove and deallocate this synth and all following nodes in this group
9 remove and deallocate this synth and pause the preceeding node
10 remove and deallocate this synth and pause the following node
11 remove and deallocate this synth
and if the preceeding node is a group then do g_deepFree on it, else n_free it.
12 remove and deallocate this synth
and if the following node is a group then do g_deepFree on it, else n_free it.
13 remove and deallocate this synth and all other nodes in this group (before and after)
// 2) ========= Envelope typees ==========
// try running the following lines and look at the output:
Env.linen(1, 2, 3, 0.6).test.plot;
Env.triangle(1, 1).test.plot;
Env.sine(1,1).test.plot;
Env.perc(0.05, 1, 1, -4).test.plot;
Env.adsr(0.02, 0.2, 0.25, 1, 1, -4).test.plot;
Env.asr(0.02, 0.5, 1, 1).test.plot;
Env.cutoff(1, 1).test(2).plot;
// using .new you can define your own envelope
Env.new([0, 1, 0.3, 0.8, 0], [2, 3, 1, 4],'sine').test.plot;
Env.new([0,1, 0.3, 0.8, 0], [2, 3, 1, 4],'linear').test.plot;
// 3) ========= Triggers and gates ==========
// gate holds the EnvGen open. Here using Dust (random impulses) to trigger a new envelope
{EnvGen.ar(Env.adsr(0.001, 0.8, 0, 0.01, 1), Dust.ar(0.1)) * SinOsc.ar}.play
// Here using Impulse (periodic impulses)
{EnvGen.ar(Env.adsr(0.0001, 0.8, 0, 0.01, 1), Impulse.ar(2)) * SinOsc.ar}.play
// With a doneAction: 2 we kill the synth after the first envelope
{EnvGen.ar(Env.adsr(0.0001, 0.8, 0, 0.01, 1), Impulse.ar(2), doneAction:2) * SinOsc.ar}.play
// Envelopes can be used everywhere in the code, not just for amplitude
(
{
40.do({
{ var freq, ratio, modulator, carrier;
// create an array of 12 midinotes, choose one, change it to cps and post it:
freq = Array.fill(12, {arg i; 60 + i}).choose.midicps.postln;
ratio = 2; // EnvGen.ar(Env.perc(0.05, 1)); // try this
modulator = SinOsc.ar(freq * ratio, 0, EnvGen.ar(Env.sine(0.5, 1))*15);
carrier = SinOsc.ar(freq + (modulator * freq), 0, 0.5);
carrier * EnvGen.ar(Env.perc(0.01, 1), doneAction:2)
}.play;
1.wait;
});
}.fork
)
// Triggers
// in the example above we saw how Dust and Impulse could be used to trigger
// an envelope.
// the trigger can be set from everywhere (code, GUI, system, etc)
// (but use "t_" in front of trigger arguments.
(
a = { arg t_gate = 1;
var freq;
freq = EnvGen.kr(Env.new([200, 200, 800], [0, 1.6]), t_gate);
SinOsc.ar(freq,
0, 0.2
) ! 2
}.play;
)
a.set(\t_gate, 1) // do this repeatedly
a.free
(
a = { arg t_gate = 1;
var env;
env = EnvGen.kr(Env.perc, t_gate);
SinOsc.ar(40, 0, 1 * env) ! 2
}.play;
)
a.set(\t_gate, 1) // do this repeatedly
a.free
// if you want to keep the same synth on the server and trigger it
// from another process than the synthesis control parameter process
// you can use gates and triggers for the envelope.
// use doneAction:0 to keep the synth on the server after the envelope is finished
// WITH A FLEXIBLE TIME ENVELOPE (USING GATE)
SynthDef(\trigtest, {arg freq, amp, dur=1, gate;
var signal, env;
env = EnvGen.ar(Env.adsr(0.01, dur, amp, 0.7), gate, doneAction:0);
signal = SinOsc.ar(freq) * env;
Out.ar(0, signal) ! 2;
}).load(s)
a = Synth(\trigtest, [\freq, 333, \amp, 1, \gate, 1])
a.set(\gate, 0)
a.set(\gate, 1)
a.set(\gate, 0)
a.set(\freq, 788)
a.set(\gate, 1)
a.set(\gate, 0)
// WITH A FIXED TIME ENVELOPE (USING TRIGGER)
// here we use a t_trig to retrigger the synth
(
SynthDef(\mygrain, {arg freq, amp, dur=1, t_trig;
var signal, env;
env = EnvGen.ar(Env.sine(dur, amp), t_trig, doneAction:0);
signal = SinOsc.ar(freq) * env;
Out.ar(0, signal ! 2);
}).writeDefFile;
Server.local.sendMsg("/d_load", SynthDef.synthDefDir ++ "mygrain.scsyndef");
)
(
var w, f, d, values, server, id, isOn = false;
var b1, b2, s, t;
t = Task({
var a;
var dur = 0.01;
var freq = 1/dur;
a = Synth(\mygrain, [\dur, dur, \freq, 160, \amp, 0.5, \t_trig, 1]);
freq.do({
a.set(\t_trig, 1);
dur.wait();
});
});
values = IdentityDictionary.new;
server = Server.local;
f = { arg name, spec = \lin, guispec;
var height = 20, v, s, n;
guispec = guispec ? spec;
spec = spec.asSpec;
guispec = guispec.asSpec;
v = GUI.staticText.new(w, Rect(0, 0, 72, height));
v.font = GUI.font.new("Helvetica", 12);
v.stringColor = Color.black;
v.align = \right;
s = GUI.slider.new(w, Rect(0, 0, 182, height));
s.resize = 2;
s.action = {
var val, guival, step;
val = spec.map(s.value);
values.put(name, val);
if (isOn, { server.sendMsg("/n_set", id, name, val); });
guival = guispec.map(s.value);
step = pow(10, floor(min(0, guival.abs.log10 - 2)));
v.string = guival.round(step).asString ++ guispec.units;
};
s.value = spec.unmap(spec.default);
s.action.value;
n = GUI.staticText.new(w, Rect(0, 0, 72, height)); n.string = name;
n.stringColor = Color.black;
n.font = GUI.font.new("Helvetica", 12);
n.resize = 3;
w.view.decorator.nextLine;
};
id = 2001;
w = GUI.window.new("trem", Rect(512, 64, 360, 130));
w.view.decorator = d = FlowLayout(w.view.bounds);
b1 = GUI.button.new(w, Rect(0,0, 80, 24));
b1.states = [["Load", Color.black, Color.green]];
b1.action = { arg view;
server.sendMsg("/d_load", "synthdefs/mygrain.scsyndef");
};
b1.enabled = server.serverRunning;
b2 = GUI.button.new(w, Rect(0,0, 80, 24));
b2.states = [
["Play", Color.black, Color.green],
["Stop", Color.white, Color.red],
];
b2.action = { arg view;
var msg;
if (view.value == 1, {
isOn = true;
t.play();
},{
isOn = false;
t.pause();
});
};
b2.enabled = server.serverRunning;
d.nextLine;
f.value(\dur);
f.value(\rate, \lofreq);
f.value(\amp, \amp, \db);
a = SimpleController(server);
f = { arg server;
b1.enabled = server.serverRunning;
b2.enabled = server.serverRunning;
if (server.serverRunning.not, { b2.value = 0 });
};
a.put(\serverRunning, f);
w.onClose = {
if (isOn, { server.sendMsg("/n_free", 2001) });
a.remove;
};
w.front;
)
(
t.play();
)
t.free();
a.set(\freq, 788)
a.set(\t_trig, 1);
a.set(\amp, 0.28)
a.set(\t_trig, 1);
a.set(\freq, 588)
a.set(\t_trig, 1);
a.set(\amp, 0.8)
a.set(\t_trig, 1);
// 4) ========= MIDI keyboard example ==========
/*
In order to use MIDI, we connect our peripherals and make sure they are working.
Then we ...
a) initialise the MIDIClient:
MIDIClient.init;
b) start the MIDIIn responcer
MIDIIn.connect;
c) then we define what midi methods we are waiting for - such as :
noteOn
noteOff
polytouch
control
program
touch
bend
sysex
NOTE: all the synthdefs in these examples are quite boring and uninteresting.
It's up to you to make them sound good!
*/
// Now we set up our MIDI functions:
MIDIClient.init(2,2); // check how many sources you have (in the post window)
// in my case: Sources: [ IAC Driver : IAC Bus 1, FireWire 410 : FireWire 410 ]
MIDIIn.connect(1, MIDIClient.sources.at(1)); // select the source you want (I want the FireWire 410 as source)
MIDIIn.noteOn = {arg src, chan, num, vel; [src, chan, num, vel].postln;};
//MIDIIn.connect(0, MIDIClient.sources.at(0).uid);
// this is the synthdef we are going to use
(
SynthDef(\midisynth1, { arg freq=440, filter=400, gate=0.0, vibrato=0.0;
var x;
x = Saw.ar(freq * SinOsc.ar(vibrato/20, 0, 1, 1), 1);
x = MidEQ.ar(x, freq+filter, 0.3, 12);
x = EnvGen.kr(Env.adsr, gate, Latch.kr(gate, gate)) * x;
Out.ar(0, x!2);
}).load(s);
)
x = Synth(\midisynth1);
// let's try our synth
x.set(\freq, 444);
x.set(\gate, 1 );
x.set(\vibrato, 20 );
x.set(\gate, 0 );
x.free;
(
//set the MIDI into action:
x = Synth(\midisynth1); // we are controlling the same synth, therefore no doneAction in the envelope
MIDIIn.noteOn = {arg src, chan, num, vel;
x.set(\freq, num.midicps);
x.set(\gate, vel / 127 );
//x.set(\formfreq, vel / 127 * 1000);
};
MIDIIn.noteOff = { arg src,chan,num,vel;
x.set(\gate, 0.0);
};
MIDIIn.bend = { arg src,chan,val;
(val * 0.048828125).postln;
//val.postln;
x.set(\filter, val*0.148828125 );
};
MIDIIn.control = {arg src, chan, num, vel;
//vel.postln;
x.set(\vibrato, vel );
}
)
// the program above is monophonic.
// for polyphony, we store each note in an array and control the array
(
SynthDef(\midisynth2, { arg freq=440, filter=400, gate=0.0, vibrato=0.0;
var x;
x = Saw.ar(freq * SinOsc.ar(vibrato/20, 0, 1, 1), 1);
x = MidEQ.ar(x, freq+filter, 0.3, 12);
x = EnvGen.kr(Env.adsr, gate, Latch.kr(gate, gate), doneAction:2) * x;
Out.ar(0, x!2);
}).load(s);
)
(
var poly;
poly = Array.fill(128, 0); // array with all possible notes
MIDIIn.noteOn = {arg src, chan, num, vel;
if( poly[num].notNil ){
poly.put(num, Synth(\midisynth2, [\freq, num.midicps, \gate, vel/127]))
}{
Ê// sustain pedal safety. if previous synth exists, free it.Ê
poly[num].set(\gate, 0);
poly.put(num, Synth(\midisynth2, [\freq, num.midicps, \gate, vel/127]))
}
};
MIDIIn.noteOff = { arg src,chan,num,vel;
poly[num].set(\gate, 0);
};
MIDIIn.bend = { arg src,chan,val;
poly.do({arg synth; if( synth!=0 , { synth.set(\filter, val*0.148828125 ) }); });
};
MIDIIn.control = {arg src, chan, num, vel;
poly.do({arg synth; if( synth!=0 , { synth.set(\vibrato, vel ) }); });
};
)
////////// another way is to use the NoteOnResponder (check the class file)
(
SynthDef(\midisynth3, {|freq=400, gate=1, vol=1|Ê
var signal;
signal = Saw.ar(freq, vol) * EnvGen.kr(Env.asr(0.1, 1, 1), gate:gate, doneAction:2);
Out.ar(0,Êsignal!2)
}).load(s);
)
(
var n, d, poly;
poly = Array.fill(128, 0);
n = NoteOnResponder({|src,chan,num,veloc|Ê
if( poly[num].notNil ){
poly.put(num, Synth(\midisynth3, [\freq, num.midicps, \vol, veloc/127]))
}{
Ê// sustain pedal safety. if previous synth exists, free it.Ê
poly[num].set(\gate, 0);
poly.put(num, Synth(\midisynth3, [\freq, num.midicps, \vol, veloc/127]))
}
});
d = NoteOffResponder({|src,chan,num,veloc| poly[num].set(\gate, 0) });
CmdPeriod.doOnce({n.remove; d.remove}); // on command + period, free the note responders (on and off)
)