Uncaught ReferenceError: WebMidi не определен - PullRequest
0 голосов
/ 03 февраля 2020

Я пытаюсь реализовать библиотеку WebMIDI. js в своем приложении, следуя инструкции здесь .

Я добавил файл webmidi.min. js к моему индексу. html страница примерно так:

<script src="script.js"></script>
<script src="webmidi.min.js"></script>

Мой скрипт. js Файл выглядит так:

if (navigator.requestMIDIAccess) {
    console.log('This browser supports WebMIDI!');
} else {
    console.log('WebMIDI is not supported in this browser.');
}

navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);

function onMIDIFailure() {
    console.log('Could not access your MIDI devices.');
}

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

    switch (command) {
        case 144: // noteOn
            if (velocity > 0) {
                noteOn(note, velocity);
                console.log("Note pressed: " + note + " at " + velocity + " velocity");
            } else {
                noteOff(note);
                console.log("Note stopped");
            }
            break;
        case 128: // noteOff
            noteOff(note);
            break;
        // we could easily expand this switch statement to cover other types of commands such as controllers or sysex
    }
}

function noteOn(note){
}

function noteOff(note){
}

WebMidi.enable(function(err) {
   if (err) console.log("An error occurred", err);
   WebMidi.outputs[0].playNote("C3");
 });

Однако консоль выдает ошибку, касающуюся WebMidi, но не на него ссылаются.

'скрипт. js: 47 Uncaught ReferenceError: WebMidi не определен'

Насколько я вижу, я включил правильный . js файл на моей html странице, и там есть ссылка на WebMidi. Я пробовал тег скрипта для онлайн-версии webmidi.min. js, то есть <script src="https://cdn.jsdelivr.net/npm/webmidi@2.0.0"></script>, но безрезультатно. Я уверен, что это достаточно простая проблема, но я не могу понять это. Любая помощь приветствуется.

1 Ответ

0 голосов
/ 03 февраля 2020

Ваш скрипт. js запущен до загрузки WEBMIDI. js. Попробуйте поменять порядок тегов скрипта.

<script src="webmidi.min.js"></script>
<script src="script.js"></script>
...