nfroidure / midifile

A MIDI file parser/writer using ArrayBuffers
http://karaoke.insertafter.com
MIT License
199 stars 30 forks source link

How to read a midi file to get the ArrayBuffer? #30

Closed prem2282 closed 5 years ago

prem2282 commented 5 years ago

Issue

I'm a gentledev i:

Expected behavior

Actual behavior

Steps to reproduce the behavior

Debugging informations

Feature request

Feature description

Use cases

prem2282 commented 5 years ago

I see that the MIDIFile.js takes input as array buffer. But I am puzzled about how to read a midi file and get the arraybuffer out of it? I know it is out of the context here. But I am new and need some help.

  1. I want to read the midi file from a URL.
  2. I want to read the midi file from a folder within the application. Lot of examples are showing reading the file as user input. But that will not help me. Please help if you can.
mk-pmb commented 5 years ago

How to obtain file contents as an ArrayBuffer in a web browser is independent from the file type and thus indeed (as you know and wrote) not a MIDI problem. My first search attempt (you can probably do better) was for "browser read file as arraybuffer" and some of the results I found (you can probably find more/better ones) were https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer (reading from a folder; for reading from URL, try the fetch API) and https://github.com/dcodeIO/ProtoBuf.js/wiki/How-to-read-binary-data-in-the-browser-or-under-node.js%3F If those don't help, and neither any you can find yourself, please ask that question in a more general JavaScript help forum.

How to find at what stage your problem really is about MIDI: Open your MIDI file in a hex editor, look at the first 8 bytes. Print the first 8 bytes of your ArrayBuffer as hex in the browser console. If they're the same, then you've probably mastered the file transfer stuff and any remainig problem might be about MIDI.