Writing MIDI Files¶
Below is a basic guide to writing .mid and .rmi files.
Writing a MIDI file¶
Important
Also see Creating MIDI Files From Scratch
writeMIDI¶
Writes the sequence as a Standard MIDI File.
| TypeScript | |
|---|---|
The returned value is an ArrayBuffer - a binary representation of the Standard MIDI File.
Example¶
Below is a basic example of writing a modified MIDI file
Writing an .rmi file¶
writeRMIDI¶
This function writes out an RMIDI file (MIDI + SF2). See more info about this format
| TypeScript | |
|---|---|
The method is called on a BasicMIDI instance; that instance is the MIDI file to embed.
Parameters¶
soundBankBinary¶
ArrayBuffer - The binary sound bank (SF2 or DLS) to embed into the file.
configuration¶
Object, optional - A partial options object. All properties are optional.
-
bankOffset -
number- The bank offset to apply to the file. Default0. See this for more info -
soundBank -
BasicSoundBank- The sound bank instance thatsoundBankBinarycontains. Used for correcting bank and program changes whencorrectBankOffsetis enabled. If omitted, bank correction may be less accurate. -
correctBankOffset -
boolean- If the function should correct all program-selects and bank-selects in the MIDI file to reflect the embedded sound bank (i.e., make it self-contained). Recommended unless a specific use-case is required. Defaulttrue. -
metadata -
Object- The metadata of the file. If left undefined, some basic metadata (like the song's title) will be copied from the MIDI.
Important
All metadata properties below are optional.
- name -
string- the name of the song. - engineer -
string- the engineer of the sound bank. - artist -
string- the artist of the song. - album -
string- the album's name. - genre -
string- the song's genre. - comment -
string- a comment about the song. - creationDate -
Date- the creation date of the file. If not provided, current day is used. - copyright -
string- the copyright string. If not provided,midi.getExtraMetadata()is used. - picture -
ArrayBuffer- the album cover of the song. Binary data of the image. - midiEncoding -
string- The encoding of the inner MIDI file. Make sure to pick a value acceptable byTextDecoder.
Warning
Providing any of the metadata fields overrides the info encoding with utf-8.
This behavior is forced due to lack of support for other encodings by the TextEncoder class.
Tip
use trim to drastically reduce the file size. consider also using compression (like shown in example) to save even more space. (using these both methods, I managed to cram a 1GB soundfont into a 5MB RMIDI!)
Example¶
Below is a simple example for exporting an RMIDI file
| HTML | |
|---|---|
Note
This example uses soundfont3 compression. Make sure you've read this