At the Sounding Edge: LilyPond, Part 1

Audio/Video

by Dave Phillips

Judging from the response to last month's column, it seems that many readers are interested in applications and utilities designed for practicing musicians. One reader brought up the subject of scoring software, which are programs designed for formatting music notation for printing. This month's column looks at music notation software for Linux, with a special focus on the LilyPond project.

First, a few remarks concerning the subject of music notation. Modern Western music notation is the result of hundreds of years of evolving practice. From its beginnings as a means of notating plainchant the system has grown to accommodate a bewildering array of symbols, signs and objects, all intended to convey a more or less accurate set of instructions describing a performance of the composer's musical intentions involving possibly huge instrumental and vocal resources. Learning to read and write standard Western music notation is a non-trivial task, and real fluency requires considerable effort.

Alas, the computer can't do your sight-reading for you yet, but it can relieve many burdensome clerical aspects of notation, particularly in regards to manuscript preparation and score printing. Programs are available that act as virtual manuscript paper, presenting the user with a palette of conventional music symbols such as staffs (or more pedantically, staves), clefs, time signatures, key signatures, notes, rests, articulations and so forth. The composer selects what he needs from the palette, beginning with staff definitions, and proceeds by freely adding and deleting elements to the staff(s). Typically, notes can be auditioned when placed or moved on the virtual staff, and the entire work or any part can be played at any time. When the composer is satisfied with the appearance and sound of his work, he can save or export it in a variety of formats, including MIDI files and PostScript graphics. The Linux applications that belong to this music software category include the MusE and Rosegarden audio/MIDI sequencers and Joerg Anders' NoteEdit program (see Resources).

Music typesetting software is another category. A music typesetting system is designed for formatting publication-quality output files. These systems typically work with a text-based interface (a language) and a compiler or preprocessor (the program). The user writes a specification file in the system's language and processes it with the typesetting program. Output usually is a scalable graphics format, such as PostScript or Adobe PDF, that can be printed with the common Linux printing tools. With a powerful enough language, every detail of a score's appearance can be customized for perfectly readable and beautiful printed music. Linux music applications in this category include MusiXTeX, Mup, abc and LilyPond.

A music specification language may seem like an odd way to deal with something so visual as music notation, but preparing a music manuscript is not necessarily a straightforward process. Note groupings, placement of accidentals, beaming requirements, necessary stem directions and many other factors influence the appearance and usability of the printed score, and the variations of those factors cannot be predicted and accommodated easily by formula. Even the most flexible notation GUI is not quite WYSIWYG, and the underlying program logic must make decisions that may not create optimal output. By contrast, a specification language allows a degree of output customization not commonly encountered in programs dependent on graphic interfaces. Which brings us to LilypPond.

"The name is Pond... LilyPond"

So say Han-Wen Nienhuys and Jan Nieuwenhuizen, the principal architects of LilyPond. They are very clever and articulate fellows, so I'll let their capsule description of LP speak for them:

"Scores made with computers almost always look bland and uninspiring, but for what reason? Machines may be mechanical, but why should their products be? With that thought in mind we started programming seven years ago. We have tried to capture the rules of good music engraving in a program, and that program is called LilyPond. We built it like we expect software to be: robust, open and flexible. The best is that you can create beautiful sheet music comfortably with LilyPond."

Bold statements, indeed, but they're backed by solid programming skills and deep research into the chosen subject. This rest of this article gives you a glimpse of LP's capabilities. If you are a musician wanting to create publication-quality scores of your music, I hope you will be inspired to try LP yourself.

Its Web page refers to LilyPond as an automated engraving system, a software music typesetter designed to create beautiful readable output. Set to its defaults, LilyPond automatically formats most music for excellent printed output, at the same time permitting highly detailed customizations to accommodate virtually any music scoring requirement, including unusual and idiosyncratic notations.

Putting in the Pond

LP's developers state that building the program from source code is a complicated process, and they advise installing a pre-built package, conveniently providing a list of available packages on the LilyPond Web site. Packages currently are available for Red Hat, Debian, Mandrakelinux and Slackware systems. The common source tarball also is available. However you install it, once installation is complete you can start using LP.

The following examples assume LP in version 2.2.0 or higher. LP is a moving target with a brisk development pace; your mileage may vary if you run the examples with another version.

Jumping In

The LilyPond documentation includes a good tutorial guide to the basics of using the language interface. The following code is a fairly simple example of a LilyPond source file:


% This is a comment.

\score {
\notes { c'4 e' g' b'}
}



