]> ruin.nu Git - moosique.git/blobdiff - MooKeyboard.java
no message
[moosique.git] / MooKeyboard.java
index 0ebfa63f063dd6f0cec8aeda527594d63156e0bd..7031c40711c1831c50802947fe01cf186066b1e2 100644 (file)
@@ -1,19 +1,28 @@
 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 boolean[] isOn;
+       private static int startNote;
+       private static int[] keyToNote;
 
-       /*
+       /**
+        * Sets up the synthesizer emulation.
+        */
+       public MooKeyboard() {
+               isOn = new boolean[120];
+               keyToNote = new int[120];
+               setOctave(4);
+       }
+
+       /**
         * Plays the appropriate MIDI NoteOn event.
         */
        public void keyPressed(KeyEvent e) {
@@ -28,7 +37,7 @@ public class MooKeyboard extends KeyAdapter {
                }
        }
        
-       /*
+       /**
         * Plays the appropriate MIDI NoteOff event.
         */
        public void keyReleased(KeyEvent e) {
@@ -43,15 +52,25 @@ public class MooKeyboard extends KeyAdapter {
                }
        }
 
-       /* Maps keycodes (array indices) to MIDI note numbers using the following layout:
+       /**
+        * 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) {
+               if (startNote == 0 || startNote == 108) return;
+               startNote += n*12;
+               makeKeyboardMapping();
+       }
+
+       /** 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 +103,4 @@ public class MooKeyboard extends KeyAdapter {
                keyToNote[48] = startNote + 27;
                keyToNote[80] = startNote + 28;
        }
-}
\ No newline at end of file
+}