forked from supercollider-quarks/ddwMixerChannel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MixerChannelDef.sc
221 lines (191 loc) · 6.71 KB
/
MixerChannelDef.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
// this class stores parameters needed to define a mixer channel
// mixers should be customizable for any number of channels, any panning methodology
// define all that in this class; definitions are reused in multiple mixers
// h. james harkins -- jamshark70@dewdrop-world.net
MixerChannelDef {
// the setters are there for the sake of inheritance
// to make a similar mcdef, .copy an existing one,
// then replace the instance variables you need to change
// outChannels, basicFader and postSendReadyFader automatically cause synthdefs
// to be resent so that running servers are in sync with the definition
var <>name, <>inChannels, <outChannels, <fader,
<>controls, <>guidef;
*initClass {
Class.initClassTree(Collection);
Library.global.put(\mixerdefs, IdentityDictionary.new);
StartUp.add {
MixerChannelDef(\mix1x1, 1, 1,
fader: SynthDef("mixers/Mxb1x1", {
arg busin, busout, level, clip = 2;
var in, bad, badEG;
in = In.ar(busin, 1) * level;
// On server quit, `clip` may be set to 0 before processing is finished
// In that case, spurious out-of-range warnings are produced
clip = max(1, clip);
bad = CheckBadValues.ar(in, post: 0) + (8 * (in.abs > clip));
SendReply.ar(bad, '/mixerChBadValue', [bad, in].flat, 0);
badEG = EnvGen.ar(Env(#[1, 0, 1], #[0, 0.05], releaseNode: 1), bad);
in = Select.ar(bad > 0, [in * badEG, DC.ar(0)]);
// so that mixerchan bus can be used as postsendbus
ReplaceOut.ar(busin, in);
Out.ar(busout, in);
}, [nil, nil, 0.08]),
controls: (
level: (spec: \amp, value: 0.75),
clip: (spec: [0.1, 100, \exp], value: 20.dbamp)
)
);
MixerChannelDef(\mix1x2, 1, 2,
fader: SynthDef("mixers/Mxb1x2", {
arg busin, busout, level, pan, clip = 2;
var in, out, bad, badEG;
in = In.ar(busin, 1) * level;
clip = max(1, clip);
bad = CheckBadValues.ar(in, post: 0) + (8 * (in.abs > clip));
SendReply.ar(bad, '/mixerChBadValue', [bad, in].flat, 0);
badEG = EnvGen.ar(Env(#[1, 0, 1], #[0, 0.05], releaseNode: 1), bad);
in = Select.ar(bad > 0, [in * badEG, DC.ar(0)]);
out = Pan2.ar(in, pan);
// so that mixerchan bus can be used as postsendbus
ReplaceOut.ar(busin, out);
Out.ar(busout, out);
}, [nil, nil, 0.08, 0.08]),
controls: (level: (spec: \amp, value: 0.75),
pan: \bipolar,
clip: (spec: [0.1, 100, \exp], value: 20.dbamp)
)
);
MixerChannelDef(\mix2x2, 2, 2,
fader: SynthDef("mixers/Mxb2x2", {
arg busin, busout, level, pan, clip = 2;
var in, l, r, out, bad, badEG, silent = DC.ar(0);
in = In.ar(busin, 2) * level;
clip = max(1, clip);
bad = (CheckBadValues.ar(in, post: 0) + (8 * (in.abs > clip)));
SendReply.ar(bad, '/mixerChBadValue', [bad, in, clip].flat, 0);
bad = bad.sum;
badEG = EnvGen.ar(Env(#[1, 0, 1], #[0, 0.05], releaseNode: 1), bad);
bad = bad > 0;
#l, r = in.collect { |chan| Select.ar(bad, [chan * badEG, silent]) };
out = Balance2.ar(l, r, pan);
ReplaceOut.ar(busin, out);
Out.ar(busout, out);
}, [nil, nil, 0.08, 0.08]),
controls: (level: (spec: \amp, value: 0.75),
pan: \bipolar,
clip: (spec: [0.1, 100, \exp], value: 20.dbamp)
)
);
};
}
*new { |name, inChannels, outChannels, /*basicFader, */fader, controls, guidef|
inChannels.isNil.if({
^this.at(name)
}, {
^super.newCopyArgs(name, inChannels, outChannels, fader,
controls, guidef).init
});
}
// validate inputs and store in Library
init {
name = name.asSymbol;
fader = fader.asSynthDef;
controls.respondsTo(\keys).not.if({
MethodError("Error constructing MixerChannelDef - controls must be a Dictionary",
this).throw
});
Library.global.put(\mixerdefs, name, this);
this.sendDefsToRunningServers;
}
*at { |name|
^Library.global[\mixerdefs, name]
}
*all { ^Library.global[\mixerdefs] }
// instance methods
synthdef { |postSendReady = false|
^fader
}
defName { |postSendReady = false|
^fader.name
}
makeControlArray { |mixer|
var out = IdentityDictionary.new, name = mixer.name, gc, gcname;
controls.keysValuesDo({ |key, gcspec|
gcname = "% %".format(name, key);
case
// (spec: ControlSpec, value: initial value)
{ gcspec.respondsTo(\keysValuesDo) } {
gc = gcspec[\spec].asSpec;
gc = MixerControl(gcname, BusDict.control(mixer.server, 1, gcname),
gcspec[\value] ?? { gc.default }, gc);
}
{ gcspec.isNumber } {
gc = MixerControl(gcname, BusDict.control(mixer.server, 1, gcname),
gcspec, \unipolar);
}
// default: use asSpec
{
gcspec = gcspec.asSpec;
gc = MixerControl(gcname, BusDict.control(mixer.server, 1, gcname),
gcspec.default, gcspec);
};
out.put(key, gc);
});
^out
}
outChannels_ { |outCh|
(outCh.notNil and: { outCh != outChannels }).if({
outChannels = outCh;
this.sendDefsToRunningServers; // update send, xfer and recorder synth
});
}
fader_ { |synthdef|
synthdef.notNil.if({
fader = synthdef;
this.sendDefsToRunningServers;
});
}
sendDefsToRunningServers {
// 3.9 has a breaking change: can't use serverRunning during server boot process
var method = if(Main.versionAtLeast(3, 9)) { \hasBooted } { \serverRunning };
Server.named.do({ |server|
if(server.perform(method)) {
this.sendSynthDefs(server);
};
});
}
// send the mixer, xfer, send and record synths for this MCDef
sendSynthDefs { |server|
var method = if(Main.versionAtLeast(3, 9)) { \hasBooted } { \serverRunning };
server = server ?? Server.default;
if(server.perform(method)) {
this.synthdef.send(server);
// this doesn't cover all cases
// if you have defs only for stereo outs, there would be no send def for mono in
// but why would you route a stereo signal into a mono mixer?
// (send is not meant to reduce the number of channels)
// so, you can break it only by doing something stupid to begin with
SynthDef("mixers/Send" ++ outChannels, {
arg busin, busout, level;
var in = In.ar(busin, outChannels) * level, bad, badEG, silent = DC.ar(0);
bad = (CheckBadValues.ar(in, post: 0) + (in.abs > 2)).asArray.sum > 0;
// SendReply.ar(bad, '/mixerChBadValue', bad, 1);
badEG = EnvGen.ar(Env(#[1, 0, 1], #[0, 0.05], releaseNode: 1), bad);
bad = bad > 0;
in = in.asArray.collect { |chan| Select.ar(bad, [chan * badEG, silent]) };
Out.ar(busout, in);
}, [nil, nil, 0.08]).send(server);
SynthDef("mixers/Rec" ++ outChannels, { arg i_in, i_bufNum = 0;
DiskOut.ar(i_bufNum, In.ar(i_in, outChannels));
}).send(server);
} {
"MixerChannelDef tried to send SynthDefs to server '%' but it isn't running"
.format(server.name).warn;
};
}
*sendSynthDefs { |server|
this.all.do({ |def|
def.sendSynthDefs(server);
});
}
}