-
-
Save molotowtales/30cd697f9c1193806c822473736bc9bf to your computer and use it in GitHub Desktop.
midi-interface for midi.city
This file contains 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
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