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.
modify
Allows easily modifying the sequence's programs and controllers. All parameters are optional.
| TypeScript | |
|---|---|
- programChanges - an array of objects, defined as follows (note the
extends MIDIPatch):
| TypeScript | |
|---|---|
- controllerChanges - an array of objects, defined as follows:
| TypeScript | |
|---|---|
- channelsToClear - an array of numbers, indicating the channel number to effectively mute.
Warning
Clearing the channel removes the messages rather than setting volume to 0! This operation is irreversible if the original midi file is lost.
- channelsToTranspose - an array of objects, defined as follows:
| TypeScript | |
|---|---|
- clearDrumParams -
boolean, if the drum editing parameters (such as pitch, pan, etc.) should be removed. Both XG and GS. - reverbParams -
ReverbProcessorSnapshot, the desired GS reverb params, leave undefined for no change. - chorusParams -
ChorusProcessorSnapshot, the desired GS chorus params, leave undefined for no change. - delayParams -
DelayProcessorSnapshot, the desired GS delay params, leave undefined for no change. - insertionParams -
InsertionProcessorSnapshot, the desired GS insertion params, leave undefined for no change. Object is defined as follows:
applySnapshot
Applies a SynthesizerSnapshot to the sequence in place. This means changing the programs and controllers if they are locked.
| TypeScript | |
|---|---|
- snapshot - the
SynthesizerSnapshotto use.
For example, if channel 1 has locked preset on Drawbar Organ,
this will remove all program changes for channel 1 and add one at the start to change the program to Drawbar organ.
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 trimSoundBank 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