Home:ALL Converter>Make Web MIDI API listen to a specific channel

Make Web MIDI API listen to a specific channel

Ask Time:2022-11-25T12:05:35         Author:DragoNico10

Json Formatter

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.

Author:DragoNico10,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74568235/make-web-midi-api-listen-to-a-specific-channel
yy