]> 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 static int startNote;
13         private static int[] keyToNote = new int[120];
14         private boolean[] isOn;
15         private boolean recording;
16         private MooTrackTitle title;
17
18         /**
19          * Sets up the synthesizer emulation.
20          */
21         public MooKeyboard(MooTrackTitle mtt) {
22                 title = mtt;
23                 isOn = new boolean[120];
24         }
25
26         static {
27                 startNote = 48;
28                 makeKeyboardMapping();
29         }
30         
31         /**
32          * Plays the appropriate MIDI NoteOn event.
33          */
34         public void keyPressed(KeyEvent e) {
35                 if (!e.isControlDown()) {
36                         try {
37                                 // Retrieves the notenumber from the conversion array.
38                                 int noteNumber = keyToNote[e.getKeyCode()];
39                                 // If note is not already on and the key is mapped to a note, sends the NoteOn event.
40                                 if (!isOn[noteNumber] && noteNumber > 0) {
41                                         if (recording) {
42                                                 ShortMessage msg = new ShortMessage();
43                                                 msg.setMessage(ShortMessage.NOTE_ON, title.getChannel(), noteNumber, 100);
44                                                 Moosique.getSequencer().getReceiver().send(msg, -1);
45                                         } else {
46                                                 Moosique.getActiveChannel().noteOn(noteNumber, 127);
47                                         }
48                                 }
49                                 isOn[noteNumber] = true;
50                         } catch (Exception x) {
51                                 return;
52                         }
53                 }
54         }
55         
56         /**
57          * Plays the appropriate MIDI NoteOff event.
58          */
59         public void keyReleased(KeyEvent e) {
60                 if (!e.isControlDown()) {
61                         try {
62                                 // Retrieves the notenumber from the conversion array.
63                                 int noteNumber = keyToNote[e.getKeyCode()];
64                                 // Sends the NoteOff event.
65                                 if (recording) {
66                                         ShortMessage msg = new ShortMessage();
67                                         msg.setMessage(ShortMessage.NOTE_OFF, title.getChannel(), noteNumber, 0);
68                                         Moosique.getSequencer().getReceiver().send(msg, -1);
69                                 } else {
70                                         Moosique.getActiveChannel().noteOff(noteNumber);
71                                 }
72                                 isOn[noteNumber] = false;
73                         } catch (Exception x) {
74                                 return;
75                         }
76                 }
77         }
78
79         /**
80          * Prepares the keyboard for recording on the current channel.
81          */
82         public void recordEnable() {
83                 recording = true;
84         }
85
86         /**
87          * Disables recording and returns to keyjazz mode.
88          */
89         public void recordDisable() {
90                 recording = false;
91         }
92
93         /**
94          * Sets the octave of the lower part of the keyboard (default = 4).
95          * @param n     the octave to start at
96          */
97         public static void setOctave(int n) {
98                 startNote = n * 12;
99                 makeKeyboardMapping();
100         }
101
102         /**
103          * Increases or decreases the octave of the lower part of the keyboard (default = 4).
104          * @param increase      true for increase, false for decrease
105          */
106         public static void setRelativeOctave(boolean increase) {
107                 if (increase) {
108                         if (startNote == 108) return;
109                         setOctave((startNote/12) + 1);
110                 } else {
111                         if (startNote == 0) return;
112                         setOctave((startNote/12) - 1);
113                 }
114         }
115
116         /** Maps keycodes (array indices) to MIDI note numbers using the following layout:
117          *
118          *  2 3   5 6 7   9 0   =>  # #   # # #   # #
119          * q w e r t y u i o p  => c d e f g a b c d e
120          *  s d   g h j   l     =>  # #   # # #   #
121          * z x c v b n m , .    => c d e f g a b c d
122          */
123         private static void makeKeyboardMapping() {
124                 keyToNote[KeyEvent.VK_Q] = startNote;
125                 keyToNote[83] = startNote + 1;
126                 keyToNote[88] = startNote + 2;
127                 keyToNote[68] = startNote + 3;
128                 keyToNote[67] = startNote + 4;
129                 keyToNote[86] = startNote + 5;
130                 keyToNote[71] = startNote + 6;
131                 keyToNote[66] = startNote + 7;
132                 keyToNote[72] = startNote + 8;
133                 keyToNote[78] = startNote + 9;
134                 keyToNote[74] = startNote + 10;
135                 keyToNote[77] = startNote + 11; 
136                 keyToNote[44] = startNote + 12;
137                 keyToNote[76] = startNote + 13;
138                 keyToNote[46] = startNote + 14;
139                 keyToNote[81] = startNote + 12;
140                 keyToNote[50] = startNote + 13;
141                 keyToNote[87] = startNote + 14;
142                 keyToNote[51] = startNote + 15;
143                 keyToNote[69] = startNote + 16;
144                 keyToNote[82] = startNote + 17;
145                 keyToNote[53] = startNote + 18;
146                 keyToNote[84] = startNote + 19;
147                 keyToNote[54] = startNote + 20;
148                 keyToNote[89] = startNote + 21;
149                 keyToNote[55] = startNote + 22;
150                 keyToNote[85] = startNote + 23;
151                 keyToNote[73] = startNote + 24;
152                 keyToNote[57] = startNote + 25;
153                 keyToNote[79] = startNote + 26;
154                 keyToNote[48] = startNote + 27;
155                 keyToNote[80] = startNote + 28;
156         }
157 }