forked from l8on/dome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEDomeAudio.pde
416 lines (340 loc) · 14.4 KB
/
LEDomeAudio.pde
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
* This base class implements the basics for connecting a parameter
* to one of the audio modulators automatically.
*/
public class LEDomeAudioParameter extends CompoundParameter {
/**
* This normalized value will be used as the range for the modulation.
*/
private double modulationRange = LEDomeAudioParameterManager.MODULATOR_RANGE_DEFAULT;
/**
* This enumerated value will be used to set the polarity of the modulation.
*/
private LXParameter.Polarity modulationPolarity = LXParameter.Polarity.UNIPOLAR;
public LEDomeAudioParameter(String label) {
super(label, 0);
}
public LEDomeAudioParameter(String label, double value) {
super(label, value, 1);
}
public LEDomeAudioParameter(String label, double value, double max) {
super(label, value, 0, max);
}
public LEDomeAudioParameter(String label, double value, double v0, double v1) {
super(label, value, v0, v1);
}
/**
* Set the modulation range to be used for this parameter when connected
* to one of the audio modulators. Will ensure it is a normalized value
* between 0 and 1.
*/
public LEDomeAudioParameter setModulationRange(double modulationRange) {
this.modulationRange = LXUtils.constrain(modulationRange, 0, 1);
return this;
}
public double getModulationRange() {
return this.modulationRange;
}
public float getModulationRangef() {
return (float)this.modulationRange;
}
/**
* Set the modulation polarity to be used for this parameter when connected
* to one of audio modulators.
* Possible valueas are:
* - LXParameter.Polarity.UNIPOLAR - The audio modulation will always be additive in the modulation range.
* Example: if the range is .2, the base param value is .5, and the audio level is .5, the modulated value will be (.5 + (.2 * .5)) = .6
* - LXParameter.Polarity.BIPOLAR - The audio modulation will move in both directions, so a 0 value on the modulation will result in a negative modulation.
* Example: if the range is .2, the base param value is .5, and the audio level is .25, the modulated value will be (.5 + (.2 * (.25 * 2 - 1))) = .45
*/
public LEDomeAudioParameter setModulationPolarity(LXParameter.Polarity polarity) {
this.modulationPolarity = polarity;
return this;
}
public LXParameter.Polarity getModulationPolarity() {
return this.modulationPolarity;
}
}
/**
* Use this class to connect a parameter to the average audio level
* of all frequencies.
*/
public class LEDomeAudioParameterFull extends LEDomeAudioParameter {
public LEDomeAudioParameterFull(String label) {
super(label, 0);
}
public LEDomeAudioParameterFull(String label, double value) {
super(label, value, 1);
}
public LEDomeAudioParameterFull(String label, double value, double max) {
super(label, value, 0, max);
}
public LEDomeAudioParameterFull(String label, double value, double v0, double v1) {
super(label, value, v0, v1);
}
}
/**
* Use this class to connect a parameter to the average audio level
* of the low (bass) frequencies of from 0 to 250 Hz
*/
public class LEDomeAudioParameterLow extends LEDomeAudioParameter {
public LEDomeAudioParameterLow(String label) {
super(label, 0);
}
public LEDomeAudioParameterLow(String label, double value) {
super(label, value, 1);
}
public LEDomeAudioParameterLow(String label, double value, double max) {
super(label, value, 0, max);
}
public LEDomeAudioParameterLow(String label, double value, double v0, double v1) {
super(label, value, v0, v1);
}
}
/**
* Use this class to connect a parameter to the average audio level
* of the middle frequencies of from 250 to 2000 Hz
*/
public class LEDomeAudioParameterMid extends LEDomeAudioParameter {
public LEDomeAudioParameterMid(String label) {
super(label, 0);
}
public LEDomeAudioParameterMid(String label, double value) {
super(label, value, 1);
}
public LEDomeAudioParameterMid(String label, double value, double max) {
super(label, value, 0, max);
}
public LEDomeAudioParameterMid(String label, double value, double v0, double v1) {
super(label, value, v0, v1);
}
}
/**
* Use this class to connect a parameter to the average audio level
* of the high frequencies of from 2 kHz to 22.5 KHz (or the maximum frequency of the input).
*/
public class LEDomeAudioParameterHigh extends LEDomeAudioParameter {
public LEDomeAudioParameterHigh(String label) {
super(label, 0);
}
public LEDomeAudioParameterHigh(String label, double value) {
super(label, value, 1);
}
public LEDomeAudioParameterHigh(String label, double value, double max) {
super(label, value, 0, max);
}
public LEDomeAudioParameterHigh(String label, double value, double v0, double v1) {
super(label, value, v0, v1);
}
}
/**
* Use this to get a beat gate that has been configured to be very sensitive to
* the bass beat of the audio input.
*/
public class LEDomeAudioBeatGate extends BandGate {
final float DEFAULT_GAIN = 7;
final float DEFAULT_THRESHOLD = .5;
final float DEFAULT_FLOOR = .88;
public LEDomeAudioBeatGate(LX lx) {
this("Beat", lx);
}
public LEDomeAudioBeatGate(String label, LX lx) {
this(label, lx.engine.audio.meter);
}
public LEDomeAudioBeatGate(String label, GraphicMeter meter) {
super(label, meter);
this.gain.setValue(DEFAULT_GAIN);
this.threshold.setValue(DEFAULT_THRESHOLD);
this.floor.setValue(DEFAULT_FLOOR);
}
public LEDomeAudioBeatGate(GraphicMeter meter, float minHz, float maxHz) {
this("Beat", meter);
setFrequencyRange(minHz, maxHz);
}
public LEDomeAudioBeatGate(String label, GraphicMeter meter, int minHz, int maxHz) {
this(label, meter);
setFrequencyRange(minHz, maxHz);
}
}
/**
* Use this to get a beat gate that has been configured to be very sensitive to
* the bass beat of the audio input.
*/
public class LEDomeAudioClapGate extends BandGate {
final float DEFAULT_GAIN = 7;
final float DEFAULT_THRESHOLD = .5;
final float DEFAULT_FLOOR = .88;
final float CLAP_MIN_FREQ = 2200;
final float CLAP_MAX_FREQ = 2800;
public LEDomeAudioClapGate(LX lx) {
this("Clap", lx);
}
public LEDomeAudioClapGate(String label, LX lx) {
this(label, lx.engine.audio.meter);
}
public LEDomeAudioClapGate(String label, GraphicMeter meter) {
super(label, meter);
this.gain.setValue(DEFAULT_GAIN);
this.threshold.setValue(DEFAULT_THRESHOLD);
this.floor.setValue(DEFAULT_FLOOR);
this.maxFreq.setValue(CLAP_MAX_FREQ);
this.minFreq.setValue(CLAP_MIN_FREQ);
}
public LEDomeAudioClapGate(GraphicMeter meter, float minHz, float maxHz) {
this("Clap", meter);
setFrequencyRange(minHz, maxHz);
}
public LEDomeAudioClapGate(String label, GraphicMeter meter, int minHz, int maxHz) {
this(label, meter);
setFrequencyRange(minHz, maxHz);
}
}
public static class LEDomeAudioParameterManager implements LXParameterListener, LXChannel.Listener {
private LX lx;
private BooleanParameter audioInputEnabled;
private List<LXCompoundModulation> currentModulations = new ArrayList<LXCompoundModulation>();;
private LXPattern currentConnectedPattern = null;
private BandGate audioModulatorFull;
private BandGate audioModulatorLow;
private BandGate audioModulatorMid;
private BandGate audioModulatorHigh;
public static final double MODULATOR_RANGE_DEFAULT = .3;
public static final double GAIN_DEFAULT = 6;
public static final double MAX_FREQ_LOW = 200;
public static final double MIN_FREQ_MID = 216;
public static final double MAX_FREQ_MID = 2200;
public static final double MIN_FREQ_HIGH = 2200;
public LEDomeAudioParameterManager(LX lx, BooleanParameter audioInputEnabled) {
this.lx = lx;
// Listen to changes to the audio input parameter to in order to
// attach/detach the audio modulators
this.audioInputEnabled = audioInputEnabled;
this.audioInputEnabled.addListener(this);
// Listen to changes to the current pattern in order to attach the
// right modulators to the new pattern, and remove modulators from the old.
LXChannel channel = (LXChannel)lx.engine.getFocusedChannel();
channel.addListener(this);
// Create the modulators that are connected to the audio input.
this.createAudioModulators();
}
public void onParameterChanged(LXParameter parameter) {
// TODO connect audio params when stuff is enabled
if (parameter != this.audioInputEnabled) { return; }
if (this.audioInputEnabled.getValueb()) {
this.connectAudioModulatorsToCurrentPattern();
} else {
this.removeAudioModulatorsFromCurrentPattern();
}
}
private void createAudioModulators() {
this.createAverageAudioModulator();
this.createLowAudioModulator();
this.createMidAudioModulator();
this.createHighAudioModulator();
}
private void createAverageAudioModulator() {
this.audioModulatorFull = new BandGate("Full", this.lx);
this.lx.engine.modulation.addModulator(this.audioModulatorFull);
this.audioModulatorFull.threshold.setValue(1);
this.audioModulatorFull.floor.setValue(0);
this.audioModulatorFull.gain.setValue(GAIN_DEFAULT);
this.audioModulatorFull.maxFreq.setValue(this.audioModulatorFull.maxFreq.range.max);
this.audioModulatorFull.minFreq.setValue(0);
this.audioModulatorFull.start();
}
private void createLowAudioModulator() {
this.audioModulatorLow = new BandGate("Low", this.lx);
this.lx.engine.modulation.addModulator(this.audioModulatorLow);
this.audioModulatorLow.threshold.setValue(1);
this.audioModulatorLow.floor.setValue(0);
this.audioModulatorLow.gain.setValue(GAIN_DEFAULT);
this.audioModulatorLow.maxFreq.setValue(MAX_FREQ_LOW);
this.audioModulatorLow.minFreq.setValue(0);
this.audioModulatorLow.start();
}
private void createMidAudioModulator() {
this.audioModulatorMid = new BandGate("Mid", this.lx);
this.lx.engine.modulation.addModulator(this.audioModulatorMid);
this.audioModulatorMid.threshold.setValue(1);
this.audioModulatorMid.floor.setValue(0);
this.audioModulatorMid.gain.setValue(GAIN_DEFAULT);
this.audioModulatorMid.maxFreq.setValue(MAX_FREQ_MID);
this.audioModulatorMid.minFreq.setValue(MIN_FREQ_MID);
this.audioModulatorMid.start();
}
private void createHighAudioModulator() {
this.audioModulatorHigh = new BandGate("High", this.lx);
this.lx.engine.modulation.addModulator(this.audioModulatorHigh);
this.audioModulatorHigh.threshold.setValue(1);
this.audioModulatorHigh.floor.setValue(0);
this.audioModulatorHigh.gain.setValue(GAIN_DEFAULT);
this.audioModulatorHigh.maxFreq.setValue(this.audioModulatorFull.maxFreq.range.max);
this.audioModulatorHigh.minFreq.setValue(MIN_FREQ_HIGH);
this.audioModulatorHigh.start();
}
private void connectAudioModulatorsToCurrentPattern() {
this.connectAudioModulatorsToPattern(((LXChannel)this.lx.engine.getFocusedChannel()).getActivePattern());
}
private void connectAudioModulatorsToPattern(LXPattern pattern) {
if (!this.shouldConnectToPattern(pattern)) { return; }
for(LXParameter patternParam: pattern.getParameters()) {
if (!(patternParam instanceof LEDomeAudioParameter)) { continue; }
LXNormalizedParameter modulationSource = this.getAudioModulationSource(patternParam);
if (modulationSource == null) {
println("Subclass of LEDomeAudioParameter should be used instead of the base class in " + pattern.getLabel());
continue;
}
LXCompoundModulation compoundModulation = new LXCompoundModulation(modulationSource, (CompoundParameter)patternParam);
this.lx.engine.modulation.addModulation(compoundModulation);
this.currentModulations.add(compoundModulation);
compoundModulation.range.setValue(((LEDomeAudioParameter)patternParam).getModulationRange());
compoundModulation.polarity.setValue(((LEDomeAudioParameter)patternParam).getModulationPolarity());
}
this.currentConnectedPattern = pattern;
}
private boolean shouldConnectToPattern(LXPattern pattern) {
if (!this.audioInputEnabled.getValueb()) { return false; }
if (currentConnectedPattern == pattern) { return false; }
return true;
}
private LXNormalizedParameter getAudioModulationSource(LXParameter patternParam) {
LXNormalizedParameter modulationSource = null;
if (patternParam instanceof LEDomeAudioParameterFull) {
modulationSource = this.audioModulatorFull.average;
} else if (patternParam instanceof LEDomeAudioParameterLow) {
modulationSource = this.audioModulatorLow.average;
} else if (patternParam instanceof LEDomeAudioParameterMid) {
modulationSource = this.audioModulatorMid.average;
} else if (patternParam instanceof LEDomeAudioParameterHigh) {
modulationSource = this.audioModulatorHigh.average;
}
return modulationSource;
}
private void removeAudioModulatorsFromCurrentPattern() {
this.removeAudioModulatorsFromPattern(((LXChannel)this.lx.engine.getFocusedChannel()).getActivePattern());
}
private void removeAudioModulatorsFromPattern(LXPattern pattern) {
println("Removing audio modulations from " + pattern.getLabel());
for(LXCompoundModulation compoundModulation: this.currentModulations) {
this.lx.engine.modulation.removeModulation(compoundModulation);
}
this.currentModulations.clear();
this.currentConnectedPattern = null;
}
public void patternWillChange(LXChannel channel, LXPattern pattern, LXPattern nextPattern) {
this.removeAudioModulatorsFromPattern(pattern);
this.connectAudioModulatorsToPattern(nextPattern);
}
public void patternDidChange(LXChannel channel, LXPattern pattern) {
if (this.currentConnectedPattern == null) {
this.connectAudioModulatorsToPattern(pattern);
}
}
public void indexChanged(LXChannel channel) { return; }
public void patternAdded(LXChannel channel, LXPattern pattern) { return; }
public void patternRemoved(LXChannel channel, LXPattern pattern) { return; }
public void patternMoved(LXChannel channel, LXPattern pattern) { return; }
public void effectAdded(LXBus channel, LXEffect effect) { return; }
public void effectRemoved(LXBus channel, LXEffect effect) { return; }
public void effectMoved(LXBus channel, LXEffect effect) { return; }
}