-public class MidiListener extends AbstractScrollItem {
-
- public static final int MIDI = 0;
- public static final int KEYS = 1;
- public static final int APC = 2;
-
- private boolean enabled = false;
- private final String name;
-
- MidiListener(MidiInputDevice d) {
- mode = MIDI;
- d.createInput(this);
- name = d.getName();
- }
-
- class NoteMeta {
- int channel;
- int number;
- NoteMeta(int channel, int number) {
- this.channel = channel;
- this.number = number;
- }
- }
-
- final Map<Character, NoteMeta> keyToNote = new HashMap<Character, NoteMeta>();
-
- private final int mode;
- private int octaveShift = 0;
-
- MidiListener(int mode) {
- this.mode = mode;
- switch (mode) {
- case APC:
- name = "QWERTY (APC Mode)";
- mapAPC();
- break;
- default:
- case KEYS:
- name = "QWERTY (Key Mode)";
- mapKeys();
- break;
- }
- }
-
- private void mapAPC() {
- mapNote('1', 0, 53);
- mapNote('2', 1, 53);
- mapNote('3', 2, 53);
- mapNote('4', 3, 53);
- mapNote('5', 4, 53);
- mapNote('6', 5, 53);
- mapNote('q', 0, 54);
- mapNote('w', 1, 54);
- mapNote('e', 2, 54);
- mapNote('r', 3, 54);
- mapNote('t', 4, 54);
- mapNote('y', 5, 54);
- mapNote('a', 0, 55);
- mapNote('s', 1, 55);
- mapNote('d', 2, 55);
- mapNote('f', 3, 55);
- mapNote('g', 4, 55);
- mapNote('h', 5, 55);
- mapNote('z', 0, 56);
- mapNote('x', 1, 56);
- mapNote('c', 2, 56);
- mapNote('v', 3, 56);
- mapNote('b', 4, 56);
- mapNote('n', 5, 56);
- registerKeyEvent(this);
- }
-
- private void mapKeys() {
- int note = 48;
- mapNote('a', 1, note++);
- mapNote('w', 1, note++);
- mapNote('s', 1, note++);
- mapNote('e', 1, note++);
- mapNote('d', 1, note++);
- mapNote('f', 1, note++);
- mapNote('t', 1, note++);
- mapNote('g', 1, note++);
- mapNote('y', 1, note++);
- mapNote('h', 1, note++);
- mapNote('u', 1, note++);
- mapNote('j', 1, note++);
- mapNote('k', 1, note++);
- mapNote('o', 1, note++);
- mapNote('l', 1, note++);
- registerKeyEvent(this);
- }
-
- void mapNote(char ch, int channel, int number) {
- keyToNote.put(ch, new NoteMeta(channel, number));
- }
-
- public String getLabel() {
- return name;
- }
-
- public void keyEvent(KeyEvent e) {
- if (!enabled) {
- return;
- }
- char c = Character.toLowerCase(e.getKeyChar());
- NoteMeta nm = keyToNote.get(c);
- if (nm != null) {
- switch (e.getID()) {
- case KeyEvent.KEY_PRESSED:
- noteOnReceived(new Note(Note.NOTE_ON, nm.channel, nm.number + octaveShift*12, 127));
- break;
- case KeyEvent.KEY_RELEASED:
- noteOffReceived(new Note(Note.NOTE_OFF, nm.channel, nm.number + octaveShift*12, 0));
- break;
- }
- }
- if ((mode == KEYS) && (e.getID() == KeyEvent.KEY_PRESSED)) {
- switch (c) {
- case 'z':
- octaveShift = constrain(octaveShift-1, -4, 4);
- break;
- case 'x':
- octaveShift = constrain(octaveShift+1, -4, 4);
- break;
- }
- }
- }
-
- public boolean isEnabled() {
- return enabled;
- }
-
- public boolean isSelected() {
- return enabled;
- }
-
- public void onMousePressed() {
- setEnabled(!enabled);
- }
-
- public MidiListener setEnabled(boolean enabled) {
- if (enabled != this.enabled) {
- this.enabled = enabled;
- if (uiMidi != null) uiMidi.redraw();
- }
- return this;
- }
-
- private SCPattern getFocusedPattern() {
- return (SCPattern) uiMidi.getFocusedDeck().getActivePattern();
- }
-
- void programChangeReceived(ProgramChange pc) {
- if (!enabled) {
- return;
- }
- if (uiMidi.logMidi()) {
- println(getLabel() + " :: Program Change :: " + pc.getNumber());
- }
- }
-
- void controllerChangeReceived(rwmidi.Controller cc) {
- if (!enabled) {
- return;
- }
- if (uiMidi.logMidi()) {
- println(getLabel() + " :: Controller :: " + cc.getCC() + ":" + cc.getValue());
- }
- getFocusedPattern().controllerChangeReceived(cc);
- }
-
- void noteOnReceived(Note note) {
- if (!enabled) {
- return;
- }
- if (uiMidi.logMidi()) {
- println(getLabel() + " :: Note On :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity());
- }
- getFocusedPattern().noteOnReceived(note);
- }
-
- void noteOffReceived(Note note) {
- if (!enabled) {
- return;
- }
- if (uiMidi.logMidi()) {
- println(getLabel() + " :: Note Off :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity());
- }
- getFocusedPattern().noteOffReceived(note);
- }
-
-}