+
+@node MIDI output
+@section MIDI output
+
+@cindex Sound
+@cindex MIDI
+
+MIDI (Musical Instrument Digital Interface) is a standard for
+connecting and controlling digital instruments. A MIDI file is a
+series of notes in a number of tracks. It is not an actual
+sound file; you need special software to translate between the
+series of notes and actual sounds.
+
+Pieces of music can be converted to MIDI files, so you can listen to
+what was entered. This is convenient for checking the music; octaves
+that are off or accidentals that were mistyped stand out very much
+when listening to the MIDI output.
+
+@c TODO Check this
+The midi output allocates a channel for each staff, and one for global
+settings. Therefore the midi file should not have more than 15 staves
+(or 14 if you do not use drums). Other staves will remain silent.
+
+@menu
+* Creating MIDI files::
+* MIDI block::
+* MIDI instrument names::
+* Repeats in MIDI::
+* What else goes into the MIDI output?::
+@end menu
+
+@node Creating MIDI files
+@subsection Creating MIDI files
+
+To create a MIDI output file from a LilyPond input file, add a @code{\midi} block
+to a score, for example,
+
+@example
+\score @{
+ @var{...music...}
+ \midi @{ @}
+@}
+@end example
+
+If there is a @code{\midi} block in a @code{\score} with no
+@code{\layout} block, only MIDI output will be produced. When
+notation is needed too, a @code{\layout} block must be also be
+present.
+
+@example
+\score @{
+ @var{...music...}
+ \midi @{ @}
+ \layout @{ @}
+@}
+@end example
+
+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 @ref{MIDI block}.
+
+The initial tempo and later tempo changes can be specified
+with the @code{\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 @ref{Metronome marks}. An alternative way
+of specifying the inital or overall MIDI tempo is described below,
+see @ref{MIDI block}.
+
+@ignore
+@c TODO Investigate dynamicAbsoluteVolumeFunction and the two
+@c midi..Volume properties, then document properly.
+
+The fractions can be adjusted by setting
+@code{dynamicAbsoluteVolumeFunction} in @rinternals{Voice} context.
+For each type of MIDI instrument, a volume range can be defined. This
+gives a basic equalizer control, which can enhance the quality of
+the MIDI output remarkably. The equalizer can be controlled by
+setting @code{instrumentEqualizer}, or by setting
+
+@example
+\set Staff.midiMinimumVolume = #0.2
+\set Staff.midiMaximumVolume = #0.8
+@end example
+
+@end ignore
+
+@knownissues
+
+@c In 2.11 the following no longer seems to be a problem -td
+@ignore
+Unterminated (de)crescendos will not render properly in the midi file,
+resulting in silent passages of music. The workaround is to explicitly
+terminate the (de)crescendo. For example,
+
+@example
+@{ a\< b c d\f @}
+@end example
+
+@noindent
+will not work properly but
+
+@example
+@{ a\< b c d\!\f @}
+@end example
+
+@noindent
+will.
+@end ignore
+
+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 @uref{http://@/timidity@/.sourceforge@/.net/,timidity}.
+
+
+@node MIDI block
+@subsection MIDI block
+@cindex MIDI block
+
+A @code{\midi} block must appear within a score block if MIDI output
+is required. It is analogous to the layout block, but somewhat
+simpler. Often, the @code{\midi} block is left empty, but it
+can contain context rearrangements, new context definitions or code
+to set the values of properties. For example, the following will
+set the initial tempo exported to a MIDI file without causing a tempo
+indication to be printed:
+
+@example
+\score @{
+ @var{...music...}
+ \midi @{
+ \context @{
+ \Score
+ tempoWholesPerMinute = #(ly:make-moment 72 4)
+ @}
+ @}
+@}
+@end example
+
+In this example the tempo is set to 72 quarter note
+beats per minute. This kind of tempo specification cannot take
+a dotted note length as an argument. If one is required, break
+the dotted note into smaller units. For example, a tempo of 90
+dotted quarter notes per minute can be specified as 270 eighth
+notes per minute:
+
+@example
+tempoWholesPerMinute = #(ly:make-moment 270 8)
+@end example
+
+@cindex MIDI context definitions
+
+Context definitions follow precisely the same syntax as those
+within a @code{\layout} block. Translation modules for sound are
+called performers. The contexts for MIDI output are defined in
+@file{ly/@/performer@/-init@/.ly},
+see @rlearning{Other sources of information}.
+For example, to remove the effect of dynamics
+from the MIDI output, insert the following lines in the
+@code{\midi@{ @}} block.
+
+@example
+\midi @{
+ ...
+ \context @{
+ \Voice
+ \remove "Dynamic_performer"
+ @}
+@}
+@end example
+
+MIDI output is created only when a @code{\midi} block is included
+within a score block defined with a @code{\score} command. If it
+is placed within an explicitly instantiated score context (i.e.
+within a @code{\new Score} block) the file will fail. To solve
+this, enclose the @code{\new Score} and the @code{\midi} commands
+in a @code{\score} block.
+
+@example
+\score @{
+ \new Score @{ @dots{}notes@dots{} @}
+ \midi @{ @}
+@}
+@end example
+
+@snippets
+
+@lilypondfile[verbatim,lilyquote,ragged-right,texidoc,doctitle]
+{changing-midi-output-to-one-channel-per-voice.ly}
+
+@node MIDI instrument names
+@subsection MIDI instrument names
+
+@cindex instrument names
+@funindex Staff.midiInstrument
+
+The MIDI instrument name is set by the @code{Staff.midiInstrument}
+property. The instrument name should be chosen from the list in
+@ref{MIDI instruments}.
+
+@example
+\set Staff.midiInstrument = "glockenspiel"
+@var{...notes...}
+@end example
+
+If the selected instrument does not exactly match an instrument from
+the list of MIDI instruments, the Grand Piano (@code{"acoustic grand"})
+instrument is used.
+
+
+@node Repeats in MIDI
+@subsection Repeats in MIDI
+
+@cindex repeats in MIDI
+@funindex \unfoldRepeats
+
+With a few minor additions, all types of repeats can be represented
+in the MIDI output. This is achieved by applying the
+@code{\unfoldRepeats} music function. This function changes all
+repeats to unfold repeats.
+
+@lilypond[quote,verbatim]
+\unfoldRepeats {
+ \repeat tremolo 8 {c'32 e' }
+ \repeat percent 2 { c''8 d'' }
+ \repeat volta 2 {c'4 d' e' f'}
+ \alternative {
+ { g' a' a' g' }
+ {f' e' d' c' }
+ }
+}
+\bar "|."
+@end lilypond
+
+When creating a score file using @code{\unfoldRepeats} for MIDI,
+it is necessary to make two @code{\score} blocks: one for MIDI
+(with unfolded repeats) and one for notation (with volta, tremolo,
+and percent repeats). For example,
+
+@example
+\score @{
+ @var{..music..}
+ \layout @{ .. @}
+@}
+\score @{
+ \unfoldRepeats @var{..music..}
+ \midi @{ .. @}
+@}
+@end example
+
+
+@node What else goes into the MIDI output?
+@subsection What else goes into the MIDI output?
+
+@c TODO Check grace notes - timing is suspect?
+
+@unnumberedsubsubsec Microtones
+
+@cindex microtones in MIDI
+
+Microtones consisting of half sharps and half flats are exported
+to the MIDI file and render correctly in MIDI players which support
+pitch bending. See @ref{Note names in other languages}. Here is
+an example showing all the half sharps and half flats. It can be
+copied out and compiled to test microtones in your MIDI player.
+
+@lilypond[verbatim,quote]
+\score {
+ \relative c' {
+ c cih cis cisih
+ d dih ees eeh
+ e eih f fih
+ fis fisih g gih
+ gis gisih a aih
+ bes beh b bih
+ }
+ \layout {}
+ \midi {}
+}
+@end lilypond
+
+@knownissues
+
+@c TODO List things that have no effect
+@c TODO Check these
+
+Many musically interesting effects, such as swing, articulation,
+slurring, etc., are not translated to midi. Also, figured bass,
+chords, and lyrics have no effect on MIDI.
+
+
+