Skip to content

Commit

Permalink
add MidiDispatcher module in new res/controllers/lib folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Jun 15, 2020
1 parent 2f7775e commit 94ae50b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions res/controllers/lib/mididispatcher.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 94ae50b

Please sign in to comment.