Hackathon anims
[SugarCubes.git] / _MIDI.pde
CommitLineData
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
19interface MidiEngineListener {
20 public void onFocusedDeck(int deckIndex);
21}
22
23class 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
114public interface SCMidiInputListener {
115 public void onEnabled(SCMidiInput controller, boolean enabled);
116}
117
0c7af01b 118public 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 }
05ebfd15
MS
221
222 protected void setNormalized(LXParameter parameter, float value) {
223 if (parameter != null) {
224 if (parameter instanceof BasicParameter) {
225 ((BasicParameter)parameter).setNormalized(value);
226 } else {
227 parameter.setValue(value);
228 }
229 }
230 }
d6ac1ee8
MS
231
232 // Subclasses may implement these to map top-level functionality
3efafb5c
MS
233 protected boolean handleProgramChange(ProgramChange pc) { return false; }
234 protected boolean handleControllerChange(rwmidi.Controller cc) { return false; }
235 protected boolean handleNoteOn(Note note) { return false; }
236 protected boolean handleNoteOff(Note note) { return false; }
d6ac1ee8
MS
237}
238
0c7af01b
MS
239public class VirtualKeyMidiInput extends SCMidiInput {
240
241 public static final int KEYS = 1;
242 public static final int APC = 2;
243
244 private final int mode;
245
246 private int octaveShift = 0;
247
248 class NoteMeta {
249 int channel;
250 int number;
251 NoteMeta(int channel, int number) {
252 this.channel = channel;
253 this.number = number;
254 }
255 }
256
257 final Map<Character, NoteMeta> keyToNote = new HashMap<Character, NoteMeta>();
258
259 VirtualKeyMidiInput(MidiEngine midiEngine, int mode) {
260 super(midiEngine, "QWERTY (" + (mode == APC ? "APC" : "Key") + " Mode)");
261 this.mode = mode;
262 if (mode == APC) {
263 mapAPC();
264 } else {
265 mapKeys();
266 }
267 registerKeyEvent(this);
268 }
269
270 private void mapAPC() {
271 mapNote('1', 0, 53);
272 mapNote('2', 1, 53);
273 mapNote('3', 2, 53);
274 mapNote('4', 3, 53);
275 mapNote('5', 4, 53);
276 mapNote('6', 5, 53);
277 mapNote('q', 0, 54);
278 mapNote('w', 1, 54);
279 mapNote('e', 2, 54);
280 mapNote('r', 3, 54);
281 mapNote('t', 4, 54);
282 mapNote('y', 5, 54);
283 mapNote('a', 0, 55);
284 mapNote('s', 1, 55);
285 mapNote('d', 2, 55);
286 mapNote('f', 3, 55);
287 mapNote('g', 4, 55);
288 mapNote('h', 5, 55);
289 mapNote('z', 0, 56);
290 mapNote('x', 1, 56);
291 mapNote('c', 2, 56);
292 mapNote('v', 3, 56);
293 mapNote('b', 4, 56);
294 mapNote('n', 5, 56);
295 }
296
297 private void mapKeys() {
298 int note = 48;
299 mapNote('a', 1, note++);
300 mapNote('w', 1, note++);
301 mapNote('s', 1, note++);
302 mapNote('e', 1, note++);
303 mapNote('d', 1, note++);
304 mapNote('f', 1, note++);
305 mapNote('t', 1, note++);
306 mapNote('g', 1, note++);
307 mapNote('y', 1, note++);
308 mapNote('h', 1, note++);
309 mapNote('u', 1, note++);
310 mapNote('j', 1, note++);
311 mapNote('k', 1, note++);
312 mapNote('o', 1, note++);
313 mapNote('l', 1, note++);
314 }
315
316 void mapNote(char ch, int channel, int number) {
317 keyToNote.put(ch, new NoteMeta(channel, number));
318 }
319
320 public void keyEvent(KeyEvent e) {
321 if (!enabled) {
322 return;
323 }
324 char c = Character.toLowerCase(e.getKeyChar());
325 NoteMeta nm = keyToNote.get(c);
326 if (nm != null) {
327 switch (e.getID()) {
328 case KeyEvent.KEY_PRESSED:
329 noteOnReceived(new Note(Note.NOTE_ON, nm.channel, nm.number + octaveShift*12, 127));
330 break;
331 case KeyEvent.KEY_RELEASED:
332 noteOffReceived(new Note(Note.NOTE_OFF, nm.channel, nm.number + octaveShift*12, 0));
333 break;
334 }
335 }
336 if ((mode == KEYS) && (e.getID() == KeyEvent.KEY_PRESSED)) {
337 switch (c) {
338 case 'z':
339 octaveShift = constrain(octaveShift-1, -4, 4);
340 break;
341 case 'x':
342 octaveShift = constrain(octaveShift+1, -4, 4);
343 break;
344 }
345 }
346 }
347}
348
349public class GenericDeviceMidiInput extends SCMidiInput {
350 GenericDeviceMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
351 super(midiEngine, d.getName().replace("Unknown vendor",""));
352 d.createInput(this);
353 }
354}
355
356public class APC40MidiInput extends GenericDeviceMidiInput {
d6ac1ee8
MS
357
358 private boolean shiftOn = false;
359 private LXEffect releaseEffect = null;
42a424d7 360 final private LXDeck targetDeck;
d6ac1ee8 361
242b19ad 362 APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d) {
1b65b8ce
MS
363 this(midiEngine, d, -1);
364 }
365
366 APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d, int deckIndex) {
367 super(midiEngine, d);
368 targetDeck = (deckIndex < 0) ? null : lx.engine.getDecks().get(deckIndex);
369 }
370
42a424d7 371 protected LXDeck getTargetDeck() {
887295bc 372 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
1b65b8ce
MS
373 }
374
375 protected SCPattern getTargetPattern() {
376 if (targetDeck != null) {
377 return (SCPattern) (targetDeck.getActivePattern());
378 }
379 return super.getTargetPattern();
242b19ad
MS
380 }
381
382 private class GridPosition {
383 public final int row, col;
384 GridPosition(int r, int c) {
385 row = r;
386 col = c;
387 }
388 }
389
390 private GridPosition getGridPosition(Note note) {
391 int channel = note.getChannel();
392 int pitch = note.getPitch();
393 if (channel < 8) {
394 if (pitch >= 53 && pitch <=57) return new GridPosition(pitch-53, channel);
242b19ad
MS
395 }
396 return null;
397 }
398
3efafb5c 399 private boolean handleGridNoteOn(Note note) {
242b19ad
MS
400 GridPosition p = getGridPosition(note);
401 if (p != null) {
402 return midiEngine.grid.gridPressed(p.row, p.col);
403 }
404 return false;
405 }
406
3efafb5c 407 private boolean handleGridNoteOff(Note note) {
242b19ad
MS
408 GridPosition p = getGridPosition(note);
409 if (p != null) {
410 return midiEngine.grid.gridReleased(p.row, p.col);
411 }
412 return false;
d6ac1ee8
MS
413 }
414
3efafb5c 415 protected boolean handleControllerChange(rwmidi.Controller cc) {
e5308763 416 int channel = cc.getChannel();
d6ac1ee8 417 int number = cc.getCC();
e5308763 418 float value = cc.getValue() / 127.;
d6ac1ee8 419 switch (number) {
e5308763 420
421 case 7:
422 switch (channel) {
423 case 0:
05ebfd15 424 uiSpeed.speed.setNormalized(0.5 - value*0.5);
3efafb5c 425 return true;
e5308763 426 case 1:
05ebfd15 427 effects.colorFucker.desat.setNormalized(value);
3efafb5c 428 return true;
e5308763 429 case 2:
05ebfd15 430 effects.colorFucker.desat.setNormalized(value);
3efafb5c 431 return true;
e5308763 432 case 3:
05ebfd15 433 effects.blur.amount.setNormalized(value);
3efafb5c 434 return true;
e5308763 435 case 4:
05ebfd15 436 effects.quantize.amount.setNormalized(value);
3efafb5c 437 return true;
e5308763 438 }
439 break;
440
441 // Master bright
442 case 14:
05ebfd15 443 effects.colorFucker.level.setNormalized(value);
3efafb5c 444 return true;
e5308763 445
d6ac1ee8
MS
446 // Crossfader
447 case 15:
05ebfd15 448 lx.engine.getDeck(GLucose.RIGHT_DECK).getFader().setNormalized(value);
3efafb5c 449 return true;
29d8acbb
MS
450
451 // Cue level
452 case 47:
453 float val = effects.colorFucker.hueShift.getValuef();
454 int cv = cc.getValue();
455 if (cv < 64) {
456 cv = 64 + cv;
457 } else {
458 cv = cv - 64;
459 }
e0847dfb 460 val += (cv - 64) / 500.;
05ebfd15 461 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
29d8acbb 462 return true;
d6ac1ee8
MS
463 }
464
465 int parameterIndex = -1;
466 if (number >= 48 && number <= 55) {
467 parameterIndex = number - 48;
468 } else if (number >= 16 && number <= 19) {
469 parameterIndex = 8 + (number-16);
470 }
471 if (parameterIndex >= 0) {
1b65b8ce 472 List<LXParameter> parameters = getTargetPattern().getParameters();
d6ac1ee8 473 if (parameterIndex < parameters.size()) {
05ebfd15 474 setNormalized(parameters.get(parameterIndex), value);
3efafb5c 475 return true;
d6ac1ee8
MS
476 }
477 }
478
479 if (number >= 20 && number <= 23) {
480 int effectIndex = number - 20;
481 List<LXParameter> parameters = glucose.getSelectedEffect().getParameters();
482 if (effectIndex < parameters.size()) {
05ebfd15 483 setNormalized(parameters.get(effectIndex), value);
3efafb5c 484 return true;
d6ac1ee8
MS
485 }
486 }
3efafb5c
MS
487
488 return false;
d6ac1ee8
MS
489 }
490
05965d3d 491 private long tap1 = 0;
3661fcee 492
05965d3d
MS
493 private boolean lbtwn(long a, long b, long c) {
494 return a >= b && a <= c;
495 }
3661fcee 496
3efafb5c
MS
497 protected boolean handleNoteOn(Note note) {
498 if (handleGridNoteOn(note)) {
499 return true;
500 }
501
e5308763 502 int nPitch = note.getPitch();
503 int nChan = note.getChannel();
3661fcee 504 switch (nPitch) {
e5308763 505
506 case 49: // SOLO/CUE
507 switch (nChan) {
508 case 4:
05ebfd15 509 effects.colorFucker.mono.setNormalized(1);
3efafb5c 510 return true;
e5308763 511 case 5:
05ebfd15 512 effects.colorFucker.invert.setNormalized(1);
3efafb5c 513 return true;
e5308763 514 case 6:
515 lx.cycleBaseHue(60000);
3efafb5c 516 return true;
e5308763 517 }
518 break;
1f974cbc 519
05965d3d 520 case 82: // scene 1
24fc0330 521 effects.boom.trigger();
3efafb5c 522 return true;
05965d3d
MS
523
524 case 83: // scene 2
24fc0330 525 effects.flash.trigger();
3efafb5c 526 return true;
05965d3d 527
ac6b3e48
MS
528 case 84: // scene 3
529 getTargetPattern().reset();
530 return true;
531
05965d3d
MS
532 case 90:
533 // dan's dirty tapping mechanism
534 lx.tempo.trigger();
535 tap1 = millis();
3efafb5c 536 return true;
af83f61c 537
538 case 91: // play
88a3dd9e
MS
539 if (shiftOn) {
540 midiEngine.setFocusedDeck(GLucose.LEFT_DECK);
541 } else {
542 uiCrossfader.setDisplayMode("A");
543 }
3efafb5c 544 return true;
88a3dd9e
MS
545
546 case 92: // stop
547 uiCrossfader.setDisplayMode("COMP");
548 return true;
549
af83f61c 550 case 93: // rec
88a3dd9e
MS
551 if (shiftOn) {
552 midiEngine.setFocusedDeck(GLucose.RIGHT_DECK);
553 } else {
554 uiCrossfader.setDisplayMode("B");
555 }
3efafb5c 556 return true;
af83f61c 557
ff23d191 558 case 94: // up bank
d6ac1ee8 559 if (shiftOn) {
ff23d191 560 glucose.incrementSelectedEffectBy(-1);
d6ac1ee8 561 } else {
1b65b8ce 562 getTargetDeck().goPrev();
d6ac1ee8 563 }
3efafb5c
MS
564 return true;
565
ff23d191 566 case 95: // down bank
d6ac1ee8 567 if (shiftOn) {
ff23d191 568 glucose.incrementSelectedEffectBy(1);
d6ac1ee8 569 } else {
1b65b8ce 570 getTargetDeck().goNext();
d6ac1ee8 571 }
3efafb5c 572 return true;
d6ac1ee8
MS
573
574 case 98: // shift
575 shiftOn = true;
3efafb5c 576 return true;
d6ac1ee8
MS
577
578 case 99: // tap tempo
579 lx.tempo.tap();
3efafb5c
MS
580 return true;
581
d6ac1ee8
MS
582 case 100: // nudge+
583 lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1));
3efafb5c
MS
584 return true;
585
d6ac1ee8
MS
586 case 101: // nudge-
587 lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1));
3efafb5c 588 return true;
d6ac1ee8 589
ff23d191 590 case 62: // Detail View / red 5
d6ac1ee8
MS
591 releaseEffect = glucose.getSelectedEffect();
592 if (releaseEffect.isMomentary()) {
593 releaseEffect.enable();
594 } else {
595 releaseEffect.toggle();
596 }
3efafb5c 597 return true;
d6ac1ee8 598
ff23d191 599 case 63: // rec quantize / red 6
d6ac1ee8 600 glucose.getSelectedEffect().disable();
3efafb5c 601 return true;
d6ac1ee8 602 }
3efafb5c
MS
603
604 return false;
d6ac1ee8
MS
605 }
606
3efafb5c
MS
607 protected boolean handleNoteOff(Note note) {
608 if (handleGridNoteOff(note)) {
609 return true;
610 }
611
e5308763 612 int nPitch = note.getPitch();
613 int nChan = note.getChannel();
614
3661fcee 615 switch (nPitch) {
e5308763 616
617 case 49: // SOLO/CUE
618 switch (nChan) {
619 case 4:
05ebfd15 620 effects.colorFucker.mono.setNormalized(0);
3efafb5c 621 return true;
e5308763 622 case 5:
05ebfd15 623 effects.colorFucker.invert.setNormalized(0);
3efafb5c 624 return true;
e5308763 625 case 6:
626 lx.setBaseHue(lx.getBaseHue());
3efafb5c 627 return true;
e5308763 628 }
629 break;
1f974cbc
MS
630
631 case 52: // CLIP STOP
632 if (nChan < PresetManager.NUM_PRESETS) {
633 if (shiftOn) {
f6394aaa 634 presetManager.store(getTargetDeck(), nChan);
1f974cbc 635 } else {
f6394aaa 636 presetManager.select(getTargetDeck(), nChan);
1f974cbc
MS
637 }
638 }
3efafb5c 639 return true;
1f974cbc 640
ff23d191 641 case 90: // SEND C
05965d3d
MS
642 long tapDelta = millis() - tap1;
643 if (lbtwn(tapDelta,5000,300*1000)) { // hackish tapping mechanism
644 double bpm = 32.*60000./(tapDelta);
645 while (bpm < 20) bpm*=2;
646 while (bpm > 40) bpm/=2;
647 lx.tempo.setBpm(bpm);
648 lx.tempo.trigger();
649 tap1 = 0;
650 println("Tap Set - " + bpm + " bpm");
651 }
3efafb5c 652 return true;
af83f61c 653
ff23d191 654 case 63: // rec quantize / RED 6
d6ac1ee8
MS
655 if (releaseEffect != null) {
656 if (releaseEffect.isMomentary()) {
657 releaseEffect.disable();
658 }
659 }
3efafb5c 660 return true;
af83f61c 661
d6ac1ee8
MS
662 case 98: // shift
663 shiftOn = false;
3efafb5c 664 return true;
d6ac1ee8 665 }
3efafb5c
MS
666
667 return false;
d6ac1ee8
MS
668 }
669}
670
0c7af01b 671class KorgNanoKontrolMidiInput extends GenericDeviceMidiInput {
d6ac1ee8 672
242b19ad
MS
673 KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
674 super(midiEngine, d);
d6ac1ee8
MS
675 }
676
3efafb5c 677 protected boolean handleControllerChange(rwmidi.Controller cc) {
d6ac1ee8
MS
678 int number = cc.getCC();
679 if (number >= 16 && number <= 23) {
680 int parameterIndex = number - 16;
242b19ad 681 List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters();
d6ac1ee8 682 if (parameterIndex < parameters.size()) {
05ebfd15 683 setNormalized(parameters.get(parameterIndex), cc.getValue() / 127.);
3efafb5c 684 return true;
d6ac1ee8
MS
685 }
686 }
687
688 if (cc.getValue() == 127) {
689 switch (number) {
3efafb5c
MS
690
691 case 58: // Left track
692 midiEngine.setFocusedDeck(GLucose.LEFT_DECK);
693 return true;
694
695 case 59: // Right track
696 midiEngine.setFocusedDeck(GLucose.RIGHT_DECK);
697 return true;
698
699 case 43: // Left chevron
d6ac1ee8 700 midiEngine.getFocusedDeck().goPrev();
3efafb5c
MS
701 return true;
702
703 case 44: // Right chevron
d6ac1ee8 704 midiEngine.getFocusedDeck().goNext();
3efafb5c 705 return true;
d6ac1ee8
MS
706 }
707 }
3efafb5c
MS
708
709 return false;
d6ac1ee8
MS
710 }
711}
712
b8bb2748 713class APC40MidiOutput implements LXParameterListener, GridOutput {
05965d3d
MS
714
715 private final MidiEngine midiEngine;
716 private final MidiOutput output;
717 private LXPattern focusedPattern = null;
718 private LXEffect focusedEffect = null;
42a424d7 719 private final LXDeck targetDeck;
05965d3d
MS
720
721 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) {
1b65b8ce
MS
722 this(midiEngine, device, -1);
723 }
724
725 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device, int deckIndex) {
05965d3d
MS
726 this.midiEngine = midiEngine;
727 output = device.createOutput();
1b65b8ce
MS
728 targetDeck = (deckIndex < 0) ? null : lx.engine.getDecks().get(deckIndex);
729 setDPatternOutputs();
730 if (targetDeck != null) {
731 midiEngine.addListener(new MidiEngineListener() {
732 public void onFocusedDeck(int deckIndex) {
733 resetPatternParameters();
2b068dd5
MS
734 for (int i = 0; i < 8; ++i) {
735 output.sendNoteOn(i, 52, 0);
736 }
1b65b8ce
MS
737 }
738 });
739 }
05965d3d
MS
740 glucose.addEffectListener(new GLucose.EffectListener() {
741 public void effectSelected(LXEffect effect) {
742 resetEffectParameters();
743 }
744 });
42a424d7
MS
745 LXDeck.Listener deckListener = new LXDeck.AbstractListener() {
746 public void patternDidChange(LXDeck deck, LXPattern pattern) {
2b068dd5
MS
747 if (deck == getTargetDeck()) {
748 resetPatternParameters();
749 }
05965d3d
MS
750 }
751 };
42a424d7 752 for (LXDeck d : lx.engine.getDecks()) {
1b65b8ce
MS
753 if (targetDeck == null || d == targetDeck) {
754 d.addListener(deckListener);
755 }
05965d3d 756 }
1f974cbc 757 presetManager.addListener(new PresetListener() {
2b068dd5 758 public void onPresetSelected(LXDeck deck, Preset preset) {
a8d1f57a
MS
759 if (deck == getTargetDeck()) {
760 for (int i = 0; i < 8; ++i) {
761 output.sendNoteOn(i, 52, (preset.index == i) ? 1 : 0);
762 }
1f974cbc
MS
763 }
764 }
42a424d7 765 public void onPresetDirty(LXDeck deck, Preset preset) {
a8d1f57a
MS
766 if (deck == getTargetDeck()) {
767 output.sendNoteOn(preset.index, 52, 2);
768 }
1f974cbc 769 }
42a424d7 770 public void onPresetStored(LXDeck deck, Preset preset) {
2910a170 771 onPresetSelected(deck, preset);
1f974cbc
MS
772 }
773 });
05965d3d 774 resetParameters();
242b19ad 775 midiEngine.grid.addOutput(this);
e5308763 776
777 lx.cycleBaseHue(60000);
778 output.sendNoteOn(6, 49, 127);
677670a6 779
1f974cbc 780 // Turn off the track selection lights and preset selectors
677670a6
MS
781 for (int i = 0; i < 8; ++i) {
782 output.sendNoteOn(i, 51, 0);
1f974cbc 783 output.sendNoteOn(i, 52, 0);
677670a6 784 }
1f974cbc
MS
785
786 // Turn off the MASTER selector
677670a6 787 output.sendNoteOn(0, 80, 0);
05965d3d 788 }
1b65b8ce
MS
789
790 private void setDPatternOutputs() {
42a424d7 791 for (LXDeck deck : lx.engine.getDecks()) {
1b65b8ce
MS
792 if (targetDeck == null || deck == targetDeck) {
793 for (LXPattern pattern : deck.getPatterns()) {
794 if (pattern instanceof DPat) {
795 ((DPat)pattern).setAPCOutput(output);
796 }
797 }
798 }
799 }
800 }
801
42a424d7 802 protected LXDeck getTargetDeck() {
2b068dd5 803 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
1b65b8ce 804 }
05965d3d
MS
805
806 private void resetParameters() {
807 resetPatternParameters();
808 resetEffectParameters();
809 }
810
811 private void resetPatternParameters() {
1b65b8ce 812 LXPattern newPattern = getTargetDeck().getActivePattern();
05965d3d
MS
813 if (newPattern == focusedPattern) {
814 return;
815 }
816 if (focusedPattern != null) {
817 for (LXParameter p : focusedPattern.getParameters()) {
818 ((LXListenableParameter) p).removeListener(this);
819 }
820 }
821 focusedPattern = newPattern;
822 int i = 0;
823 for (LXParameter p : focusedPattern.getParameters()) {
824 ((LXListenableParameter) p).addListener(this);
825 sendKnob(i++, p);
826 }
827 while (i < 12) {
828 sendKnob(i++, 0);
829 }
66067e53
MS
830 if (focusedPattern instanceof DPat) {
831 ((DPat)focusedPattern).updateLights();
832 } else {
a1396b9e
MS
833 for (int j = 0; j < 8; ++j) {
834 output.sendNoteOn(j, 48, 0);
835 }
66067e53
MS
836 for (int row = 0; row < 7; ++row) {
837 for (int col = 0; col < 8; ++col) {
838 setGridState(row, col, 0);
839 }
242b19ad
MS
840 }
841 }
05965d3d
MS
842 }
843
844 private void resetEffectParameters() {
845 LXEffect newEffect = glucose.getSelectedEffect();
846 if (newEffect == focusedEffect) {
847 return;
848 }
849 if (focusedEffect != null) {
850 for (LXParameter p : focusedPattern.getParameters()) {
851 ((LXListenableParameter) p).removeListener(this);
852 }
853 }
854 focusedEffect = newEffect;
855 int i = 0;
856 for (LXParameter p : focusedEffect.getParameters()) {
857 ((LXListenableParameter) p).addListener(this);
858 sendKnob(12 + i++, p);
859 }
860 while (i < 4) {
861 sendKnob(12 + i++, 0);
862 }
863 }
864
865 private void sendKnob(int i, LXParameter p) {
866 sendKnob(i, (int) (p.getValuef() * 127.));
867 }
868
869 private void sendKnob(int i, int value) {
870 if (i < 8) {
871 output.sendController(0, 48+i, value);
872 } else if (i < 16) {
873 output.sendController(0, 8+i, value);
874 }
875 }
876
877 public void onParameterChanged(LXParameter parameter) {
878 int i = 0;
879 for (LXParameter p : focusedPattern.getParameters()) {
880 if (p == parameter) {
881 sendKnob(i, p);
882 break;
883 }
884 ++i;
885 }
886 i = 12;
887 for (LXParameter p : focusedEffect.getParameters()) {
888 if (p == parameter) {
889 sendKnob(i, p);
890 break;
891 }
892 ++i;
893 }
894 }
242b19ad
MS
895
896 public void setGridState(int row, int col, int state) {
e0794d3a
MS
897 if (col < 8 && row < 5) {
898 output.sendNoteOn(col, 53+row, state);
242b19ad
MS
899 }
900 }
901}
902
d93c968f
MS
903class ArturiaMinilabMidiInput extends GenericDeviceMidiInput {
904 ArturiaMinilabMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
905 super(midiEngine, d);
906 }
907
908 protected boolean handleControllerChange(rwmidi.Controller cc) {
909 int parameterIndex = -1;
910 switch (cc.getCC()) {
911 case 7: parameterIndex = 0; break;
912 case 74: parameterIndex = 1; break;
913 case 71: parameterIndex = 2; break;
914 case 76: parameterIndex = 3; break;
915 case 114: parameterIndex = 4; break;
916 case 18: parameterIndex = 5; break;
917 case 19: parameterIndex = 6; break;
918 case 16: parameterIndex = 7; break;
29d8acbb
MS
919
920 case 75:
921 float val = effects.colorFucker.hueShift.getValuef();
922 val += (cc.getValue() - 64) / 256.;
05ebfd15 923 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
29d8acbb 924 break;
d93c968f
MS
925 }
926 if (parameterIndex >= 0) {
927 List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters();
928 if (parameterIndex < parameters.size()) {
929 LXParameter p = parameters.get(parameterIndex);
930 float curVal = p.getValuef();
931 curVal += (cc.getValue() - 64) / 127.;
05ebfd15 932 setNormalized(p, constrain(curVal, 0, 1));
d93c968f
MS
933 }
934 }
935 return false;
936 }
937}
938
242b19ad
MS
939interface GridOutput {
940 public static final int OFF = 0;
941 public static final int GREEN = 1;
942 public static final int GREEN_BLINK = 2;
943 public static final int RED = 3;
944 public static final int RED_BLINK = 4;
945 public static final int YELLOW = 5;
946 public static final int YELLOW_BLINK = 6;
947 public static final int ON = 127;
948
949 public void setGridState(int row, int col, int state);
05965d3d 950}
242b19ad
MS
951
952class GridController {
953 private final List<GridOutput> outputs = new ArrayList<GridOutput>();
954
955 private final MidiEngine midiEngine;
956
957 GridController(MidiEngine midiEngine) {
958 this.midiEngine = midiEngine;
959 }
960
961 public void addOutput(GridOutput output) {
962 outputs.add(output);
963 }
964
965 public boolean gridPressed(int row, int col) {
966 return midiEngine.getFocusedPattern().gridPressed(row, col);
967 }
968
969 public boolean gridReleased(int row, int col) {
970 return midiEngine.getFocusedPattern().gridReleased(row, col);
971 }
972
973 public void setState(int row, int col, int state) {
974 for (GridOutput g : outputs) {
975 g.setGridState(row, col, state);
976 }
977 }
978}
979