]> ruin.nu Git - moosique.git/blob - MooKeyboard.java
nu är även den fina scale velocity dialogen färdig
[moosique.git] / MooKeyboard.java
1 import javax.sound.midi.*;
2 import java.awt.event.*;
3
4 /**
5  * A keyboard listener emulating a synthesizer.
6  * 
7  * @author  Einar Pehrson
8  */
9  
10 public class MooKeyboard extends KeyAdapter {
11
12         private boolean[] isOn;
13         private static int startNote;
14         private static int[] keyToNote = new int[120];
15
16         /**
17          * Sets up the synthesizer emulation.
18          */
19         public MooKeyboard() {
20                 isOn = new boolean[120];
21         }
22
23         static {
24                 startNote = 48;
25                 makeKeyboardMapping();
26         }
27         
28         /**
29          * Plays the appropriate MIDI NoteOn event.
30          */
31         public void keyPressed(KeyEvent e) {
32                 if (!e.isControlDown()) {
33                         try {
34                                 // Retrieves the notenumber from the conversion array.
35                                 int noteNumber = keyToNote[e.getKeyCode()];
36                                 // If note is not already on and the key is mapped to a note, sends the NoteOn event.
37                                 if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127);
38                                 isOn[noteNumber] = true;
39                         } catch (ArrayIndexOutOfBoundsException x) {
40                                 return;
41                         }
42                 }
43         }
44         
45         /**
46          * Plays the appropriate MIDI NoteOff event.
47          */
48         public void keyReleased(KeyEvent e) {
49                 if (!e.isControlDown()) {
50                         try {
51                                 // Retrieves the notenumber from the conversion array.
52                                 int noteNumber = keyToNote[e.getKeyCode()];
53                                 // Sends the NoteOff event.
54                                 Moosique.getActiveChannel().noteOff(noteNumber);
55                                 isOn[noteNumber] = false;
56                         } catch (ArrayIndexOutOfBoundsException x) {
57                                 return;
58                         }
59                 }
60         }
61
62         /**
63          * Sets the octave of the lower part of the keyboard (default = 4)
64          * @param n     the octave to start at
65          */
66         public static void setOctave(int n) {
67                 startNote = n * 12;
68                 makeKeyboardMapping();
69         }
70
71         /**
72          * Increases or decreases the octave of the lower part of the keyboard (default = 4)
73          * @param increase      true for increase, false for decrease
74          */
75         public static void setRelativeOctave(boolean increase) {
76                 if (increase) {
77                         if (startNote == 108) return;
78                         setOctave((startNote/12) + 1);
79                 } else {
80                         if (startNote == 0) return;
81                         setOctave((startNote/12) - 1);
82                 }
83         }
84
85         /** Maps keycodes (array indices) to MIDI note numbers using the following layout:
86          *
87          *  2 3   5 6 7   9 0   =>  # #   # # #   # #
88          * q w e r t y u i o p  => c d e f g a b c d e
89          *  s d   g h j   l     =>  # #   # # #   #
90          * z x c v b n m , .    => c d e f g a b c d
91          */
92         private static void makeKeyboardMapping() {
93                 keyToNote[KeyEvent.VK_Q] = startNote;
94                 keyToNote[83] = startNote + 1;
95                 keyToNote[88] = startNote + 2;
96                 keyToNote[68] = startNote + 3;
97                 keyToNote[67] = startNote + 4;
98                 keyToNote[86] = startNote + 5;
99                 keyToNote[71] = startNote + 6;
100                 keyToNote[66] = startNote + 7;
101                 keyToNote[72] = startNote + 8;
102                 keyToNote[78] = startNote + 9;
103                 keyToNote[74] = startNote + 10;
104                 keyToNote[77] = startNote + 11; 
105                 keyToNote[44] = startNote + 12;
106                 keyToNote[76] = startNote + 13;
107                 keyToNote[46] = startNote + 14;
108                 keyToNote[81] = startNote + 12;
109                 keyToNote[50] = startNote + 13;
110                 keyToNote[87] = startNote + 14;
111                 keyToNote[51] = startNote + 15;
112                 keyToNote[69] = startNote + 16;
113                 keyToNote[82] = startNote + 17;
114                 keyToNote[53] = startNote + 18;
115                 keyToNote[84] = startNote + 19;
116                 keyToNote[54] = startNote + 20;
117                 keyToNote[89] = startNote + 21;
118                 keyToNote[55] = startNote + 22;
119                 keyToNote[85] = startNote + 23;
120                 keyToNote[73] = startNote + 24;
121                 keyToNote[57] = startNote + 25;
122                 keyToNote[79] = startNote + 26;
123                 keyToNote[48] = startNote + 27;
124                 keyToNote[80] = startNote + 28;
125         }
126 }