Skip to content

Instantly share code, notes, and snippets.

@sc0ttj
Forked from adambutler/index.js
Created June 12, 2025 08:52
Show Gist options
  • Save sc0ttj/ba67a27b52f11c3bb13947bdeb52f3ec to your computer and use it in GitHub Desktop.
Save sc0ttj/ba67a27b52f11c3bb13947bdeb52f3ec 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