Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Created July 14, 2016 21:24
Show Gist options
  • Select an option

  • Save mohayonao/95aca84ffbbc95df50d178fb189ba223 to your computer and use it in GitHub Desktop.

Select an option

Save mohayonao/95aca84ffbbc95df50d178fb189ba223 to your computer and use it in GitHub Desktop.
"use strict";
const events = require("events");
class WebMIDIEmitter extends events.EventEmitter {
constructor(access, deviceName) {
super();
this.access = access;
this.deviceName = deviceName;
this._setupInputPort();
this._setupOutputPort();
this.access.addEventListener("statechange", (e) => {
this._setupInputPort();
this._setupOutputPort();
this.emit("statechange", e);
});
}
write(data) {
if (this.output) {
this.output.send(data);
}
}
_setupInputPort() {
if (!this.input) {
this.input = getPort(this.access.inputs, this.deviceName);
if (this.input) {
this.input.onmidimessage = (e) => {
this.emit("data", e.data);
};
}
}
return this.input;
}
_setupOutputPort() {
if (!this.output) {
this.output = getPort(this.access.outputs, this.deviceName);
}
return this.output
}
}
function getPort(map, deviceName) {
return Array.from(map.values()).filter(device => device.name === deviceName)[0] || null;
}
module.exports = WebMIDIEmitter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment