1

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
}
  • MIDI files don't have chords, only individual note-on/-off messages. And nobody knows how many instruments your unknown MIDI file uses. – CL. Nov 26 '17 at 13:28
  • Im using piano only tunes, so that means that in order for me to play through all the song sounding good do I need to play all the notes on? – Raul Navarrete Nov 26 '17 at 19:26
  • Are all notes on the same channel? – CL. Nov 26 '17 at 21:36
  • Some are 0 and some are 1, but im passing the channel value inside the parsedMidi array – Raul Navarrete Nov 27 '17 at 15:53
  • Is `if (buffer)` skipped at https://github.com/mudcube/MIDI.js/blob/abcjs/js/adaptors-AudioAPI.js#L236 ? Then the `programId` might be initialized wrong: https://github.com/mudcube/MIDI.js/issues/268 – root Dec 29 '22 at 14:38

0 Answers0