Skip to content

Instantly share code, notes, and snippets.

@molotowtales
Created October 19, 2019 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molotowtales/30cd697f9c1193806c822473736bc9bf to your computer and use it in GitHub Desktop.
Save molotowtales/30cd697f9c1193806c822473736bc9bf to your computer and use it in GitHub Desktop.
midi-interface for midi.city
if (navigator.requestMIDIAccess) {
var keys = 'AWSEDFTGYHUJKOLP;'
function send(note, velocity) {
if (note >= 48 && note <= 64) {
var i = keys.charCodeAt(note-48);
document.dispatchEvent(new KeyboardEvent('key' + (velocity == 0 ? 'up' : 'down'), {'keyCode': i, 'which': i}));
}
}
navigator.requestMIDIAccess().then(
function(access) {
for (var input of access.inputs.values()) {
input.onmidimessage = function(midi) {
switch (midi.data[0] & 0xf0) {
case 0x80:
send(midi.data[1], 0)
break;
case 0x90:
send(midi.data[1], midi.data[2])
break;
}
}
}
},
function(){
alert('No access :(')
}
);
} else {
alert('No MIDI-support :(')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment