![](https://camo.githubusercontent.com/d3d6f73a2f99558164f7fc2128d3dcc63679421f8f98c336febb5a53dfa91a31/68747470733a2f2f726561646d652d747970696e672d7376672e6865726f6b756170702e636f6d2f3f666f6e743d526f626f746f2b4d6f6e6f2673697a653d32352677696474683d33353026636f6c6f723d3436424541336475726174696f6e3d31363030266c696e65733df09f8eb54f6d672b53696d706c652b506c61796572f09f8eb6)
A JS applet reading standard MIDI files, emits JSON events in real time and plays them in the browser.
Create a new player by instantiating MidiPlayer.Player
with an event handler to be called for every MIDI event, then you can load and play a MIDI file.
Using MidiWriterJS is pretty simple.
const MidiPlayer = require('midi-player-js');
// Initialize player and register event handler
const Player = new MidiPlayer.Player(function(event) {
console.log(event);
});
// Load a MIDI file
Player.loadFile('./test.mid');
Player.play();
There are a handful of events on the Player
object which you can subscribe to using the Player.on()
method.
Some events pass data as the first argument of the callback as described below:
Player.on('fileLoaded', function() {
// Do something when file is loaded
});
Player.on('playing', function(currentTick) {
// Do something while player is playing
// (this is repeatedly triggered within the play loop)
});
Player.on('midiEvent', function(event) {
// Do something when a MIDI event is fired.
// (this is the same as passing a function to MidiPlayer.Player() when instantiating.
});
Player.on('endOfFile', function() {
// Do something when end of the file has been reached.
});
Note that because of a common practice called "running status" many MIDI files may use Note on
events with 0
velocity in place of Note off
events.
To be updated... 👀