Last active
March 28, 2019 10:47
-
-
Save kaave/6a59ba89195f2f682ba02817707f48c4 to your computer and use it in GitHub Desktop.
WebMidi: get inputs sample
This file contains hidden or 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
yarn add -D @types/webmidi |
This file contains hidden or 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
export async function initializeWebMidi(onMidiMessage: (e: WebMidi.MIDIMessageEvent) => void) { | |
if (!navigator.requestMIDIAccess) { | |
return; | |
} | |
const midi = await navigator.requestMIDIAccess(); | |
const inputIterator = midi.inputs.values(); | |
const inputs: WebMidi.MIDIInput[] = []; | |
for (let o = inputIterator.next(); !o.done; o = inputIterator.next()) { | |
inputs.push(o.value); | |
} | |
inputs.forEach(input => { | |
console.log(`${input.state} [${input.name}]`); | |
input.onmidimessage = onMidiMessage; | |
}); | |
} | |
initializeWebMidi(({ data: [command, ch, value] }) => { | |
if (command == 0xfe) return; | |
console.log(command.toString(16).toUpperCase(), ch, value.toString(16).toUpperCase()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment