X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooKeyboard.java;h=29ffeecd6207ea2b5ac1348178478528ca4c4a65;hp=0ebfa63f063dd6f0cec8aeda527594d63156e0bd;hb=HEAD;hpb=4ac3b6210979ea78dceb609a0501cb0265d00065 diff --git a/MooKeyboard.java b/MooKeyboard.java index 0ebfa63..29ffeec 100644 --- a/MooKeyboard.java +++ b/MooKeyboard.java @@ -1,57 +1,127 @@ import javax.sound.midi.*; import java.awt.event.*; -/* - * Functional representation of a MIDI note, which contains two MIDI events, note on and note off. +/** + * A keyboard listener emulating a synthesizer. * * @author Einar Pehrson */ public class MooKeyboard extends KeyAdapter { - private boolean[] isOn = new boolean[120]; - private static final int startNote = 48; - private static final int[] keyToNote = new int[120]; + private static int startNote; + private static int[] keyToNote = new int[120]; + private boolean[] isOn; + private boolean recording; + private MooTrackTitle title; - /* + /** + * Sets up the synthesizer emulation. + */ + public MooKeyboard(MooTrackTitle mtt) { + title = mtt; + isOn = new boolean[120]; + } + + static { + startNote = 48; + makeKeyboardMapping(); + } + + /** * Plays the appropriate MIDI NoteOn event. */ public void keyPressed(KeyEvent e) { - try { - // Retrieves the notenumber from the conversion array. - int noteNumber = keyToNote[e.getKeyCode()]; - // If note is not already on and the key is mapped to a note, sends the NoteOn event. - if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127); - isOn[noteNumber] = true; - } catch (ArrayIndexOutOfBoundsException x) { - return; + if (!e.isControlDown()) { + try { + // Retrieves the notenumber from the conversion array. + int noteNumber = keyToNote[e.getKeyCode()]; + // If note is not already on and the key is mapped to a note, sends the NoteOn event. + if (!isOn[noteNumber] && noteNumber > 0) { + if (recording) { + ShortMessage msg = new ShortMessage(); + msg.setMessage(ShortMessage.NOTE_ON, title.getChannel(), noteNumber, 100); + Moosique.getSequencer().getReceiver().send(msg, -1); + } else { + Moosique.getActiveChannel().noteOn(noteNumber, 127); + } + } + isOn[noteNumber] = true; + } catch (Exception x) { + return; + } } } - /* + /** * Plays the appropriate MIDI NoteOff event. */ public void keyReleased(KeyEvent e) { - try { - // Retrieves the notenumber from the conversion array. - int noteNumber = keyToNote[e.getKeyCode()]; - // Sends the NoteOff event. - Moosique.getActiveChannel().noteOff(noteNumber); - isOn[noteNumber] = false; - } catch (ArrayIndexOutOfBoundsException x) { - return; + if (!e.isControlDown()) { + try { + // Retrieves the notenumber from the conversion array. + int noteNumber = keyToNote[e.getKeyCode()]; + // Sends the NoteOff event. + if (recording) { + ShortMessage msg = new ShortMessage(); + msg.setMessage(ShortMessage.NOTE_OFF, title.getChannel(), noteNumber, 0); + Moosique.getSequencer().getReceiver().send(msg, -1); + } else { + Moosique.getActiveChannel().noteOff(noteNumber); + } + isOn[noteNumber] = false; + } catch (Exception x) { + return; + } + } + } + + /** + * Prepares the keyboard for recording on the current channel. + */ + public void recordEnable() { + recording = true; + } + + /** + * Disables recording and returns to keyjazz mode. + */ + public void recordDisable() { + recording = false; + } + + /** + * Sets the octave of the lower part of the keyboard (default = 4). + * @param n the octave to start at + */ + public static void setOctave(int n) { + startNote = n * 12; + makeKeyboardMapping(); + } + + /** + * Increases or decreases the octave of the lower part of the keyboard (default = 4). + * @param increase true for increase, false for decrease + */ + public static void setRelativeOctave(boolean increase) { + if (increase) { + if (startNote == 108) return; + setOctave((startNote/12) + 1); + } else { + if (startNote == 0) return; + setOctave((startNote/12) - 1); } } - /* Maps keycodes (array indices) to MIDI note numbers using the following layout: + /** Maps keycodes (array indices) to MIDI note numbers using the following layout: * * 2 3 5 6 7 9 0 => # # # # # # # * q w e r t y u i o p => c d e f g a b c d e * s d g h j l => # # # # # # * z x c v b n m , . => c d e f g a b c d */ - static { - keyToNote[90] = startNote; + private static void makeKeyboardMapping() { + keyToNote[KeyEvent.VK_Q] = startNote; keyToNote[83] = startNote + 1; keyToNote[88] = startNote + 2; keyToNote[68] = startNote + 3; @@ -84,4 +154,4 @@ public class MooKeyboard extends KeyAdapter { keyToNote[48] = startNote + 27; keyToNote[80] = startNote + 28; } -} \ No newline at end of file +}