2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
9 * EXPERTS ONLY!! EXPERTS ONLY!!
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
19 interface MidiEngineListener {
20 public void onFocusedDeck(int deckIndex);
25 public final GridController grid;
26 private final List<MidiEngineListener> listeners = new ArrayList<MidiEngineListener>();
27 private final List<SCMidiInput> midiControllers = new ArrayList<SCMidiInput>();
29 public MidiEngine addListener(MidiEngineListener l) {
34 public MidiEngine removeListener(MidiEngineListener l) {
39 private SCMidiInput midiQwertyKeys;
40 private SCMidiInput midiQwertyAPC;
42 private int activeDeckIndex = 0;
45 grid = new GridController(this);
46 midiControllers.add(midiQwertyKeys = new VirtualKeyMidiInput(this, VirtualKeyMidiInput.KEYS));
47 midiControllers.add(midiQwertyAPC = new VirtualKeyMidiInput(this, VirtualKeyMidiInput.APC));
49 for (MidiInputDevice device : RWMidi.getInputDevices()) {
50 if (device.getName().contains("APC")) {
56 for (MidiInputDevice device : RWMidi.getInputDevices()) {
57 if (device.getName().contains("APC")) {
59 if (apcCount > 1 && apcIndex < 2) {
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));
69 device.getName().contains("KEYBOARD KORG") ||
70 device.getName().contains("Bus 1 Apple");
71 midiControllers.add(new GenericDeviceMidiInput(this, device).setEnabled(enabled));
76 for (MidiOutputDevice device : RWMidi.getOutputDevices()) {
77 if (device.getName().contains("APC")) {
79 if (apcCount > 1 && apcIndex < 2) {
82 new APC40MidiOutput(this, device, apcDeck);
87 public List<SCMidiInput> getControllers() {
88 return this.midiControllers;
91 public LXDeck getFocusedDeck() {
92 return lx.engine.getDeck(activeDeckIndex);
95 public SCPattern getFocusedPattern() {
96 return (SCPattern) getFocusedDeck().getActivePattern();
99 public MidiEngine setFocusedDeck(int deckIndex) {
100 if (this.activeDeckIndex != deckIndex) {
101 this.activeDeckIndex = deckIndex;
102 for (MidiEngineListener listener : listeners) {
103 listener.onFocusedDeck(deckIndex);
109 public boolean isQwertyEnabled() {
110 return midiQwertyKeys.isEnabled() || midiQwertyAPC.isEnabled();
114 public interface SCMidiInputListener {
115 public void onEnabled(SCMidiInput controller, boolean enabled);
118 public abstract class SCMidiInput extends UIScrollList.AbstractItem {
120 protected boolean enabled = false;
121 private final String name;
123 protected final MidiEngine midiEngine;
125 final List<SCMidiInputListener> listeners = new ArrayList<SCMidiInputListener>();
127 protected SCMidiInput(MidiEngine midiEngine, String name) {
128 this.midiEngine = midiEngine;
132 public SCMidiInput addListener(SCMidiInputListener l) {
137 public SCMidiInput removeListener(SCMidiInputListener l) {
142 public String getLabel() {
146 public boolean isEnabled() {
150 public boolean isSelected() {
154 public void onMousePressed() {
155 setEnabled(!enabled);
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);
168 private boolean logMidi() {
169 return (uiMidi != null) && uiMidi.logMidi();
172 protected SCPattern getTargetPattern() {
173 return midiEngine.getFocusedPattern();
176 final void programChangeReceived(ProgramChange pc) {
181 println(getLabel() + " :: Program Change :: " + pc.getNumber());
183 handleProgramChange(pc);
186 final void controllerChangeReceived(rwmidi.Controller cc) {
191 println(getLabel() + " :: Controller :: " + cc.getChannel() + " :: " + cc.getCC() + ":" + cc.getValue());
193 if (!handleControllerChange(cc)) {
194 getTargetPattern().controllerChange(cc);
198 final void noteOnReceived(Note note) {
203 println(getLabel() + " :: Note On :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity());
205 if (!handleNoteOn(note)) {
206 getTargetPattern().noteOn(note);
210 final void noteOffReceived(Note note) {
215 println(getLabel() + " :: Note Off :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity());
217 if (!handleNoteOff(note)) {
218 getTargetPattern().noteOff(note);
222 protected void setNormalized(LXParameter parameter, float value) {
223 if (parameter != null) {
224 if (parameter instanceof BasicParameter) {
225 ((BasicParameter)parameter).setNormalized(value);
227 parameter.setValue(value);
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; }
239 public class VirtualKeyMidiInput extends SCMidiInput {
241 public static final int KEYS = 1;
242 public static final int APC = 2;
244 private final int mode;
246 private int octaveShift = 0;
251 NoteMeta(int channel, int number) {
252 this.channel = channel;
253 this.number = number;
257 final Map<Character, NoteMeta> keyToNote = new HashMap<Character, NoteMeta>();
259 VirtualKeyMidiInput(MidiEngine midiEngine, int mode) {
260 super(midiEngine, "QWERTY (" + (mode == APC ? "APC" : "Key") + " Mode)");
267 registerKeyEvent(this);
270 private void mapAPC() {
297 private void mapKeys() {
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++);
316 void mapNote(char ch, int channel, int number) {
317 keyToNote.put(ch, new NoteMeta(channel, number));
320 public void keyEvent(KeyEvent e) {
324 char c = Character.toLowerCase(e.getKeyChar());
325 NoteMeta nm = keyToNote.get(c);
328 case KeyEvent.KEY_PRESSED:
329 noteOnReceived(new Note(Note.NOTE_ON, nm.channel, nm.number + octaveShift*12, 127));
331 case KeyEvent.KEY_RELEASED:
332 noteOffReceived(new Note(Note.NOTE_OFF, nm.channel, nm.number + octaveShift*12, 0));
336 if ((mode == KEYS) && (e.getID() == KeyEvent.KEY_PRESSED)) {
339 octaveShift = constrain(octaveShift-1, -4, 4);
342 octaveShift = constrain(octaveShift+1, -4, 4);
349 public class GenericDeviceMidiInput extends SCMidiInput {
350 GenericDeviceMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
351 super(midiEngine, d.getName().replace("Unknown vendor",""));
356 public class APC40MidiInput extends GenericDeviceMidiInput {
358 private boolean shiftOn = false;
359 private LXEffect releaseEffect = null;
360 final private LXDeck targetDeck;
362 APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d) {
363 this(midiEngine, d, -1);
366 APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d, int deckIndex) {
367 super(midiEngine, d);
368 targetDeck = (deckIndex < 0) ? null : lx.engine.getDecks().get(deckIndex);
371 protected LXDeck getTargetDeck() {
372 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
375 protected SCPattern getTargetPattern() {
376 if (targetDeck != null) {
377 return (SCPattern) (targetDeck.getActivePattern());
379 return super.getTargetPattern();
382 private class GridPosition {
383 public final int row, col;
384 GridPosition(int r, int c) {
390 private GridPosition getGridPosition(Note note) {
391 int channel = note.getChannel();
392 int pitch = note.getPitch();
394 if (pitch >= 53 && pitch <=57) return new GridPosition(pitch-53, channel);
399 private boolean handleGridNoteOn(Note note) {
400 GridPosition p = getGridPosition(note);
402 return midiEngine.grid.gridPressed(p.row, p.col);
407 private boolean handleGridNoteOff(Note note) {
408 GridPosition p = getGridPosition(note);
410 return midiEngine.grid.gridReleased(p.row, p.col);
415 protected boolean handleControllerChange(rwmidi.Controller cc) {
416 int channel = cc.getChannel();
417 int number = cc.getCC();
418 float value = cc.getValue() / 127.;
424 uiSpeed.speed.setNormalized(0.5 - value*0.5);
427 effects.colorFucker.desat.setNormalized(value);
430 effects.colorFucker.desat.setNormalized(value);
433 effects.blur.amount.setNormalized(value);
436 effects.quantize.amount.setNormalized(value);
443 effects.colorFucker.level.setNormalized(value);
448 lx.engine.getDeck(GLucose.RIGHT_DECK).getFader().setNormalized(value);
453 float val = effects.colorFucker.hueShift.getValuef();
454 int cv = cc.getValue();
460 val += (cv - 64) / 500.;
461 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
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);
471 if (parameterIndex >= 0) {
472 List<LXParameter> parameters = getTargetPattern().getParameters();
473 if (parameterIndex < parameters.size()) {
474 setNormalized(parameters.get(parameterIndex), value);
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);
491 private long tap1 = 0;
493 private boolean lbtwn(long a, long b, long c) {
494 return a >= b && a <= c;
497 protected boolean handleNoteOn(Note note) {
498 if (handleGridNoteOn(note)) {
502 int nPitch = note.getPitch();
503 int nChan = note.getChannel();
509 effects.colorFucker.mono.setNormalized(1);
512 effects.colorFucker.invert.setNormalized(1);
515 lx.cycleBaseHue(60000);
521 effects.boom.trigger();
525 effects.flash.trigger();
529 getTargetPattern().reset();
533 // dan's dirty tapping mechanism
540 midiEngine.setFocusedDeck(GLucose.LEFT_DECK);
542 uiCrossfader.setDisplayMode("A");
547 uiCrossfader.setDisplayMode("COMP");
552 midiEngine.setFocusedDeck(GLucose.RIGHT_DECK);
554 uiCrossfader.setDisplayMode("B");
560 glucose.incrementSelectedEffectBy(-1);
562 getTargetDeck().goPrev();
566 case 95: // down bank
568 glucose.incrementSelectedEffectBy(1);
570 getTargetDeck().goNext();
578 case 99: // tap tempo
583 lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1));
587 lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1));
590 case 62: // Detail View / red 5
591 releaseEffect = glucose.getSelectedEffect();
592 if (releaseEffect.isMomentary()) {
593 releaseEffect.enable();
595 releaseEffect.toggle();
599 case 63: // rec quantize / red 6
600 glucose.getSelectedEffect().disable();
607 protected boolean handleNoteOff(Note note) {
608 if (handleGridNoteOff(note)) {
612 int nPitch = note.getPitch();
613 int nChan = note.getChannel();
620 effects.colorFucker.mono.setNormalized(0);
623 effects.colorFucker.invert.setNormalized(0);
626 lx.setBaseHue(lx.getBaseHue());
631 case 52: // CLIP STOP
632 if (nChan < PresetManager.NUM_PRESETS) {
634 presetManager.store(getTargetDeck(), nChan);
636 presetManager.select(getTargetDeck(), nChan);
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);
650 println("Tap Set - " + bpm + " bpm");
654 case 63: // rec quantize / RED 6
655 if (releaseEffect != null) {
656 if (releaseEffect.isMomentary()) {
657 releaseEffect.disable();
671 class KorgNanoKontrolMidiInput extends GenericDeviceMidiInput {
673 KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
674 super(midiEngine, d);
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.);
688 if (cc.getValue() == 127) {
691 case 58: // Left track
692 midiEngine.setFocusedDeck(GLucose.LEFT_DECK);
695 case 59: // Right track
696 midiEngine.setFocusedDeck(GLucose.RIGHT_DECK);
699 case 43: // Left chevron
700 midiEngine.getFocusedDeck().goPrev();
703 case 44: // Right chevron
704 midiEngine.getFocusedDeck().goNext();
713 class APC40MidiOutput implements LXParameterListener, GridOutput {
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;
721 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) {
722 this(midiEngine, device, -1);
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);
740 glucose.addEffectListener(new GLucose.EffectListener() {
741 public void effectSelected(LXEffect effect) {
742 resetEffectParameters();
745 LXDeck.Listener deckListener = new LXDeck.AbstractListener() {
746 public void patternDidChange(LXDeck deck, LXPattern pattern) {
747 if (deck == getTargetDeck()) {
748 resetPatternParameters();
752 for (LXDeck d : lx.engine.getDecks()) {
753 if (targetDeck == null || d == targetDeck) {
754 d.addListener(deckListener);
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);
765 public void onPresetDirty(LXDeck deck, Preset preset) {
766 if (deck == getTargetDeck()) {
767 output.sendNoteOn(preset.index, 52, 2);
770 public void onPresetStored(LXDeck deck, Preset preset) {
771 onPresetSelected(deck, preset);
775 midiEngine.grid.addOutput(this);
777 lx.cycleBaseHue(60000);
778 output.sendNoteOn(6, 49, 127);
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);
786 // Turn off the MASTER selector
787 output.sendNoteOn(0, 80, 0);
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);
802 protected LXDeck getTargetDeck() {
803 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
806 private void resetParameters() {
807 resetPatternParameters();
808 resetEffectParameters();
811 private void resetPatternParameters() {
812 LXPattern newPattern = getTargetDeck().getActivePattern();
813 if (newPattern == focusedPattern) {
816 if (focusedPattern != null) {
817 for (LXParameter p : focusedPattern.getParameters()) {
818 ((LXListenableParameter) p).removeListener(this);
821 focusedPattern = newPattern;
823 for (LXParameter p : focusedPattern.getParameters()) {
824 ((LXListenableParameter) p).addListener(this);
830 if (focusedPattern instanceof DPat) {
831 ((DPat)focusedPattern).updateLights();
833 for (int j = 0; j < 8; ++j) {
834 output.sendNoteOn(j, 48, 0);
836 for (int row = 0; row < 7; ++row) {
837 for (int col = 0; col < 8; ++col) {
838 setGridState(row, col, 0);
844 private void resetEffectParameters() {
845 LXEffect newEffect = glucose.getSelectedEffect();
846 if (newEffect == focusedEffect) {
849 if (focusedEffect != null) {
850 for (LXParameter p : focusedPattern.getParameters()) {
851 ((LXListenableParameter) p).removeListener(this);
854 focusedEffect = newEffect;
856 for (LXParameter p : focusedEffect.getParameters()) {
857 ((LXListenableParameter) p).addListener(this);
858 sendKnob(12 + i++, p);
861 sendKnob(12 + i++, 0);
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();
870 sendKnob(i, (int) (pv * 127.));
873 private void sendKnob(int i, int value) {
875 output.sendController(0, 48+i, value);
877 output.sendController(0, 8+i, value);
881 public void onParameterChanged(LXParameter parameter) {
883 for (LXParameter p : focusedPattern.getParameters()) {
884 if (p == parameter) {
891 for (LXParameter p : focusedEffect.getParameters()) {
892 if (p == parameter) {
900 public void setGridState(int row, int col, int state) {
901 if (col < 8 && row < 5) {
902 output.sendNoteOn(col, 53+row, state);
907 class ArturiaMinilabMidiInput extends GenericDeviceMidiInput {
908 ArturiaMinilabMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
909 super(midiEngine, d);
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;
925 float val = effects.colorFucker.hueShift.getValuef();
926 val += (cc.getValue() - 64) / 256.;
927 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
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));
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;
953 public void setGridState(int row, int col, int state);
956 class GridController {
957 private final List<GridOutput> outputs = new ArrayList<GridOutput>();
959 private final MidiEngine midiEngine;
961 GridController(MidiEngine midiEngine) {
962 this.midiEngine = midiEngine;
965 public void addOutput(GridOutput output) {
969 public boolean gridPressed(int row, int col) {
970 return midiEngine.getFocusedPattern().gridPressed(row, col);
973 public boolean gridReleased(int row, int col) {
974 return midiEngine.getFocusedPattern().gridReleased(row, col);
977 public void setState(int row, int col, int state) {
978 for (GridOutput g : outputs) {
979 g.setGridState(row, col, state);