Commit | Line | Data |
---|---|---|
d6ac1ee8 MS |
1 | /** |
2 | * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND | |
3 | * | |
4 | * //\\ //\\ //\\ //\\ | |
5 | * ///\\\ ///\\\ ///\\\ ///\\\ | |
6 | * \\\/// \\\/// \\\/// \\\/// | |
7 | * \\// \\// \\// \\// | |
8 | * | |
9 | * EXPERTS ONLY!! EXPERTS ONLY!! | |
10 | * | |
11 | * This file defines the MIDI mapping interfaces. This shouldn't | |
12 | * need editing unless you're adding top level support for a | |
13 | * specific MIDI device of some sort. Generally, all MIDI devices | |
14 | * will just work with the default configuration, and you can | |
15 | * set your SCPattern class to respond to the controllers that you | |
16 | * care about. | |
17 | */ | |
18 | ||
19 | interface MidiEngineListener { | |
20 | public void onFocusedDeck(int deckIndex); | |
21 | } | |
22 | ||
23 | class MidiEngine { | |
24 | ||
242b19ad | 25 | public final GridController grid; |
d6ac1ee8 MS |
26 | private final List<MidiEngineListener> listeners = new ArrayList<MidiEngineListener>(); |
27 | private final List<SCMidiInput> midiControllers = new ArrayList<SCMidiInput>(); | |
28 | ||
29 | public MidiEngine addListener(MidiEngineListener l) { | |
30 | listeners.add(l); | |
31 | return this; | |
32 | } | |
33 | ||
34 | public MidiEngine removeListener(MidiEngineListener l) { | |
35 | listeners.remove(l); | |
36 | return this; | |
37 | } | |
38 | ||
39 | private SCMidiInput midiQwertyKeys; | |
40 | private SCMidiInput midiQwertyAPC; | |
41 | ||
42 | private int activeDeckIndex = 0; | |
43 | ||
44 | public MidiEngine() { | |
242b19ad | 45 | grid = new GridController(this); |
0c7af01b MS |
46 | midiControllers.add(midiQwertyKeys = new VirtualKeyMidiInput(this, VirtualKeyMidiInput.KEYS)); |
47 | midiControllers.add(midiQwertyAPC = new VirtualKeyMidiInput(this, VirtualKeyMidiInput.APC)); | |
1b65b8ce | 48 | int apcCount = 0; |
d6ac1ee8 MS |
49 | for (MidiInputDevice device : RWMidi.getInputDevices()) { |
50 | if (device.getName().contains("APC")) { | |
1b65b8ce MS |
51 | ++apcCount; |
52 | } | |
53 | } | |
54 | ||
55 | int apcIndex = 0; | |
56 | for (MidiInputDevice device : RWMidi.getInputDevices()) { | |
57 | if (device.getName().contains("APC")) { | |
58 | int apcDeck = -1; | |
59 | if (apcCount > 1 && apcIndex < 2) { | |
60 | apcDeck = apcIndex++; | |
61 | } | |
62 | midiControllers.add(new APC40MidiInput(this, device, apcDeck).setEnabled(true)); | |
d6ac1ee8 | 63 | } else if (device.getName().contains("SLIDER/KNOB KORG")) { |
242b19ad | 64 | midiControllers.add(new KorgNanoKontrolMidiInput(this, device).setEnabled(true)); |
d93c968f MS |
65 | } else if (device.getName().contains("Arturia MINILAB")) { |
66 | midiControllers.add(new ArturiaMinilabMidiInput(this, device).setEnabled(true)); | |
d6ac1ee8 | 67 | } else { |
d93c968f MS |
68 | boolean enabled = |
69 | device.getName().contains("KEYBOARD KORG") || | |
70 | device.getName().contains("Bus 1 Apple"); | |
0c7af01b | 71 | midiControllers.add(new GenericDeviceMidiInput(this, device).setEnabled(enabled)); |
d6ac1ee8 MS |
72 | } |
73 | } | |
1b65b8ce MS |
74 | |
75 | apcIndex = 0; | |
05965d3d MS |
76 | for (MidiOutputDevice device : RWMidi.getOutputDevices()) { |
77 | if (device.getName().contains("APC")) { | |
1b65b8ce MS |
78 | int apcDeck = -1; |
79 | if (apcCount > 1 && apcIndex < 2) { | |
80 | apcDeck = apcIndex++; | |
81 | } | |
82 | new APC40MidiOutput(this, device, apcDeck); | |
05965d3d MS |
83 | } |
84 | } | |
d6ac1ee8 MS |
85 | } |
86 | ||
87 | public List<SCMidiInput> getControllers() { | |
88 | return this.midiControllers; | |
89 | } | |
90 | ||
42a424d7 | 91 | public LXDeck getFocusedDeck() { |
242b19ad MS |
92 | return lx.engine.getDeck(activeDeckIndex); |
93 | } | |
94 | ||
95 | public SCPattern getFocusedPattern() { | |
96 | return (SCPattern) getFocusedDeck().getActivePattern(); | |
97 | } | |
98 | ||
d6ac1ee8 MS |
99 | public MidiEngine setFocusedDeck(int deckIndex) { |
100 | if (this.activeDeckIndex != deckIndex) { | |
101 | this.activeDeckIndex = deckIndex; | |
102 | for (MidiEngineListener listener : listeners) { | |
103 | listener.onFocusedDeck(deckIndex); | |
104 | } | |
105 | } | |
106 | return this; | |
107 | } | |
108 | ||
d6ac1ee8 MS |
109 | public boolean isQwertyEnabled() { |
110 | return midiQwertyKeys.isEnabled() || midiQwertyAPC.isEnabled(); | |
111 | } | |
112 | } | |
113 | ||
114 | public interface SCMidiInputListener { | |
115 | public void onEnabled(SCMidiInput controller, boolean enabled); | |
116 | } | |
117 | ||
0c7af01b | 118 | public abstract class SCMidiInput extends AbstractScrollItem { |
d6ac1ee8 | 119 | |
0c7af01b | 120 | protected boolean enabled = false; |
d6ac1ee8 | 121 | private final String name; |
d6ac1ee8 | 122 | |
242b19ad MS |
123 | protected final MidiEngine midiEngine; |
124 | ||
d6ac1ee8 MS |
125 | final List<SCMidiInputListener> listeners = new ArrayList<SCMidiInputListener>(); |
126 | ||
0c7af01b MS |
127 | protected SCMidiInput(MidiEngine midiEngine, String name) { |
128 | this.midiEngine = midiEngine; | |
129 | this.name = name; | |
130 | } | |
131 | ||
d6ac1ee8 MS |
132 | public SCMidiInput addListener(SCMidiInputListener l) { |
133 | listeners.add(l); | |
134 | return this; | |
135 | } | |
136 | ||
137 | public SCMidiInput removeListener(SCMidiInputListener l) { | |
138 | listeners.remove(l); | |
139 | return this; | |
140 | } | |
141 | ||
d6ac1ee8 MS |
142 | public String getLabel() { |
143 | return name; | |
144 | } | |
145 | ||
d6ac1ee8 MS |
146 | public boolean isEnabled() { |
147 | return enabled; | |
148 | } | |
149 | ||
150 | public boolean isSelected() { | |
151 | return enabled; | |
152 | } | |
153 | ||
154 | public void onMousePressed() { | |
155 | setEnabled(!enabled); | |
156 | } | |
157 | ||
158 | public SCMidiInput setEnabled(boolean enabled) { | |
159 | if (enabled != this.enabled) { | |
160 | this.enabled = enabled; | |
161 | for (SCMidiInputListener l : listeners) { | |
162 | l.onEnabled(this, enabled); | |
163 | } | |
164 | } | |
165 | return this; | |
166 | } | |
167 | ||
d6ac1ee8 MS |
168 | private boolean logMidi() { |
169 | return (uiMidi != null) && uiMidi.logMidi(); | |
170 | } | |
1b65b8ce MS |
171 | |
172 | protected SCPattern getTargetPattern() { | |
173 | return midiEngine.getFocusedPattern(); | |
174 | } | |
d6ac1ee8 MS |
175 | |
176 | final void programChangeReceived(ProgramChange pc) { | |
177 | if (!enabled) { | |
178 | return; | |
179 | } | |
180 | if (logMidi()) { | |
181 | println(getLabel() + " :: Program Change :: " + pc.getNumber()); | |
182 | } | |
183 | handleProgramChange(pc); | |
184 | } | |
185 | ||
186 | final void controllerChangeReceived(rwmidi.Controller cc) { | |
187 | if (!enabled) { | |
188 | return; | |
189 | } | |
190 | if (logMidi()) { | |
242b19ad | 191 | println(getLabel() + " :: Controller :: " + cc.getChannel() + " :: " + cc.getCC() + ":" + cc.getValue()); |
d6ac1ee8 | 192 | } |
3efafb5c MS |
193 | if (!handleControllerChange(cc)) { |
194 | getTargetPattern().controllerChange(cc); | |
d6ac1ee8 MS |
195 | } |
196 | } | |
197 | ||
198 | final void noteOnReceived(Note note) { | |
199 | if (!enabled) { | |
200 | return; | |
201 | } | |
202 | if (logMidi()) { | |
203 | println(getLabel() + " :: Note On :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity()); | |
204 | } | |
3efafb5c MS |
205 | if (!handleNoteOn(note)) { |
206 | getTargetPattern().noteOn(note); | |
d6ac1ee8 MS |
207 | } |
208 | } | |
209 | ||
210 | final void noteOffReceived(Note note) { | |
211 | if (!enabled) { | |
212 | return; | |
213 | } | |
214 | if (logMidi()) { | |
215 | println(getLabel() + " :: Note Off :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity()); | |
216 | } | |
3efafb5c MS |
217 | if (!handleNoteOff(note)) { |
218 | getTargetPattern().noteOff(note); | |
d6ac1ee8 MS |
219 | } |
220 | } | |
221 | ||
222 | // Subclasses may implement these to map top-level functionality | |
3efafb5c MS |
223 | protected boolean handleProgramChange(ProgramChange pc) { return false; } |
224 | protected boolean handleControllerChange(rwmidi.Controller cc) { return false; } | |
225 | protected boolean handleNoteOn(Note note) { return false; } | |
226 | protected boolean handleNoteOff(Note note) { return false; } | |
d6ac1ee8 MS |
227 | } |
228 | ||
0c7af01b MS |
229 | public class VirtualKeyMidiInput extends SCMidiInput { |
230 | ||
231 | public static final int KEYS = 1; | |
232 | public static final int APC = 2; | |
233 | ||
234 | private final int mode; | |
235 | ||
236 | private int octaveShift = 0; | |
237 | ||
238 | class NoteMeta { | |
239 | int channel; | |
240 | int number; | |
241 | NoteMeta(int channel, int number) { | |
242 | this.channel = channel; | |
243 | this.number = number; | |
244 | } | |
245 | } | |
246 | ||
247 | final Map<Character, NoteMeta> keyToNote = new HashMap<Character, NoteMeta>(); | |
248 | ||
249 | VirtualKeyMidiInput(MidiEngine midiEngine, int mode) { | |
250 | super(midiEngine, "QWERTY (" + (mode == APC ? "APC" : "Key") + " Mode)"); | |
251 | this.mode = mode; | |
252 | if (mode == APC) { | |
253 | mapAPC(); | |
254 | } else { | |
255 | mapKeys(); | |
256 | } | |
257 | registerKeyEvent(this); | |
258 | } | |
259 | ||
260 | private void mapAPC() { | |
261 | mapNote('1', 0, 53); | |
262 | mapNote('2', 1, 53); | |
263 | mapNote('3', 2, 53); | |
264 | mapNote('4', 3, 53); | |
265 | mapNote('5', 4, 53); | |
266 | mapNote('6', 5, 53); | |
267 | mapNote('q', 0, 54); | |
268 | mapNote('w', 1, 54); | |
269 | mapNote('e', 2, 54); | |
270 | mapNote('r', 3, 54); | |
271 | mapNote('t', 4, 54); | |
272 | mapNote('y', 5, 54); | |
273 | mapNote('a', 0, 55); | |
274 | mapNote('s', 1, 55); | |
275 | mapNote('d', 2, 55); | |
276 | mapNote('f', 3, 55); | |
277 | mapNote('g', 4, 55); | |
278 | mapNote('h', 5, 55); | |
279 | mapNote('z', 0, 56); | |
280 | mapNote('x', 1, 56); | |
281 | mapNote('c', 2, 56); | |
282 | mapNote('v', 3, 56); | |
283 | mapNote('b', 4, 56); | |
284 | mapNote('n', 5, 56); | |
285 | } | |
286 | ||
287 | private void mapKeys() { | |
288 | int note = 48; | |
289 | mapNote('a', 1, note++); | |
290 | mapNote('w', 1, note++); | |
291 | mapNote('s', 1, note++); | |
292 | mapNote('e', 1, note++); | |
293 | mapNote('d', 1, note++); | |
294 | mapNote('f', 1, note++); | |
295 | mapNote('t', 1, note++); | |
296 | mapNote('g', 1, note++); | |
297 | mapNote('y', 1, note++); | |
298 | mapNote('h', 1, note++); | |
299 | mapNote('u', 1, note++); | |
300 | mapNote('j', 1, note++); | |
301 | mapNote('k', 1, note++); | |
302 | mapNote('o', 1, note++); | |
303 | mapNote('l', 1, note++); | |
304 | } | |
305 | ||
306 | void mapNote(char ch, int channel, int number) { | |
307 | keyToNote.put(ch, new NoteMeta(channel, number)); | |
308 | } | |
309 | ||
310 | public void keyEvent(KeyEvent e) { | |
311 | if (!enabled) { | |
312 | return; | |
313 | } | |
314 | char c = Character.toLowerCase(e.getKeyChar()); | |
315 | NoteMeta nm = keyToNote.get(c); | |
316 | if (nm != null) { | |
317 | switch (e.getID()) { | |
318 | case KeyEvent.KEY_PRESSED: | |
319 | noteOnReceived(new Note(Note.NOTE_ON, nm.channel, nm.number + octaveShift*12, 127)); | |
320 | break; | |
321 | case KeyEvent.KEY_RELEASED: | |
322 | noteOffReceived(new Note(Note.NOTE_OFF, nm.channel, nm.number + octaveShift*12, 0)); | |
323 | break; | |
324 | } | |
325 | } | |
326 | if ((mode == KEYS) && (e.getID() == KeyEvent.KEY_PRESSED)) { | |
327 | switch (c) { | |
328 | case 'z': | |
329 | octaveShift = constrain(octaveShift-1, -4, 4); | |
330 | break; | |
331 | case 'x': | |
332 | octaveShift = constrain(octaveShift+1, -4, 4); | |
333 | break; | |
334 | } | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | public class GenericDeviceMidiInput extends SCMidiInput { | |
340 | GenericDeviceMidiInput(MidiEngine midiEngine, MidiInputDevice d) { | |
341 | super(midiEngine, d.getName().replace("Unknown vendor","")); | |
342 | d.createInput(this); | |
343 | } | |
344 | } | |
345 | ||
346 | public class APC40MidiInput extends GenericDeviceMidiInput { | |
d6ac1ee8 MS |
347 | |
348 | private boolean shiftOn = false; | |
349 | private LXEffect releaseEffect = null; | |
42a424d7 | 350 | final private LXDeck targetDeck; |
d6ac1ee8 | 351 | |
242b19ad | 352 | APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d) { |
1b65b8ce MS |
353 | this(midiEngine, d, -1); |
354 | } | |
355 | ||
356 | APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d, int deckIndex) { | |
357 | super(midiEngine, d); | |
358 | targetDeck = (deckIndex < 0) ? null : lx.engine.getDecks().get(deckIndex); | |
359 | } | |
360 | ||
42a424d7 | 361 | protected LXDeck getTargetDeck() { |
887295bc | 362 | return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck(); |
1b65b8ce MS |
363 | } |
364 | ||
365 | protected SCPattern getTargetPattern() { | |
366 | if (targetDeck != null) { | |
367 | return (SCPattern) (targetDeck.getActivePattern()); | |
368 | } | |
369 | return super.getTargetPattern(); | |
242b19ad MS |
370 | } |
371 | ||
372 | private class GridPosition { | |
373 | public final int row, col; | |
374 | GridPosition(int r, int c) { | |
375 | row = r; | |
376 | col = c; | |
377 | } | |
378 | } | |
379 | ||
380 | private GridPosition getGridPosition(Note note) { | |
381 | int channel = note.getChannel(); | |
382 | int pitch = note.getPitch(); | |
383 | if (channel < 8) { | |
384 | if (pitch >= 53 && pitch <=57) return new GridPosition(pitch-53, channel); | |
242b19ad MS |
385 | } |
386 | return null; | |
387 | } | |
388 | ||
3efafb5c | 389 | private boolean handleGridNoteOn(Note note) { |
242b19ad MS |
390 | GridPosition p = getGridPosition(note); |
391 | if (p != null) { | |
392 | return midiEngine.grid.gridPressed(p.row, p.col); | |
393 | } | |
394 | return false; | |
395 | } | |
396 | ||
3efafb5c | 397 | private boolean handleGridNoteOff(Note note) { |
242b19ad MS |
398 | GridPosition p = getGridPosition(note); |
399 | if (p != null) { | |
400 | return midiEngine.grid.gridReleased(p.row, p.col); | |
401 | } | |
402 | return false; | |
d6ac1ee8 MS |
403 | } |
404 | ||
3efafb5c | 405 | protected boolean handleControllerChange(rwmidi.Controller cc) { |
e5308763 | 406 | int channel = cc.getChannel(); |
d6ac1ee8 | 407 | int number = cc.getCC(); |
e5308763 | 408 | float value = cc.getValue() / 127.; |
d6ac1ee8 | 409 | switch (number) { |
e5308763 | 410 | |
411 | case 7: | |
412 | switch (channel) { | |
413 | case 0: | |
24fc0330 | 414 | effects.colorFucker.hueShift.setValue(value); |
3efafb5c | 415 | return true; |
e5308763 | 416 | case 1: |
24fc0330 | 417 | effects.colorFucker.desat.setValue(value); |
3efafb5c | 418 | return true; |
e5308763 | 419 | case 2: |
24fc0330 | 420 | effects.colorFucker.sharp.setValue(value); |
3efafb5c | 421 | return true; |
e5308763 | 422 | case 3: |
24fc0330 | 423 | effects.blur.amount.setValue(value); |
3efafb5c | 424 | return true; |
e5308763 | 425 | case 4: |
24fc0330 | 426 | effects.quantize.amount.setValue(value); |
3efafb5c | 427 | return true; |
e5308763 | 428 | } |
429 | break; | |
430 | ||
431 | // Master bright | |
432 | case 14: | |
24fc0330 | 433 | effects.colorFucker.level.setValue(value); |
3efafb5c | 434 | return true; |
e5308763 | 435 | |
d6ac1ee8 MS |
436 | // Crossfader |
437 | case 15: | |
42a424d7 | 438 | lx.engine.getDeck(GLucose.RIGHT_DECK).getFader().setValue(value); |
3efafb5c | 439 | return true; |
d6ac1ee8 MS |
440 | } |
441 | ||
442 | int parameterIndex = -1; | |
443 | if (number >= 48 && number <= 55) { | |
444 | parameterIndex = number - 48; | |
445 | } else if (number >= 16 && number <= 19) { | |
446 | parameterIndex = 8 + (number-16); | |
447 | } | |
448 | if (parameterIndex >= 0) { | |
1b65b8ce | 449 | List<LXParameter> parameters = getTargetPattern().getParameters(); |
d6ac1ee8 | 450 | if (parameterIndex < parameters.size()) { |
e5308763 | 451 | parameters.get(parameterIndex).setValue(value); |
3efafb5c | 452 | return true; |
d6ac1ee8 MS |
453 | } |
454 | } | |
455 | ||
456 | if (number >= 20 && number <= 23) { | |
457 | int effectIndex = number - 20; | |
458 | List<LXParameter> parameters = glucose.getSelectedEffect().getParameters(); | |
459 | if (effectIndex < parameters.size()) { | |
e5308763 | 460 | parameters.get(effectIndex).setValue(value); |
3efafb5c | 461 | return true; |
d6ac1ee8 MS |
462 | } |
463 | } | |
3efafb5c MS |
464 | |
465 | return false; | |
d6ac1ee8 MS |
466 | } |
467 | ||
05965d3d | 468 | private long tap1 = 0; |
3661fcee | 469 | |
05965d3d MS |
470 | private boolean lbtwn(long a, long b, long c) { |
471 | return a >= b && a <= c; | |
472 | } | |
3661fcee | 473 | |
3efafb5c MS |
474 | protected boolean handleNoteOn(Note note) { |
475 | if (handleGridNoteOn(note)) { | |
476 | return true; | |
477 | } | |
478 | ||
e5308763 | 479 | int nPitch = note.getPitch(); |
480 | int nChan = note.getChannel(); | |
3661fcee | 481 | switch (nPitch) { |
e5308763 | 482 | |
483 | case 49: // SOLO/CUE | |
484 | switch (nChan) { | |
485 | case 4: | |
24fc0330 | 486 | effects.colorFucker.mono.setValue(1); |
3efafb5c | 487 | return true; |
e5308763 | 488 | case 5: |
24fc0330 | 489 | effects.colorFucker.invert.setValue(1); |
3efafb5c | 490 | return true; |
e5308763 | 491 | case 6: |
492 | lx.cycleBaseHue(60000); | |
3efafb5c | 493 | return true; |
e5308763 | 494 | } |
495 | break; | |
1f974cbc | 496 | |
05965d3d | 497 | case 82: // scene 1 |
24fc0330 | 498 | effects.boom.trigger(); |
3efafb5c | 499 | return true; |
05965d3d MS |
500 | |
501 | case 83: // scene 2 | |
24fc0330 | 502 | effects.flash.trigger(); |
3efafb5c | 503 | return true; |
05965d3d | 504 | |
ac6b3e48 MS |
505 | case 84: // scene 3 |
506 | getTargetPattern().reset(); | |
507 | return true; | |
508 | ||
05965d3d MS |
509 | case 90: |
510 | // dan's dirty tapping mechanism | |
511 | lx.tempo.trigger(); | |
512 | tap1 = millis(); | |
3efafb5c | 513 | return true; |
af83f61c | 514 | |
515 | case 91: // play | |
88a3dd9e MS |
516 | if (shiftOn) { |
517 | midiEngine.setFocusedDeck(GLucose.LEFT_DECK); | |
518 | } else { | |
519 | uiCrossfader.setDisplayMode("A"); | |
520 | } | |
3efafb5c | 521 | return true; |
88a3dd9e MS |
522 | |
523 | case 92: // stop | |
524 | uiCrossfader.setDisplayMode("COMP"); | |
525 | return true; | |
526 | ||
af83f61c | 527 | case 93: // rec |
88a3dd9e MS |
528 | if (shiftOn) { |
529 | midiEngine.setFocusedDeck(GLucose.RIGHT_DECK); | |
530 | } else { | |
531 | uiCrossfader.setDisplayMode("B"); | |
532 | } | |
3efafb5c | 533 | return true; |
af83f61c | 534 | |
ff23d191 | 535 | case 94: // up bank |
d6ac1ee8 | 536 | if (shiftOn) { |
ff23d191 | 537 | glucose.incrementSelectedEffectBy(-1); |
d6ac1ee8 | 538 | } else { |
1b65b8ce | 539 | getTargetDeck().goPrev(); |
d6ac1ee8 | 540 | } |
3efafb5c MS |
541 | return true; |
542 | ||
ff23d191 | 543 | case 95: // down bank |
d6ac1ee8 | 544 | if (shiftOn) { |
ff23d191 | 545 | glucose.incrementSelectedEffectBy(1); |
d6ac1ee8 | 546 | } else { |
1b65b8ce | 547 | getTargetDeck().goNext(); |
d6ac1ee8 | 548 | } |
3efafb5c | 549 | return true; |
d6ac1ee8 MS |
550 | |
551 | case 98: // shift | |
552 | shiftOn = true; | |
3efafb5c | 553 | return true; |
d6ac1ee8 MS |
554 | |
555 | case 99: // tap tempo | |
556 | lx.tempo.tap(); | |
3efafb5c MS |
557 | return true; |
558 | ||
d6ac1ee8 MS |
559 | case 100: // nudge+ |
560 | lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1)); | |
3efafb5c MS |
561 | return true; |
562 | ||
d6ac1ee8 MS |
563 | case 101: // nudge- |
564 | lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1)); | |
3efafb5c | 565 | return true; |
d6ac1ee8 | 566 | |
ff23d191 | 567 | case 62: // Detail View / red 5 |
d6ac1ee8 MS |
568 | releaseEffect = glucose.getSelectedEffect(); |
569 | if (releaseEffect.isMomentary()) { | |
570 | releaseEffect.enable(); | |
571 | } else { | |
572 | releaseEffect.toggle(); | |
573 | } | |
3efafb5c | 574 | return true; |
d6ac1ee8 | 575 | |
ff23d191 | 576 | case 63: // rec quantize / red 6 |
d6ac1ee8 | 577 | glucose.getSelectedEffect().disable(); |
3efafb5c | 578 | return true; |
d6ac1ee8 | 579 | } |
3efafb5c MS |
580 | |
581 | return false; | |
d6ac1ee8 MS |
582 | } |
583 | ||
3efafb5c MS |
584 | protected boolean handleNoteOff(Note note) { |
585 | if (handleGridNoteOff(note)) { | |
586 | return true; | |
587 | } | |
588 | ||
e5308763 | 589 | int nPitch = note.getPitch(); |
590 | int nChan = note.getChannel(); | |
591 | ||
3661fcee | 592 | switch (nPitch) { |
e5308763 | 593 | |
594 | case 49: // SOLO/CUE | |
595 | switch (nChan) { | |
596 | case 4: | |
24fc0330 | 597 | effects.colorFucker.mono.setValue(0); |
3efafb5c | 598 | return true; |
e5308763 | 599 | case 5: |
24fc0330 | 600 | effects.colorFucker.invert.setValue(0); |
3efafb5c | 601 | return true; |
e5308763 | 602 | case 6: |
603 | lx.setBaseHue(lx.getBaseHue()); | |
3efafb5c | 604 | return true; |
e5308763 | 605 | } |
606 | break; | |
1f974cbc MS |
607 | |
608 | case 52: // CLIP STOP | |
609 | if (nChan < PresetManager.NUM_PRESETS) { | |
610 | if (shiftOn) { | |
f6394aaa | 611 | presetManager.store(getTargetDeck(), nChan); |
1f974cbc | 612 | } else { |
f6394aaa | 613 | presetManager.select(getTargetDeck(), nChan); |
1f974cbc MS |
614 | } |
615 | } | |
3efafb5c | 616 | return true; |
1f974cbc | 617 | |
ff23d191 | 618 | case 90: // SEND C |
05965d3d MS |
619 | long tapDelta = millis() - tap1; |
620 | if (lbtwn(tapDelta,5000,300*1000)) { // hackish tapping mechanism | |
621 | double bpm = 32.*60000./(tapDelta); | |
622 | while (bpm < 20) bpm*=2; | |
623 | while (bpm > 40) bpm/=2; | |
624 | lx.tempo.setBpm(bpm); | |
625 | lx.tempo.trigger(); | |
626 | tap1 = 0; | |
627 | println("Tap Set - " + bpm + " bpm"); | |
628 | } | |
3efafb5c | 629 | return true; |
af83f61c | 630 | |
ff23d191 | 631 | case 63: // rec quantize / RED 6 |
d6ac1ee8 MS |
632 | if (releaseEffect != null) { |
633 | if (releaseEffect.isMomentary()) { | |
634 | releaseEffect.disable(); | |
635 | } | |
636 | } | |
3efafb5c | 637 | return true; |
af83f61c | 638 | |
d6ac1ee8 MS |
639 | case 98: // shift |
640 | shiftOn = false; | |
3efafb5c | 641 | return true; |
d6ac1ee8 | 642 | } |
3efafb5c MS |
643 | |
644 | return false; | |
d6ac1ee8 MS |
645 | } |
646 | } | |
647 | ||
0c7af01b | 648 | class KorgNanoKontrolMidiInput extends GenericDeviceMidiInput { |
d6ac1ee8 | 649 | |
242b19ad MS |
650 | KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) { |
651 | super(midiEngine, d); | |
d6ac1ee8 MS |
652 | } |
653 | ||
3efafb5c | 654 | protected boolean handleControllerChange(rwmidi.Controller cc) { |
d6ac1ee8 MS |
655 | int number = cc.getCC(); |
656 | if (number >= 16 && number <= 23) { | |
657 | int parameterIndex = number - 16; | |
242b19ad | 658 | List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters(); |
d6ac1ee8 MS |
659 | if (parameterIndex < parameters.size()) { |
660 | parameters.get(parameterIndex).setValue(cc.getValue() / 127.); | |
3efafb5c | 661 | return true; |
d6ac1ee8 MS |
662 | } |
663 | } | |
664 | ||
665 | if (cc.getValue() == 127) { | |
666 | switch (number) { | |
3efafb5c MS |
667 | |
668 | case 58: // Left track | |
669 | midiEngine.setFocusedDeck(GLucose.LEFT_DECK); | |
670 | return true; | |
671 | ||
672 | case 59: // Right track | |
673 | midiEngine.setFocusedDeck(GLucose.RIGHT_DECK); | |
674 | return true; | |
675 | ||
676 | case 43: // Left chevron | |
d6ac1ee8 | 677 | midiEngine.getFocusedDeck().goPrev(); |
3efafb5c MS |
678 | return true; |
679 | ||
680 | case 44: // Right chevron | |
d6ac1ee8 | 681 | midiEngine.getFocusedDeck().goNext(); |
3efafb5c | 682 | return true; |
d6ac1ee8 MS |
683 | } |
684 | } | |
3efafb5c MS |
685 | |
686 | return false; | |
d6ac1ee8 MS |
687 | } |
688 | } | |
689 | ||
242b19ad | 690 | class APC40MidiOutput implements LXParameter.Listener, GridOutput { |
05965d3d MS |
691 | |
692 | private final MidiEngine midiEngine; | |
693 | private final MidiOutput output; | |
694 | private LXPattern focusedPattern = null; | |
695 | private LXEffect focusedEffect = null; | |
42a424d7 | 696 | private final LXDeck targetDeck; |
05965d3d MS |
697 | |
698 | APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) { | |
1b65b8ce MS |
699 | this(midiEngine, device, -1); |
700 | } | |
701 | ||
702 | APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device, int deckIndex) { | |
05965d3d MS |
703 | this.midiEngine = midiEngine; |
704 | output = device.createOutput(); | |
1b65b8ce MS |
705 | targetDeck = (deckIndex < 0) ? null : lx.engine.getDecks().get(deckIndex); |
706 | setDPatternOutputs(); | |
707 | if (targetDeck != null) { | |
708 | midiEngine.addListener(new MidiEngineListener() { | |
709 | public void onFocusedDeck(int deckIndex) { | |
710 | resetPatternParameters(); | |
2b068dd5 MS |
711 | for (int i = 0; i < 8; ++i) { |
712 | output.sendNoteOn(i, 52, 0); | |
713 | } | |
1b65b8ce MS |
714 | } |
715 | }); | |
716 | } | |
05965d3d MS |
717 | glucose.addEffectListener(new GLucose.EffectListener() { |
718 | public void effectSelected(LXEffect effect) { | |
719 | resetEffectParameters(); | |
720 | } | |
721 | }); | |
42a424d7 MS |
722 | LXDeck.Listener deckListener = new LXDeck.AbstractListener() { |
723 | public void patternDidChange(LXDeck deck, LXPattern pattern) { | |
2b068dd5 MS |
724 | if (deck == getTargetDeck()) { |
725 | resetPatternParameters(); | |
726 | } | |
05965d3d MS |
727 | } |
728 | }; | |
42a424d7 | 729 | for (LXDeck d : lx.engine.getDecks()) { |
1b65b8ce MS |
730 | if (targetDeck == null || d == targetDeck) { |
731 | d.addListener(deckListener); | |
732 | } | |
05965d3d | 733 | } |
1f974cbc | 734 | presetManager.addListener(new PresetListener() { |
2b068dd5 | 735 | public void onPresetSelected(LXDeck deck, Preset preset) { |
a8d1f57a MS |
736 | if (deck == getTargetDeck()) { |
737 | for (int i = 0; i < 8; ++i) { | |
738 | output.sendNoteOn(i, 52, (preset.index == i) ? 1 : 0); | |
739 | } | |
1f974cbc MS |
740 | } |
741 | } | |
42a424d7 | 742 | public void onPresetDirty(LXDeck deck, Preset preset) { |
a8d1f57a MS |
743 | if (deck == getTargetDeck()) { |
744 | output.sendNoteOn(preset.index, 52, 2); | |
745 | } | |
1f974cbc | 746 | } |
42a424d7 | 747 | public void onPresetStored(LXDeck deck, Preset preset) { |
2910a170 | 748 | onPresetSelected(deck, preset); |
1f974cbc MS |
749 | } |
750 | }); | |
05965d3d | 751 | resetParameters(); |
242b19ad | 752 | midiEngine.grid.addOutput(this); |
e5308763 | 753 | |
754 | lx.cycleBaseHue(60000); | |
755 | output.sendNoteOn(6, 49, 127); | |
677670a6 | 756 | |
1f974cbc | 757 | // Turn off the track selection lights and preset selectors |
677670a6 MS |
758 | for (int i = 0; i < 8; ++i) { |
759 | output.sendNoteOn(i, 51, 0); | |
1f974cbc | 760 | output.sendNoteOn(i, 52, 0); |
677670a6 | 761 | } |
1f974cbc MS |
762 | |
763 | // Turn off the MASTER selector | |
677670a6 | 764 | output.sendNoteOn(0, 80, 0); |
05965d3d | 765 | } |
1b65b8ce MS |
766 | |
767 | private void setDPatternOutputs() { | |
42a424d7 | 768 | for (LXDeck deck : lx.engine.getDecks()) { |
1b65b8ce MS |
769 | if (targetDeck == null || deck == targetDeck) { |
770 | for (LXPattern pattern : deck.getPatterns()) { | |
771 | if (pattern instanceof DPat) { | |
772 | ((DPat)pattern).setAPCOutput(output); | |
773 | } | |
774 | } | |
775 | } | |
776 | } | |
777 | } | |
778 | ||
42a424d7 | 779 | protected LXDeck getTargetDeck() { |
2b068dd5 | 780 | return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck(); |
1b65b8ce | 781 | } |
05965d3d MS |
782 | |
783 | private void resetParameters() { | |
784 | resetPatternParameters(); | |
785 | resetEffectParameters(); | |
786 | } | |
787 | ||
788 | private void resetPatternParameters() { | |
1b65b8ce | 789 | LXPattern newPattern = getTargetDeck().getActivePattern(); |
05965d3d MS |
790 | if (newPattern == focusedPattern) { |
791 | return; | |
792 | } | |
793 | if (focusedPattern != null) { | |
794 | for (LXParameter p : focusedPattern.getParameters()) { | |
795 | ((LXListenableParameter) p).removeListener(this); | |
796 | } | |
797 | } | |
798 | focusedPattern = newPattern; | |
799 | int i = 0; | |
800 | for (LXParameter p : focusedPattern.getParameters()) { | |
801 | ((LXListenableParameter) p).addListener(this); | |
802 | sendKnob(i++, p); | |
803 | } | |
804 | while (i < 12) { | |
805 | sendKnob(i++, 0); | |
806 | } | |
66067e53 MS |
807 | if (focusedPattern instanceof DPat) { |
808 | ((DPat)focusedPattern).updateLights(); | |
809 | } else { | |
a1396b9e MS |
810 | for (int j = 0; j < 8; ++j) { |
811 | output.sendNoteOn(j, 48, 0); | |
812 | } | |
66067e53 MS |
813 | for (int row = 0; row < 7; ++row) { |
814 | for (int col = 0; col < 8; ++col) { | |
815 | setGridState(row, col, 0); | |
816 | } | |
242b19ad MS |
817 | } |
818 | } | |
05965d3d MS |
819 | } |
820 | ||
821 | private void resetEffectParameters() { | |
822 | LXEffect newEffect = glucose.getSelectedEffect(); | |
823 | if (newEffect == focusedEffect) { | |
824 | return; | |
825 | } | |
826 | if (focusedEffect != null) { | |
827 | for (LXParameter p : focusedPattern.getParameters()) { | |
828 | ((LXListenableParameter) p).removeListener(this); | |
829 | } | |
830 | } | |
831 | focusedEffect = newEffect; | |
832 | int i = 0; | |
833 | for (LXParameter p : focusedEffect.getParameters()) { | |
834 | ((LXListenableParameter) p).addListener(this); | |
835 | sendKnob(12 + i++, p); | |
836 | } | |
837 | while (i < 4) { | |
838 | sendKnob(12 + i++, 0); | |
839 | } | |
840 | } | |
841 | ||
842 | private void sendKnob(int i, LXParameter p) { | |
843 | sendKnob(i, (int) (p.getValuef() * 127.)); | |
844 | } | |
845 | ||
846 | private void sendKnob(int i, int value) { | |
847 | if (i < 8) { | |
848 | output.sendController(0, 48+i, value); | |
849 | } else if (i < 16) { | |
850 | output.sendController(0, 8+i, value); | |
851 | } | |
852 | } | |
853 | ||
854 | public void onParameterChanged(LXParameter parameter) { | |
855 | int i = 0; | |
856 | for (LXParameter p : focusedPattern.getParameters()) { | |
857 | if (p == parameter) { | |
858 | sendKnob(i, p); | |
859 | break; | |
860 | } | |
861 | ++i; | |
862 | } | |
863 | i = 12; | |
864 | for (LXParameter p : focusedEffect.getParameters()) { | |
865 | if (p == parameter) { | |
866 | sendKnob(i, p); | |
867 | break; | |
868 | } | |
869 | ++i; | |
870 | } | |
871 | } | |
242b19ad MS |
872 | |
873 | public void setGridState(int row, int col, int state) { | |
e0794d3a MS |
874 | if (col < 8 && row < 5) { |
875 | output.sendNoteOn(col, 53+row, state); | |
242b19ad MS |
876 | } |
877 | } | |
878 | } | |
879 | ||
d93c968f MS |
880 | class ArturiaMinilabMidiInput extends GenericDeviceMidiInput { |
881 | ArturiaMinilabMidiInput(MidiEngine midiEngine, MidiInputDevice d) { | |
882 | super(midiEngine, d); | |
883 | } | |
884 | ||
885 | protected boolean handleControllerChange(rwmidi.Controller cc) { | |
886 | int parameterIndex = -1; | |
887 | switch (cc.getCC()) { | |
888 | case 7: parameterIndex = 0; break; | |
889 | case 74: parameterIndex = 1; break; | |
890 | case 71: parameterIndex = 2; break; | |
891 | case 76: parameterIndex = 3; break; | |
892 | case 114: parameterIndex = 4; break; | |
893 | case 18: parameterIndex = 5; break; | |
894 | case 19: parameterIndex = 6; break; | |
895 | case 16: parameterIndex = 7; break; | |
896 | } | |
897 | if (parameterIndex >= 0) { | |
898 | List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters(); | |
899 | if (parameterIndex < parameters.size()) { | |
900 | LXParameter p = parameters.get(parameterIndex); | |
901 | float curVal = p.getValuef(); | |
902 | curVal += (cc.getValue() - 64) / 127.; | |
903 | p.setValue(constrain(curVal, 0, 1)); | |
904 | } | |
905 | } | |
906 | return false; | |
907 | } | |
908 | } | |
909 | ||
242b19ad MS |
910 | interface GridOutput { |
911 | public static final int OFF = 0; | |
912 | public static final int GREEN = 1; | |
913 | public static final int GREEN_BLINK = 2; | |
914 | public static final int RED = 3; | |
915 | public static final int RED_BLINK = 4; | |
916 | public static final int YELLOW = 5; | |
917 | public static final int YELLOW_BLINK = 6; | |
918 | public static final int ON = 127; | |
919 | ||
920 | public void setGridState(int row, int col, int state); | |
05965d3d | 921 | } |
242b19ad MS |
922 | |
923 | class GridController { | |
924 | private final List<GridOutput> outputs = new ArrayList<GridOutput>(); | |
925 | ||
926 | private final MidiEngine midiEngine; | |
927 | ||
928 | GridController(MidiEngine midiEngine) { | |
929 | this.midiEngine = midiEngine; | |
930 | } | |
931 | ||
932 | public void addOutput(GridOutput output) { | |
933 | outputs.add(output); | |
934 | } | |
935 | ||
936 | public boolean gridPressed(int row, int col) { | |
937 | return midiEngine.getFocusedPattern().gridPressed(row, col); | |
938 | } | |
939 | ||
940 | public boolean gridReleased(int row, int col) { | |
941 | return midiEngine.getFocusedPattern().gridReleased(row, col); | |
942 | } | |
943 | ||
944 | public void setState(int row, int col, int state) { | |
945 | for (GridOutput g : outputs) { | |
946 | g.setGridState(row, col, state); | |
947 | } | |
948 | } | |
949 | } | |
950 |