Cha*_*kal 2 javascript midi ecmascript-6 web-midi
假设我已经在使用Web MIDI API在MIDI输入上侦听消息,现在我正在尝试理解和利用我接收的数据。
如何从中解析一些基本信息MIDIMessageEvent
?
我如何解释一些基本的MIDI事件的解析信息?
用ES6编写的示例。
在data
一个MIDIMessageEvent
可以分割了一个解析函数是这样的:
/**
* Parse basic information out of a MIDI message.
*/
function parseMidiMessage(message) {
return {
command: message.data[0] >> 4,
channel: message.data[0] & 0xf,
note: message.data[1],
velocity: message.data[2] / 127
}
}
Run Code Online (Sandbox Code Playgroud)
提供了一些用于处理基本MIDI事件的事件函数:
function onNote(note, velocity) {}
function onPad(pad, velocity) {}
function onPitchBend(value) {}
function onModWheel(value) {}
Run Code Online (Sandbox Code Playgroud)
我们可能会使用上面的解析函数来解析MIDI消息并调用上述事件函数:
/**
* Handle a MIDI message from a MIDI input.
*/
function handleMidiMessage(message) {
// Parse the MIDIMessageEvent.
const {command, channel, note, velocity} = parseMidiMessage(message)
// Stop command.
// Negative velocity is an upward release rather than a downward press.
if (command === 8) {
if (channel === 0) onNote(note, -velocity)
else if (channel === 9) onPad(note, -velocity)
}
// Start command.
else if (command === 9) {
if (channel === 0) onNote(note, velocity)
else if (channel === 9) onPad(note, velocity)
}
// Knob command.
else if (command === 11) {
if (note === 1) onModWheel(velocity)
}
// Pitch bend command.
else if (command === 14) {
onPitchBend(velocity)
}
}
Run Code Online (Sandbox Code Playgroud)
该处理程序已附加到正确的MIDI输入上:
midiInput.onmidimessage = handleMidiMessage
Run Code Online (Sandbox Code Playgroud)
资源: