-
Notifications
You must be signed in to change notification settings - Fork 1
/
02. class 2 - gui, paths.scd
401 lines (325 loc) · 10.4 KB
/
02. class 2 - gui, paths.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
// Class 2
//----------------------------------------------------------------
//file Paths
//----------------------------------------------------------------
//It's a good idea to have all the related files (SC code, audio files, other files if used) in one folder.
//Now how to tell SC they're right next to the SC code? Or in any other RELATIVE location?
"".resolveRelative; //whoa
//now some basic *nix path navigation
"../audio/".resolveRelative; //this is up one level... although you can't tell yet probably
//let's combine that with another useful String method: .pathMatch; use wildcards to return number of results
"*".resolveRelative.pathMatch; //all files in the directory; returns an array of paths
//print it nicely
"*".resolveRelative.pathMatch.do({arg thisPath, inc; thisPath.postln;}); //iterate over these paths and print them with carriage return (".postln")
//syntax shortcut
"*".resolveRelative.pathMatch.do(_.postln); //syntax shortcut for iterating over stuff and doing single thing
//now let's see if we can list the parent directory
"../*".resolveRelative.pathMatch; //yeah
//and print nicely
"../*".resolveRelative.pathMatch.do(_.postln)
//still not sure if that ../ actually gets us anywhere? let's try with an audio file
a = "../audio/1-01 Berio_ Sequenza 1 For Flute.wav".resolveRelative;
b = File.exists(a); //quite useful check!
//we could even be lazy and try to resolve the name for us...
c = "audio/*Flute*".resolveRelative.pathMatch;
c.class;//array!
//so to get the path
d = c[1];
d.class; //string
File.exists(d);
//but if we have more than one file containing key word, like
e = "audio/*.wav".resolveRelative.pathMatch;
e.class;
e.size; // paths
//then
f = e[0];//takes the first file path
//so... back to our soundfile
d.postln;//our file path
s.boot;
g = CtkBuffer.playbuf(d).load;
x = {PlayBuf.ar(2, g)}.play;
x.free;
//clean up
g.free
//now multiple buffers
//using previous example
"../../../../2014-2015/DX598*/*".resolveRelative.pathMatch;
(
~allBuffers = "../../../../2014-2015/DX598*/audio/01*/Kontakte/*.wav".resolveRelative.pathMatch.collect({|thisPath| //iterate over all the matching file paths and return all the results as an array (".collect")
thisPath.postln; //so we can see what's happening
CtkBuffer.playbuf(thisPath).load(sync: true);//last element of the function is always returned; load causes the buffer to be actually loaded on the server
});
)
~allBuffers //an array of CtkBuffers
//so for test we can do
x = {PlayBuf.ar(2, ~allBuffers[0])}.play;
x.free;
//or randomly
x = {PlayBuf.ar(2, ~allBuffers.choose)}.play;
x.free;
//last, but not least - free everythin
~allBuffers.do({|thisBuffer| thisBuffer.free})
//or
~allBuffers.do(_.free);
//----------------------------------------------------------------
//iteration
//----------------------------------------------------------------
(
10.do({|increment|
increment.postln;
});
)
(
a = 10.collect({|increment|
increment.postln;
});
)
a
(
a = 10.collect({|increment|
increment.postln;
[increment, "i'm just a string"];
});
)
(
a = 10.collect({|increment|
increment.postln;
increment + 23;
});
)
a.at(0);
a[0];
a[3..6];
a[0, 2, 5];
a.at(0, 2, 5);
a.at()
a
a.indexOf(32)
(
b = a.collect({|thisItem, increment|
thisItem * increment;
})
)
b;
b = b.add("something completely else");
b
Collection.openHelpFile
//----------------------------------------------------------------
//note on organizing projects
//----------------------------------------------------------------
//SuperCollider doesn't provide version control system, nor straightforward ability to use multiple files for one project
//I believe it's a good idea to increment version number in the file name (e.g. project01.scd, project02.scd) to be able to revert to previous version if needed
//of course it's a good idea to move on to the next version when you have code in a working state, not broken...
//alternatively one can use an external version control system, like git (I almost never bother, but it's seems like a good idea)
//----------------------------------------------------------------
//GUI
//----------------------------------------------------------------
(
w = Window.new("GUI Introduction", Rect(200,200,255,100));
b = Button.new(w,Rect(10,0,80,30)).states_([["Hide"],["Show"],["third state"]]);
s = Slider.new(w,Rect(95,0,150,30));
c = CompositeView.new(w,Rect(20,35,100,60));
StaticText.new(c,Rect(0,0,80,30)).string_("Hello");
StaticText.new(c,Rect(20,30,80,30)).string_("World!");
b.action = { c.visible = b.value.asBoolean.not };
s.action = { c.bounds = Rect( s.value * 150 + 20, 35, 100, 100 ) };
w.front;
)
//Layout! keeps things nice and neat
(
w = Window.new("GUI Introduction").layout_(
VLayout(
HLayout( Button(), TextField(), Button() ),
TextView()
)
).front;
)
//simple player
(
var window, layout, allBuffers, relativeSearchPath, fileNames, synth;
relativeSearchPath = "../../../../2014-2015/DX598*/audio/01*/Kontakte/*.wav"; //configuration
s = Server.default;
s.waitForBoot({
allBuffers = relativeSearchPath.resolveRelative.pathMatch.collect({|thisPath|
thisPath.postln; //so we can see what's happening
CtkBuffer.playbuf(thisPath).load(sync: true);
});
//prepare synth
synth = CtkSynthDef(\stereoPlayer, {|buffer = 0|
Out.ar(0, PlayBuf.ar(2, buffer));
});
//prepare window and layout
window = Window.new("simple player").front; //put window to front right away
layout = VLayout.new;
window.layout_(layout);
allBuffers.do({|thisBuffer, inc|
var note, filename;
filename = thisBuffer.path.basename;
filename.postln; //just the filename, without full path, use for display
layout.add(//add the button to the layout
Button(window)
.states_([[filename], [filename ++ " playing"]])
.action_({|buttonObject|
buttonObject.value.postln;
if(buttonObject.value.asBoolean, {
note = synth.note.buffer_(thisBuffer).play;
}, {
note.free;
});
});
);
});
window.onClose_({
"freeing buffers".postln;
allBuffers.do(_.free);
})
});
)
//same but with keyboard control - preset mappings with keys
(
var window, layout, allBuffers, relativeSearchPath, fileNames, synth, allButtons, mappingDictionary;
relativeSearchPath = "../../../../2014-2015/DX598*/audio/01*/Kontakte/*.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);
});
//prepare synth
synth = CtkSynthDef(\stereoPlayer, {|buffer = 0|
Out.ar(0, PlayBuf.ar(2, buffer));
});
//prepare window and layout
window = Window.new("simple player").front; //put window to front right away
layout = VLayout.new;
window.layout_(layout);
allButtons = allBuffers.collect({|thisBuffer, inc|
var note, filename, button;
filename = thisBuffer.path.basename;
filename.postln; //just the filename, without full path, use for display
layout.add(//add the button to the layout
button = Button(window)
.states_([[filename], [filename ++ " playing"]])
.action_({|buttonObject|
buttonObject.value.postln;
if(buttonObject.value.asBoolean, {
note = synth.note.buffer_(thisBuffer).play;
}, {
note.free;
});
});
);
button;//IMPORTANT - this will go to allButtons
});
window.onClose_({
"freeing buffers".postln;
allBuffers.do(_.free);
});
mappingDictionary = Dictionary.new.putPairs([
$1, 0,
$2, 1,
$3, 2,
$a, 3
]);
//see "Char".openHelpFile;
//also "Dictionary".openHelpFile;
window.view.keyUpAction_({|thisView, char, modifiers, unicode, keycode, key|
var thisIndex, thisButton;
[char, modifiers, unicode, keycode, key].postln;
// char.class.postln;
thisIndex = mappingDictionary[char];
"thisIndex: ".post; thisIndex.postln;
thisButton = allButtons[thisIndex];
"thisButton.value.asBoolean: ".post; thisButton.value.booleanValue.postln;
if(thisButton.value.booleanValue, {
thisButton.valueAction = 0;
}, {
thisButton.valueAction = 1;
});
});
});
)
//alternative way of mapping - offset
(
var window, layout, allBuffers, relativeSearchPath, fileNames, synth, allButtons, mappingDictionary;
relativeSearchPath = "../../../../2014-2015/DX598*/audio/01*/Kontakte/*.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);
});
//prepare synth
synth = CtkSynthDef(\stereoPlayer, {|buffer = 0|
Out.ar(0, PlayBuf.ar(2, buffer));
});
//prepare window and layout
window = Window.new("simple player").front; //put window to front right away
layout = VLayout.new;
window.layout_(layout);
allButtons = allBuffers.collect({|thisBuffer, inc|
var note, filename, button;
filename = thisBuffer.path.basename;
filename.postln; //just the filename, without full path, use for display
layout.add(//add the button to the layout
button = Button(window)
.states_([[filename], [filename ++ " playing"]])
.action_({|buttonObject|
buttonObject.value.postln;
if(buttonObject.value.asBoolean, {
note = synth.note.buffer_(thisBuffer).play;
}, {
note.free;
});
});
);
button;//IMPORTANT - this will go to allButtons
});
window.onClose_({
"freeing buffers".postln;
allBuffers.do(_.free);
});
//see "Char".openHelpFile;
//also "Dictionary".openHelpFile;
window.view.keyUpAction_({|thisView, char, modifiers, unicode, keycode, key|
var thisIndex, thisButton;
[char, modifiers, unicode, keycode, key].postln;
// char.class.postln;
// thisIndex = mappingDictionary[char];
thisIndex = unicode - 49; //49 is unicode for number 0
"thisIndex: ".post; thisIndex.postln;
thisButton = allButtons[thisIndex];
"thisButton.value.asBoolean: ".post; thisButton.value.booleanValue.postln;
if(thisButton.value.booleanValue, {
thisButton.valueAction = 0;
}, {
thisButton.valueAction = 1;
});
});
});
)
Server.killAll //if server hangs, e.g. after disconnecting audio interface
//see more at
"Introduction to GUI".openHelpFile;
//
//clocking
(
w=Window.new.front;
Routine{
20.do{
w.bounds=Rect(200.rand, 200+200.rand, 300,300);
0.1.wait;
};
w.close;
}.play(AppClock)
)
//vs
(
w=Window.new.front;
Routine{
20.do{
{w.bounds=Rect(200.rand, 200+200.rand, 300,300) }.defer; // you must defer this
0.1.wait;
};
{w.close}.defer; // you must defer this
}.play(SystemClock);
)
//this will be important when you want to control GUI from messages from the synth for example!