2

假设我已经在使用 Web MIDI API 来监听 MIDI 输入以获取消息,现在我正在尝试理解和利用我收到的数据。

我怎样才能解析出一些基本信息MIDIMessageEvent

  • 命令
  • 渠道
  • 笔记
  • 速度

我如何解释一些基本 MIDI 事件的解析信息?

  • onNote
  • 平板电脑
  • onPitchBend
  • onModWheel
4

1 回答 1

10

解析和解释 Web MIDI API 输入消息数据

用 ES6 编写的示例。

datain aMIDIMessageEvent可以用这样的解析函数拆分:

/**
 * 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
  }
}

给定一些用于处理基本 MIDI 事件的事件函数:

function onNote(note, velocity) {}
function onPad(pad, velocity) {}
function onPitchBend(value) {}
function onModWheel(value) {}

我们可以使用上面的解析函数来解释 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)
  }
}

处理程序附加到正确的MIDI 输入:

midiInput.onmidimessage = handleMidiMessage

资源:

于 2016-12-01T04:50:23.523 回答