forked from supercollider-quarks/ddwMixerChannel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MixerSend.sc
366 lines (297 loc) · 10 KB
/
MixerSend.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
357
358
359
360
361
362
363
364
365
366
MixerSend {
// container for aux sends for MixerChannels & boards
// input must be a mixerchannel; output can be an mc, bus or bus#
var <inMixer, <outbus, <levelControl, <level,
<sendSynth, /*<levelSynth,*/ <guiUpdater = nil, targ,
<>guiIndex; // change this from outside at your peril!
var <>guiUpdateTime = 0.25; // see .levelLine / .levelTo
// creation & init methods in MixerPre/PostSend
// setting level
level_ { arg lev, updateGUI = true, stopAutomation = true;
stopAutomation.if({ this.stopLevelAuto; });
level = lev;
sendSynth.set(\level, level);
// gui update
(updateGUI && inMixer.mcgui.notNil && guiIndex.notNil).if({
inMixer.mcgui.updateView(this.guiKey, level);
});
this.changed(\sendLevel, lev);
^this
}
// automation
levelAuto {
// place a .kr synthdef onto level control bus
arg synthdef, args;
var levelSynth;
// if levelSynth is active, kill it
this.stopLevelAuto;
// create levelControl if it doesn't exist
levelControl.isNil.if({
levelControl = GenericGlobalControl("send level", nil, level, \amp);
});
levelSynth = levelControl.automate(synthdef, args, inMixer.fadergroup, \addToHead);
sendSynth.map(\level, levelControl.bus.index);
^levelSynth
}
levelSynth { ^levelControl.tryPerform(\autoSynth) }
// line automation -- ...To uses current value as start
// any warp that can be used in a ControlSpec can be used here
levelLine {
arg start = 0, end = 1, dur = 1, warp = \amp;
var startTime, timeSpec;
this.stopLevelAuto; // begin by clearing automation
// start the automation process on the server
// this line will be revived when bus.get is implemented
// schedule periodic updates for level and gui
// spec to convert time into level
timeSpec = ControlSpec.new(start, end, warp);
guiUpdater = Routine({
startTime = thisThread.clock.beats;
{ (thisThread.clock.beats - startTime) <= dur }.while({
this.level_(timeSpec.map(
(thisThread.clock.beats - startTime) / dur
), true, false); // update gui but don't stop automation
guiUpdateTime.yield // wait before going again
});
nil.yield // stop
}).play(SystemClock);
}
levelTo { // start from current level
arg end, dur, warp = \amp;
this.levelLine(level, end, dur, warp);
}
stopLevelAuto {
this.levelSynth.notNil.if({
levelControl.stopAuto;
// sendSynth.map(\level, -1); // return to static level
// this.levelSynth.free; // kill kr synth
// levelSynth = nil;
});
guiUpdater.notNil.if({
guiUpdater.stop;
guiUpdater = nil;
});
^this
}
outbus_ { arg bus, updateGUI = true, target, moveAction;
var bundle;
if(outbus.notNil) {
bundle = List.new;
this.outbusSetToBundle(bus, updateGUI, target, moveAction, bundle);
MixerChannelReconstructor.queueBundle(inMixer.server, bundle);
};
}
outbusSetToBundle { // repatch send to another bus
arg bus, updateGUI = true, target, moveAction, bundle;
var oldmc, mc, incr = 0;
targ = targ ? target;
bus.isMixerChannel.if({
bus = bus.inbus;
});
bus.isNumber.if({
bus = Bus.new(\audio, bus, inMixer.inChannels, inMixer.server);
});
((mc = bus.asMixer).notNil).if({
mc.descendentsInclude(inMixer).if({
MethodError("MixerSend % -> % will produce an infinite loop."
.format(inMixer, mc), this).throw;
}, {
// if the dest mixer is routed to another mixer,
// place the dest mixer before its destination
incr = incr + mc.numMixersInChain;
mc.prAddAntecedent(inMixer);
inMixer.prAddDescendent(mc);
});
});
// bookkeeping for old outbus if it's a mixer
(oldmc = outbus.asMixer).notNil.if({
incr = incr + oldmc.numMixersInChain.neg;
oldmc.prRemoveAntecedent(inMixer);
inMixer.prRemoveDescendent(oldmc);
});
// if target of send is not created in the bundle yet, create it
// before adjusting order of exec and setting outbus
(mc.tryPerform(\bundled) == 0).if({
bundle.add(mc.makeServerObjectsToBundle(bundle));
});
// adjust myself and dependents if needed
(incr != 0).if({
inMixer.updateMixersInChain(incr);
// can't think of a better way than brute force
// to get all the channels in the right order
// it would be manageable if I could guarantee that the client always knows the
// server's node order, but the order can be changed in so many places
// thus, I just force it.
MixerChannel.fixNodeOrderToBundle(inMixer.server, bundle);
});
bundle.add(["/n_set", sendSynth.nodeID, \busout, bus.index]);
outbus = bus;
if(updateGUI and: { inMixer.mcgui.notNil }) { inMixer.mcgui.refresh };
}
makeServerObjects { |bus| // details are handled by subclass - objectsToBundle
var bundle;
bundle = List.new;
this.makeServerObjectsToBundle(bus, bundle);
MixerChannelReconstructor.queueBundle(inMixer.server, bundle);
}
postSettings { arg indent = 0;
var destMixer;
indent.reptChar.post; " Send synth: ".post; sendSynth.postln;
indent.reptChar.post; "Destination: ".post;
destMixer = outbus.asMixer; // am I routing to a mixer?
destMixer.notNil.if({
destMixer.postln; // show as mixer
}, {
(BusDict.audioNames.at(inMixer.server).at(outbus.index) ++ " -> " ++ outbus).postln; // show as bus
});
indent.reptChar.post;
(" Level: " ++ level.round(0.001) ++ " ("
++ level.ampdb.round(0.001) ++ " dB)").postln;
}
}
MixerPreSend : MixerSend {
*new { arg in, out, level = 1;
out.notNil.if({
^super.new.initPreSend(in, out, level);
}, {
format("No destination for presend for %", in).warn;
^nil
});
}
initPreSend { arg in1, out1, lev = 1, target;
var testChannels;
inMixer = in1; // must be mixerchannel in
level = lev ? 1;
targ = target;
// Buses should be left alone, other types are fixed by following rules:
case
{ out1.isKindOf(Bus) } {
testChannels = out1.numChannels;
}
{ out1.isMixerChannel } {
testChannels = out1.inChannels;
out1 = out1.inbus;
}
{ out1.isNumber } {
out1 = Bus.new(\audio, out1, inMixer.inChannels, inMixer.server);
testChannels = out1.numChannels;
};
// check numchannels
(inMixer.inChannels != testChannels).if({
("Warning: numChannel mismatch creating send for " ++ inMixer.name
++ " -- " ++ inMixer.inChannels ++ " --> " ++ testChannels).postln;
});
level = lev;
this.makeServerObjects(out1);
guiIndex = inMixer.preSends.size;
inMixer.preSends = inMixer.preSends.add(this);
// fix gui display
inMixer.mcgui.notNil.if({ inMixer.mcgui.refresh });
^this
}
makeServerObjectsToBundle { arg bus, bundle;
// reroute mixerchannel output to transfer bus
// this is the send itself, not merely a transfer
// takes from sendbus, sends to the send destination
sendSynth = Synth.basicNew("mixers/Send" ++ inMixer.inChannels, inMixer.server,
inMixer.server.nodeAllocator.allocPerm);
bundle.add(sendSynth.addBeforeMsg(inMixer.synth,
[\busin, inMixer.inbus.index, \busout, (bus ? outbus).index,
\level, level]
));
bus.notNil.if({ this.outbusSetToBundle(bus, false, targ, bundle:bundle); });
}
free {
arg updateGUI = true;
var mc, bundle;
this.stopLevelAuto; // cancel automation
// update node order variables
(mc = outbus.asMixer).notNil.if({
bundle = List.new;
inMixer.updateMixersInChain(mc.numMixersInChain.neg);
mc.prRemoveAntecedent(inMixer); // requiring both method calls is bad form
inMixer.prRemoveDescendent(mc);
});
inMixer.preSends.remove(this);
sendSynth.free; // stop sending
sendSynth.server.nodeAllocator.freePerm(sendSynth.nodeID);
levelControl.free;
// fix gui display
(inMixer.mcgui.notNil && updateGUI).if({ inMixer.mcgui.board.refresh });
this.changed(\didFree);
}
guiKey { ^("presend" ++ guiIndex).asSymbol }
}
MixerPostSend : MixerSend {
*new { arg in, out, level = 1;
out.notNil.if({
^super.new.initPostSend(in, out, level);
}, {
format("No destination for postsend for %", in).warn;
^nil
});
}
initPostSend { arg in1, out1, lev = 1, target;
var testChannels;
inMixer = in1; // must be mixerchannel in
level = lev ? 1;
// Buses should be left alone, other types are fixed by following rules:
case
{ out1.isKindOf(Bus) } {
testChannels = out1.numChannels;
}
{ out1.isMixerChannel } {
testChannels = out1.inChannels;
out1 = out1.inbus;
}
{ out1.isNumber } {
out1 = Bus.new(\audio, out1, inMixer.outChannels, inMixer.server);
testChannels = out1.numChannels;
};
// check numchannels
// outChannels here b/c this is postfader, using output channels
(inMixer.outChannels != testChannels).if({
("Warning: numChannel mismatch creating send for " ++ inMixer.name
++ " -- " ++ inMixer.outChannels ++ " --> " ++ testChannels).postln;
});
this.makeServerObjects(out1);
guiIndex = inMixer.postSends.size;
inMixer.postSends = inMixer.postSends.add(this);
// fix gui display
inMixer.mcgui.notNil.if({ inMixer.mcgui.refresh });
^this
}
makeServerObjectsToBundle { arg bus, bundle;
// reroute mixerchannel output to transfer bus
// this is the send itself, not merely a transfer
// takes from sendbus, sends to the send destination
sendSynth = Synth.basicNew("mixers/Send" ++ inMixer.outChannels, inMixer.server,
inMixer.server.nodeAllocator.allocPerm);
bundle.add(sendSynth.addAfterMsg(inMixer.synth,
[\busin, inMixer.inbus.index,
\busout, (bus ? outbus).index,
\level, level]
));
bus.notNil.if({ this.outbusSetToBundle(bus, false, targ, bundle:bundle); });
}
free {
arg updateGUI = true;
var mc, bundle;
this.stopLevelAuto; // cancel automation
// update node order variables
(mc = outbus.asMixer).notNil.if({
bundle = List.new;
inMixer.updateMixersInChain(mc.numMixersInChain.neg);
mc.prRemoveAntecedent(inMixer); // requiring both method calls is bad form
inMixer.prRemoveDescendent(mc);
});
inMixer.postSends.remove(this); // pull from channel
sendSynth.free; // stop sending
sendSynth.server.nodeAllocator.freePerm(sendSynth.nodeID);
levelControl.free;
// fix gui display
(inMixer.mcgui.notNil && updateGUI).if({ inMixer.mcgui.board.refresh });
this.changed(\didFree);
}
guiKey { ^("postsend" ++ guiIndex).asSymbol }
}