]> ruin.nu Git - moosique.git/blobdiff - MooKeyboard.java
Added MooInstrumentList - a subcomponent to MooToolbar -
[moosique.git] / MooKeyboard.java
diff --git a/MooKeyboard.java b/MooKeyboard.java
new file mode 100644 (file)
index 0000000..0ebfa63
--- /dev/null
@@ -0,0 +1,87 @@
+import javax.sound.midi.*;
+import java.awt.event.*;
+
+/*
+ * Functional representation of a MIDI note, which contains two MIDI events, note on and note off.
+ * 
+ * @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];
+
+       /*
+        * 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;
+               }
+       }
+       
+       /*
+        * 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;
+               }
+       }
+
+       /* 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;
+               keyToNote[83] = startNote + 1;
+               keyToNote[88] = startNote + 2;
+               keyToNote[68] = startNote + 3;
+               keyToNote[67] = startNote + 4;
+               keyToNote[86] = startNote + 5;
+               keyToNote[71] = startNote + 6;
+               keyToNote[66] = startNote + 7;
+               keyToNote[72] = startNote + 8;
+               keyToNote[78] = startNote + 9;
+               keyToNote[74] = startNote + 10;
+               keyToNote[77] = startNote + 11; 
+               keyToNote[44] = startNote + 12;
+               keyToNote[76] = startNote + 13;
+               keyToNote[46] = startNote + 14;
+               keyToNote[81] = startNote + 12;
+               keyToNote[50] = startNote + 13;
+               keyToNote[87] = startNote + 14;
+               keyToNote[51] = startNote + 15;
+               keyToNote[69] = startNote + 16;
+               keyToNote[82] = startNote + 17;
+               keyToNote[53] = startNote + 18;
+               keyToNote[84] = startNote + 19;
+               keyToNote[54] = startNote + 20;
+               keyToNote[89] = startNote + 21;
+               keyToNote[55] = startNote + 22;
+               keyToNote[85] = startNote + 23;
+               keyToNote[73] = startNote + 24;
+               keyToNote[57] = startNote + 25;
+               keyToNote[79] = startNote + 26;
+               keyToNote[48] = startNote + 27;
+               keyToNote[80] = startNote + 28;
+       }
+}
\ No newline at end of file