Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including midifile with all npm packages installed. Try it out:

var midifile = require("midifile")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

midifile v2.0.0

Read/write standard MIDI files.


Read/write standard MIDI files.

NPM version Build status Dependency Status devDependency Status Coverage Status Code Climate Dependency Status

MIDIFile uses the MIDIEvents project and is part of the MIDIPlayer one. You can also check this Karaoke Player built on top of those libraries.

What it does

  • Read MIDI files
  • Check MIDI file structure (using strictMode)
  • Write MIDI files (still experimental)

What it doesn't do


// Your variable with your MIDI file as an ArrayBuffer or UInt8Array instance
var anyBuffer;

// Creating the MIDIFile instance
var midiFile = new MIDIFile(anyBuffer);

// Reading headers
midiFile.header.getFormat(); // 0, 1 or 2
midiFile.header.getTracksCount(); // n
// Time division
if(midiFile.header.getTimeDivision() === MIDIFile.Header.TICKS_PER_BEAT) {
} else {

// MIDI events retriever
var events = midiFile.getMidiEvents();
events[0].subtype; // type of [MIDI event](
events[0].playTime; // time in ms at wich the event must be played
events[0].param1; // first parameter
events[0].param2; // second one

// Lyrics retriever
var lyrics = midiFile.getLyrics();
if ( lyrics.length ) {
    lyrics[0].playTime; // Time at wich the text must be displayed
    lyrics[0].text; // The text content to be displayed

// Reading whole track events and filtering them yourself
var events = midiFile.getTrackEvents(0);


// Or for a single track
var trackEventsChunk = midiFile.tracks[0].getTrackContent();
var events = MIDIEvents.createParser(trackEventsChunk);

var event;
while(event = {
    // Printing meta events containing text only
    if(event.type === MIDIEvents.EVENT_META && event.text) {
        console.log('Text meta: '+event.text);


Unit tests are using mocha and NodeJS. Install them and run the following command:

mocha tests/*.mocha.js

Why ArrayBuffers ?

ArrayBuffer instances are the best way to manage binary data like MIDI files.

Why not streams ?

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).


  • ArrayBuffer, DataView or their polyfills


  • Feel free to PR
  • If you find a MIDI File the library can't read an if it's under a free, PR the file in the sounds folder and add tests for him. I'll work on it asap.




RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free