Created
July 14, 2016 21:24
-
-
Save mohayonao/95aca84ffbbc95df50d178fb189ba223 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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