Playing chords with Midi.js using a parsed midi file for asynchronous playing

465 Views Asked by At

I´m trying to basically play piano midi files asynchronously (When a key is pressed I want to advance throughout the midi file notes, basically to play it note by note.

I´ve managed to play a midi file that way but the parsed midi file is throwing notes off (muted), I don´t know if this is because of a missing intrument or because its not that able to parse chords. Here's the MIDI Parser I´m using.

var parser = new MidiParser();
const fileReader = new FileReader();
var counter = 0;
var midiFileInput = document.getElementById('midiFileInput');
var parsedMidi;
const onLoadFile = (loadEvent) => {};
const file = fileReader.result;
const onMidiFileChange = (inputEvent) => {
fileReader.readAsArrayBuffer(inputEvent.target.files[0]);
}
midiFileInput.addEventListener('change', onMidiFileChange);



window.onload = function () {
    MIDI.loadPlugin({
        soundfontUrl: "./soundfont/",
        instrument: "acoustic_grand_piano",
        onprogress: function(state, progress) {
            console.log(state, progress);
        }
    });

};


fileReader.onload = function (file) {
        var uint8Midi = new Uint8Array(file.target.result);
        parsedMidi = parser.parseUint8(uint8Midi);
        console.log(parsedMidi.note)
        // Use parsed midi

        document.addEventListener("keypress", function(event) {
    if (event.keyCode != 1) {

        MIDI.setVolume(0, 127);
        console.log(parsedMidi[counter].note, counter);
        counter = counter +1;
        var velocity = 127; // how hard the note hits
        MIDI.setVolume(0, 127);
        MIDI.noteOn(parsedMidi[counter].channel, parsedMidi[counter].note, parsedMidi[counter].velocity, parsedMidi[counter].delay);
        MIDI.noteOff(parsedMidi[counter].channel, parsedMidi[counter].note, parsedMidi[counter].velocity, parsedMidi[counter].delay + 0.75);  
    }
}) 

        return parsedMidi
}
0

There are 0 best solutions below