DEV Community

loading...

Typesetting Musical Scores Using Code

Paul Bradley
Technical Development Manager. AWS Architect. IHE Expert. HL7 & FHIR health interoperability. GoLang. Teaches kids coding. A budding cornet musician.
・7 min read

An introductory guide to typesetting musical scores using LilyPond. In this guide, we'll learn how to typeset the music for the lullaby “Hush, Little Baby”. Besides adding musical notes we'll also cover adding song lyrics.


Introduction

For the last couple of years, I've been teaching an after-school coding club at several schools in my region. For one term each year we focus on coding music using the excellent programmable synth Sonic Pi.

Music is a fun way to learn the concepts of computer programming as many songs learnt as children use structures which are commonly used in computer programming. Note durations can be defined using variables and songs typically have repeated sections or rhythms which can be easily coded using simple loops and functions.

To create the worksheets which the children used during class I needed to typeset sections of musical scores to demonstrate a key concept or learning objective.

In the photograph below we’re peer programming the Star Wars theme tune :-)

Coding the Star Wars Theme Tune

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'm a big fan of the document preparation system LaTeX, so I was looking for something similar for music.

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 quite easy to produce the music you need.

Getting Started

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

Mocking Bird Score

Start by creating a text file called mocking.ly in your favourite text editor program. 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
    }
}
Enter fullscreen mode Exit fullscreen mode

Every LilyPond file should start with a version statement. The version statement is a line which describes which version of LilyPond the source file is intended for. 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. Other time signatures could be 3/4 for a waltz or 2/2 for a march.

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 note on a piano keyboard. The letters and numbers within the curly brackets represent 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

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

Assuming there are no errors in your mocking.ly file, 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 Score

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.

\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
    }
}
Enter fullscreen mode Exit fullscreen mode

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 source file and specifying an indent value of zero. See the code below which has the layout statement added.

\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
    }
}
Enter fullscreen mode Exit fullscreen mode

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 default font size by three points.

When typing out the lyrics it's a good idea to keep 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
    }
}
Enter fullscreen mode Exit fullscreen mode

Adding comments

If you re-look at my finished score, you'll notice that above the first stave the first two notes have their 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 weren’t confident at reading music.

Mocking Bird Score

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
    }
}
Enter fullscreen mode Exit fullscreen mode

I hope you found this introduction to Lilypond useful. Please check out the official manuals for LilyPond as there are lots of commands and statements you can use to tweak and configure the output of your musical scores.

If you don't have the need to typeset music at least give Sonic Pi a try, it really is great fun.

Discussion (4)

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

Still missing adding chords.

Collapse
nhsdeveloper profile image
Paul Bradley Author

If I get some time I'll add some examples of using chords. In the meantime there are some examples here:-

lilypond.org/doc/v2.18/Documentati...

Collapse
toqueparacelul1 profile image
Toques para celular

Thank you so much, great pos.t
toqueparacelular.net/animais

Collapse
chaser20 profile image
Chaser20 • Edited

Those are the great things about music. Thank you very much. toqueparacelular.com/ is where I find and do it.