Hackmidi

A JavaScript library to play MIDI files in Edge, Firefox, Safari, and Chrome.

A fork of midihackery. Powered by libtimidity. Used in Hacklily.

// Click a link in the example below to run that line.


import {playerFromMIDIBuffer} from "hackmidi";

fetch("Chop-28-4.mid")
  .then(response => response.arrayBuffer())
  .then(buffer => playerFromMIDIBuffer(buffer, "samples/"))
  .then(player => {
    player.addChangeListener((timeInSeconds, isPlaying) => {
      console.log(timeInSeconds, isPlaying);
    });

    player.play();

    // ...
    
    player.pause();

    // ...
    
    player.seek(20.5 /* seconds */);

    // ...
    
    player.destroy();
  });


// Want to use this in your own project?
//
//  1. Run `npm install hackmidi --save` to add Hackmidi to
//     your webpack project. Confused? Create an app using
//     https://github.com/facebookincubator/create-react-app
//     and then run that npm install command.
//  2. Serve some samples (patches) somewhere. You can steal
//     them from the docs/public folder in
//     https://github.com/hacklily/hackmidi or an installation
//     of timidity.
//  3. Copy and paste the code above and bend it to your will.