]> 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 = 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                 if ((startNote == 0 && n == -1) || (startNote == 108 && n == 1)) return;
68                 startNote += n*12;
69                 makeKeyboardMapping();
70         }
71
72         /** Maps keycodes (array indices) to MIDI note numbers using the following layout:
73          *
74          *  2 3   5 6 7   9 0   =>  # #   # # #   # #
75          * q w e r t y u i o p  => c d e f g a b c d e
76          *  s d   g h j   l     =>  # #   # # #   #
77          * z x c v b n m , .    => c d e f g a b c d
78          */
79         private static void makeKeyboardMapping() {
80                 keyToNote[KeyEvent.VK_Q] = startNote;
81                 keyToNote[83] = startNote + 1;
82                 keyToNote[88] = startNote + 2;
83                 keyToNote[68] = startNote + 3;
84                 keyToNote[67] = startNote + 4;
85                 keyToNote[86] = startNote + 5;
86                 keyToNote[71] = startNote + 6;
87                 keyToNote[66] = startNote + 7;
88                 keyToNote[72] = startNote + 8;
89                 keyToNote[78] = startNote + 9;
90                 keyToNote[74] = startNote + 10;
91                 keyToNote[77] = startNote + 11; 
92                 keyToNote[44] = startNote + 12;
93                 keyToNote[76] = startNote + 13;
94                 keyToNote[46] = startNote + 14;
95                 keyToNote[81] = startNote + 12;
96                 keyToNote[50] = startNote + 13;
97                 keyToNote[87] = startNote + 14;
98                 keyToNote[51] = startNote + 15;
99                 keyToNote[69] = startNote + 16;
100                 keyToNote[82] = startNote + 17;
101                 keyToNote[53] = startNote + 18;
102                 keyToNote[84] = startNote + 19;
103                 keyToNote[54] = startNote + 20;
104                 keyToNote[89] = startNote + 21;
105                 keyToNote[55] = startNote + 22;
106                 keyToNote[85] = startNote + 23;
107                 keyToNote[73] = startNote + 24;
108                 keyToNote[57] = startNote + 25;
109                 keyToNote[79] = startNote + 26;
110                 keyToNote[48] = startNote + 27;
111                 keyToNote[80] = startNote + 28;
112         }
113 }