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