Skip to content

Instantly share code, notes, and snippets.

@vabarbosa
Created January 3, 2019 17:17
Show Gist options
  • Save vabarbosa/42a50a2d95aaf319dcbf7bf19fd78d2e to your computer and use it in GitHub Desktop.
Save vabarbosa/42a50a2d95aaf319dcbf7bf19fd78d2e to your computer and use it in GitHub Desktop.
let midiOutputs
let midiInputs
// check MIDI support/availability
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess().then(function (access) {
// get output devices
midiOutputs = Array.from(access.outputs.values())
// get input devices
midiInputs = Array.from(access.inputs.values())
midiInputs.forEach(m => {
// incoming MIDI message listener
m.value.onmidimessage = messageRcvd
})
})
}
// handle received MIDI message
function messageRcvd (message) {
let command = message.data[0]
let note = message.data[1]
let velocity = message.data[2]
// MIDI message received, do something
}
// send a MIDI message
function messageSend (command, note, velocity) {
midiOutputs.forEach(m => {
m.send([command, note, velocity])
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment