2016-12-01 13 views
1

Web MIDI API를 사용하여 이미 메시지의 MIDI 입력을 수신하고 있다고 가정 해 봅시다. 이제는 데이터를 이해하고 사용하려고합니다. 전수.Web MIDI API 입력 메시지 (onmidimessage) 구문 분석 방법

MIDIMessageEvent에서 몇 가지 기본 정보를 어떻게 파싱 할 수 있습니까?

  • 명령
  • 채널
  • 참고
  • 속도 내가 몇 가지 기본적인 MIDI 이벤트에 대한 구문 분석 된 정보를 해석하는 방법

?

  • 는 onNote
  • onPad
  • onPitchBend
  • onModWheel

답변

3

구문 분석하고 해석 웹 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 메시지를 통해 해석 할 수 있습니다. 상기 이벤트 기능 및 통화 :

midiInput.onmidimessage = handleMidiMessage 

자원 :

    핸들러 올바른 MIDI 입력 (S)에 부착 된

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

  • Web MIDI API
  • MIDI message data summary
  • Script by cwilso
  • Script by cotejp