forked from marierm/mmExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrimmer.sc
237 lines (216 loc) · 5.15 KB
/
Trimmer.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
// Copyright 2012 Martin Marier
// The trim feature is used to trim other features. In works like the trim
// automation mode available in most DAW. Its output is its input and its
// target mixed together. We have to specify how much the input will affect
// the target. Therefore, the range of the input (inMin and inMax) and the
// amount applied to the target (amount) must be specified.
TrimFeature : Feature {
var <target, <inMin, <inMax, <amount, <synth, def;
// Unlike the other features, input is a Feature, not an Array of
// Features.
*new { |name, interface, input, target, inMin=0, inMax=1023, amount=512.0|
^super.newCopyArgs(name, interface, [input]).init(
target, inMin, inMax, amount
);
}
// saveDictionary {
// var dict;
// dict = super.saveDictionary.interpret;
// dict.put(\target, target.name);
// dict.put(\inMin, inMin);
// dict.put(\inMax, inMax);
// dict.put(\amount, amount);
// ^dict.asCompileString;
// }
saveDictionary {
^IdentityDictionary.newFrom([
\class, this.class,
\name, name.asSymbol,
\interface, interface.class,
\input, input.collect(_.name),
\target, target.name,
\inMin, inMin,
\inMax, inMax,
\amount, amount
]);
}
init { |tgt, min, max, amnt|
super.init;
input[0].dependantFeatures.add(this);
server.serverRunning.not.if {
("Synth features need a server to work properly.").warn;
("Feature '" ++ name ++ "' will not be activated.").warn;
^nil;
};
target = tgt;
inMin = min;
inMax = max;
amount = amnt;
def = SynthDef(
\trimmer,
// in0 is the bus number of the feature that will trim another
// one. target is is the bus number of the feature that will be
// trimmed by in0. out is the bus number of the result. We have
// to specify how much the input will affect the target.
// Therefore, the range of the input (inMin and inMax) and the
// amount applied to the target (amount) must be specified.
{ |out=100, in0=0, target=1, inMin=0, inMax=1023, amount=512.0|
var in, tgt;
in = In.kr(in0, 1);
tgt = In.kr(target, 1);
// scale in between -1.0 and 1.0
in = ( (2 * (in - inMin)) * (inMax - inMin).reciprocal) - 1;
Out.kr( out, Mix([tgt, in * amount]) );
}
);
fork {
bus = Bus.control(server);
def.add;
server.sync;
synth = Synth.tail(
server,
def.name,
[
\out, bus.index,
\in0, input[0].bus.index,
\target, target.bus.index,
\inMin, inMin,
\inMax, inMax,
\amount, amount
]
);
server.sync;
};
fullFunc = {
bus.get{|value|
action.value(value);
// netAddr.sendMsg(oscPath, value);
};
};
interface.action_(interface.action.addFunc(fullFunc));
interface.features.add(this);
interface.featureNames.add(name);
interface.changed(\featureActivated);
}
remove {
super.remove;
// remove myself from the
// dependantFeatures list of others.
input[0].dependantFeatures.remove(this);
}
inMin_ { |val|
inMin = val;
synth.set(\inMin, val);
this.changed(\inMin);
}
inMax_ { |val|
inMax = val;
synth.set(\inMax, val);
this.changed(\inMax);
}
amount_ { |val|
amount = val;
synth.set(\amount, val);
this.changed(\amount);
}
guiClass { ^TrimFeatureGui }
}
TrimFeatureGui : FeatureGui {
var inMinBox, inMaxBox, amountBox;
init {
super.init;
actions.put( \inMin,
{|model, what|
inMinBox.value_(model.inMin);
}
);
actions.put( \inMax,
{|model, what|
inMaxBox.value_(model.inMax);
}
);
actions.put( \amount,
{|model, what|
amountBox.value_(model.amount);
}
);
}
gui {
super.gui;
w.layout.add(
QHLayout(
[
StaticText(w).string_(
"Minimum Incoming Value: "
).align_(\right).minWidth_(250).maxSize_(300@22), s:1
],
[
inMinBox = NumberBox(w, 85@22).value_(
model.inMin
).action_({ |nb|
model.inMin_(nb.value);
}).maxHeight_(22), s:1
]
)
);
w.layout.add(
QHLayout(
[
StaticText(w).string_(
"Maximum Incoming Value: "
).align_(\right).minWidth_(250).maxSize_(300@22), s:1
],
[
inMaxBox = NumberBox(w, 85@22).value_(
model.inMax
).action_({ |nb|
model.inMax_(nb.value);
}).maxHeight_(22), s:1
]
)
);
w.layout.add(
QHLayout(
[
StaticText(w).string_(
"Amount: "
).align_(\right).minWidth_(250).maxSize_(300@22), s:1
],
[
amountBox = NumberBox(w, 85@22).value_(
model.amount
).action_({ |nb|
model.amount_(nb.value);
}).maxHeight_(22), s:1
]
)
);
}
}
+ Feature {
trim { |input, inMin=0, inMax=1023, amount=512.0|
// Add a number and increment it if a Feature with the same name
// already exists.
var newName, i;
newName = (name ++ "Trimmed").asSymbol;
i = 1;
{interface.featureNames.includes( newName)}.while({
newName = (name ++ "Trimmed" ++ i).asSymbol;
i = i + 1;
});
^Feature.trim(
name: newName,
interface: interface,
input: input,
target: this,
inMin: inMin,
inMax: inMax,
amount: amount
);
}
*trim { |name, interface, input, target, inMin=0, inMax=1023, amount=1.0|
^TrimFeature.new(
name, interface, input, target, inMin, inMax, amount
)
}
}