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(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 // TODO(mclsee): fix selected effect
482 List<LXParameter> parameters = getSelectedEffect().getParameters();
483 if (effectIndex < parameters.size()) {
484 setNormalized(parameters.get(effectIndex), value);
492 private long tap1 = 0;
494 private boolean lbtwn(long a, long b, long c) {
495 return a >= b && a <= c;
498 protected boolean handleNoteOn(Note note) {
499 if (handleGridNoteOn(note)) {
503 int nPitch = note.getPitch();
504 int nChan = note.getChannel();
510 effects.colorFucker.mono.setNormalized(1);
513 effects.colorFucker.invert.setNormalized(1);
516 lx.cycleBaseHue(60000);
522 effects.boom.trigger();
526 effects.flash.trigger();
530 getTargetPattern().reset();
534 // dan's dirty tapping mechanism
541 midiEngine.setFocusedDeck(LEFT_DECK);
543 uiCrossfader.setDisplayMode("A");
548 uiCrossfader.setDisplayMode("COMP");
553 midiEngine.setFocusedDeck(RIGHT_DECK);
555 uiCrossfader.setDisplayMode("B");
561 selectedEffect.setValue(selectedEffect.getValuei() - 1);
563 getTargetDeck().goPrev();
567 case 95: // down bank
569 selectedEffect.setValue(selectedEffect.getValuei() + 1);
571 getTargetDeck().goNext();
579 case 99: // tap tempo
584 lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1));
588 lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1));
591 case 62: // Detail View / red 5
592 releaseEffect = getSelectedEffect();
593 if (releaseEffect.isMomentary()) {
594 releaseEffect.enable();
596 releaseEffect.toggle();
600 case 63: // rec quantize / red 6
601 getSelectedEffect().disable();
608 protected boolean handleNoteOff(Note note) {
609 if (handleGridNoteOff(note)) {
613 int nPitch = note.getPitch();
614 int nChan = note.getChannel();
621 effects.colorFucker.mono.setNormalized(0);
624 effects.colorFucker.invert.setNormalized(0);
627 lx.setBaseHue(lx.getBaseHue());
632 case 52: // CLIP STOP
633 if (nChan < PresetManager.NUM_PRESETS) {
635 presetManager.store(getTargetDeck(), nChan);
637 presetManager.select(getTargetDeck(), nChan);
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);
651 println("Tap Set - " + bpm + " bpm");
655 case 63: // rec quantize / RED 6
656 if (releaseEffect != null) {
657 if (releaseEffect.isMomentary()) {
658 releaseEffect.disable();
672 class KorgNanoKontrolMidiInput extends GenericDeviceMidiInput {
674 KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
675 super(midiEngine, d);
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.);
689 if (cc.getValue() == 127) {
692 case 58: // Left track
693 midiEngine.setFocusedDeck(LEFT_DECK);
696 case 59: // Right track
697 midiEngine.setFocusedDeck(RIGHT_DECK);
700 case 43: // Left chevron
701 midiEngine.getFocusedDeck().goPrev();
704 case 44: // Right chevron
705 midiEngine.getFocusedDeck().goNext();
714 class APC40MidiOutput implements LXParameterListener, GridOutput {
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;
722 APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) {
723 this(midiEngine, device, -1);
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);
741 selectedEffect.addListener(new LXParameterListener() {
742 public void onParameterChanged(LXParameter parameter) {
743 resetEffectParameters();
746 LXDeck.Listener deckListener = new LXDeck.AbstractListener() {
747 public void patternDidChange(LXDeck deck, LXPattern pattern) {
748 if (deck == getTargetDeck()) {
749 resetPatternParameters();
753 for (LXDeck d : lx.engine.getDecks()) {
754 if (targetDeck == null || d == targetDeck) {
755 d.addListener(deckListener);
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);
766 public void onPresetDirty(LXDeck deck, Preset preset) {
767 if (deck == getTargetDeck()) {
768 output.sendNoteOn(preset.index, 52, 2);
771 public void onPresetStored(LXDeck deck, Preset preset) {
772 onPresetSelected(deck, preset);
776 midiEngine.grid.addOutput(this);
778 lx.cycleBaseHue(60000);
779 output.sendNoteOn(6, 49, 127);
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);
787 // Turn off the MASTER selector
788 output.sendNoteOn(0, 80, 0);
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);
803 protected LXDeck getTargetDeck() {
804 return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck();
807 private void resetParameters() {
808 resetPatternParameters();
809 resetEffectParameters();
812 private void resetPatternParameters() {
813 LXPattern newPattern = getTargetDeck().getActivePattern();
814 if (newPattern == focusedPattern) {
817 if (focusedPattern != null) {
818 for (LXParameter p : focusedPattern.getParameters()) {
819 ((LXListenableParameter) p).removeListener(this);
822 focusedPattern = newPattern;
824 for (LXParameter p : focusedPattern.getParameters()) {
825 ((LXListenableParameter) p).addListener(this);
831 if (focusedPattern instanceof DPat) {
832 ((DPat)focusedPattern).updateLights();
834 for (int j = 0; j < 8; ++j) {
835 output.sendNoteOn(j, 48, 0);
837 for (int row = 0; row < 7; ++row) {
838 for (int col = 0; col < 8; ++col) {
839 setGridState(row, col, 0);
845 private void resetEffectParameters() {
846 LXEffect newEffect = getSelectedEffect();
847 if (newEffect == focusedEffect) {
850 if (focusedEffect != null) {
851 for (LXParameter p : focusedPattern.getParameters()) {
852 ((LXListenableParameter) p).removeListener(this);
855 focusedEffect = newEffect;
857 for (LXParameter p : focusedEffect.getParameters()) {
858 ((LXListenableParameter) p).addListener(this);
859 sendKnob(12 + i++, p);
862 sendKnob(12 + i++, 0);
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();
871 sendKnob(i, (int) (pv * 127.));
874 private void sendKnob(int i, int value) {
876 output.sendController(0, 48+i, value);
878 output.sendController(0, 8+i, value);
882 public void onParameterChanged(LXParameter parameter) {
884 for (LXParameter p : focusedPattern.getParameters()) {
885 if (p == parameter) {
892 for (LXParameter p : focusedEffect.getParameters()) {
893 if (p == parameter) {
901 public void setGridState(int row, int col, int state) {
902 if (col < 8 && row < 5) {
903 output.sendNoteOn(col, 53+row, state);
908 class ArturiaMinilabMidiInput extends GenericDeviceMidiInput {
909 ArturiaMinilabMidiInput(MidiEngine midiEngine, MidiInputDevice d) {
910 super(midiEngine, d);
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;
926 float val = effects.colorFucker.hueShift.getValuef();
927 val += (cc.getValue() - 64) / 256.;
928 effects.colorFucker.hueShift.setNormalized((val+1) % 1);
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));
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;
954 public void setGridState(int row, int col, int state);
957 class GridController {
958 private final List<GridOutput> outputs = new ArrayList<GridOutput>();
960 private final MidiEngine midiEngine;
962 GridController(MidiEngine midiEngine) {
963 this.midiEngine = midiEngine;
966 public void addOutput(GridOutput output) {
970 public boolean gridPressed(int row, int col) {
971 return midiEngine.getFocusedPattern().gridPressed(row, col);
974 public boolean gridReleased(int row, int col) {
975 return midiEngine.getFocusedPattern().gridReleased(row, col);
978 public void setState(int row, int col, int state) {
979 for (GridOutput g : outputs) {
980 g.setGridState(row, col, state);