npm install midifile --save
You can also find a very trivial MIDI player in the test folder.
// Your variable with a ArrayBuffer instance containing your MIDI filevar anyBuffer;// Creating the MIDIFile instancevar midiFile = anyBuffer;// Reading headersmidiFileheader; // 0, 1 or 2midiFileheader; // n// Time divisionifmidiFileheader === MIDIFileHeaderTICKS_PER_BEATmidiFileheader;elsemidiFileheader;midiFileheader;// MIDI events retrievervar events = midiFile;events0subtype; // type of [MIDI event]()events0playTime; // time in ms at wich the event must be playedevents0param1; // first parameterevents0param2; // second one// Lyrics retrievervar lyrics = midiFile;lyrics0playTime; // Time at wich the text must be displayedlyrics0text; // The text content to be displayed// Reading whole track events and filtering them yourselfvar trackEventsChunk = midiFile;var events = trackEventsChunk;var event;whileevent=eventsnext// Printing meta events containing text onlyifeventtype === MIDIFileEVENT_META && eventtextconsole;
Unit tests are using mocha and NodeJS. Install them and run the following command :
ArrayBuffer instances are the best way to manage binary data like MIDI files.
The Standard MIDI files format isn't streamable by nature. If you want to stream MIDI file contents, you should consider transforming your files in another format (plain linearized MIDI events should do the job).
Copyright Nicolas Froidure. MIT licence.