How to specify Web MIDI channel?

942 views Asked by At

How can I specify which channel to send a midi message on, with the MIDI Web API?

This example from the official docs shows how to send a message on channel 1. But in the code snippet, I don't find any reference to that.

// This example sends a middle C note on message immediately on MIDI channel 1 
function sendMiddleC( midiAccess, portID ) {
  var noteOnMessage = [0x90, 60, 0x7f];    // note on, middle C, full velocity
  var output = midiAccess.outputs.get(portID);
  output.send( noteOnMessage );  //omitting the timestamp means send immediately.
}

How can I send that same message, on say channel 2?

1

There are 1 answers

0
TGrif On BEST ANSWER

The midi protocol uses hexadecimal representation for its "messages".

First half is dedicated to the command while the second part is used to specify the channel. They are 16 possible channels (MIDI channels are 0-indexed, as specified in documentation).

Example:
0x90 means Note on (0x90) on Chan 1 (0x90)

So, if you want to send the same message en channel 2, you have to change it like this:

var noteOnMessage = [0x91, 60, 0x7f];

Reference:
Essentials of the MIDI protocol