]> ruin.nu Git - moosique.git/blobdiff - MooKeyboard.java
no message
[moosique.git] / MooKeyboard.java
index 8b4389ddbda97bf47f4fbc7f768cae8b386cc30a..5581172e6d10792cbe5d6cbbae30c10b88ce492e 100644 (file)
@@ -2,29 +2,43 @@ 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 int startNote = 48;
-       private static final int[] keyToNote = new int[120];
+       private boolean[] isOn;
+       private static int startNote;
+       private static int[] keyToNote = new int[120];
 
+       /**
+        * Sets up the synthesizer emulation.
+        */
+       public MooKeyboard() {
+               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) Moosique.getActiveChannel().noteOn(noteNumber, 127);
+                               isOn[noteNumber] = true;
+                       } catch (ArrayIndexOutOfBoundsException x) {
+                               return;
+                       }
                }
        }
        
@@ -32,14 +46,16 @@ public class MooKeyboard extends KeyAdapter {
         * 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.
+                               Moosique.getActiveChannel().noteOff(noteNumber);
+                               isOn[noteNumber] = false;
+                       } catch (ArrayIndexOutOfBoundsException x) {
+                               return;
+                       }
                }
        }
 
@@ -47,8 +63,10 @@ public class MooKeyboard extends KeyAdapter {
         * Sets the octave of the lower part of the keyboard (default = 4)
         * @param n     the octave to start at
         */
-       public void setOctave(int n) {
-               startNote = n * 12;
+       public static void setOctave(int n) {
+               if ((startNote == 0 && n == -1) || (startNote == 108 && n == 1)) return;
+               startNote += n*12;
+               makeKeyboardMapping();
        }
 
        /** Maps keycodes (array indices) to MIDI note numbers using the following layout:
@@ -58,8 +76,8 @@ public class MooKeyboard extends KeyAdapter {
         *  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;
@@ -92,4 +110,4 @@ public class MooKeyboard extends KeyAdapter {
                keyToNote[48] = startNote + 27;
                keyToNote[80] = startNote + 28;
        }
-}
\ No newline at end of file
+}