mercredi 29 août 2018

How to handle device plug/unplug with Web MIDI?

I am working on an online musical score reading trainer, a prototype is available here. I was able to implement access to previously connected MIDI keyboard, however, I am struggling to implement a hot plug/unplug scenario.

My last take looks like this:

    setupDevices(inputs){
    let deviceName = '';

    inputs.forEach((value, key, map) => {
        if (value.connection === 'open' || value.connection === 'pending' ){

            value.open().then((midiInput) => {
                console.log(`Opened ${midiInput.name}`);
                midiInput.onmidimessage = midiController.onMIDIMessage;
            }, null);
        } else {
            value.onmidimessage = null;
        }
    });

    if (deviceName.length === 0) {
        deviceName = 'none';
    }

    document.getElementById('connectedDevice').setAttribute('value', deviceName);
},

onMIDISuccess(midiAccess) {
    midiAccess.onstatechange = (evt) => {
        console.log('MIDI config changed');
        midiController.setupDevices(evt.currentTarget.inputs);
    };
}

When debugging all the promises get called with good looking arguments, but I am not able to receive any key down messages. Also console.log(Opened ${midiInput.name}) gets called many more times than expected:

midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:40 Opened KOMPLETE KONTROL EXT - 1
midiController.js:40 Opened KOMPLETE KONTROL - 1
3
midiController.js:40 Opened KOMPLETE KONTROL EXT - 1
midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:40 Opened KOMPLETE KONTROL EXT - 1
midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:40 Opened KOMPLETE KONTROL EXT - 1
2
midiController.js:40 Opened Komplete Kontrol DAW - 1
midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:40 Opened KOMPLETE KONTROL EXT - 1
midiController.js:57 MIDI config changed
midiController.js:40 Opened KOMPLETE KONTROL - 1
midiController.js:40 Opened KOMPLETE KONTROL EXT - 1

 (all this gets printed just after I just turned the controller on). Any hints?




Aucun commentaire:

Enregistrer un commentaire