2016-12-01 88 views
1

讓我們假設我已經在使用網絡MIDI API來偵聽消息的MIDI輸入,現在我試圖理解和利用數據我接收。如何解析網絡MIDI API輸入消息(onmidimessage)

如何解析MIDIMessageEvent中的一些基本信息?

  • 命令
  • 通道
  • 速度

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

  • onNote
  • onPad
  • onPitchBend
  • onModWheel

回答

3

分析和解釋的Web MIDI API輸入消息數據

寫入ES6實施例。

MIDIMessageEventdata可以用解析功能被分拆這樣的:

/** 
* 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輸入(S):

​​

資源: