I am making a proyect where I'm using the web MIDI API and a loopback MIDI port, but I want to know if there is any way tomakeit listen to different channel other than the first?
Here is my code:
var notes=[]
navigator.requestMIDIAccess()
.then(onMIDISuccess, onMIDIFailure);
function onMIDISuccess(midiAccess) {
for (var input of midiAccess.inputs.values()){
input.onmidimessage = getMIDIMessage;
}
}
function getMIDIMessage(message) {
var command = message.data[0];
var note = message.data[1];
var velocity = (message.data.length > 2) ? message.data[2] : 0; // a velocity value might not be included with a noteOff command
console.log(message)
switch (command) {
case 144: // noteOn
if (velocity > 0) {
noteOn(note, velocity);
} else {
noteOff(note);
}
break;
case 128: // noteOff
noteOff(note);
break;
}
}
function onMIDIFailure() {
console.log('Could not access your MIDI devices.');
}
Also, I haven't seen any property in the MIDI messages related to the channels, so I really don't know how to do it.
The channel is encoded in the first byte. If the value is 144 it means the message is for the first channel. A value of 145 would be for the second channel. Up to 159 which is a note on message for the 16th channel.
The same principle applies to note off messages. A value of 128 is for the first channel. A value of 129 is for the second channel. ...
You can get the index of the channel with a binary operation like this:
const channelIndex = message.data[0] & 0x0F;
The type of event can be checked by shifting the value by four bits.
const eventType = message.data[0] >> 4;
A value of 8 would be a note off message. A value of 9 is a note on message.