> I would like to include some Staff and Voice context settings in the
> \midi block only if a flag is set to ##t and I have not been able to
> figure out how to do this.  When I try to wrap the \context blocks
> in a code block with #{…#} I get this error:
> trackPerVoiceMWE.ly:31:16: error: syntax error, unexpected '{', expecting
> SCM_IDENTIFIER or SCM_TOKEN or STRING or SYMBOL
>       \context                 {


Probably the easiest way is to include or exclude a whole \midi block like this:


\version "2.24.0"
\include "english.ly"

global = {
  \key c \major
  \time 4/4
}

TrackPerVoice = ##t
\score {
  \new ChoirStaff <<
    \new Staff <<
      \new Voice = "soprano" <<
        \global
        \relative c' { c'4 d e f << g1 \\ { g4 f e2 } >> }
      >>
      \new Voice = "alto" <<
        \global
        \voiceTwo
        \relative c' { c'4 b a g | f e d2 }
      >>
    >>
  >>
  \layout { }
  #(if TrackPerVoice
       (begin
         (display "One track per Voice (may make too many voices with 
polyphony)")
         (display "Turn this off to get sop and alto combined in one track.")
         #{
           \midi {
             \context {
               \Staff
               \remove "Staff_performer"
             }
             \context {
               \Voice
               \consists "Staff_performer"
             }
           }
         #})
       #{ \midi { } #})
}



Alternatively, you can do it more like you were envisioning, but for technical
reasons, you have to start the Scheme code with $ , not # , and you also have
to include an extra \midi { } around the \context blocks (basically because
\context outside \layout or \midi is a different command).



\version "2.24.0"
\include "english.ly"

global = {
  \key c \major
  \time 4/4
}
TrackPerVoice = ##t
\score {
  \new ChoirStaff <<
    \new Staff <<
      \new Voice = "soprano" <<
        \global
        \relative c' { c'4 d e f << g1 \\ { g4 f e2 } >> }
      >>
      \new Voice = "alto" <<
        \global
        \voiceTwo
        \relative c' { c'4 b a g | f e d2 }
      >>
    >>
  >>
  \layout { }
  \midi {
    $(if TrackPerVoice
         (begin
           (display "One track per Voice (may make too many voices with 
polyphony)")
           (display "Turn this off to get sop and alto combined in one track.")
           #{
             \midi {
               \context {
                 \Staff
                 \remove "Staff_performer"
               }
               \context {
                 \Voice
                 \consists "Staff_performer"
               }
             }
           #}))
  }
}




The # vs $ issue is explained here: 
https://extending-lilypond.gitlab.io/en/extending/lily-and-scheme.html#hash-vs-dollar

HTH
Jean

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to