I'm trying to implement the WebMIDI.js library in my application by following the guide here.
I've added the webmidi.min.js file to my index.html page like so:
<script src="script.js"></script>
<script src="webmidi.min.js"></script>
My script.js file looks like this:
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");
});
However the console is throwing an error regarding WebMidi not being referenced.
'script.js:47 Uncaught ReferenceError: WebMidi is not defined'
As far as I can see, I've included the correct .js file in my html page, and WebMidi is referenced there. I've tried the script tag to the online version of webmidi.min.js, i.e.
<script src="https://cdn.jsdelivr.net/npm/webmidi@2.0.0"></script>
, but to no avail. I'm sure it's a simple enough issue but I can't seem to figure it out. Any help appreciated.