-public class MidiListener extends AbstractScrollItem {
- private boolean enabled = false;
- private final String name;
-
- MidiListener(MidiInputDevice d) {
- 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>();
-
- MidiListener() {
- name = "QWERTY Keyboard";
- 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);
- }
-
- 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) {
- 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, 127));
- break;
- case KeyEvent.KEY_RELEASED:
- noteOffReceived(new Note(Note.NOTE_OFF, nm.channel, nm.number, 0));
- 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;
- uiMidi.redraw();
- }
- return this;
- }
-
- private SCPattern getFocusedPattern() {
- return (SCPattern) uiMidi.getFocusedDeck().getActivePattern();
- }
-
- void programChangeReceived(ProgramChange pc) {
- if (!enabled) {
- return;
- }
- println("PC: " + pc.toString());
- }
-
- void controllerChangeReceived(rwmidi.Controller cc) {
- if (!enabled) {
- return;
- }
- println("CC: " + cc.toString());
- getFocusedPattern().controllerChangeReceived(cc);
- }
-
- void noteOnReceived(Note note) {
- if (!enabled) {
- return;
- }
- println("Note On: " + note.toString());
- getFocusedPattern().noteOnReceived(note);
-
- }
-
- void noteOffReceived(Note note) {
- if (!enabled) {
- return;
- }
- println("Note Off: " + note.toString());
- getFocusedPattern().noteOffReceived(note);
- }
-
-}
-