5

With quite a bit of trial and error I figured out a way to write the following bar in Lilypond:

enter image description here

There are four voices, the top voice has upward stems, the two lower voices have downward stems, and the stems of the middle voice (in eighth notes) should be chosen as if there were no other voices. Furthermore, the notes of the middle voice should be beamed per beat.

This is the code:

\new Staff  \relative c' {
  \key b \minor
  \time 3/4
  \clef "treble_8"
  \mergeDifferentlyHeadedOn

<< { \voiceOne d2 d4 } \\
   { \voiceTwo s4 fis,2 } \\
   { \voiceThree \stemNeutral d'8[ b] fis[ b] d[ b] } \\
   { \voiceFour b,2. } >> |
}

I think the code is quite clumsy and I was wondering if there is a more elegant and efficient way to do this. There are still about 50 similar bars to come ...

I would be very curious to know how someone with better Lilypond skills than me would solve such a problem.

2
  • Just FYI, the normal standard would be that you have two different note heads--a solid one for the eighth notes and an unfilled one for the half note. The eighth notes would also be slightly separated from the half note. I understand if you're avoiding that intentionally for a cleaner look. But I just wanted to mention the possibility.
    – trlkly
    Dec 7, 2022 at 14:06
  • @trlkly: Yes, I know, but I think it looks indeed much cleaner that way.
    – Matt L.
    Dec 7, 2022 at 14:22

1 Answer 1

7

If you have 50 similar bars, I would definitely separate each voice into its own variable. The usual way to go about this is making a variable that contains the things that you want to have in each voice, like this:

global = {
    \key b \minor
    \time 3/4
    \mergeDifferentlyHeadedOn
    \mergeDifferentlyDottedOn
    % whatever else you prefer
}

Then you make a variable for each voice, and you include the global variable into the voice like this

one = {
    \global    % this inserts the whole content of the "global" variable
    \voiceOne
    d2 d4 |
    e2 e4 |   % I made up some more notes
    cis2 cis4 |
    % etc.
}

and you make variables for all the other voices in a similar way. Then you combine them together:

\score
{
    \new Staff <<
        \clef "G_8"  % shorter than "treble_8", that's all
        \new Voice \one
        \new Voice \two
        \new Voice \three
        \new Voice \four
    >>
    \layout { }
    \midi { } % only if you want MIDI output
}

In this way, you have four voices, each of them is filled in from one variable (here, they are called \one through \four but you can use more imaginative names). It's a pretty clean solution (you can even split it into multiple files easily) because you can "factor out" the common stuff into the \global variable (again, you can call this variable however you want) and often you can easily make use of things like \repeat unfold etc.

7
  • Thanks for your answer, that's indeed neater. The disadvantage for me is just that the code for a single bar is then spread out over the source file (one part per voice). But I guess there is no way around that, or is there?
    – Matt L.
    Dec 7, 2022 at 8:45
  • 2
    @MattL. - I don’t think there’s a way around that. On the other hand, as far as I know, LilyPond is not designed to work this way — at least not at scale. Using <<{}\\{}>> should be temporary, not fundamental to the coding of the piece. If I understand correctly, every time <<{}\\{}>> is used, LilyPond silently adds at least two additional Voice contexts to your \score. One <<{}\\{}>> for every measure could end up basically creating hundreds of Voice contexts and make the application choke. Ask me how I know.
    – Neal
    Dec 7, 2022 at 14:44
  • @MattL. — I don't think so either. And I would actually argue that it's (mostly) better to have it this way. Removing whole bars is more difficult, that's true, but otherwise, you keep together things that musically belong together. Then you can make good use of \repeat unfold, automatic filling of notes/durations etc. (For extreme case, imagine an orchestral score with 20 instruments interleaved like in your snippet. Things like "I need to change the violins to play this melody in these 4 bars" become a nightmare.)
    – Ramillies
    Dec 7, 2022 at 15:38
  • 1
    Re "the code for a single bar is then spread out over the source file": have a look at \parallelMusic. Re "could end up basically creating hundreds of Voice contexts and make the application choke": did you witness it choking? I don't see why those extra Voice contexts would be particularly more expensive compared to the objects LilyPond already has to create to typeset the music. Dec 7, 2022 at 18:34
  • @ABOUSAMRAJean: Thanks for pointing out \parallelMusic, didn't know about this.
    – Matt L.
    Dec 7, 2022 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.