-
Notifications
You must be signed in to change notification settings - Fork 7
/
midi.pde
355 lines (286 loc) · 10.8 KB
/
midi.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
public static class KorgNanoKontrol2 extends LXMidiRemote {
private static final boolean DEBUG = true;
private int channelIndex = -1;
private LX lx;
public static final int SLIDER_1 = 0;
public static final int SLIDER_2 = 1;
public static final int SLIDER_3 = 2;
public static final int SLIDER_4 = 3;
public static final int SLIDER_5 = 4;
public static final int SLIDER_6 = 5;
public static final int SLIDER_7 = 6;
public static final int SLIDER_8 = 7;
public static final int[] SLIDERS = { SLIDER_1, SLIDER_2, SLIDER_3, SLIDER_4, SLIDER_5, SLIDER_6, SLIDER_7, SLIDER_8 };
public static final int KNOB_1 = 16;
public static final int KNOB_2 = 17;
public static final int KNOB_3 = 18;
public static final int KNOB_4 = 19;
public static final int KNOB_5 = 20;
public static final int KNOB_6 = 21;
public static final int KNOB_7 = 22;
public static final int KNOB_8 = 23;
public static final int[] KNOBS = { KNOB_1, KNOB_2, KNOB_3, KNOB_4, KNOB_5, KNOB_6, KNOB_7, KNOB_8 };
public static final int S_BUTTON_1 = 32;
public static final int S_BUTTON_2 = 33;
public static final int S_BUTTON_3 = 34;
public static final int S_BUTTON_4 = 35;
public static final int S_BUTTON_5 = 36;
public static final int S_BUTTON_6 = 37;
public static final int S_BUTTON_7 = 38;
public static final int S_BUTTON_8 = 39;
public static final int[] S_BUTTONS = { S_BUTTON_1, S_BUTTON_2, S_BUTTON_3, S_BUTTON_4, S_BUTTON_5, S_BUTTON_6, S_BUTTON_7, S_BUTTON_8 };
public static final int M_BUTTON_1 = 48;
public static final int M_BUTTON_2 = 49;
public static final int M_BUTTON_3 = 50;
public static final int M_BUTTON_4 = 51;
public static final int M_BUTTON_5 = 52;
public static final int M_BUTTON_6 = 53;
public static final int M_BUTTON_7 = 54;
public static final int M_BUTTON_8 = 55;
public static final int[] M_BUTTONS = { M_BUTTON_1, M_BUTTON_2, M_BUTTON_3, M_BUTTON_4, M_BUTTON_5, M_BUTTON_6, M_BUTTON_7, M_BUTTON_8 };
public static final int R_BUTTON_1 = 64;
public static final int R_BUTTON_2 = 65;
public static final int R_BUTTON_3 = 66;
public static final int R_BUTTON_4 = 67;
public static final int R_BUTTON_5 = 68;
public static final int R_BUTTON_6 = 69;
public static final int R_BUTTON_7 = 70;
public static final int R_BUTTON_8 = 71;
public static final int[] R_BUTTONS = { R_BUTTON_1, R_BUTTON_2, R_BUTTON_3, R_BUTTON_4, R_BUTTON_5, R_BUTTON_6, R_BUTTON_7, R_BUTTON_8 };
public static final String[] DEVICE_NAMES = { "CoreMIDI4J - SLIDER/KNOB", "nanoKONTROL2", "NANOKONTROL2", "SLIDER/KNOB" };
public static final int TRACK_LEFT = 58;
public static final int TRACK_RIGHT = 59;
public static final int CYCLE = 46;
public static final int MARKER_SET = 60;
public static final int MARKER_LEFT = 61;
public static final int MARKER_RIGHT = 62;
public static final int REWIND = 43;
public static final int PLAY = 41;
public static final int FORWARD = 44;
public static final int LOOP = 49;
public static final int STOP = 42;
public static final int RECORD = 45;
public static boolean hasName(String name) {
for(String deviceName: DEVICE_NAMES) {
if (name.contains(deviceName)) {
return true;
}
}
return false;
}
public KorgNanoKontrol2(LXMidiInput input, LX lx) {
super(input);
this.lx = lx;
println("Input created! " + input.getName());
}
public KorgNanoKontrol2 bindSlider(LXParameter parameter, int slider) {
bindController(parameter, 0, SLIDERS[slider]);
return this;
}
public KorgNanoKontrol2 unbindSliders() {
for (int cc: SLIDERS) {
unbindController(0, cc);
}
return this;
}
private int getChannelIndex() {
if (this.channelIndex < 0) {
this.channelIndex = 0;
}
return this.channelIndex;
}
public KorgNanoKontrol2 unbindPatternSliders() {
for (int i = 0; i < 4; i++) {
unbindController(0, SLIDERS[i]);
}
return this;
}
public KorgNanoKontrol2 unbindEffectSliders() {
for (int i = 4; i < SLIDERS.length; i++) {
unbindController(0, SLIDERS[i]);
}
return this;
}
public KorgNanoKontrol2 bindSButton(LXParameter parameter, int sButton) {
bindController(parameter, 0, S_BUTTONS[sButton]);
return this;
}
public KorgNanoKontrol2 unbindSButtons() {
for (int cc: S_BUTTONS) {
unbindController(0, cc);
}
return this;
}
public KorgNanoKontrol2 bindMButton(LXParameter parameter, int mButton) {
bindController(parameter, 0, M_BUTTONS[mButton]);
return this;
}
public KorgNanoKontrol2 unbindMButtons() {
for (int cc: M_BUTTONS) {
unbindController(0, cc);
}
return this;
}
public KorgNanoKontrol2 bindRButton(LXParameter parameter, int rButton) {
bindController(parameter, 0, R_BUTTONS[rButton]);
return this;
}
public KorgNanoKontrol2 unbindRButtons() {
for (int cc: R_BUTTONS) {
unbindController(0, cc);
}
return this;
}
public KorgNanoKontrol2 bindKnob(LXParameter parameter, int knob) {
bindController(parameter, 0, KNOBS[knob]);
return this;
}
public KorgNanoKontrol2 unbindKnobs() {
for (int cc: KNOBS) {
unbindController(0, cc);
}
return this;
}
public KorgNanoKontrol2 bindKnobsAndSlidersToPattern(LXPattern pattern) {
List<LXParameter> patternParameters = new ArrayList<LXParameter>(pattern.getParameters());
if (DEBUG) {
println("Attempting to bind knobs and sliders to pattern: " + pattern.getLabel());
println("The pattern has this many parameters: " + patternParameters.size());
println("Is my channel enabled? : " + this.getInput().enabled.getValueb());
}
if(patternParameters.size() == 0) { return this; }
int maxKnobs = KorgNanoKontrol2.KNOBS.length;
int maxSliders = KorgNanoKontrol2.SLIDERS.length;
int knobIndex = 0;
int sliderIndex = 0;
for(LXParameter parameter: patternParameters) {
if (!(parameter instanceof LXListenableNormalizedParameter)) {
if (DEBUG) {
println("Skipping parameter, not instance of LXListenableNormalizedParameter: " + parameter.getLabel());
}
continue;
}
if (lx.engine.audio.enabled.getValueb() && (parameter instanceof LEDomeAudioParameter)) {
if (DEBUG) {
println("Skipping parameter, audio enabled and is LEDomeAudioParameter: " + parameter.getLabel());
}
continue;
}
if (knobIndex < maxKnobs) {
if (DEBUG) {
println("Binding knob " + knobIndex + " to " + parameter.getLabel());
}
this.bindKnob(parameter, knobIndex);
knobIndex++;
} else if (sliderIndex < maxSliders) {
if (DEBUG) {
println("Binding slider " + sliderIndex + " to " + parameter.getLabel());
}
this.bindSlider(parameter, sliderIndex);
sliderIndex++;
} else {
// We have bound all we can bind
break;
}
}
return this;
}
public KorgNanoKontrol2 unbindPatternKnobsAndSliders() {
this.unbindKnobs();
this.unbindPatternSliders();
return this;
}
}
/**
* The LEDomeMidiListener connects the midi controller to the interface.
*/
public class KorgNanoKontrol2MidiListener implements LXMidiListener, LXChannel.Listener {
private boolean DEBUG = true;
private LX lx;
private KorgNanoKontrol2 nanoKontrol2;
public KorgNanoKontrol2MidiListener(LX lx, KorgNanoKontrol2 nanoKontrol2) {
this.lx = lx;
this.nanoKontrol2 = nanoKontrol2;
}
public void aftertouchReceived(MidiAftertouch aftertouch) {
if (DEBUG) {
println("aftertouchReceived aftertouch: " + aftertouch.getAftertouch());
}
}
public void controlChangeReceived(MidiControlChange cc) {
// Wire up specific buttons.
// Bind does not work for toggling as the value toggles both ways on each key press.
switch(cc.getCC()) {
case KorgNanoKontrol2.MARKER_SET:
if (cc.getValue() > 0) { lx.engine.audio.enabled.toggle(); }
break;
case KorgNanoKontrol2.MARKER_LEFT:
case KorgNanoKontrol2.TRACK_LEFT:
case KorgNanoKontrol2.REWIND:
if (cc.getValue() > 0) { lx.goPrev(); }
break;
case KorgNanoKontrol2.MARKER_RIGHT:
case KorgNanoKontrol2.TRACK_RIGHT:
case KorgNanoKontrol2.FORWARD:
if (cc.getValue() > 0) { lx.goNext(); }
break;
case KorgNanoKontrol2.CYCLE:
if (cc.getValue() > 0) { lx.engine.getDefaultChannel().autoCycleEnabled.toggle(); }
break;
case KorgNanoKontrol2.R_BUTTON_8:
if (cc.getValue() > 0) {
for(LXPattern pattern: lx.getPatterns()) {
if (pattern instanceof LEDomePattern) {
((LEDomePattern)pattern).reset();
} else {
for(LXParameter param: pattern.getParameters()) {
if (!(param instanceof LXListenableNormalizedParameter)) { continue; }
param.reset();
}
}
}
}
break;
}
if (DEBUG) {
println("controlChangeReceived cc: " + cc.getCC());
println("controlChangeReceived value: " + cc.getValue());
}
}
public void noteOffReceived(MidiNote note) {
if (DEBUG) {
println("noteOffReceived pitch: " + note.getPitch());
println("noteOffReceived velocity: " + note.getVelocity());
}
}
public void noteOnReceived(MidiNoteOn note) {
if (DEBUG) {
println("noteOnReceived pitch: " + note.getPitch());
println("noteOnReceived velocity: " + note.getVelocity());
}
}
public void pitchBendReceived(MidiPitchBend pitchBend) {
if (DEBUG) {
println("noteOnReceived pitch: " + pitchBend.getPitchBend());
}
}
public void programChangeReceived(MidiProgramChange pc) {
if (DEBUG) {
println("LXMidiProgramChange pitch: " + pc.getProgram());
}
}
public void effectAdded(LXBus channel, LXEffect effect) {}
public void effectRemoved(LXBus channel, LXEffect effect) {}
public void effectMoved(LXBus channel, LXEffect effect) {}
public void patternAdded(LXChannel channel, LXPattern pattern) {}
public void patternDidChange(LXChannel channel, LXPattern pattern) {
if (nanoKontrol2 == null) { return; }
println("The focused Channel index is: " + ((LXChannel)lx.engine.getFocusedChannel()).getIndex());
nanoKontrol2.unbindPatternKnobsAndSliders();
nanoKontrol2.bindKnobsAndSlidersToPattern(pattern);
}
public void patternRemoved(LXChannel channel, LXPattern pattern) {}
public void patternMoved(LXChannel channel, LXPattern pattern) {}
public void patternWillChange(LXChannel channel, LXPattern pattern, LXPattern nextPattern) {}
public void indexChanged(LXChannel channel) {}
}