### 3.5.1 Creating MIDI files

To create a MIDI output file from a LilyPond file, insert a `\midi` block inside a `\score` block;

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

If there is only a `\midi` block in a `\score` (i.e. without any `\layout` block), then only MIDI output will be produced. No musical notation will be typeset.

>     \score {
>       …music…
>       \midi { }
>     }

Dynamics, pitches, rhythms, tempo changes and ties are all interpreted and translated correctly. Dynamic ‘marks’ translate into volume levels with a ‘fixed fraction’ of the available MIDI volume range; crescendi and decrescendi make the volume vary linearly between their two extremes.

All `\tempo` indications, including all subsequent tempo changes, specified within the music notation will be reflected in the MIDI output.

Usually it is enough to leave the `\midi` block empty, but it can contain context rearrangements, new context definitions or code to set the values of properties. Here the tempo is set to 72 quarter-note beats per minute, but only for the MIDI’s audio playback.

>     \score {
>       …music…
>       \midi {
>         \tempo 4 = 72
>       }
>     }

Note that `\tempo` is actually a command for setting properties during the interpretation of the music and in the context of output definitions, like a `\midi` block, is reinterpreted as if it were a context modification.

Context definitions follow the same syntax as those in a `\layout` block;

>     \score {
>       …music…
>       \midi {
>         \context {
>           \Voice
>           \remove "Dynamic_performer"
>         }
>       }
>     }

removes the effect of dynamics from the MIDI output. Translation modules for sound are called ‘performers’.

#### 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 MIDI port, and most devices support only one port.

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"
>         }
>         \tempo 2 = 72
>       }
>     }

#### Known issues and warnings

Some operating systems require a specific file extension for MIDI files. 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 "mid")

This will set the default extension for MIDI files to `.mid`.

Alternatively, an option can be supplied on the command line:

>     lilypond -dmidi-extension=mid MyFile.ly

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

Some MIDI players may not always correctly handle tempo changes in the midi output.

#### See also

Installed Files: ‘`../ly/performer-init.ly`’.

Learning Manual: Other sources of information.

其他语言：deutsch, español, français, italiano, 日本語。
About automatic language selection.

#### LilyPond — Notation Reference v2.18.2 (稳定分支).