From 94ae50b2f20fbe413175e9ee6efab4ba117d19b1 Mon Sep 17 00:00:00 2001 From: Be Date: Sun, 14 Jun 2020 15:43:41 -0500 Subject: [PATCH] add MidiDispatcher module in new res/controllers/lib folder --- res/controllers/lib/mididispatcher.mjs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 res/controllers/lib/mididispatcher.mjs diff --git a/res/controllers/lib/mididispatcher.mjs b/res/controllers/lib/mididispatcher.mjs new file mode 100644 index 000000000000..367a247d54b6 --- /dev/null +++ b/res/controllers/lib/mididispatcher.mjs @@ -0,0 +1,24 @@ +export class MidiDispatcher { + constructor(noteOff) { + this.noteOff = noteOff; + this.inputMap = new Map(); + } + registerInputCallback(midiBytes, callback) { + // JavaScript is broken and believes [1,2] === [1,2] is false, so + // JSONify the Array to make it usable as a Map key. + const key = JSON.stringify(midiBytes); + this.inputMap.set(key, callback); + if (this.noteOff === true && ((midiBytes[0] & 0xF0) === 0x90)) { + const noteOffBytes = [midiBytes[0] - 0x10, midiBytes[1]]; + const noteOffKey = JSON.stringify(noteOffBytes); + this.inputMap.set(noteOffKey, callback); + } + } + receiveData(data, timestamp) { + const key = JSON.stringify([data[0], data[1]]); + const callback = this.inputMap.get(key); + if (typeof callback === 'function') { + callback(data, timestamp); + } + } +}