-
Notifications
You must be signed in to change notification settings - Fork 1
/
03.1. Dictionary, GUI, multiple functions.scd
335 lines (278 loc) · 8.79 KB
/
03.1. Dictionary, GUI, multiple functions.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
//DX463
//Fall 2015
//class 03.1
/*
03.1
- Synth vs Ctk approach
- streamline variable declarations
- CtkProtoNotes
- cmdPeriod to free up resources
- slider for synth parameters
- commandPeriod for freeing buffers!
- clocking for the GUI
03.2 (see another file)
- function for creating tasks
- task -> button
- slider for task's/synth's parameters
- multiple tasks, buttons
- lab:
- extend previous assignment to create:
- multiple tasks
- control them on/off with bottons
- control at least one parameter inside each of them with a slider
- hw
- use the framework from the lab
- create more tasks (think modular)
- control more parameters with sliders
*/
//--------------------------------------------------
// Synth, SynthDef and Ctk
//--------------------------------------------------
s.boot;
//in the help and on the web you'll usually see examples that don't use Ctk... let's see a simple example to see the relation
//Synth and SynthDef classes
(
SynthDef(\SimpleSine, {|freq = 440|
Out.ar(0, SinOsc.ar(freq, 0, 0.2)) }
).add; //.add is important!
)
// now make a synth from it, using the default value for freq
x = Synth(\SimpleSine);
x.set(\freq, 880);
x.set(\freq, 280);
x.free;
//or with defined value
x = Synth(\SimpleSine, [\freq, 660]);
x.free;
//with Ctk
(
~def = CtkSynthDef(\SimpleSinedifferent, {|freq = 440|
Out.ar(0, SinOsc.ar(freq, 0, 0.2)) }
); //note no explicit .add
)
~note = ~def.note.play;
~note.freq_(567);
~note.free;
//--------------------------------------------------
// Streamlining vairable declaration
//--------------------------------------------------
"IdentityDictionary".openHelpFile;
"Dictionary".openHelpFile;
"Symbol".openHelpFile;
//you can use them to aviod having too many variables... there will be more examples on the way
//right now - some abstract example
(
var synths; //variables for other things, not used here
var coolSound, coolerSound, coolestSound, another;
//synths etc...
//...
//notes
coolSound = "some Ctk note";
coolerSound = "some other Ctk note";
coolestSound = "different Ctk note";
another = "yet another Ctk note";
//task/play
// coolSound.play;
// coolerSound.play; etc...
)
//---
//same with a dictionary to group notes into one variable
(
var synths; //variables for other things, not used here
var notes;//no need to declare separate variables
notes = IdentityDictionary.new;
//synths etc...
//...
//notes
notes[\coolSound] = "some Ctk note"; //use any convenient name for dictionary key; shortcut for notes.put(\coolSound, "some Ctk note")
notes[\coolerSound] = "some other Ctk note";
notes[\coolestSound] = "different Ctk note";
notes[\another] = "yet another Ctk note";
//task/play
// notes[\coolSound].play;
// notes[\coolerSound].play; etc...
)
// example adapted from the score-task conversion
//let's say you need to set note's parameters, so we need to assign them to variables. Quickly we can end up with way too many of them
s.reboot;
(
var task, synth, options;
var note0, note1, note2, coolNote; //etc
synth = CtkSynthDef(\oscili, {arg freq, amp, envDur;
var osc, osc2, env;
osc = SinOsc.ar(freq, 0, amp);
env = Line.kr(1, 0, envDur, doneAction: 0);
Out.ar(0, osc * env)
});
//create a notes, DON'T PLAY yet
note0 = synth.note(0, 8).freq_(420).envDur_(8).amp_(0.2);
note1 = synth.note(0, 6).freq_(540).envDur_(6).amp_(0.2);
note2 = synth.note(0, 10).freq_(640).envDur_(10).amp_(0.2);
coolNote = synth.note(0, 6).freq_(1870).envDur_(6).amp_(0.2);
//use task to play and change parameters
task = Task({
0.2.wait; //wait for 0.2 seconds; same as 0.2.yield
note0.play; //play the first note
1.8.wait; //wait
note0.freq_(1220); //change the frequency while playing
3.5.wait;
note1.play; //play the second note
1.1.wait;
note1.amp_(0.8); //change amp while playing
3.1.wait;
note2.play; //play the first note
1.8.wait; //wait
note2.freq_(220); //change the frequency while playing
4.wait;
coolNote.play; //play the second note
1.1.wait;
coolNote.amp_(0.8); //change amp while playing
3.1.wait;
});
task.play; //play the task
)
//now version with a dictionary
(
var task, synth, options;
var notes; //just one variable declared for all the notes
notes = IdentityDictionary.new; //create a dictionary
synth = CtkSynthDef(\oscili, {arg freq, amp, envDur;
var osc, osc2, env;
osc = SinOsc.ar(freq, 0, amp);
env = Line.kr(1, 0, envDur, doneAction: 0);
Out.ar(0, osc * env)
});
//create a notes, DON'T PLAY yet
notes[\note0] = synth.note(0, 8).freq_(420).envDur_(8).amp_(0.2);
notes[\note1] = synth.note(0, 6).freq_(540).envDur_(6).amp_(0.2);
notes[\note2] = synth.note(0, 10).freq_(640).envDur_(10).amp_(0.2);
notes[\coolNote] = synth.note(0, 6).freq_(1870).envDur_(6).amp_(0.2);
//we could freely add notes here, without adding variable declarations
//use task to play and change parameters
task = Task({
0.2.wait; //wait for 0.2 seconds; same as 0.2.yield
notes[\note0].play; //play the first note
1.8.wait; //wait
notes[\note0].freq_(1220); //change the frequency while playing
3.5.wait;
notes[\note1].play; //play the second note
1.1.wait;
notes[\note1].amp_(0.8); //change amp while playing
3.1.wait;
notes[\note2].play; //play the first note
1.8.wait; //wait
notes[\note2].freq_(220); //change the frequency while playing
4.wait;
notes[\coolNote].play; //play the second note
1.1.wait;
notes[\coolNote].amp_(0.8); //change amp while playing
3.1.wait;
});
task.play; //play the task
)
//--------------------------------------------------
// CtkProtoNotes
//--------------------------------------------------
//one nice thing about the Synth class is that we don't need to create variables for our synthdefs... but we can have something similar with Ctk. Very handy with multiple SynthDefs!
(
~allSynthDefs = CtkProtoNotes(
SynthDef(\SimpleSine, {|freq = 440| //NOT CtkSynthDef!
Out.ar(0, SinOsc.ar(freq, 0, 0.2)) }
); //note no explicit .add
);//CtkProtoNotes closing parenthesis
)
~note = ~allSynthDefs[\SimpleSine].note.play; //access like a Dictionary
~note.freq_(567);
~note.free;
//makes more sense with multiple synths
(
~allSynthDefs = CtkProtoNotes(
SynthDef(\SimpleSine, {|freq = 440| //NOT CtkSynthDef!
Out.ar(0, SinOsc.ar(freq, 0, 0.2)) }
), //NOT a semicolon! all SynthDefs are subsequent arguments to CtkProtoNotes
SynthDef(\SimpleNoise, {|amp = 0.5|
Out.ar(0, PinkNoise.ar(amp));
});
);
)
~note = ~allSynthDefs[\SimpleSine].note.play;
~note.freq_(567);
~note.free;
~note2 = ~allSynthDefs[\SimpleNoise].note.play;
~note2.free;
//--------------------------------------------------
// CmdPeriod
//--------------------------------------------------
//if you try things out, or want to have a safer "panic" solution for the performance, you can assign actions to pressing Cmd-.
(
CmdPeriod.doOnce({
Window.new("I'm a Cmd-. window").front;
});
)
//usually you would like to free buffers there
(
var window, allBuffers, relativeSearchPath, fileNames, synth, allButtons, mappingDictionary, vLayout;
relativeSearchPath = "../02/audio/*.wav"; //configuration
s.waitForBoot({
allBuffers = relativeSearchPath.resolveRelative.pathMatch.collect({|thisPath|
thisPath.postln; //so we can see what's happening
CtkBuffer.playbuf(thisPath).load(sync: true);
});
//test play
{PlayBuf.ar(2, allBuffers.choose)}.play;
// a = allBuffers;
CmdPeriod.doOnce({
"Clearing all buffers!".postln;
allBuffers.do(_.free);
});
});
)
//--------------------------------------------------
// using a slider to control frequency
//--------------------------------------------------
(
var task, synth, options;
var window, slider;
var notes; //just one variable declared for all the notes
notes = IdentityDictionary.new; //create a dictionary
//GUI
window = Window.new("slider").front;
window.layout_(
VLayout(
slider = Slider.new.orientation_(\horizontal),
nil//empty space underneath
)
);
synth = CtkSynthDef(\oscili, {arg freq, amp, envDur;
var osc, osc2, env;
osc = SinOsc.ar(freq, 0, amp); //vs
// osc = SinOsc.ar(freq.lag(0.05), 0, amp);
env = Line.kr(1, 0, envDur, doneAction: 0);
Out.ar(0, osc * env)
});
//create a notes
notes[\note0] = synth.note(0, 18).freq_(420).envDur_(18).amp_(0.2);
//once we have a note assigned to variable, let's use that variable to address it when we move the slider
slider.action_({arg sliderObject;
var sliderValue;
sliderValue = sliderObject.value;
"sliderValue before scaling: ".post; sliderValue.postln;
//scale it
sliderValue = (sliderValue * 1000) + 400;
"sliderValue after scaling: ".post; sliderValue.postln;
notes[\note0].freq_(sliderValue);
});
//use task to play and change parameters
task = Task({
0.2.wait; //wait for 0.2 seconds; same as 0.2.yield
notes[\note0].play; //play the first note
});
task.play; //play the task
//release thinggs
window.onClose_({
notes.do({arg thisNote;
thisNote.free;
});
});
//!!!! what about that discontinuity when changing the frequency? check other option in the synthdef
)