MidiAudioPlayer
A lightweight JavaScript MIDI audio player built on top of the Web Audio API using WebAudioFont. This package enables playback of MIDI files directly in the browser with minimal setup and no heavy dependencies.
Features
- MIDI file playback in modern browsers
- Built on the Web Audio API
- Uses WebAudioFont for preset rendering
- Lightweight and dependency-minimal
- Simple programmatic API
- CLI tool for preset/font handling
Installation
npm install midi-audio-player
Usage
Basic Example
import MidiAudioPlayer from 'midi-audio-player';
const response = await fetch('/examples/data/iwillsurvive.mid');
const buffer = await response.arrayBuffer();
const player = new MidiAudioPlayer({
preset: presetData,
volume: 0.6,
onEndFile: async () => await this.playNextSong()
});
player.play(buffer);
Control Playback
player.play();
player.pause();
player.stop();
Working with AudioContext
Due to browser autoplay restrictions, you should ensure that your AudioContext is resumed after a user interaction.
CLI
This package provides a CLI tool for downloading and converting WebAudioFont assets. You need to provide a WebAudioFont ID and the json file for the destination.
webaudiofont "0000_Chaos_sf2_file" "dest/preset.json"
You can find presets here: Srgy Surkv's WebAudioFonts
Browser Compatibility
Requires a modern browser with support for:
- Depends on WebAudioFont instrument quality and availability
- Not intended for high-fidelity or professional audio rendering
- First playback may be delayed if the AudioContext is not initialized properly