-
Notifications
You must be signed in to change notification settings - Fork 1
/
14. lag, granular.scd
405 lines (333 loc) · 10.5 KB
/
14. lag, granular.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
//14
// ----------------------------------------
// ----- smoothing out control signals --------
// ----------------------------------------
//instroducing lag
//inside the synth: value.lag, value.lagUD, value.lag2, value.lag2UD, value.lag3, value.lag3UD
//same as Lag.kr(value)
//they take time as argument
//help:
"Lag".openHelpFile;
"Lag2".openHelpFile;
"Lag3".openHelpFile;
"VarLag".openHelpFile;
//slider example
(
var window, layout, sliders, synths, note, bus, resp, respPath, labels;
labels = [\input, \lag, \lag2, \lag3, \lagud, \lag2ud, \lag3ud, \varlag];
respPath='/lagged';
s.waitForBoot({
synths = CtkProtoNotes(
SynthDef(\lags, {arg inVal = 0, lagTime = 1, lagTimeU = 1, lagTimeD = 3, curve = 0;
var trig;
// inVal = In.kr(in, 1);
trig = Impulse.kr(30);
SendReply.kr(trig, respPath, [
inVal.lag(lagTime),
inVal.lag2(lagTime),
inVal.lag3(lagTime),
inVal.lagud(lagTimeU, lagTimeD),
inVal.lag2ud(lagTimeU, lagTimeD),
inVal.lag3ud(lagTimeU, lagTimeD),
inVal.varlag(lagTime, curve),
]
)
})
);
s.sync; //sync before playing
note = synths[\lags].note.play;
~note = note; //to set something later
window = Window.new.front;
layout = HLayout.new;
window.layout = layout; //assigning layout to the window
sliders = 8.collect({|inc|
var thisSlider;
thisSlider = Slider.new;
layout.add(
VLayout(
thisSlider,
StaticText.new.string_(labels[inc])
)
);
thisSlider;
});
sliders[0].action_({arg sl;
// bus.setSynchronous(sl.value)
note.inVal_(sl.value);
}); //first slider changes values
resp = OSCdef(\lags, {arg values;
var sliderValues;
// values.postln;
sliderValues = values[3..];
{
sliderValues.do({|thisVal, inc|
sliders[inc+1].value = thisVal
});
}.defer;
}, respPath);
//free:
window.onClose_({
note.free;
resp.free;
});
});
)
//slider example with sinewave
(
var window, layout, sliders, synths, note, bus, resp, respPath, labels;
labels = [\input, \lag, \lag2, \lag3, \lagud, \lag2ud, \lag3ud, \varlag];
respPath='/lagged';
s.waitForBoot({
synths = CtkProtoNotes(
SynthDef(\lags, {arg inVal = 0, lagTime = 1, lagTimeU = 2, lagTimeD = 0.5, curve = 0, amp = 0.063, out = 0;
var trig;
// inVal = In.kr(in, 1);
trig = Impulse.kr(30);
Out.ar(out, SinOsc.ar(500 * ([
inVal,
inVal.lag(lagTime),
inVal.lag2(lagTime),
inVal.lag3(lagTime),
inVal.lagud(lagTimeU, lagTimeD),
inVal.lag2ud(lagTimeU, lagTimeD),
inVal.lag3ud(lagTimeU, lagTimeD),
inVal.varlag(lagTime, curve),
] + 1), 0, amp).sum.dup(2));
SendReply.kr(trig, respPath, [
inVal.lag(lagTime),
inVal.lag2(lagTime),
inVal.lag3(lagTime),
inVal.lagud(lagTimeU, lagTimeD),
inVal.lag2ud(lagTimeU, lagTimeD),
inVal.lag3ud(lagTimeU, lagTimeD),
inVal.varlag(lagTime, curve),
]
)
})
);
s.sync; //sync before playing
note = synths[\lags].note.play;
~note = note; //to set something later
window = Window.new.front;
layout = HLayout.new;
window.layout = layout; //assigning layout to the window
sliders = 8.collect({|inc|
var thisSlider;
thisSlider = Slider.new;
layout.add(
VLayout(
thisSlider,
StaticText.new.string_(labels[inc])
)
);
thisSlider;
});
sliders[0].action_({arg sl;
note.inVal_(sl.value);
}); //first slider changes values
resp = OSCdef(\lags, {arg values;
var sliderValues;
// values.postln;
sliderValues = values[3..];
{
sliderValues.do({|thisVal, inc|
sliders[inc+1].value = thisVal
});
}.defer;
}, respPath);
//free:
window.onClose_({
note.free;
resp.free;
});
});
)
//slider example with trigger
(
var window, layout, sliders, synths, note, bus, resp, respPath, labels;
labels = [\input, \lag, \lag2, \lag3, \lagud, \lag2ud, \lag3ud, \varlag];
respPath='/lagged';
s.waitForBoot({
synths = CtkProtoNotes(
SynthDef(\lags, {arg inVal = 0, lagTime = 1, lagTimeU = 0.1, lagTimeD = 2, curve = 0, amp = 0.5, out = 0, trigLo = 0.2, trigHi = 0.8;
var trig;
trig = Impulse.kr(30);
Out.ar(out, PinkNoise.ar(amp).dup(2) * Schmidt.kr( //uncomment ONE LINE at a time
// inVal,
// inVal.lag(lagTime),
// inVal.lag2(lagTime),
// inVal.lag3(lagTime),
// inVal.lagud(lagTimeU, lagTimeD),
// inVal.lag2ud(lagTimeU, lagTimeD),
inVal.lag3ud(lagTimeU, lagTimeD),
// inVal.varlag(lagTime, curve),
trigLo, trigHi)
);
SendReply.kr(trig, respPath, [
inVal.lag(lagTime),
inVal.lag2(lagTime),
inVal.lag3(lagTime),
inVal.lagud(lagTimeU, lagTimeD),
inVal.lag2ud(lagTimeU, lagTimeD),
inVal.lag3ud(lagTimeU, lagTimeD),
inVal.varlag(lagTime, curve),
]
)
})
);
s.sync; //sync before playing
note = synths[\lags].note.play;
~note = note; //to set something later
window = Window.new.front;
layout = HLayout.new;
window.layout = layout; //assigning layout to the window
sliders = 8.collect({|inc|
var thisSlider;
thisSlider = Slider.new;
layout.add(
VLayout(
thisSlider,
StaticText.new.string_(labels[inc])
)
);
thisSlider;
});
sliders[0].action_({arg sl;
note.inVal_(sl.value);
}); //first slider changes values
resp = OSCdef(\lags, {arg values;
var sliderValues;
// values.postln;
sliderValues = values[3..];
{
sliderValues.do({|thisVal, inc|
sliders[inc+1].value = thisVal
});
}.defer;
}, respPath);
//free:
window.onClose_({
note.free;
resp.free;
});
});
)
// ----------------------------------------
// ----- granular synthesis from input signal ----
// ----------------------------------------
(
s.waitForBoot({
~synths = CtkProtoNotes(
SynthDef(\recorder, {arg in = 0, buffer = 0;
var inSig;
inSig = In.ar(in, 1);
RecordBuf.ar(inSig, buffer);
}),
SynthDef(\player, {arg out = 0, buffer = 0, playRateLo = 0.5, playRateHi = 1.5, amp = 1, pan = 0, gate = 1;
var outSig, env;
env = EnvGen.kr(Env([0, 1, 0], [1, 1], \sin, 1), gate, doneAction: 2);
outSig = GrainBuf.ar(
2, //number of channels for panning
Impulse.kr(10), //trigger; on each change of the signal from 0 to >0, new grain is being produced
0.1, //grain duration
buffer, //monophonic buffer
LFNoise1.kr.range(playRateLo, playRateHi), //playback rate
LFNoise2.kr(0.1).range(0, 1), //position in the file (0-1)
4, //interpolation - here cubic for best sound (but more computationally expensive)
pan //panning
);
Out.ar(out, outSig * amp * env);
})
);
~someBuffer = CtkBuffer(size: s.sampleRate * 10 /*10 seconds*/, numChannels: 1, server: s).load; //remember to .load!
s.sync; //wait for the buffer to load on the server; this is possible since we're in the .waitForBoot routine
});
)
s.plotTree;
s.meter;
~someBuffer.plot;
//record
~recNote = ~synths[\recorder].note.in_(s.options.numOutputBusChannels + 0).buffer_(~someBuffer).play;
~recNote.free;
~someBuffer.plot;
//play
~playNote = ~synths[\player].note.out_(0).buffer_(~someBuffer).play;
~playNote.free;
//saving
~someBuffer.write(path: "mySound.wav".resolveRelative, headerFormat: 'wav', sampleFormat: 'float');
//free when done!
~someBuffer.free;
// --------------------------------------------------------------------------------
// ----- granular synthesis from input signal - advanced, keeping track of endposition ----
// --------------------------------------------------------------------------------
(
~responder.free; ~someBuffer.free; ~posBus.free; ~playNote.free; ~recNote.free;
~startTimes = List.new;
s.waitForBoot({
~synths = CtkProtoNotes(
SynthDef(\recorderTrigTimeLog, {arg in = 0, buffer = 0, ampFollowerAtt = 0, ampFollowerRel = 0.2, trigAmpStart = 0.5, trigAmpStop = 0.2, curPosOut = 0;
var inSig, amplitude, trigger, currentFrame, currentTime, inverseTrigger, lastFrameValue;
inSig = In.ar(in, 1);
amplitude = Amplitude.kr(inSig, ampFollowerAtt, ampFollowerRel);
amplitude = Amplitude.kr(inSig, ampFollowerAtt, ampFollowerRel);
trigger = Schmidt.kr(amplitude, trigAmpStop, trigAmpStart);
inverseTrigger = trigger.neg + 1;
lastFrameValue = Latch.kr(LocalIn.kr(1), inverseTrigger);
currentFrame = Phasor.kr(trigger, 1, 0, BufFrames.kr(buffer), lastFrameValue); //timer to report current sample
LocalOut.kr(currentFrame);
currentTime = currentFrame / ControlRate.ir; //convert to time; ControlDur is inverse of ControlRate
Out.kr(curPosOut, Gate.kr(currentTime, trigger));
RecordBuf.ar(inSig, buffer, run: trigger);
SendReply.kr(trigger, '/trigger', [trigger, currentTime, currentFrame]); //when rec starts
SendReply.kr(inverseTrigger, '/trigger', [trigger, currentTime, currentFrame]); //when rec stops
}),
SynthDef(\player, {arg out = 0, buffer = 0, playRateLo = 0.5, playRateHi = 1.5, amp = 1, pan = 0, gate = 1, curPosIn = 0;
var outSig, env, curPosSig, trig, pos;
curPosSig = In.kr(curPosIn, 1);
env = EnvGen.kr(Env([0, 1, 0], [1, 1], \sin, 1), gate, doneAction: 2);
trig = Impulse.kr(10);
pos = TRand.kr(0, (((curPosSig-0.1)/BufDur.kr(buffer)) ).clip(0, 1), trig);//limit current position, subtract current grain duration
outSig = GrainBuf.ar(
2, //number of channels for panning
trig, //trigger; on each change of the signal from 0 to >0, new grain is being produced
0.1, //grain duration
buffer, //monophonic buffer
LFNoise1.kr.range(playRateLo, playRateHi), //playback rate
pos, //position in the file (0-1)
4, //interpolation - here cubic for best sound (but more computationally expensive)
pan //panning
);
Out.ar(out, outSig * amp * env);
})
);
~posBus = CtkControl.play(1);
~someBuffer = CtkBuffer(size: s.sampleRate * 30 /*30 seconds*/, numChannels: 1, server: s).load; //remember to .load!
s.sync; //wait for the buffer to load on the server; this is possible since we're in the .waitForBoot routine
~responder = OSCdef(\trig, {|val|
var trigVal, curTime;
val.postln;
trigVal = val[3];
curTime = val[4];
if(trigVal == 1, {
~startTimes.add([curTime, 0]); //0 as end time
}, {
if(~startTimes.lastIndex.notNil, {
~startTimes[~startTimes.lastIndex][1] = curTime;
});
});
}, '/trigger');
});
)
//record
~recNote = ~synths[\recorderTrigTimeLog].note.in_(s.options.numOutputBusChannels + 0).buffer_(~someBuffer).trigAmpStart_(-12.dbamp).trigAmpStop_(-36.dbamp).ampFollowerRel_(2).curPosOut_(~posBus).play;
//play
~playNote = ~synths[\player].note.out_(0).buffer_(~someBuffer).curPosIn_(~posBus).play;
//check current position
s.getControlBusValue(~posBus.bus);
~someBuffer.plot;
~playNote.free;
~responder.free; ~someBuffer.free; ~posBus.free; ~playNote.free; ~recNote.free;
s.plotTree;
s.meter;
s.scope
~someBuffer.plot;