1 import javax.sound.midi.*;
2 import java.awt.event.*;
5 * A keyboard listener emulating a synthesizer.
7 * @author Einar Pehrson
10 public class MooKeyboard extends KeyAdapter {
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;
19 * Sets up the synthesizer emulation.
21 public MooKeyboard(MooTrackTitle mtt) {
23 isOn = new boolean[120];
28 makeKeyboardMapping();
32 * Plays the appropriate MIDI NoteOn event.
34 public void keyPressed(KeyEvent e) {
35 if (!e.isControlDown()) {
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) {
42 ShortMessage msg = new ShortMessage();
43 msg.setMessage(ShortMessage.NOTE_ON, title.getChannel(), noteNumber, 100);
44 Moosique.getSequencer().getReceiver().send(msg, -1);
46 Moosique.getActiveChannel().noteOn(noteNumber, 127);
49 isOn[noteNumber] = true;
50 } catch (Exception x) {
57 * Plays the appropriate MIDI NoteOff event.
59 public void keyReleased(KeyEvent e) {
60 if (!e.isControlDown()) {
62 // Retrieves the notenumber from the conversion array.
63 int noteNumber = keyToNote[e.getKeyCode()];
64 // Sends the NoteOff event.
66 ShortMessage msg = new ShortMessage();
67 msg.setMessage(ShortMessage.NOTE_OFF, title.getChannel(), noteNumber, 0);
68 Moosique.getSequencer().getReceiver().send(msg, -1);
70 Moosique.getActiveChannel().noteOff(noteNumber);
72 isOn[noteNumber] = false;
73 } catch (Exception x) {
80 * Prepares the keyboard for recording on the current channel.
82 public void recordEnable() {
87 * Disables recording and returns to keyjazz mode.
89 public void recordDisable() {
94 * Sets the octave of the lower part of the keyboard (default = 4).
95 * @param n the octave to start at
97 public static void setOctave(int n) {
99 makeKeyboardMapping();
103 * Increases or decreases the octave of the lower part of the keyboard (default = 4).
104 * @param increase true for increase, false for decrease
106 public static void setRelativeOctave(boolean increase) {
108 if (startNote == 108) return;
109 setOctave((startNote/12) + 1);
111 if (startNote == 0) return;
112 setOctave((startNote/12) - 1);
116 /** Maps keycodes (array indices) to MIDI note numbers using the following layout:
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
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;