Skip to content

Instantly share code, notes, and snippets.

@adambutler
Created December 21, 2016 12:45
Show Gist options
  • Save adambutler/d40d27ae286b17db8bcd284e8b0d6f93 to your computer and use it in GitHub Desktop.
Save adambutler/d40d27ae286b17db8bcd284e8b0d6f93 to your computer and use it in GitHub Desktop.
Web Midi - Hello World
// Source: https://jsfiddle.net/KeithMcMillenInstruments/zma6pzt9
var midi, data;
// request MIDI access
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess({
sysex: false
}).then(onMIDISuccess, onMIDIFailure);
} else {
alert("No MIDI support in your browser.");
}
// midi functions
function onMIDISuccess(midiAccess) {
// when we get a succesful response, run this code
midi = midiAccess; // this is our raw MIDI data, inputs, outputs, and sysex status
var inputs = midi.inputs.values();
// loop over all available inputs and listen for any MIDI input
for (var input = inputs.next(); input && !input.done; input = inputs.next()) {
// each time there is a midi message call the onMIDIMessage function
input.value.onmidimessage = onMIDIMessage;
}
}
function onMIDIFailure(error) {
// when we get a failed response, run this code
console.log("No access to MIDI devices or your browser doesn't support WebMIDI API. Please use WebMIDIAPIShim " + error);
}
function onMIDIMessage(message) {
data = message.data; // this gives us our [command/channel, note, velocity] data.
console.log('MIDI data', data); // MIDI data [144, 63, 73]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment