|
| 1 | +// Copyright (c) 2012-2022 John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file. |
| 2 | + |
| 3 | +import {Config} from "../synth/SynthConfig"; |
| 4 | +import {SongDocument} from "./SongDocument"; |
| 5 | + |
| 6 | +export class KeyboardLayout { |
| 7 | + private static _pianoAtC: ReadonlyArray<ReadonlyArray<number | null>> = [ |
| 8 | + [ 0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17], |
| 9 | + [null, 1, 3,null, 6, 8, 10,null, 13, 15,null, 18], |
| 10 | + [ 12, 14, 16, 17, 19, 21, 23, 24, 26, 28, 29, 31, 33], |
| 11 | + [null, 13, 15,null, 18, 20, 22,null, 25, 27,null, 30, 32], |
| 12 | + ]; |
| 13 | + private static _pianoAtA: ReadonlyArray<ReadonlyArray<number | null>> = [ |
| 14 | + [ 0, 2, 3, 5, 7, 8, 10, 12, 14, 15, 17], |
| 15 | + [ -1, 1,null, 4, 6,null, 9, 11, 13,null, 16, 18], |
| 16 | + [ 12, 14, 15, 17, 19, 20, 22, 24, 26, 27, 29, 31, 32], |
| 17 | + [ 11, 13,null, 16, 18,null, 21, 23, 25,null, 28, 30,null], |
| 18 | + ]; |
| 19 | + |
| 20 | + private _possiblyPlayingPitchesFromKeyboard: boolean = false; |
| 21 | + |
| 22 | + constructor(private _doc: SongDocument) { |
| 23 | + window.addEventListener("blur", this._onWindowBlur); |
| 24 | + } |
| 25 | + |
| 26 | + private _onWindowBlur = (event: Event) => { |
| 27 | + // Browsers don't explicitly release keys when the page isn't in focus so let's just assume they're all released. |
| 28 | + if (this._possiblyPlayingPitchesFromKeyboard) { |
| 29 | + this._doc.liveInput.clearAllPitches(); |
| 30 | + this._possiblyPlayingPitchesFromKeyboard = false; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public handleKeyEvent(event: KeyboardEvent, pressed: boolean): void { |
| 35 | + // See: https://www.w3.org/TR/uievents-code/#key-alphanumeric-writing-system |
| 36 | + switch (event.code) { |
| 37 | + case "Backquote": this.handleKey(-1, 3, pressed); break; |
| 38 | + case "Digit1": this.handleKey(0, 3, pressed); break; |
| 39 | + case "Digit2": this.handleKey(1, 3, pressed); break; |
| 40 | + case "Digit3": this.handleKey(2, 3, pressed); break; |
| 41 | + case "Digit4": this.handleKey(3, 3, pressed); break; |
| 42 | + case "Digit5": this.handleKey(4, 3, pressed); break; |
| 43 | + case "Digit6": this.handleKey(5, 3, pressed); break; |
| 44 | + case "Digit7": this.handleKey(6, 3, pressed); break; |
| 45 | + case "Digit8": this.handleKey(7, 3, pressed); break; |
| 46 | + case "Digit9": this.handleKey(8, 3, pressed); break; |
| 47 | + case "Digit0": this.handleKey(9, 3, pressed); break; |
| 48 | + case "Minus": this.handleKey(10, 3, pressed); break; |
| 49 | + case "Equal": this.handleKey(11, 3, pressed); break; |
| 50 | + case "IntlYen": this.handleKey(12, 3, pressed); break; // Present on Russian and Japanese keyboards. |
| 51 | + |
| 52 | + case "KeyQ": this.handleKey(0, 2, pressed); break; |
| 53 | + case "KeyW": this.handleKey(1, 2, pressed); break; |
| 54 | + case "KeyE": this.handleKey(2, 2, pressed); break; |
| 55 | + case "KeyR": this.handleKey(3, 2, pressed); break; |
| 56 | + case "KeyT": this.handleKey(4, 2, pressed); break; |
| 57 | + case "KeyY": this.handleKey(5, 2, pressed); break; |
| 58 | + case "KeyU": this.handleKey(6, 2, pressed); break; |
| 59 | + case "KeyI": this.handleKey(7, 2, pressed); break; |
| 60 | + case "KeyO": this.handleKey(8, 2, pressed); break; |
| 61 | + case "KeyP": this.handleKey(9, 2, pressed); break; |
| 62 | + case "BracketLeft": this.handleKey(10, 2, pressed); break; |
| 63 | + case "BracketRight": this.handleKey(11, 2, pressed); break; |
| 64 | + case "Backslash": |
| 65 | + // Present on US keyboards... but on non-US keyboards it's also used at a different location, see "IntlHash" below. :/ |
| 66 | + if (event.key == "\\" || event.key == "|") { |
| 67 | + this.handleKey(12, 2, pressed); |
| 68 | + } else { |
| 69 | + this.handleKey(11, 1, pressed); |
| 70 | + } |
| 71 | + break; |
| 72 | + |
| 73 | + case "KeyA": this.handleKey(0, 1, pressed); break; |
| 74 | + case "KeyS": this.handleKey(1, 1, pressed); break; |
| 75 | + case "KeyD": this.handleKey(2, 1, pressed); break; |
| 76 | + case "KeyF": this.handleKey(3, 1, pressed); break; |
| 77 | + case "KeyG": this.handleKey(4, 1, pressed); break; |
| 78 | + case "KeyH": this.handleKey(5, 1, pressed); break; |
| 79 | + case "KeyJ": this.handleKey(6, 1, pressed); break; |
| 80 | + case "KeyK": this.handleKey(7, 1, pressed); break; |
| 81 | + case "KeyL": this.handleKey(8, 1, pressed); break; |
| 82 | + case "Semicolon": this.handleKey(9, 1, pressed); break; |
| 83 | + case "Quote": this.handleKey(10, 1, pressed); break; |
| 84 | + case "IntlHash": this.handleKey(11, 1, pressed); break; // Present on non-US keyboards... but in practice it is actually represented as "Backslash" so this is obsolete. Oh well. :/ |
| 85 | + |
| 86 | + case "IntlBackslash": this.handleKey(-1, 0, pressed); break; // Present on Brazillian and many European keyboards. |
| 87 | + case "KeyZ": this.handleKey(0, 0, pressed); break; |
| 88 | + case "KeyX": this.handleKey(1, 0, pressed); break; |
| 89 | + case "KeyC": this.handleKey(2, 0, pressed); break; |
| 90 | + case "KeyV": this.handleKey(3, 0, pressed); break; |
| 91 | + case "KeyB": this.handleKey(4, 0, pressed); break; |
| 92 | + case "KeyN": this.handleKey(5, 0, pressed); break; |
| 93 | + case "KeyM": this.handleKey(6, 0, pressed); break; |
| 94 | + case "Comma": this.handleKey(7, 0, pressed); break; |
| 95 | + case "Period": this.handleKey(8, 0, pressed); break; |
| 96 | + case "Slash": this.handleKey(9, 0, pressed); break; |
| 97 | + case "IntlRo": this.handleKey(10, 0, pressed); break; // Present on Brazillian and Japanese keyboards. |
| 98 | + |
| 99 | + default: return; //unhandled, don't prevent default. |
| 100 | + } |
| 101 | + |
| 102 | + // If the key event was handled as a note, prevent default behavior. |
| 103 | + event.preventDefault(); |
| 104 | + } |
| 105 | + |
| 106 | + public handleKey(x: number, y: number, pressed: boolean): void { |
| 107 | + |
| 108 | + const isDrum: boolean = this._doc.song.getChannelIsNoise(this._doc.channel); |
| 109 | + if (isDrum) { |
| 110 | + if (x >= 0 && x < Config.drumCount) { |
| 111 | + if (pressed) { |
| 112 | + this._doc.synth.preferLowerLatency = true; |
| 113 | + this._doc.liveInput.addPerformedPitch(x); |
| 114 | + this._possiblyPlayingPitchesFromKeyboard = true; |
| 115 | + } else { |
| 116 | + this._doc.liveInput.removePerformedPitch(x); |
| 117 | + } |
| 118 | + } |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + let pitchOffset: number | null = null; |
| 123 | + let forcedKey: number | null = null; |
| 124 | + switch (this._doc.prefs.keyboardLayout) { |
| 125 | + case "wickiHayden": |
| 126 | + pitchOffset = y * 5 + x * 2; |
| 127 | + break; |
| 128 | + case "songScale": |
| 129 | + const scaleFlags: ReadonlyArray<boolean> = Config.scales[this._doc.song.scale].flags; |
| 130 | + const scaleIndices: number[] = <number[]> scaleFlags.map((flag, index) => flag ? index : null).filter((index) => index != null); |
| 131 | + pitchOffset = (y - 1 + Math.floor(x / scaleIndices.length)) * Config.pitchesPerOctave + scaleIndices[x % scaleIndices.length]; |
| 132 | + break; |
| 133 | + case "pianoAtC": |
| 134 | + pitchOffset = KeyboardLayout._pianoAtC[y][x]; |
| 135 | + forcedKey = Config.keys.dictionary["C"].basePitch; |
| 136 | + break; |
| 137 | + case "pianoAtA": |
| 138 | + pitchOffset = KeyboardLayout._pianoAtA[y][x]; |
| 139 | + forcedKey = Config.keys.dictionary["A"].basePitch; |
| 140 | + break; |
| 141 | + case "pianoTransposingC": |
| 142 | + pitchOffset = KeyboardLayout._pianoAtC[y][x]; |
| 143 | + break; |
| 144 | + case "pianoTransposingA": |
| 145 | + pitchOffset = KeyboardLayout._pianoAtA[y][x]; |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + if (pitchOffset != null) { |
| 150 | + const octaveOffset: number = Math.max(0, this._doc.song.channels[this._doc.channel].octave - 1) * Config.pitchesPerOctave; |
| 151 | + let keyOffset: number = 0; // The basePitch of the song key is implicit. |
| 152 | + |
| 153 | + if (forcedKey != null) { |
| 154 | + const keyBasePitch: number = Config.keys[this._doc.song.key].basePitch; |
| 155 | + keyOffset = (forcedKey - keyBasePitch + 144) % 12; |
| 156 | + } |
| 157 | + |
| 158 | + const pitch = octaveOffset + keyOffset + pitchOffset; |
| 159 | + if (pitch < 0 || pitch > Config.maxPitch) return; |
| 160 | + |
| 161 | + if (pressed) { |
| 162 | + this._doc.synth.preferLowerLatency = true; |
| 163 | + this._doc.liveInput.addPerformedPitch(pitch); |
| 164 | + this._possiblyPlayingPitchesFromKeyboard = true; |
| 165 | + } else { |
| 166 | + this._doc.liveInput.removePerformedPitch(pitch); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments