Play MIDI files in a browser with a simple API.
This package is used on BitMidi.com, the wayback machine for old-school MIDI files! Check out some examples here:
- Backstreet Boys - I Want It That Way MIDI
- Beethoven Moonlight Sonata MIDI
- Kingdom Hearts - Dearly Beloved MIDI
- Camptown Races MIDI
- Michael Jackson - Billie Jean MIDI
- Michael Jackson - Don't Stop Till You Get Enough MIDI
- Passenger - Let Her Go MIDI
- Red Hot Chili Peppers - Otherside MIDI
- Red Hot Chili Peppers - Californication MIDI
- Golden Sun - Overworld MIDI
- Pokemon - Pokemon Center Theme MIDI
- Pokemon Red Blue Yellow - Opening MIDI
- Pokemon Red Blue Yellow - Wild Pokemon Battle MIDI
- Legend of Zelda - Overworld MIDI
npm install timidity
- Lightweight – Just 23 KB of JavaScript and 22 KB of lazy-loaded WebAssembly
- Simple – No bells and whistles. Just what is needed to play MIDI files.
- Works with the FreePats General MIDI soundset.
const Timidity = require('timidity')
const player = new Timidity()
player.load('/my-file.mid')
player.play()
player.on('playing', () => {
console.log(player.duration) // => 351.521
})
If you just want to play MIDI files in the browser and don't need a JavaScript
API interface, consider using the
bg-sound
package, which supports
this much simpler usage:
<script src="bg-sound.min.js"></script>
<bg-sound src="sound.mid"></bg-sound>
Create a new MIDI player.
Optionally, provide a baseUrl
to customize where the player will look for the
lazy-loaded WebAssembly file libtimidity.wasm
and the
FreePats General MIDI soundset files.
The default baseUrl
is /
.
For example, here is how to mount the necessary files at /
with the express
server:
const timidityPath = path.dirname(require.resolve('timidity'))
app.use(express.static(timidityPath))
const freepatsPath = path.dirname(require.resolve('freepats'))
app.use(express.static(freepatsPath))
This function loads the specified MIDI file urlOrBuf
, which is a string
path
to the MIDI file or a Uint8Array
which contains the MIDI file data.
This should be the first function called on a new Timidity
instance.
Plays the currently loaded MIDI file.
Pauses the currently loaded MIDI file.
Seeks to a specified time in the MIDI file.
If the player is paused when the function is called, it will remain paused. If the function is called from another state (playing, etc.), the player will continue playing.
Returns the duration in seconds (number
) of the currently playing MIDI file.
Note that duration
will return 0
until the file is loaded, which normally
happens just before the playing
event.
Returns the elapsed time in seconds since the MIDI file started playing.
Destroys the entire player instance, stops the current MIDI file from playing, cleans up all resources.
Note: It's best to reuse the same player instance for as long as possible. It is
not recommended to call player.destroy()
to stop or change MIDI files. Rather,
just call player.pause()
to pause or player.load()
to load a new MIDI file.
Returns true
if destroy()
has been called on the player. Returns false
otherwise.
This event fires if a fatal error occurs in the player, including if a MIDI file is unable to be played.
This event fires when the time indicated by the currentTime
property has been
updated.
This event fires when a new MIDI file is being loaded.
This event fires when a MIDI file has played until the end.
This event fires when a MIDI file starts playing.
This event fires when a MIDI file is paused.
This event fires when a MIDI file is loading.
Copyright (c) Feross Aboukhadijeh.