LilyPond Notation Reference: 3.5.1 Creating MIDI files

### 3.5.1 Creating MIDI files

To create a MIDI output file from a LilyPond input file, add a `\midi` block to a score, for example,

>     \score {
>       ...music...
>       \midi { }
>     }

If there is a `\midi` block in a `\score` with no `\layout` block, only MIDI output will be produced. When notation is needed too, a `\layout` block must also be present.

>     \score {
>       ...music...
>       \midi { }
>       \layout { }
>     }

Pitches, rhythms, ties, dynamics, and tempo changes are interpreted and translated correctly to the MIDI output. Dynamic marks, crescendi and decrescendi translate into MIDI volume levels. Dynamic marks translate to a fixed fraction of the available MIDI volume range. Crescendi and decrescendi make the volume vary linearly between their two extremes. The effect of dynamic markings on the MIDI output can be removed completely, see MIDI block.

The initial tempo and later tempo changes can be specified with the `\tempo` command within the music notation. These are reflected in tempo changes in the MIDI output. This command will normally result in the metronome mark being printed, but this can be suppressed, see Metronome marks. An alternative way of specifying the initial or overall MIDI tempo is described below, see MIDI block.

Due to some limitations on Windows, the default extension for MIDI files on Windows is `.mid`. Other operating systems still use the extension `.midi`. If a different extension is preferred, insert the following line at the top-level of the input file, before the start of any `\book`, `\bookpart` or `\score` blocks:

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

The line above will set the default extension for MIDI files to `.midi`.

Alternatively, this option can also be supplied on the command line:

>     lilypond … -dmidi-extension=midi lilyFile.ly

#### Instrument names

The MIDI instrument to be used is specified by setting the `Staff.midiInstrument` property to the instrument name. The name should be chosen from the list in MIDI instruments.

>     \new Staff {
>       \set Staff.midiInstrument = #"glockenspiel"
>       ...notes...
>     }

>     \new Staff \with {midiInstrument = #"cello"} {
>       ...notes...
>     }

If the selected instrument does not exactly match an instrument from the list of MIDI instruments, the Grand Piano (`"acoustic grand"`) instrument 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 `Staff_performer` to the `Voice` 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 `midiInstrument`.

>     \score {
>       \new Staff <<
>         \new Voice \relative c''' {
>           \set midiInstrument = #"flute"
>           \voiceOne
>           \key g \major
>           \time 2/2
>           r2 g-"Flute" ~
>           g fis ~
>           fis4 g8 fis e2 ~
>           e4 d8 cis 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"
>         }
>         \context {
>           \Score
>           tempoWholesPerMinute = #(ly:make-moment 72 2)
>         }
>       }
>     }

#### Known issues and warnings

Changes in the MIDI volume take place only on starting a note, so crescendi and decrescendi cannot affect the volume of a single note.

Not all midi players correctly handle tempo changes in the midi output. Players that are known to work include MS Windows Media Player and timidity.

Andere talen: deutsch, español, français, 日本語.

#### LilyPond — Notation Reference