Merge branch 'master' of https://github.com/sugarcubes/SugarCubes
[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 AbstractScrollItem {
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(GLucose.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 List<LXParameter> parameters = glucose.getSelectedEffect().getParameters();
482 if (effectIndex < parameters.size()) {
483 setNormalized(parameters.get(effectIndex), value);
484 return true;
485 }
486 }
487
488 return false;
489 }
490
491 private long tap1 = 0;
492
493 private boolean lbtwn(long a, long b, long c) {
494 return a >= b && a <= c;
495 }
496
497 protected boolean handleNoteOn(Note note) {
498 if (handleGridNoteOn(note)) {
499 return true;
500 }
501
502 int nPitch = note.getPitch();
503 int nChan = note.getChannel();
504 switch (nPitch) {
505
506 case 49: // SOLO/CUE
507 switch (nChan) {
508 case 4:
509 effects.colorFucker.mono.setNormalized(1);
510 return true;
511 case 5:
512 effects.colorFucker.invert.setNormalized(1);
513 return true;
514 case 6:
515 lx.cycleBaseHue(60000);
516 return true;
517 }
518 break;
519
520 case 82: // scene 1
521 effects.boom.trigger();
522 return true;
523
524 case 83: // scene 2
525 effects.flash.trigger();
526 return true;
527
528 case 84: // scene 3
529 getTargetPattern().reset();
530 return true;
531
532 case 90:
533 // dan's dirty tapping mechanism
534 lx.tempo.trigger();
535 tap1 = millis();
536 return true;
537
538 case 91: // play
539 if (shiftOn) {
540 midiEngine.setFocusedDeck(GLucose.LEFT_DECK);
541 } else {
542 uiCrossfader.setDisplayMode("A");
543 }
544 return true;
545
546 case 92: // stop
547 uiCrossfader.setDisplayMode("COMP");
548 return true;
549
550 case 93: // rec
551 if (shiftOn) {
552 midiEngine.setFocusedDeck(GLucose.RIGHT_DECK);
553 } else {
554 uiCrossfader.setDisplayMode("B");
555 }
556 return true;
557
558 case 94: // up bank
559 if (shiftOn) {
560 glucose.incrementSelectedEffectBy(-1);
561 } else {
562 getTargetDeck().goPrev();
563 }
564 return true;
565
566 case 95: // down bank
567 if (shiftOn) {
568 glucose.incrementSelectedEffectBy(1);
569 } else {
570 getTargetDeck().goNext();
571 }
572 return true;
573
574 case 98: // shift
575 shiftOn = true;
576 return true;
577
578 case 99: // tap tempo
579 lx.tempo.tap();
580 return true;
581
582 case 100: // nudge+
583 lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1));
584 return true;
585
586 case 101: // nudge-
587 lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1));
588 return true;
589
590 case 62: // Detail View / red 5
591 releaseEffect = glucose.getSelectedEffect();
592 if (releaseEffect.isMomentary()) {
593 releaseEffect.enable();
594 } else {
595 releaseEffect.toggle();
596 }
597 return true;
598
599 case 63: // rec quantize / red 6
600 glucose.getSelectedEffect().disable();
601 return true;
602 }
603
604 return false;
605 }
606
607 protected boolean handleNoteOff(Note note) {
608 if (handleGridNoteOff(note)) {
609 return true;
610 }
611
612 int nPitch = note.getPitch();
613 int nChan = note.getChannel();
614
615 switch (nPitch) {
616
617 case 49: // SOLO/CUE
618 switch (nChan) {
619 case 4:
620 effects.colorFucker.mono.setNormalized(0);
621 return true;
622 case 5:
623 effects.colorFucker.invert.setNormalized(0);
624 return true;
625 case 6:
626 lx.setBaseHue(lx.getBaseHue());
627 return true;
628 }
629 break;
630
631 case 52: // CLIP STOP
632 if (nChan < PresetManager.NUM_PRESETS) {
633 if (shiftOn) {
634 presetManager.store(getTargetDeck(), nChan);
635 } else {
636 presetManager.select(getTargetDeck(), nChan);
637 }
638 }
639 return true;
640
641 case 90: // SEND C
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 }
652 return true;
653
654 case 63: // rec quantize / RED 6
655 if (releaseEffect != null) {
656 if (releaseEffect.isMomentary()) {
657 releaseEffect.disable();
658 }
659 }
660 return true;
661
662 case 98: // shift
663 shiftOn = false;
664 return true;
665 }
666
667 return false;
668 }
669 }
670
671 class KorgNanoKontrolMidiInput extends GenericDeviceMidiInput {
672
673 KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
674 super(midiEngine, d);
675 }
676
677 protected boolean handleControllerChange(rwmidi.Controller cc) {
678 int number = cc.getCC();
679 if (number >= 16 && number <= 23) {
680 int parameterIndex = number - 16;
681 List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters();
682 if (parameterIndex < parameters.size()) {
683 setNormalized(parameters.get(parameterIndex), cc.getValue() / 127.);
684 return true;
685 }
686 }
687
688 if (cc.getValue() == 127) {
689 switch (number) {
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
700 midiEngine.getFocusedDeck().goPrev();
701 return true;
702
703 case 44: // Right chevron
704 midiEngine.getFocusedDeck().goNext();
705 return true;
706 }
707 }
708
709 return false;
710 }
711 }
712
713 class APC40MidiOutput implements LXParameterListener, GridOutput {
714
715 private final MidiEngine midiEngine;
716 private final MidiOutput output;
717 private LXPattern focusedPattern = null;
718 private LXEffect focusedEffect = null;
719 private final LXDeck targetDeck;
720
721 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) {
722 this(midiEngine, device, -1);
723 }
724
725 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device, int deckIndex) {
726 this.midiEngine = midiEngine;
727 output = device.createOutput();
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();
734 for (int i = 0; i < 8; ++i) {
735 output.sendNoteOn(i, 52, 0);
736 }
737 }
738 });
739 }
740 glucose.addEffectListener(new GLucose.EffectListener() {
741 public void effectSelected(LXEffect effect) {
742 resetEffectParameters();
743 }
744 });
745 LXDeck.Listener deckListener = new LXDeck.AbstractListener() {
746 public void patternDidChange(LXDeck deck, LXPattern pattern) {
747 if (deck == getTargetDeck()) {
748 resetPatternParameters();
749 }
750 }
751 };
752 for (LXDeck d : lx.engine.getDecks()) {
753 if (targetDeck == null || d == targetDeck) {
754 d.addListener(deckListener);
755 }
756 }
757 presetManager.addListener(new PresetListener() {
758 public void onPresetSelected(LXDeck deck, Preset preset) {
759 if (deck == getTargetDeck()) {
760 for (int i = 0; i < 8; ++i) {
761 output.sendNoteOn(i, 52, (preset.index == i) ? 1 : 0);
762 }
763 }
764 }
765 public void onPresetDirty(LXDeck deck, Preset preset) {
766 if (deck == getTargetDeck()) {
767 output.sendNoteOn(preset.index, 52, 2);
768 }
769 }
770 public void onPresetStored(LXDeck deck, Preset preset) {
771 onPresetSelected(deck, preset);
772 }
773 });
774 resetParameters();
775 midiEngine.grid.addOutput(this);
776
777 lx.cycleBaseHue(60000);
778 output.sendNoteOn(6, 49, 127);
779
780 // Turn off the track selection lights and preset selectors
781 for (int i = 0; i < 8; ++i) {
782 output.sendNoteOn(i, 51, 0);
783 output.sendNoteOn(i, 52, 0);
784 }
785
786 // Turn off the MASTER selector
787 output.sendNoteOn(0, 80, 0);
788 }
789
790 private void setDPatternOutputs() {
791 for (LXDeck deck : lx.engine.getDecks()) {
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
802 protected LXDeck getTargetDeck() {
803 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
804 }
805
806 private void resetParameters() {
807 resetPatternParameters();
808 resetEffectParameters();
809 }
810
811 private void resetPatternParameters() {
812 LXPattern newPattern = getTargetDeck().getActivePattern();
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 }
830 if (focusedPattern instanceof DPat) {
831 ((DPat)focusedPattern).updateLights();
832 } else {
833 for (int j = 0; j < 8; ++j) {
834 output.sendNoteOn(j, 48, 0);
835 }
836 for (int row = 0; row < 7; ++row) {
837 for (int col = 0; col < 8; ++col) {
838 setGridState(row, col, 0);
839 }
840 }
841 }
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 float pv = constrain(p.getValuef(), 0, 1);
867 if (p instanceof LXNormalizedParameter) {
868 pv = ((LXNormalizedParameter)p).getNormalizedf();
869 }
870 sendKnob(i, (int) (pv * 127.));
871 }
872
873 private void sendKnob(int i, int value) {
874 if (i < 8) {
875 output.sendController(0, 48+i, value);
876 } else if (i < 16) {
877 output.sendController(0, 8+i, value);
878 }
879 }
880
881 public void onParameterChanged(LXParameter parameter) {
882 int i = 0;
883 for (LXParameter p : focusedPattern.getParameters()) {
884 if (p == parameter) {
885 sendKnob(i, p);
886 break;
887 }
888 ++i;
889 }
890 i = 12;
891 for (LXParameter p : focusedEffect.getParameters()) {
892 if (p == parameter) {
893 sendKnob(i, p);
894 break;
895 }
896 ++i;
897 }
898 }
899
900 public void setGridState(int row, int col, int state) {
901 if (col < 8 && row < 5) {
902 output.sendNoteOn(col, 53+row, state);
903 }
904 }
905 }
906
907 class ArturiaMinilabMidiInput extends GenericDeviceMidiInput {
908 ArturiaMinilabMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
909 super(midiEngine, d);
910 }
911
912 protected boolean handleControllerChange(rwmidi.Controller cc) {
913 int parameterIndex = -1;
914 switch (cc.getCC()) {
915 case 7: parameterIndex = 0; break;
916 case 74: parameterIndex = 1; break;
917 case 71: parameterIndex = 2; break;
918 case 76: parameterIndex = 3; break;
919 case 114: parameterIndex = 4; break;
920 case 18: parameterIndex = 5; break;
921 case 19: parameterIndex = 6; break;
922 case 16: parameterIndex = 7; break;
923
924 case 75:
925 float val = effects.colorFucker.hueShift.getValuef();
926 val += (cc.getValue() - 64) / 256.;
927 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
928 break;
929 }
930 if (parameterIndex >= 0) {
931 List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters();
932 if (parameterIndex < parameters.size()) {
933 LXParameter p = parameters.get(parameterIndex);
934 float curVal = p.getValuef();
935 curVal += (cc.getValue() - 64) / 127.;
936 setNormalized(p, constrain(curVal, 0, 1));
937 }
938 }
939 return false;
940 }
941 }
942
943 interface GridOutput {
944 public static final int OFF = 0;
945 public static final int GREEN = 1;
946 public static final int GREEN_BLINK = 2;
947 public static final int RED = 3;
948 public static final int RED_BLINK = 4;
949 public static final int YELLOW = 5;
950 public static final int YELLOW_BLINK = 6;
951 public static final int ON = 127;
952
953 public void setGridState(int row, int col, int state);
954 }
955
956 class GridController {
957 private final List<GridOutput> outputs = new ArrayList<GridOutput>();
958
959 private final MidiEngine midiEngine;
960
961 GridController(MidiEngine midiEngine) {
962 this.midiEngine = midiEngine;
963 }
964
965 public void addOutput(GridOutput output) {
966 outputs.add(output);
967 }
968
969 public boolean gridPressed(int row, int col) {
970 return midiEngine.getFocusedPattern().gridPressed(row, col);
971 }
972
973 public boolean gridReleased(int row, int col) {
974 return midiEngine.getFocusedPattern().gridReleased(row, col);
975 }
976
977 public void setState(int row, int col, int state) {
978 for (GridOutput g : outputs) {
979 g.setGridState(row, col, state);
980 }
981 }
982 }
983