Typesetting musical scores with LilyPond

An introductory guide to typesetting musical scores using LilyPond. In this example we typeset the music for the “Hush, Little Baby” lullaby

For the last couple of years I’ve been teaching an after school coding club at several schools in the region. Recently I’ve wrote a six week coding course which uses the Sonic Pi programmable synth. To create the worksheets that the children used in class I needed to typeset sections of musical scores to demonstrate a key concept. In the photograph below we’re coding the Star Wars theme tune.

Coding music

LilyPond

When I started looking for a suitable program to create the musical scores I wanted something that was free and preferably something that would work from the command line. I don’t particularly like using graphical interfaces.

Then I found the perfect fit for my needs, LillyPond. LilyPond is a music engraving program, devoted to producing the highest-quality sheet music possible. It brings the aesthetics of traditionally engraved music to computer printouts.

LilyPond is similar to a programming language, you don’t write music by dragging notes from a graphical toolbar and placing them on a dynamically refreshing score; you write music by typing text.

Yes, that’s right, you type LilyPond commands into a simple text file and this text is then interpreted by LilyPond producing beautifully engraved sheet music. Once you get a basic understanding of the various LilyPond commands, it’s easy to quickly produce the music you need.

Getting Started

In this tutorial we’re going create the musical score shown below, so start by going and grabbing a copy of LilyPond from the projects download page. Choose the appropriate link for your operating system.

Mocking Bird

Start by creating a text file called mocking.ly in your favourite text editor program. I use the Geany text editor as it’s simple to use and is available for all major operating systems. It doesn’t matter which editor you use, Windows notepad will suffice.

To get started, type in the commands shown below into your text editor.

\version "2.14.2"
\header {title = "Mocking Bird"}

{
    \time 4/4
    \clef treble

    \relative c' {
        d4 b'8 b8 b4 b4
    }
}

Every LilyPond file should contain a version statement. A version statement is a line that describes the version of LilyPond for which the file was written. By convention, the version statement is placed at the top of the LilyPond file. The second command sets up a header with an appropriate title for the score

The time statement sets the time signature for the piece. In this example we’re using common time or four four time. The clef statement instructs LilyPond to output the treble clef at the start of the score. Other styles are available, such as bass, baritone, tenor and soprano.

The relative c' statement instructs LilyPond to typeset all the notes enclosed within the curly brackets relative to the middle C an a piano keyboard. The letters and numbers within the curly brackets represents the notes for the first bar of music. That’s one d followed by four b notes.

The numbers after the letters depict the duration of the note to typeset. As we’ve already set the time signature to 4/4 (or common time) then to depict a crochet we use the number four, as there are four crochets per bar in common time.

So to depict a minim we would follow the letter with a number two. To depict a quaver we use the number eight, and a semi-quaver would therefore be represented with a number sixteen. Lilypond is clever enough to join the stems of multiple quavers together.

Generating the score

When you’ve finished typing in the code then save the text file mocking.ly and shell out to a command line or terminal session depending on the operating system you’re using. You then run the LilyPond program against your text file to generate an image which will contain the musical score. The command syntax is:

lilypond --png mocking.ly

As the system runs you will see output like:

GNU LilyPond 2.14.2
Processing `mocking.ly'
Preprocessing graphical objects...
Layout output to `mocking.ps'...
Converting to PNG...
success: Compilation successfully completed

Assuming there are no errors in your mocking.ly file, then you should find a new image file in your folder called mocking.png. If you view this file you should have something which looks like:

Mocking Bird

Yeah we’ve typeset our first musical score! now we just need to continue with the remaining bars of music. I’d recommend putting each subsequent bar of music on a separate line within your text file as it’ll make it easier to keep track of where in the score you’re up to.

Modify your mocking.ly file by adding the additional lines shown in green.

\version "2.14.2"
\header {title = "Mocking Bird"}

{
    \time 4/4
    \clef treble

    \relative c' {
        d4 b'8 b8 b4 b4
        b4 a8 a8 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
        d4 b'4 b4 b4
        b4 a4 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
    }
}

Removing the indent on the first stave

When you re-run the text file through LilyPond you should get a png file which shows two staves of music. Notice how each bar line is automatically numbered for you. This is a great help when you’re trying to explain something to children as you can refer to these numbers.

Notice how the first stave is indented slightly. This is the default behaviour of LilyPond, however it’s very easy to change this behaviour by inserting a layout statement to your text file and specifying an indent value of zero. See the code below where we’ve added the layout statement.

\version "2.14.2"
\header {title  = "Mocking Bird"}
\layout {indent = 0}

{
    \time 4/4
    \clef treble

    \relative c' {
        d4 b'8 b8 b4 b4
        b4 a8 a8 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
        d4 b'4 b4 b4
        b4 a4 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
    }
}

Adding lyrics

The next step is to add the lyrics, so that they appear under the correct notes. We do this by including an addlyrics statement with an opening and closing curly brackets. Again, everything that is included within the curly brackets is applied to the lyrics command. By default I found the text size to be too big for lyrics, so I use the set statement to reduce the font size by three points.

Then you type out the lyrics keeping each bar to a single line for easier reading. The key to getting the lyrics to line up with the correct notes is to add the correct duration numbers after the words or part of words.

\version "2.14.2"
\header {title  = "Mocking Bird"}
\layout {indent = 0}

{
    \time 4/4
    \clef treble

    \relative c' {
        d4 b'8 b8 b4 b4
        b4 a8 a8 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
        d4 b'4 b4 b4
        b4 a4 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
    }

    \addlyrics {
        \set fontSize = #-3

        Hush4 lit-8 tle8 ba4 by4
        don't4 say8 a8 word,2
        Mam8 my's8 going8 to8 buy4 you8 a8
        mock4 ing4 bird2

        If4 that4 mock4 ing4
        bird4 won't4 sing2
        Mam8 my's8 going8 to8 buy4 you8 a8
        dia mond4 ring2
    }
}

Adding comments

If you re-look at my finished score, you’ll notice that above the first stave the first two notes have their letter value above, for example D and B. Every time a note appears for the first time in the music I wanted to add its notation above the stave to help those children who were’nt confident in reading music.

Mocking Bird

To achieve this you can use the markup statement enclosing the text you want to appear above the stave within the curly brackets. The tiny statement sets the font size to a smaller font so that the text doesn’t look too big and out of place.

\version "2.14.2"
\header {title  = "Mocking Bird"}
\layout {indent = 0}

{
    \time 4/4
    \clef treble

    \relative c' {
        d^\markup{\tiny D} b'8^\markup{\tiny B} b8 b4 b4
        b4 a8^\markup{\tiny A} a8 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2^\markup{\tiny G}
        d4 b'4 b4 b4
        b4 a4 a2
        d,8 d8 a'8 a8 a4 a8 b8
        a4 g4 g2
    }

    \addlyrics {
        \set fontSize = #-3

        Hush4 lit-8 tle8 ba4 by4
        don't4 say8 a8 word,2
        Mam8 my's8 going8 to8 buy4 you8 a8
        mock4 ing4 bird2

        If4 that4 mock4 ing4
        bird4 won't4 sing2
        Mam8 my's8 going8 to8 buy4 you8 a8
        dia mond4 ring2
    }
}

I hope you found this article useful, if you did please consider sharing it on social media. You can contact me if you have any questions, comments or corrections relating to this article. Also check out the official manuals for LilyPond, there are lot’s of commands and statements you can use to tweak and configure the output of your musical scores.