In this example, the \score element prepares a default 5-line staff with treble clef and common (4/4) time signature. Its braces hold the data to be represented in the score. In this example, the score includes a sequence of notes that starts from middle C and arpeggiates a Cmaj7 chord in quarter notes. Pitch elements include pitch name (c), an octave specifier (') and a duration value (4). If no duration value is given for the next element, it takes its duration from the preceding note.

To create a PostScript graphics file from this code, simply process it with the LP compiler, as follows:


lilypond lj-ex-1.ly



Figure 1 shows the output file, lj-ex-1.ps, as displayed by GhostView.

Figure 1. Basic LilyPond

Now we add a few more language elements to create something a little more complete:


title = "Simple"
composer = "DLP 2004"
}
\score {
\notes { c'4 e' g'8 a'8 b'4 c''1 \bar "|."}
\midi {\tempo 4=132}
\paper { }
}



The new elements include a header block, further specifications for rhythm and pitch, a specifier for a double-bar and directives for MIDI output (with tempocontrol) and LilyPond's default print-ready formats. Figure 2 displays the compiled output, again in the GhostView PostScript file viewer.

Figure 2. More Symbols and Signs

Notation-savvy musicians may notice that I did not specify a bar line expected between b' and c''. As advertised, LilyPond automates many aspects of the score layout but always allows the possibility of customizing the output to virtually any degree.

As a last code example, here's a complex fragment in two staves with considerably more complicated rhythms:


\version "2.2.0"
title = "Toccata Vivace"
subtitle = "For Flute and Bassoon"
composer = "DLP 2004"
}

Flute = \notes \context Voice = Flute {
\set Staff.instrument = "Flute"
\set Staff.midiInstrument = "flute"
\key c \major
\clef treble
\time 3/4 \partial4 r4 | r8. g''16 -\staccato ges'' -\staccato f'' -\staccato b'-\mf -\accent fis''-\accent r e'-\sf  r8 |
r4 \times 2/3 { ees'16 -\staccato d'' -\staccato des'' } f''8\< ~ f''4\! \bar "||"
}

Bassoon = \notes \context Voice = Bassoon {
\set Staff.instrument = "Bassoon"
\set Staff.midiInstrument = "bassoon"
\key c \major
\clef bass
\time 3/4 \partial4 r16 cis'-\mf  -\staccato c' -\staccato d' -\staccato |
f-\accent e'-\accent r a,-\sf  r4 r8 \times 2/3 { a,16 -\staccato g -\staccato cis'\< ~ } |
cis'4\! \>~ \times 2/3 { cis'8\! ( b bes, } d,4-\accent) \bar "||"
}

FluteStaff = \context Staff = FluteStaff <<
\Flute
>>

BassoonStaff = \context Staff = BassoonStaff <<
\Bassoon
>>

\score {
<<
\FluteStaff
\BassoonStaff
>>
\paper { }
\midi {\tempo 4 = 160}
}



Figure 3 displays the PostScript output. With a little thought you should be able to figure out the logic of the code. One point of assistance: LP uses 'is' to indicate a sharp, 'es' to indicate a flat. The other new elements are fairly self-explanatory, especially with reference to Figure 3, and I refer the interested reader to LilyPond's documentation to clarify any remaining obscurity.

Figure 3. A More Complicated Score Engraved by LP

The LilyPond Import Filters

LilyPond provides import filters for files created by abc Finale, and Mup, as well as MIDI, MuseData and certain XML-formatted files. These filters work in varying degrees, and there usually is some amount of manual editing required after importing a file in one of those formats to LilyPond. The LilyPond documentation includes suggestions for using each filter optimally, and the reader is advised to study the current documentation for the latest directions for their best use.

The midi2ly utility provides a good example. It's simple to use, following this basic syntax midi2ly [OPTIONS] foo.mid. However, as with all MIDI-to-notation conversion programs, you must take extra care with your original MIDI file for the best results from midi2ly. Unquantized MIDI recordings are unlikely to render well, and you should quantize both start-times and durations. The utility currently does not transcribe polyphonic music on a single staff, so you may need to split your MIDI data into individual tracks and channels, treating each track/channel as a LilyPond voice. You may need to assign all tracks to the same MIDI channel, and you should save your work as a Type 1 standard MIDI file. Type 0 MIDI files put all data into a single track, defeating midi2ly's efforts at separating parts.

Figure 4 illustrates the results from the conversion of a simple 2-track MIDI sequence. Notice that in the second measure of the upper staff there is a misaligned half-note. The notation is supposed to be two-tied quarter notes, easy enough to repair with a little manual correction. Figure 5 displays the corrected score, with header information (title, author, date) and a cautionary accidental added to the final G in the lower staff.

Figure 4. A MIDI File Converted to LilyPond Format

Figure 5. Output from the Corrected File

My experiments with the other LP import filters yielded similar results. In some cases I could produce a valid LY file with the convert-ly utility by adding this line to the invalid file: \version "1.8". I then could bring the file up to date by processing it with convert-ly:


convert-ly filtered-foo.ly > converted-foo.ly



The converted file should be ready for processing with the LilyPond compiler, but some manual formatting and repair still may be required.

Next month, I'll introduce the LilyPond support found in Rosegarden, NoteEdit and Denemo.

Dave Phillips (dlphilp@bright.net) is a musician, teacher and writer living in Findlay, Ohio. He has been an active member of the Linux audio community since his first contact with Linux in 1995. He is the author of The Book of Linux Music & Sound, as well as numerous articles in Linux Journal.