MidiPlayer: loading multiple instruments from a multi-channel MIDI file

This demo constructs a small MIDI file in JavaScript that uses three different instruments on three different MIDI channels (piano, clarinet, and cello). It then hands the file to music21.miditools.MidiPlayer, which prescans the file for every programChange event, loads every required soundfont up front, and plays each channel with its assigned patch.

How the file is built

A Standard MIDI File is just a header chunk plus one or more track chunks containing delta-time + event byte sequences. The helpers below build a Format-0 file (one track, multi-channel) entirely from a JavaScript array of events:



What the player does

On base64Load(), the player overrides midicube's default loadPlugin hook so that — once the file has been parsed — every soundfont the file references is requested in a single batch. Each channel's initial program is also seeded explicitly via MIDI.programChange() so that a noteOn at tick 0 picks the correct buffer.