Search code examples
javascriptencodingaxiosmidi

Midi/Binary File Decoding in Javascript


I have some midi files that I am returning to the client using Axios, whenever I try and parse them using tone.js I get some encoding issues:

Uncaught (in promise) Bad MIDI file.  Expected 'MTrk', got: '¿½MT'

I have tried using a FileReader, copying to a typed array as per this post but whatever I do I get the same error.

Here is the method in question:

static convertMidiToJson(midi: Blob) {
let result = new Midi();

midi.arrayBuffer().then((it) => {
  result = new Midi(it);
});

return result;

}

midi is just the result.Data from the Axios call.

Any help greatly appreciated!


Solution

  • I had missed the responseType: blob in the axios call. Once I added that, it parsed correctly:

    return axios
          .get(it.data, {
            responseType: "blob",
          })
          .then((itr) => {
            return new Blob([itr.data], { type: "audio/midi" });
          });