X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=MooKeyboard.java;h=ef4785e67c5081e343983c06d411800dab2e1d1c;hb=fa159bea01bba1b1294fcc5ed14546b7510dcd3d;hp=7031c40711c1831c50802947fe01cf186066b1e2;hpb=a8b0b5e27d120df964c5b6d8554a6207951b00d0;p=moosique.git diff --git a/MooKeyboard.java b/MooKeyboard.java index 7031c40..ef4785e 100644 --- a/MooKeyboard.java +++ b/MooKeyboard.java @@ -11,29 +11,34 @@ public class MooKeyboard extends KeyAdapter { private boolean[] isOn; private static int startNote; - private static int[] keyToNote; + private static int[] keyToNote = new int[120]; /** * Sets up the synthesizer emulation. */ public MooKeyboard() { isOn = new boolean[120]; - keyToNote = new int[120]; - setOctave(4); } + static { + startNote = 48; + makeKeyboardMapping(); + } + /** * Plays the appropriate MIDI NoteOn event. */ public void keyPressed(KeyEvent e) { - try { - // Retrieves the notenumber from the conversion array. - int noteNumber = keyToNote[e.getKeyCode()]; - // If note is not already on and the key is mapped to a note, sends the NoteOn event. - if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127); - isOn[noteNumber] = true; - } catch (ArrayIndexOutOfBoundsException x) { - return; + if (!e.isControlDown()) { + try { + // Retrieves the notenumber from the conversion array. + int noteNumber = keyToNote[e.getKeyCode()]; + // If note is not already on and the key is mapped to a note, sends the NoteOn event. + if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127); + isOn[noteNumber] = true; + } catch (ArrayIndexOutOfBoundsException x) { + return; + } } } @@ -41,14 +46,16 @@ public class MooKeyboard extends KeyAdapter { * Plays the appropriate MIDI NoteOff event. */ public void keyReleased(KeyEvent e) { - try { - // Retrieves the notenumber from the conversion array. - int noteNumber = keyToNote[e.getKeyCode()]; - // Sends the NoteOff event. - Moosique.getActiveChannel().noteOff(noteNumber); - isOn[noteNumber] = false; - } catch (ArrayIndexOutOfBoundsException x) { - return; + if (!e.isControlDown()) { + try { + // Retrieves the notenumber from the conversion array. + int noteNumber = keyToNote[e.getKeyCode()]; + // Sends the NoteOff event. + Moosique.getActiveChannel().noteOff(noteNumber); + isOn[noteNumber] = false; + } catch (ArrayIndexOutOfBoundsException x) { + return; + } } } @@ -57,11 +64,24 @@ public class MooKeyboard extends KeyAdapter { * @param n the octave to start at */ public static void setOctave(int n) { - if (startNote == 0 || startNote == 108) return; - startNote += n*12; + startNote = n * 12; makeKeyboardMapping(); } + /** + * Increases or decreases the octave of the lower part of the keyboard (default = 4) + * @param increase true for increase, false for decrease + */ + public static void setRelativeOctave(boolean increase) { + if (increase) { + if (startNote == 108) return; + setOctave((startNote/12) + 1); + } else { + if (startNote == 0) return; + setOctave((startNote/12) - 1); + } + } + /** Maps keycodes (array indices) to MIDI note numbers using the following layout: * * 2 3 5 6 7 9 0 => # # # # # # #