How do you create MIDI files

3.5.1 Create MIDI files

To create a MIDI file from a LilyPond source file, an environment must be added to the environment, something like this:

\ score {\ midi {}}

If there is only one environment and no environment in an environment, only MIDI is produced. If the notation is also required, the environment must also be present:

\ score {\ midi {} \ layout {}}

Pitch, rhythms, transitions, dynamics, and tempo changes are correctly translated into MIDI format. Dynamic signs, crescendo and decrescendo are transmitted in the MIDI volume channel. Dynamic signs are translated into a certain volume value, crescendo and decrescendo achieve a transition between volume values. The effect of dynamic signs can also be removed from the MIDI file. See The MIDI Block.

The initial tempo and subsequent changes in tempo can be noted with the command within the notation. It also changes the tempo in the MIDI file. The command also sets a tempo designation in the notes, which can also be suppressed, see metronome markings. Another way of specifying your own MIDI tempo is shown below, see The MIDI Block.

Due to some limitations on Windows, the default file extension for MIDI files is on Windows systems. Other operating systems continue to use. If a different extension is desired, the following line can be inserted at the top level of the source file, before the beginning of a, or block:

# (ly: set-option 'midi-extension "midi")

This line of code adds the file extension.

As an alternative, this option can also be passed as a command line parameter:

lilypond… -dmidi-extension = midi

Instrument names

The MIDI instrument with which a particular system is to be played is determined by the property that must be set on an instrument name. The names are listed in MIDI instruments and must be noted in the notation defined there.

\ new Staff {\ set Staff.midiInstrument = "glockenspiel"}
\ new Staff \ with {midiInstrument = "cello"} {}

If the spelling does not exactly match a defined instrument from the list, a piano sound is used ().

Selected snippets

Changing MIDI output to one channel per voice

When outputting MIDI, the default behavior is for each staff to represent one MIDI channel, with all the voices on a staff amalgamated. This minimizes the risk of running out of MIDI channels, since there are only 16 available per track.

However, by moving the to the context, each voice on a staff can have its own MIDI channel, as is demonstrated by the following example: despite being on the same staff, two MIDI channels are created, each with a different.

 \ score {\ new Staff << \ new Voice \ relative c '' '{\ set midiInstrument = # "flute" \ voiceOne \ key g \ major \ time 2/2 r2 g- "Flute" ~ g f sharp ~ f sharp4 g8 f sharp e2 ~ e4 d8 c sharp d2} \ new Voice \ relative c '' {\ set midiInstrument = # "clarinet" \ voiceTwo b1- "Clarinet" a2. b8 a g2. fis8 e fis2 r} >> \ layout {} \ midi {\ context {\ Staff \ remove "Staff_performer"} \ context {\ Voice \ consists "Staff_performer"} \ tempo 2 = 72}}

Known issues and warnings

Changes in MIDI volume are only effective when requested at the beginning of a note, so the volume cannot be changed during a note's duration.

Not all MIDI players can properly reflect changes in tempo. Players able to do this include MS Windows Media Player and timidity.