LilyPond Notation Reference: 3.5.7 MIDI channel mapping

### 3.5.7 MIDI channel mapping

When generating a MIDI file from a score, LilyPond will automatically assign every note in the score to a MIDI channel, the one on which it should be played when it is sent to a MIDI device. A MIDI channel has a number of controls available to select, for example, the instrument to be used to play the notes on that channel, or to request the MIDI device to apply various effects to the sound produced on the channel. At all times, every control on a MIDI channel can have only a single value assigned to it (which can be modified, however, for example, to switch to another instrument in the middle of a score).

The MIDI standard supports only 16 channels per MIDI device. This limit on the number of channels also limits the number of different instruments which can be played at the same time.

LilyPond creates separate MIDI tracks for each staff, (or discrete instrument or voice, depending on the value of `Score.midiChannelMapping`), and also for each lyrics context. There is no limit to the number of tracks.

To work around the limited number of MIDI channels, LilyPond supports a number of different modes for MIDI channel allocation, selected using the `Score.midiChannelMapping` context property. In each case, if more MIDI channels than the limit are required, the allocated channel numbers wrap around back to 0, possibly causing the incorrect assignment of instruments to some notes. This context property can be set to one of the following values:

`'staff`

  Allocate a separate MIDI channel to each staff in the score (this is the default). All notes in all voices contained within each staff will share the MIDI channel of their enclosing staff, and all are encoded in the same MIDI track.

  The limit of 16 channels is applied to the total number of staff and lyrics contexts, even though MIDI lyrics do not take up a MIDI channel.

`'instrument`

  Allocate a separate MIDI channel to each distinct MIDI instrument specified in the score. This means that all the notes played with the same MIDI instrument will share the same MIDI channel (and track), even if the notes come from different voices or staves.

  In this case the lyrics contexts do not count towards the MIDI channel limit of 16 (as they will not be assigned to a MIDI instrument), so this setting may allow a better allocation of MIDI channels when the number of staves and lyrics contexts in a score exceeds 16.

`'voice`

  Allocate a separate MIDI channel to each voice in the score that has a unique name among the voices in its enclosing staff. Voices in different staves are always assigned separate MIDI channels, but any two voices contained within the same staff will share the same MIDI channel if they have the same name. Because `midiInstrument` and the several MIDI controls for effects are properties of the staff context, they cannot be set separately for each voice. The first voice will be played with the instrument and effects specified for the staff, and voices with a different name from the first will be assigned the default instrument and effects.

  Note: different instruments and/or effects can be assigned to several voices on the same staff by moving the `Staff_performer` from the `Staff` to the `Voice` context, and leaving `midiChannelMapping` to default to `'staff` or set to `'instrument`; see the snippet below.

For example, the default MIDI channel mapping of a score can be changed to the `'instrument` setting as shown:

>     \score {
>       ...music...
>       \midi {
>         \context {
>           \Score
>           midiChannelMapping = #'instrument
>         }
>       }
>     }

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

#### LilyPond — Notation Reference v2.22.2 (stable-branch).