# Can Lilypond change MIDI instruments during a piece?

I am writing a brass piece that includes the instruction con sordino for the trumpet at one point. As there is in fact a muted trumpet MIDI instrument, I would like to switch the staff's channel to that instrument there. However, the only documentation I see suggests that Staff.midiInstrument is a global for the lifetime of the staff.

I tried inserting \override Staff.midiInstrument = "muted trumpet" in the music block, but I got bad grob property path. Aside from shenanigans like creating multiple inline Voices, is there a clean way to switch MIDI instruments during the music?

• FWIW, have you read through 3.5.7 MIDI channel mapping. A quick glance by my non-expert eyes suggests that you can accomplish your goal by changing the midi channel for the trumpet (i.e., instrument) part. Jun 12, 2022 at 3:22
• @Aaron There's no Lilypond way to say "switch channels" other than something as drastic as creating separate Voices, and each Staff appears to have one single permanent midiInstrument setting. Jun 12, 2022 at 3:29
• Allowing I've misunderstood the help page, but it seems to say you can change the instrument used on a particular channel. Jun 12, 2022 at 3:32
• @Aaron Once. I'm trying to switch instruments in the middle of a piece. Jun 12, 2022 at 4:05
• I see. My misunderstanding. I took the page to mean the midi channel could be changed ad hoc. Jun 12, 2022 at 4:16

Fortunately there is (as you've noted) midi instruments for both regular trumpet and muted trumpet.
You can change between them just by placing:
\set Staff.midiInstrument = #"trumpet" or
\set Staff.midiInstrument = #"muted trumpet" in the music at any point.

But, to get the midi channel mapping to change with the midi instrument, you must tell LilyPond to map channels in this way, by placing:
\context { \Score midiChannelMapping = #'instrument } in the midi block.

Here is a working example:

\version "2.23.6"
\language "english"

trumpet = {
\new Staff {
\set Staff.instrumentName =
\markup {
\center-column {
"Trumpet" \vspace #-0.3
\tiny\concat {"in B"\flat}
}
}
\transposition bf
\set Staff.midiInstrument = #"trumpet"
c'4 g' c'' g' |
R1^"con sord." |
\set Staff.midiInstrument = #"muted trumpet"
c'4 g' c''8 8 g'4 |
R1^"senza sord." |
\set Staff.midiInstrument = #"trumpet"
c'4 g' c'' g' |
c'1 |
}
}

\score {
\trumpet
\layout { }
\midi {
\context { \Score midiChannelMapping = #'instrument }
}
}


However, whether the program you are playing your midi file with knows what to do with a "muted trumpet" is another question.

For example if you import the midi file generated from the above into RealPlayer, the program will know what to do, and changes instrument sound at the right time as desired.

But if you import into MuseScore, it will recognise that there are two channels and separate them, but (unfortunately) give them both the same MuseScore instrument.* To get a muted trumpet sound in MuseScore you need to use the "Mute" text for the second instrument, which can be found in the text palette.

* This is because both "trumpet" and "muted trumpet" midi instruments are interpreted as just the "trumpet" MuseScore instrument. Muting is seen as applying an effect, rather than changing instruments (which kinda makes sense). Changing between other instruments (such and flute and piccolo, or oboe and english horn) won't have this problem.

• This is precisely what I tried, and I still got the trumpet instrument throughout the entire playback. I'm using Fluidsynth, which should understand just fine. I'm running this in Lilypond 2.19.83; is this something that may have been implemented/fixed/improved recently? Jun 13, 2022 at 14:32
• @chrylis-cautiouslyoptimistic- The method above should still work just as well with LilyPond v2.19. I have no experience with FluidSynth, so I can only guess what might be going wrong, or what to try next. I think I'd try using a different instrument to see if that helped, (perhaps FluidSynth doesn't have a "mute trumpet" sound, or needs to install a sound-font with one). But, I do suspect the problem is with FluidSynth or the way it is reading/handling the midi file, (not with LilyPond or the midi file). In which case, I suggest asking a new question, one about FluidSynth. Jun 14, 2022 at 1:25