-
Notifications
You must be signed in to change notification settings - Fork 4
/
VoicerProcess.sc
357 lines (296 loc) · 8.91 KB
/
VoicerProcess.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
// DEPRECATED. Chucklib is more powerful.
VoicerProcess {
// a gui-able process that will be played by a voicer
// the func should reference the voicer (or other object) by variable defined outside
// unfortunately, there is no way to pass the voicer in as an argument
// while it plays in real time
// on creation, "task" may be an already-existing task or a function
// if a func, it will be wrapped in a Task
classvar <>defaultAction;
var <voicer, // the voicer that will play this process
<>action, // this func gets executed on doAction
<>tag,
<>midiNote, // midi note assigned by VPTrigSocket--already converted to string
<foreground,
<background,
<parent,
<>task; // may be shared with other VoicerProcesses
// (so that one can start and the other can pause, for instance)
*initClass {
// typically this is what you want, but you can override at creation
// you shouldn't use this for "stop" processes
defaultAction = { arg p, g; g.stopOthers(p); p.play(doReset:true) };
}
*new { arg voicer, tag, action, task, foreground, background, parent;
^super.new.init(voicer, tag, action, task, foreground, background, parent)
}
init { arg v, t, a, tk, fg, bg, p;
var tk2;
voicer = v ? NullVoicer.new;
action = a ? defaultAction;
tag = t;
parent = p;
foreground = fg ? Color.black;
background = bg ? Color.grey;
tk2 = tk.isFunction.if({ tk.value(voicer) }, { tk });
tk2.isFunction.if({
task = QuantTask.new(tk2, voicer.clock);
}, {
task = tk2;
});
}
free {
this.stop;
voicer = action = tag = parent = foreground = background = task = midiNote
= nil;
}
doAction { arg group;
action.value(this, group)
}
play { arg quant, clock, doReset = false; // if nil, task fills in the voicer's clock
var q, result;
//("VoicerProcess-play : " ++ tag).postln;
clock = clock ?? { task.clock };
q = this.getQuant(quant, voicer.latency);
//(["VoicerProcess-play", clock.elapsedBeats, q.nextTimeOnGrid(clock)]).postln;
// QuantTasks have a different argument order than PauseStreams,
// so use keyword addressing
result = task.play(quant: q, argClock: clock, doReset: doReset ? false); // nil if play failed
this.playUpdateGui(result);
}
playUpdateGui { arg playResult;
parent.view.notClosed.if({
{ parent.view.background_( // yellow if no play, green if it went ok
playResult.isNil.if({ Color.new255(255, 248, 60) }, { Color.green })
); }.defer;
});
}
stop { arg quant;
//("VoicerProcess-stop : " ++ tag).postln;
task.notNil.if({
task.clock.schedAbs(this.getQuant(quant, 0.2+(voicer.latency ? 0))
.asTimeSpec.nextTimeOnGrid(task.clock), { task.stop; nil });
});
this.stopUpdateGui;
}
stopUpdateGui {
parent.view.notClosed.if({
{ parent.view.background_(Color.clear); }.defer;
});
}
pause { arg q; this.stop(q) }
resume { arg quant;
task.notNil.if({
task.clock.schedAbs(this.getQuant(quant, 0.2+(voicer.latency ? 0))
.asTimeSpec.nextTimeOnGrid(task.clock), { task.resume; nil });
});
parent.view.notClosed.if({
{ parent.view.background_(Color.green); }.defer;
});
}
reset { arg quant;
task.notNil.if({
task.clock.schedAbs(this.getQuant(quant, 0.2+(voicer.latency ? 0))
.asTimeSpec.nextTimeOnGrid(task.clock), { task.reset; nil });
});
}
isPlaying {
task.notNil.if({
parent.view.notClosed.if({
{ parent.view.background_(task.isPlaying.if
({ Color.red }, { Color.clear })); }.defer;
});
^task.isPlaying
}, {
^false
});
}
clock {
task.notNil.if({ ^task.clock }, { ^nil });
}
state { // returns an array that can be used in an SCButton states array
^[this.displayTag, foreground, background]
}
displayTag {
^midiNote.notNil.if({ midiNote ++ ": " }) ++ tag
}
getQuant { arg quant, adjustment;
voicer.editor.notNil.if({ // if a gui, schedule according to quantize setting
quant = (quant ?? { voicer.editor.quant.string.interpret })
.asTimeSpec.applyLatency(adjustment)
}, {
quant = quant ? QuantOffsetLatencyTimeSpec(1, 0, adjustment)
});
//["getQuant", tag, quant.asTimeSpec.nextTimeOnGrid(this.clock)].postln;
//quant.asTimeSpec.dump;
^quant
}
}
////////////////////////////////////////////////////
VoicerProcessGroup {
// a group of voicer processes
// states is not exactly like SCButton.states
// here you give
// [["tag", { action }, { task } or nil, fgcolor, bgcolor] ... ]
// the action func gets passed this VoicerProcess and VoicerProcessGroup as an arg
// the task func gets passed the voicer as an arg and it should return a function:
// { arg v; { loop({ play stuff on v }) } }
// if task is nil, it uses the same task as the previous VoicerProcess
// to start a process on one item and stop it on the next, use:
// [["Stopped", { arg proc, group; proc.play }, { arg v; { /* the player routine */ } }],
// ["Playing", { arg proc, group; proc.stop }, nil]]
var <voicer,
<processes, // array of VoicerProcess
<view,
<value = 0; // current process to hit on .doAction
*new { arg voicer, states;
^super.new.init(voicer, states)
}
init { arg v, states;
voicer = v;
processes = Array.new;
states.do({ arg st, i; this.add(st, i == 0) });
value = processes.size * 10; // allow room to go backward and forward w/midi control
voicer.editor.notNil.if({
this.makeGUI;
voicer.editor.sizeWindow(0, 1);
});
}
states { // returns states as applicable to SCButton
^processes.collect({ arg p; p.state });
}
items {
^processes.collect({ arg p; p.displayTag })
}
value_ { arg v, updateGUI = true;
value = v;
(updateGUI && { view.notClosed }).if({
{ view.value_(v) }.defer;
});
}
stopAll {
processes.do({ arg p; p.stop });
}
stopOthers { arg p;
// only do this if the clock is before the time for scheduling of current task
// if you trigger a change too late, the previously running thing shouldn't die
var quant;
quant = p.getQuant;
(voicer.clock.elapsedBeats < quant.nextTimeOnGrid(p.task.clock ? TempoClock.default)).if({
processes.do({ arg pp;
(pp.task != p.task).if({
pp.stop
}, {
});
});
});
}
doAction { arg p;
//["VoicerProcessGroup-doAction", p, value].postln;
processes.wrapAt(p ? value).doAction(this);
}
makeGUI {
(view.isNil and: { voicer.editor.notNil and: { voicer.editor.processView.notNil } }).if({
view = GUI.popUpMenu.new(voicer.editor.processView, Rect(0, 0, 120, 20))
.items_(this.items)
.action_({ arg v;
this.value_(v.value, false); // set value but don't update gui
processes.at(v.value).doAction(this)
});
});
}
removeGUI {
(view.notNil && voicer.editor.notNil).if({
view.remove;
view = nil;
voicer.editor.refresh;
});
}
updateView {
view.notNil.if({
{ view.items_(this.items);
view.refresh; }.defer;
});
}
add { arg st, first = false;
{ st.size < 5 }.while({
st = st.add(nil);
});
(st.at(2).isNil && first.not).if({
st.put(2, processes.last.task);
});
processes = processes.add(VoicerProcess.performList(\new, [voicer] ++ st ++ [this]
// st.at(0), // tag
// st.at(1), // action
// st.at(2), // task
// st.at(3), // fg
// st.at(4), // bg
// processes.size // index
));
this.updateView;
}
removeAt { arg i;
var p;
p = processes.removeAt(i);
p.isPlaying.if({ p.stop });
(value >= processes.size).if({ this.value_((processes.size - 1).max(0)) });
this.updateView;
// view.doAction;
^p
}
free {
processes.do({ arg p; p.stop }); // stop all processes
processes = Array.new;
(view.notNil && voicer.editor.notNil).if({
view.remove;
voicer.editor.refresh;
});
view = nil;
}
at { |i| ^processes[i] } // for easier syntax to get to VoicerProcesses
active { ^voicer.active }
clearMIDINotes { // upon clearing VPTrigSocket pointing to me, I have to drop midi assg's
processes.do({ arg p; p.midiNote = nil });
this.updateView;
}
}
ProcessToggleGroup : VoicerProcessGroup {
// a simpler version: takes only one task IN VoicerProcessGroup SYNTAX
// and cycles thru button states
// states here is [["tag", { arg p; p.play or other }, task, foreground, background] ... ]
// tasks after the first array will be ignored
init { arg v, states;
voicer = v;
processes = Array.new;
states.do({ arg st, i;
this.add(st, i == 0)
});
voicer.editor.notNil.if({
this.makeGUI;
voicer.editor.sizeWindow(0, 1);
});
}
makeGUI {
(view.isNil and: { voicer.editor.notNil and: { voicer.editor.processView.notNil } }).if({
view = GUI.button.new(voicer.editor.processView, Rect(0, 0, 120, 20))
.states_(this.states)
.action_({ arg v;
processes.at(v.value).doAction
});
});
}
add { arg st, first;
{ st.size < 3 }.while({
st = st.add(nil);
});
// all processes in a toggle group must share the same task
first.not.if({ st.put(2, nil); });
super.add(st, first);
}
updateView {
view.notNil.if({
view.states_(this.states);
view.refresh;
});
}
}