2013-05-13

Hallelujah -- Adventures in LilyPond 4: Notes

Adding music

I define three commands: \sopMusic, \altoMusic, and \bassMusic, to collect and transpose the notes for the different voices.  They are similar and very simple.

sopMusic = \transpose g a {
    \relative c' {
        \global

        \firstVerse
        \refrS

        \secondVerseS
        \refrS

        \thirdVerseS
        \refrS

        \bar "|."
    }
}


The \global command could alternatively be inserted before the \relative block; it doesn't really matter which.  Notes can be entered as absolute (no special command needed) or relative (inside a \relative block): in this case I choose to enter the notes relative to middle c (c').  At the end of the song, I insert a double bar line.

The commands \firstVerse, \refrS, \secondVerseS and so on are defined earlier and contain different parts of the notes (in this case the (unison) notes for the first verse, the soprano notes for the refrain, and the soprano notes for the second verse).  It isn't strictly necessary to break up the notes like this, but I find that it 1) becomes more readable and 2) makes it possible to reuse snippets (such as the notes for the first verse in all three voices)

Note collisions

When adding notes in more than one voice to a staff, sometimes notes from different voices need to be set in the same place because they are at the same pitch at the same time in the song.  LilyPond detects these collisions and either merges the notes together, or moves them apart if they can't be merged.  Example (the double backspace is shorthand for voice partition):

\relative c'' <<
    { a2. b4 a1 }
    \\
    { a2. g4 a1 }

>>


The first A notes can be merged, the B and G notes don't collide, and the final A notes collide but can't be merged, so they are moved apart:
To me the second measure looks very confusing.  One solution is to replace one of the colliding notes with a spacer rest:

\relative c'' <<
    { a2. b4 s1 }
    \\
    { a2. g4 a1 }

>>

This works, but now the notes for the first voice have been tampered with and information is lost.  Lyrics can't be connected to the voice, and the voice can't be moved to its own staff.

Another solution is to tell LilyPond to ignore collisions and just engrave the notes on top of each other:

\relative c'' <<
    \override NoteColumn #'ignore-collision = ##t
    { a2. b4 a1 }
    \\
    { a2. g4 a1 }

>>

This creates new problems, such as turning the first note into some kind of double-dotted monstrosity:
The cleanest and best solution is to switch off collision detection just for that specific note:

\relative c'' <<
    { a2. b4

      \once \override NoteColumn #'ignore-collision = ##t
      a1 }
    \\
    { a2. g4 a1 }
>>

This problem occurs in a few places in the ``Hallelujah'' notes, for instance in the first verse.  I define a command for switching collision detection off temporarily, partly to make it less intrusive, and partly because I can then invoke the same command in the other relevant places:

ignore = \once \override NoteColumn #'ignore-collision = ##t

firstVerse = {
    \partial 8 d8 |
    d4 d8 d4 d8 e8 e e4. d8   | d4 d8 d d d e4 e8 e4 e8   |
    e4 e8 e4 e8 e4 d8 d4 c8   | d8 \ignore d1 r4 d8       |
    d4 d8 d4 d8 e4 e8 fis4 d8 | g8 g8 g4. g8 g8 g8 a4. a8 |
    a4 a8 a4 a8 b4 b b8 a     | a4. g2
}


By the way, LilyPond doesn't care that much about whitespace and aligning bar checks, it's just me being pedantic.  Yes, bar checks.  LilyPond calculates the extent of measures and puts in bar lines by itself without needing any specification in the source.  However, by inserting bar checks (|) where I think a measure should end the notes become more readable (I can count bars to get to a specific place in the source) and I will get a warning from LilyPond if my bar placing doesn't add up correctly (which either means I just counted wrong or, worse, that I've messed up a duration somewhere).

Also note the fis in the next measure.  LilyPond by default uses the Dutch language convention for naming accidentals: -is for sharps and -es for flats.  You can choose between twelve different languages with different note/accidentals naming standards, but I've always used the default one.  For one thing, if I used Swedish naming, the B notes would be H notes, and that's just plain wrong.  (Unless you're J. S. Bach and you want to spell your name in chords.)    


The DRY principle

In programming, the DRY principle ("Don't Repeat Yourself") is a powerful tool used to avoid contradictions.  The principle asserts that every piece of knowledge must have a single representation.  Applying this to LilyPond, this means it's good practice to detect duplicate ranges of notes and bring them together into a single command, which is then invoked in every place where the duplicate notes are used.  For example, both the soprano and the alto voices use the same notes for the first half of their respective second verses.  Instead of having the same notes in both the definitions of \secondVerseS and \secondVerseA, I use the following:

secondVerseFirstHalfSA = {
    r4. d'8 |
    d4 d4. d8 e4 e8 e4 d8  | d4 d8 d4 d8 e4 e8 e4 e8 |
    e4 e8 e4 e8 e4 d8 d c4 | d8 \ignore d1 r4 d8     |
}

secondVerseS = {
    \secondVerseFirstHalfSA
    % ...
}

secondVerseA = {
    \secondVerseFirstHalfSA

    % ... 
}

This means that whatever happens, this particular piece of music will be consistent.  If one voice is correct, the other will also be correct.

Now, in the soprano voice, the second halves of the second and third verses are almost identical: only half a measure in the middle differs.  Maybe this shouldn't bother me, but it does, so by Knuth I will solve it.  I can't use a command to define the relevant notes, but I can use a substitution function:

secondAndThirdVerseSecondHalfS =
#(define-music-function
     (parser location notes)
     (ly:music?)
   #{
    d4 d8 d d d e4 e8 fis4. | g4 g8 g4 g8 #notes |
    a4 a8 a4 a8 b4 b b8 a   | a4. g2
   #})


This function contains the duplicate notes, with a placeholder (#notes) for the differing notes.  It can be used like this:

secondVerseS = {
    \secondVerseFirstHalfSA
    \secondAndThirdVerseSecondHalfS { g8 g a a4 a16 a }
}


Edit: Nnnope.  In the end I had to make some more changes to the second half of the third verse (more about this in part 5), so the secondAndThirdVerseSecondHalfS function isn't viable any more.  The concept is still sound, though.

To be concluded.

No comments:

Post a Comment