-
Notifications
You must be signed in to change notification settings - Fork 2
/
Engine_Glut.sc
274 lines (217 loc) · 6.14 KB
/
Engine_Glut.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
Engine_Glut : CroneEngine {
classvar nvoices = 7;
var pg;
var effect;
var <buffersL;
var <buffersR;
var <voices;
var mixBus;
var <phases;
var <levels;
var <seek_tasks;
*new { arg context, doneCallback;
^super.new(context, doneCallback);
}
// disk read
readBuf { arg i, path;
if(buffersL[i].notNil && buffersR[i].notNil, {
if (File.exists(path), {
var numChannels;
var newbuf;
numChannels = SoundFile.use(path.asString(), { |f| f.numChannels });
newbuf = Buffer.readChannel(context.server, path, 0, -1, [0], { |b|
voices[i].set(\buf_l, b);
buffersL[i].free;
buffersL[i] = b;
});
if (numChannels > 1, {
newbuf = Buffer.readChannel(context.server, path, 0, -1, [1], { |b|
voices[i].set(\buf_r, b);
buffersR[i].free;
buffersR[i] = b;
});
}, {
voices[i].set(\buf_r, newbuf);
buffersR[i].free;
buffersR[i] = newbuf;
});
});
});
}
alloc {
buffersL = Array.fill(nvoices, { arg i;
Buffer.alloc(
context.server,
context.server.sampleRate * 1,
);
});
buffersR = Array.fill(nvoices, { arg i;
Buffer.alloc(
context.server,
context.server.sampleRate * 1,
);
});
SynthDef(\synth, {
arg out, phase_out, level_out, buf_l, buf_r,
gate=0, pos=0, speed=1, jitter=0,
size=0.1, density=20, pitch=1, pan=0, spread=0, gain=1, envscale=1,
freeze=0, t_reset_pos=0;
var grain_trig;
var jitter_sig;
var buf_dur;
var pan_sig;
var buf_pos;
var pos_sig;
var sig_l;
var sig_r;
var sig_mix;
var env;
var level;
grain_trig = Impulse.kr(density);
buf_dur = BufDur.kr(buf_l);
pan_sig = TRand.kr(trig: grain_trig,
lo: spread.neg,
hi: spread);
jitter_sig = TRand.kr(trig: grain_trig,
lo: buf_dur.reciprocal.neg * jitter,
hi: buf_dur.reciprocal * jitter);
buf_pos = Phasor.kr(trig: t_reset_pos,
rate: buf_dur.reciprocal / ControlRate.ir * speed,
resetPos: pos);
pos_sig = Wrap.kr(Select.kr(freeze, [buf_pos, pos]));
sig_l = GrainBuf.ar(1, grain_trig, size, buf_l, pitch, pos_sig + jitter_sig, 2);
sig_r = GrainBuf.ar(1, grain_trig, size, buf_r, pitch, pos_sig + jitter_sig, 2);
sig_mix = Balance2.ar(sig_l, sig_r, pan + pan_sig);
env = EnvGen.kr(Env.asr(1, 1, 1), gate: gate, timeScale: envscale);
level = env;
Out.ar(out, sig_mix * level * gain);
Out.kr(phase_out, pos_sig);
// ignore gain for level out
Out.kr(level_out, level);
}).add;
SynthDef(\effect, {
arg in, out, mix=0.5, room=0.5, damp=0.5;
var sig = In.ar(in, 2);
sig = FreeVerb.ar(sig, mix, room, damp);
Out.ar(out, sig);
}).add;
context.server.sync;
// mix bus for all synth outputs
mixBus = Bus.audio(context.server, 2);
effect = Synth.new(\effect, [\in, mixBus.index, \out, context.out_b.index], target: context.xg);
phases = Array.fill(nvoices, { arg i; Bus.control(context.server); });
levels = Array.fill(nvoices, { arg i; Bus.control(context.server); });
pg = ParGroup.head(context.xg);
voices = Array.fill(nvoices, { arg i;
Synth.new(\synth, [
\out, mixBus.index,
\phase_out, phases[i].index,
\level_out, levels[i].index,
\buf_l, buffersL[i],
\buf_r, buffersR[i],
], target: pg);
});
context.server.sync;
this.addCommand("reverb_mix", "f", { arg msg; effect.set(\mix, msg[1]); });
this.addCommand("reverb_room", "f", { arg msg; effect.set(\room, msg[1]); });
this.addCommand("reverb_damp", "f", { arg msg; effect.set(\damp, msg[1]); });
this.addCommand("read", "is", { arg msg;
this.readBuf(msg[1] - 1, msg[2]);
});
this.addCommand("seek", "if", { arg msg;
var voice = msg[1] - 1;
var lvl, pos;
var seek_rate = 1 / 750;
seek_tasks[voice].stop;
// TODO: async get
lvl = levels[voice].getSynchronous();
if (false, { // disable seeking until fully implemented
var step;
var target_pos;
// TODO: async get
pos = phases[voice].getSynchronous();
voices[voice].set(\freeze, 1);
target_pos = msg[2];
step = (target_pos - pos) * seek_rate;
seek_tasks[voice] = Routine {
while({ abs(target_pos - pos) > abs(step) }, {
pos = pos + step;
voices[voice].set(\pos, pos);
seek_rate.wait;
});
voices[voice].set(\pos, target_pos);
voices[voice].set(\freeze, 0);
voices[voice].set(\t_reset_pos, 1);
};
seek_tasks[voice].play();
}, {
pos = msg[2];
voices[voice].set(\pos, pos);
voices[voice].set(\t_reset_pos, 1);
voices[voice].set(\freeze, 0);
});
});
this.addCommand("gate", "ii", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\gate, msg[2]);
});
this.addCommand("speed", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\speed, msg[2]);
});
this.addCommand("jitter", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\jitter, msg[2]);
});
this.addCommand("size", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\size, msg[2]);
});
this.addCommand("density", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\density, msg[2]);
});
this.addCommand("pitch", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\pitch, msg[2]);
});
this.addCommand("pan", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\pan, msg[2]);
});
this.addCommand("spread", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\spread, msg[2]);
});
this.addCommand("volume", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\gain, msg[2]);
});
this.addCommand("envscale", "if", { arg msg;
var voice = msg[1] - 1;
voices[voice].set(\envscale, msg[2]);
});
nvoices.do({ arg i;
this.addPoll(("phase_" ++ (i+1)).asSymbol, {
var val = phases[i].getSynchronous;
val
});
this.addPoll(("level_" ++ (i+1)).asSymbol, {
var val = levels[i].getSynchronous;
val
});
});
seek_tasks = Array.fill(nvoices, { arg i;
Routine {}
});
}
free {
voices.do({ arg voice; voice.free; });
phases.do({ arg bus; bus.free; });
levels.do({ arg bus; bus.free; });
buffersL.do({ arg b; b.free; });
buffersR.do({ arg b; b.free; });
effect.free;
mixBus.free;
}
}