Merge branch 'grizzly-dev'
[SugarCubes.git] / _MIDI.pde
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
25 public final GridController grid;
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() {
45 grid = new GridController(this);
46 midiControllers.add(midiQwertyKeys = new VirtualKeyMidiInput(this, VirtualKeyMidiInput.KEYS));
47 midiControllers.add(midiQwertyAPC = new VirtualKeyMidiInput(this, VirtualKeyMidiInput.APC));
48 int apcCount = 0;
49 for (MidiInputDevice device : RWMidi.getInputDevices()) {
50 if (device.getName().contains("APC")) {
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));
63 } else if (device.getName().contains("SLIDER/KNOB KORG")) {
64 midiControllers.add(new KorgNanoKontrolMidiInput(this, device).setEnabled(true));
65 } else if (device.getName().contains("Arturia MINILAB")) {
66 midiControllers.add(new ArturiaMinilabMidiInput(this, device).setEnabled(true));
67 } else {
68 boolean enabled =
69 device.getName().contains("KEYBOARD KORG") ||
70 device.getName().contains("Bus 1 Apple");
71 midiControllers.add(new GenericDeviceMidiInput(this, device).setEnabled(enabled));
72 }
73 }
74
75 apcIndex = 0;
76 for (MidiOutputDevice device : RWMidi.getOutputDevices()) {
77 if (device.getName().contains("APC")) {
78 int apcDeck = -1;
79 if (apcCount > 1 && apcIndex < 2) {
80 apcDeck = apcIndex++;
81 }
82 new APC40MidiOutput(this, device, apcDeck);
83 }
84 }
85 }
86
87 public List<SCMidiInput> getControllers() {
88 return this.midiControllers;
89 }
90
91 public LXDeck getFocusedDeck() {
92 return lx.engine.getDeck(activeDeckIndex);
93 }
94
95 public SCPattern getFocusedPattern() {
96 return (SCPattern) getFocusedDeck().getActivePattern();
97 }
98
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
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
118 public abstract class SCMidiInput extends UIScrollList.AbstractItem {
119
120 protected boolean enabled = false;
121 private final String name;
122
123 protected final MidiEngine midiEngine;
124
125 final List<SCMidiInputListener> listeners = new ArrayList<SCMidiInputListener>();
126
127 protected SCMidiInput(MidiEngine midiEngine, String name) {
128 this.midiEngine = midiEngine;
129 this.name = name;
130 }
131
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
142 public String getLabel() {
143 return name;
144 }
145
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
168 private boolean logMidi() {
169 return (uiMidi != null) && uiMidi.logMidi();
170 }
171
172 protected SCPattern getTargetPattern() {
173 return midiEngine.getFocusedPattern();
174 }
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()) {
191 println(getLabel() + " :: Controller :: " + cc.getChannel() + " :: " + cc.getCC() + ":" + cc.getValue());
192 }
193 if (!handleControllerChange(cc)) {
194 getTargetPattern().controllerChange(cc);
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 }
205 if (!handleNoteOn(note)) {
206 getTargetPattern().noteOn(note);
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 }
217 if (!handleNoteOff(note)) {
218 getTargetPattern().noteOff(note);
219 }
220 }
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 }
231
232 // Subclasses may implement these to map top-level functionality
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; }
237 }
238
239 public 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
349 public 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
356 public class APC40MidiInput extends GenericDeviceMidiInput {
357
358 private boolean shiftOn = false;
359 private LXEffect releaseEffect = null;
360 final private LXDeck targetDeck;
361
362 APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d) {
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
371 protected LXDeck getTargetDeck() {
372 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
373 }
374
375 protected SCPattern getTargetPattern() {
376 if (targetDeck != null) {
377 return (SCPattern) (targetDeck.getActivePattern());
378 }
379 return super.getTargetPattern();
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);
395 }
396 return null;
397 }
398
399 private boolean handleGridNoteOn(Note note) {
400 GridPosition p = getGridPosition(note);
401 if (p != null) {
402 return midiEngine.grid.gridPressed(p.row, p.col);
403 }
404 return false;
405 }
406
407 private boolean handleGridNoteOff(Note note) {
408 GridPosition p = getGridPosition(note);
409 if (p != null) {
410 return midiEngine.grid.gridReleased(p.row, p.col);
411 }
412 return false;
413 }
414
415 protected boolean handleControllerChange(rwmidi.Controller cc) {
416 int channel = cc.getChannel();
417 int number = cc.getCC();
418 float value = cc.getValue() / 127.;
419 switch (number) {
420
421 case 7:
422 switch (channel) {
423 case 0:
424 uiSpeed.speed.setNormalized(0.5 - value*0.5);
425 return true;
426 case 1:
427 effects.colorFucker.desat.setNormalized(value);
428 return true;
429 case 2:
430 effects.colorFucker.desat.setNormalized(value);
431 return true;
432 case 3:
433 effects.blur.amount.setNormalized(value);
434 return true;
435 case 4:
436 effects.quantize.amount.setNormalized(value);
437 return true;
438 }
439 break;
440
441 // Master bright
442 case 14:
443 effects.colorFucker.level.setNormalized(value);
444 return true;
445
446 // Crossfader
447 case 15:
448 lx.engine.getDeck(RIGHT_DECK).getFader().setNormalized(value);
449 return true;
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 }
460 val += (cv - 64) / 500.;
461 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
462 return true;
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) {
472 List<LXParameter> parameters = getTargetPattern().getParameters();
473 if (parameterIndex < parameters.size()) {
474 setNormalized(parameters.get(parameterIndex), value);
475 return true;
476 }
477 }
478
479 if (number >= 20 && number <= 23) {
480 int effectIndex = number - 20;
481 // TODO(mclsee): fix selected effect
482 List<LXParameter> parameters = getSelectedEffect().getParameters();
483 if (effectIndex < parameters.size()) {
484 setNormalized(parameters.get(effectIndex), value);
485 return true;
486 }
487 }
488
489 return false;
490 }
491
492 private long tap1 = 0;
493
494 private boolean lbtwn(long a, long b, long c) {
495 return a >= b && a <= c;
496 }
497
498 protected boolean handleNoteOn(Note note) {
499 if (handleGridNoteOn(note)) {
500 return true;
501 }
502
503 int nPitch = note.getPitch();
504 int nChan = note.getChannel();
505 switch (nPitch) {
506
507 case 49: // SOLO/CUE
508 switch (nChan) {
509 case 4:
510 effects.colorFucker.mono.setNormalized(1);
511 return true;
512 case 5:
513 effects.colorFucker.invert.setNormalized(1);
514 return true;
515 case 6:
516 lx.cycleBaseHue(60000);
517 return true;
518 }
519 break;
520
521 case 82: // scene 1
522 effects.boom.trigger();
523 return true;
524
525 case 83: // scene 2
526 effects.flash.trigger();
527 return true;
528
529 case 84: // scene 3
530 getTargetPattern().reset();
531 return true;
532
533 case 90:
534 // dan's dirty tapping mechanism
535 lx.tempo.trigger();
536 tap1 = millis();
537 return true;
538
539 case 91: // play
540 if (shiftOn) {
541 midiEngine.setFocusedDeck(LEFT_DECK);
542 } else {
543 uiCrossfader.setDisplayMode("A");
544 }
545 return true;
546
547 case 92: // stop
548 uiCrossfader.setDisplayMode("COMP");
549 return true;
550
551 case 93: // rec
552 if (shiftOn) {
553 midiEngine.setFocusedDeck(RIGHT_DECK);
554 } else {
555 uiCrossfader.setDisplayMode("B");
556 }
557 return true;
558
559 case 94: // up bank
560 if (shiftOn) {
561 selectedEffect.setValue(selectedEffect.getValuei() - 1);
562 } else {
563 getTargetDeck().goPrev();
564 }
565 return true;
566
567 case 95: // down bank
568 if (shiftOn) {
569 selectedEffect.setValue(selectedEffect.getValuei() + 1);
570 } else {
571 getTargetDeck().goNext();
572 }
573 return true;
574
575 case 98: // shift
576 shiftOn = true;
577 return true;
578
579 case 99: // tap tempo
580 lx.tempo.tap();
581 return true;
582
583 case 100: // nudge+
584 lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1));
585 return true;
586
587 case 101: // nudge-
588 lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1));
589 return true;
590
591 case 62: // Detail View / red 5
592 releaseEffect = getSelectedEffect();
593 if (releaseEffect.isMomentary()) {
594 releaseEffect.enable();
595 } else {
596 releaseEffect.toggle();
597 }
598 return true;
599
600 case 63: // rec quantize / red 6
601 getSelectedEffect().disable();
602 return true;
603 }
604
605 return false;
606 }
607
608 protected boolean handleNoteOff(Note note) {
609 if (handleGridNoteOff(note)) {
610 return true;
611 }
612
613 int nPitch = note.getPitch();
614 int nChan = note.getChannel();
615
616 switch (nPitch) {
617
618 case 49: // SOLO/CUE
619 switch (nChan) {
620 case 4:
621 effects.colorFucker.mono.setNormalized(0);
622 return true;
623 case 5:
624 effects.colorFucker.invert.setNormalized(0);
625 return true;
626 case 6:
627 lx.setBaseHue(lx.getBaseHue());
628 return true;
629 }
630 break;
631
632 case 52: // CLIP STOP
633 if (nChan < PresetManager.NUM_PRESETS) {
634 if (shiftOn) {
635 presetManager.store(getTargetDeck(), nChan);
636 } else {
637 presetManager.select(getTargetDeck(), nChan);
638 }
639 }
640 return true;
641
642 case 90: // SEND C
643 long tapDelta = millis() - tap1;
644 if (lbtwn(tapDelta,5000,300*1000)) { // hackish tapping mechanism
645 double bpm = 32.*60000./(tapDelta);
646 while (bpm < 20) bpm*=2;
647 while (bpm > 40) bpm/=2;
648 lx.tempo.setBpm(bpm);
649 lx.tempo.trigger();
650 tap1 = 0;
651 println("Tap Set - " + bpm + " bpm");
652 }
653 return true;
654
655 case 63: // rec quantize / RED 6
656 if (releaseEffect != null) {
657 if (releaseEffect.isMomentary()) {
658 releaseEffect.disable();
659 }
660 }
661 return true;
662
663 case 98: // shift
664 shiftOn = false;
665 return true;
666 }
667
668 return false;
669 }
670 }
671
672 class KorgNanoKontrolMidiInput extends GenericDeviceMidiInput {
673
674 KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
675 super(midiEngine, d);
676 }
677
678 protected boolean handleControllerChange(rwmidi.Controller cc) {
679 int number = cc.getCC();
680 if (number >= 16 && number <= 23) {
681 int parameterIndex = number - 16;
682 List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters();
683 if (parameterIndex < parameters.size()) {
684 setNormalized(parameters.get(parameterIndex), cc.getValue() / 127.);
685 return true;
686 }
687 }
688
689 if (cc.getValue() == 127) {
690 switch (number) {
691
692 case 58: // Left track
693 midiEngine.setFocusedDeck(LEFT_DECK);
694 return true;
695
696 case 59: // Right track
697 midiEngine.setFocusedDeck(RIGHT_DECK);
698 return true;
699
700 case 43: // Left chevron
701 midiEngine.getFocusedDeck().goPrev();
702 return true;
703
704 case 44: // Right chevron
705 midiEngine.getFocusedDeck().goNext();
706 return true;
707 }
708 }
709
710 return false;
711 }
712 }
713
714 class APC40MidiOutput implements LXParameterListener, GridOutput {
715
716 private final MidiEngine midiEngine;
717 private final MidiOutput output;
718 private LXPattern focusedPattern = null;
719 private LXEffect focusedEffect = null;
720 private final LXDeck targetDeck;
721
722 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) {
723 this(midiEngine, device, -1);
724 }
725
726 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device, int deckIndex) {
727 this.midiEngine = midiEngine;
728 output = device.createOutput();
729 targetDeck = (deckIndex < 0) ? null : lx.engine.getDecks().get(deckIndex);
730 setDPatternOutputs();
731 if (targetDeck != null) {
732 midiEngine.addListener(new MidiEngineListener() {
733 public void onFocusedDeck(int deckIndex) {
734 resetPatternParameters();
735 for (int i = 0; i < 8; ++i) {
736 output.sendNoteOn(i, 52, 0);
737 }
738 }
739 });
740 }
741 selectedEffect.addListener(new LXParameterListener() {
742 public void onParameterChanged(LXParameter parameter) {
743 resetEffectParameters();
744 }
745 });
746 LXDeck.Listener deckListener = new LXDeck.AbstractListener() {
747 public void patternDidChange(LXDeck deck, LXPattern pattern) {
748 if (deck == getTargetDeck()) {
749 resetPatternParameters();
750 }
751 }
752 };
753 for (LXDeck d : lx.engine.getDecks()) {
754 if (targetDeck == null || d == targetDeck) {
755 d.addListener(deckListener);
756 }
757 }
758 presetManager.addListener(new PresetListener() {
759 public void onPresetSelected(LXDeck deck, Preset preset) {
760 if (deck == getTargetDeck()) {
761 for (int i = 0; i < 8; ++i) {
762 output.sendNoteOn(i, 52, (preset.index == i) ? 1 : 0);
763 }
764 }
765 }
766 public void onPresetDirty(LXDeck deck, Preset preset) {
767 if (deck == getTargetDeck()) {
768 output.sendNoteOn(preset.index, 52, 2);
769 }
770 }
771 public void onPresetStored(LXDeck deck, Preset preset) {
772 onPresetSelected(deck, preset);
773 }
774 });
775 resetParameters();
776 midiEngine.grid.addOutput(this);
777
778 lx.cycleBaseHue(60000);
779 output.sendNoteOn(6, 49, 127);
780
781 // Turn off the track selection lights and preset selectors
782 for (int i = 0; i < 8; ++i) {
783 output.sendNoteOn(i, 51, 0);
784 output.sendNoteOn(i, 52, 0);
785 }
786
787 // Turn off the MASTER selector
788 output.sendNoteOn(0, 80, 0);
789 }
790
791 private void setDPatternOutputs() {
792 for (LXDeck deck : lx.engine.getDecks()) {
793 if (targetDeck == null || deck == targetDeck) {
794 for (LXPattern pattern : deck.getPatterns()) {
795 if (pattern instanceof DPat) {
796 ((DPat)pattern).setAPCOutput(output);
797 }
798 }
799 }
800 }
801 }
802
803 protected LXDeck getTargetDeck() {
804 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
805 }
806
807 private void resetParameters() {
808 resetPatternParameters();
809 resetEffectParameters();
810 }
811
812 private void resetPatternParameters() {
813 LXPattern newPattern = getTargetDeck().getActivePattern();
814 if (newPattern == focusedPattern) {
815 return;
816 }
817 if (focusedPattern != null) {
818 for (LXParameter p : focusedPattern.getParameters()) {
819 ((LXListenableParameter) p).removeListener(this);
820 }
821 }
822 focusedPattern = newPattern;
823 int i = 0;
824 for (LXParameter p : focusedPattern.getParameters()) {
825 ((LXListenableParameter) p).addListener(this);
826 sendKnob(i++, p);
827 }
828 while (i < 12) {
829 sendKnob(i++, 0);
830 }
831 if (focusedPattern instanceof DPat) {
832 ((DPat)focusedPattern).updateLights();
833 } else {
834 for (int j = 0; j < 8; ++j) {
835 output.sendNoteOn(j, 48, 0);
836 }
837 for (int row = 0; row < 7; ++row) {
838 for (int col = 0; col < 8; ++col) {
839 setGridState(row, col, 0);
840 }
841 }
842 }
843 }
844
845 private void resetEffectParameters() {
846 LXEffect newEffect = getSelectedEffect();
847 if (newEffect == focusedEffect) {
848 return;
849 }
850 if (focusedEffect != null) {
851 for (LXParameter p : focusedPattern.getParameters()) {
852 ((LXListenableParameter) p).removeListener(this);
853 }
854 }
855 focusedEffect = newEffect;
856 int i = 0;
857 for (LXParameter p : focusedEffect.getParameters()) {
858 ((LXListenableParameter) p).addListener(this);
859 sendKnob(12 + i++, p);
860 }
861 while (i < 4) {
862 sendKnob(12 + i++, 0);
863 }
864 }
865
866 private void sendKnob(int i, LXParameter p) {
867 float pv = constrain(p.getValuef(), 0, 1);
868 if (p instanceof LXNormalizedParameter) {
869 pv = ((LXNormalizedParameter)p).getNormalizedf();
870 }
871 sendKnob(i, (int) (pv * 127.));
872 }
873
874 private void sendKnob(int i, int value) {
875 if (i < 8) {
876 output.sendController(0, 48+i, value);
877 } else if (i < 16) {
878 output.sendController(0, 8+i, value);
879 }
880 }
881
882 public void onParameterChanged(LXParameter parameter) {
883 int i = 0;
884 for (LXParameter p : focusedPattern.getParameters()) {
885 if (p == parameter) {
886 sendKnob(i, p);
887 break;
888 }
889 ++i;
890 }
891 i = 12;
892 for (LXParameter p : focusedEffect.getParameters()) {
893 if (p == parameter) {
894 sendKnob(i, p);
895 break;
896 }
897 ++i;
898 }
899 }
900
901 public void setGridState(int row, int col, int state) {
902 if (col < 8 && row < 5) {
903 output.sendNoteOn(col, 53+row, state);
904 }
905 }
906 }
907
908 class ArturiaMinilabMidiInput extends GenericDeviceMidiInput {
909 ArturiaMinilabMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
910 super(midiEngine, d);
911 }
912
913 protected boolean handleControllerChange(rwmidi.Controller cc) {
914 int parameterIndex = -1;
915 switch (cc.getCC()) {
916 case 7: parameterIndex = 0; break;
917 case 74: parameterIndex = 1; break;
918 case 71: parameterIndex = 2; break;
919 case 76: parameterIndex = 3; break;
920 case 114: parameterIndex = 4; break;
921 case 18: parameterIndex = 5; break;
922 case 19: parameterIndex = 6; break;
923 case 16: parameterIndex = 7; break;
924
925 case 75:
926 float val = effects.colorFucker.hueShift.getValuef();
927 val += (cc.getValue() - 64) / 256.;
928 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
929 break;
930 }
931 if (parameterIndex >= 0) {
932 List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters();
933 if (parameterIndex < parameters.size()) {
934 LXParameter p = parameters.get(parameterIndex);
935 float curVal = p.getValuef();
936 curVal += (cc.getValue() - 64) / 127.;
937 setNormalized(p, constrain(curVal, 0, 1));
938 }
939 }
940 return false;
941 }
942 }
943
944 interface GridOutput {
945 public static final int OFF = 0;
946 public static final int GREEN = 1;
947 public static final int GREEN_BLINK = 2;
948 public static final int RED = 3;
949 public static final int RED_BLINK = 4;
950 public static final int YELLOW = 5;
951 public static final int YELLOW_BLINK = 6;
952 public static final int ON = 127;
953
954 public void setGridState(int row, int col, int state);
955 }
956
957 class GridController {
958 private final List<GridOutput> outputs = new ArrayList<GridOutput>();
959
960 private final MidiEngine midiEngine;
961
962 GridController(MidiEngine midiEngine) {
963 this.midiEngine = midiEngine;
964 }
965
966 public void addOutput(GridOutput output) {
967 outputs.add(output);
968 }
969
970 public boolean gridPressed(int row, int col) {
971 return midiEngine.getFocusedPattern().gridPressed(row, col);
972 }
973
974 public boolean gridReleased(int row, int col) {
975 return midiEngine.getFocusedPattern().gridReleased(row, col);
976 }
977
978 public void setState(int row, int col, int state) {
979 for (GridOutput g : outputs) {
980 g.setGridState(row, col, state);
981 }
982 }
983 }
984