1 @c -*- coding: utf-8; mode: texinfo; -*-
4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
6 When revising a translation, copy the HEAD committish of the
7 version that you are working on. For details, see the Contributors'
8 Guide, node Updating translation committishes..
14 @chapter Music engraving
16 This essay describes why LilyPond was created and how it can produce
17 such beautiful sheet music.
20 @c remove 3mm eps bounding box left padding for Sarabande (This will
21 @c require adding a new snippet option to lilypond-book.py
22 @c check formatting of HTML output
25 * The LilyPond story::
27 * Automated engraving::
29 * Putting LilyPond to work::
30 * Engraved examples (BWV 861)::
33 @node The LilyPond story
34 @section The LilyPond story
36 Long before LilyPond had been used to engrave beautiful performance
37 scores, before it could create university course notes or even simple
38 melodies, before there was a community of users around the world or even
39 an essay on music engraving, LilyPond began with a question:
42 Why does most computer output fail to achieve the beauty and balance of
43 a hand-engraved score?
47 Some of the answers can be found by examining the two scores
52 on the following pages.
54 The first score is a beautiful hand-engraved score from 1950 and the
55 second is a modern, computer-engraved edition.
60 Bärenreiter BA 320, @copyright{}1950:
62 @sourceimage{baer-suite1-fullpage,,,png}
67 Henle no. 666, @copyright{}2000:
69 @sourceimage{henle-suite1-fullpage,,,png}
73 The notes here are identical, taken from Bach's first Suite for solo
74 cello, but the appearance is different, especially if you print them out
75 and view them from a distance.
77 (The PDF version of this manual has high-resolution images suitable for
80 Try reading or playing from each of the scores and you will find that
81 the hand-engraved score is more enjoyable to use. It has flowing lines
82 and movement, and it feels like a living, breathing piece of music,
83 while the newer edition seems cold and mechanical.
85 It is hard to immediately see what makes the difference with the newer
86 edition. Everything looks neat and tidy, possibly even @qq{better}
87 because it looks more computerized and uniform. This really puzzled us
88 for quite a while. We wanted to improve computer notation, but we first
89 had to figure out what was wrong with it.
91 The answer lies in the precise, mathematical uniformity of the newer
92 edition. Find the bar line in the middle of each line: in the
93 hand-engraved score the position of these bar lines has some
94 natural variation, while in the newer version they line up almost
95 perfectly. This is shown in these simplified page layout diagrams,
96 traced from the hand-engraved (left) and computer-generated music
101 @sourceimage{pdf/page-layout-comparison,,,}
104 @sourceimage{page-layout-comparison,,,png}
109 In the computer-generated output, even the individual note heads are
110 aligned in vertical columns, making the contour of the melody disappear
111 into a rigid grid of musical markings.
113 There are other differences as well: in the hand-engraved edition the
114 vertical lines are all stronger, the slurs lie closer to the note heads,
115 and there is more variety in the slopes of the beams. Although such
116 details may seem like nitpicking, the result is a score that is easier
117 to read. In the computer-generated output, each line is nearly identical
118 and if the musician looks away for a moment she will be lost on the
121 LilyPond was designed to solve the problems we found in existing
122 software and to create beautiful music that mimics the finest
123 hand-engraved scores.
128 Bärenreiter BA 320, @copyright{}1950:
130 @sourceimage{baer-suite1-fullpage-hires,16cm,,}
133 Henle no. 666, @copyright{}2000:
135 @sourceimage{henle-suite1-fullpage-hires,16cm,,}
139 @node Engraving details
140 @section Engraving details
143 @cindex typography, music
144 @cindex music typography
145 @cindex plate engraving
146 @cindex music engraving
148 The art of music typography is called @emph{(plate) engraving}, a term
149 that derives from the manual process of music printing@footnote{Early
150 European printers explored several processes, including hand-carved
151 wooden blocks, movable type, and engraved sheets of thin metal.
152 Typesetting had the advantage of being more easily corrected and
153 facilitating the inclusion of text and lyrics, but only engraving
154 offered the ability to do unimpeded layout and unanticipated notation.
155 In the end, hand-engraved scores became the standard for all printed
156 music, with the exception of some hymnals and songbooks where
157 typesetting was justified by its ease and economy, even into the
158 twentieth century.}. Just a few decades ago, sheet music was made by
159 cutting and stamping the music into a zinc or pewter plate in mirror
160 image. The plate would be inked, and the depressions caused by the
161 cutting and stamping would hold ink. An image was formed by pressing
162 paper to the plate. The stamping and cutting was done completely by hand
163 and making a correction was cumbersome, so the engraving had to be
164 nearly perfect in one go. Engraving was a highly specialized skill; a
165 craftsman had to complete around five years of training before earning
166 the title of master engraver, and another five years of experience were
167 necessary to become truly skilled.
171 @sourceimage{hader-slaan,,7cm,}
174 @sourceimage{hader-slaan,,,jpg}
178 LilyPond is inspired by traditional manual engravings published by
179 European music publishers in and towards the end of the first half of
180 the twentieth century, including Bärenreiter, Duhem, Durand,
181 Hofmeister, Peters, and Schott. This is sometimes regarded as the peak
182 of traditional musical engraving practice. As we have studied these
183 editions we have learned a great deal about what goes into a
184 well-engraved score, and the aspects that we wanted to imitate in
187 @c Now all newly printed music is produced with computers. This has
188 @c obvious advantages: prints are cheaper to make, editorial work can be
189 @c delivered by email, and the original data can be easily stored.
190 @c Unfortunately, computer-generated scores rarely match the quality of
191 @c hand-engraved scores. Instead, computer printouts have a bland,
192 @c mechanical look, which makes them unpleasant to play from.
199 * Why work so hard?::
203 @unnumberedsubsec Music fonts
205 The images below illustrate some differences between traditional
206 engraving and typical computer output. The left picture shows a scan of
207 a flat symbol from a hand-engraved Bärenreiter edition, while the right
208 picture depicts a symbol from an edition of the same music published in
209 2000. Although both images are printed in the same shade of ink, the
210 earlier version looks darker: the staff lines are heavier, and the
211 Bärenreiter flat has a bold, almost voluptuous rounded look. The right
212 scan, on the other hand, has thinner lines and a straight layout with
215 @multitable @columnfractions .25 .25 .25 .25
219 @sourceimage{baer-flat-gray,,4cm,}
222 @sourceimage{baer-flat-gray,,,png}
227 @sourceimage{henle-flat-gray,,4cm,}
230 @sourceimage{henle-flat-gray,,,png}
235 @sourceimage{henle-flat-bw,,,png}
236 @sourceimage{baer-flat-bw,,,png}
237 @sourceimage{lily-flat-bw,,,png}
248 @cindex musical symbols
253 When we wanted to write a computer program to create music typography,
254 there were no musical fonts freely available that could match the
255 elegance of our favorite scores. Undeterred, we created a font of
256 musical symbols, relying on nice printouts of hand-engraved music. The
257 experience helped develop a typographical taste, and it made us
258 appreciate subtle design details. Without that experience, we would not
259 have realized how ugly the fonts were that we admired at first.
261 Below is a sample of two music fonts: the upper set is the default font
262 in the Sibelius software (the @emph{Opus} font), and the lower set is
263 our own LilyPond font.
267 @sourceimage{pdf/OpusAndFeta,,,}
270 @sourceimage{OpusAndFeta,,,png}
274 The LilyPond symbols are heavier and their weight is more consistent,
275 which makes them easier to read. Fine endings, such as the ones on the
276 sides of the quarter rest, should not end in sharp points, but rather
277 in rounded shapes. This is because sharp corners of the punching dies
278 are fragile and quickly wear out when stamping in metal. Taken together,
279 the blackness of the font must be carefully tuned together with the
280 thickness of lines, beams and slurs to give a strong yet balanced
283 Also, notice that our half-note head is not elliptic but slightly diamond
284 shaped. The vertical stem of a flat symbol is slightly brushed, becoming
285 wider at the top. The sharp and the natural are easier to distinguish
286 from a distance because their angled lines have different slopes and the
287 vertical strokes are heavier.
289 @node Optical spacing
290 @unnumberedsubsec Optical spacing
292 In spacing, the distribution of space should reflect the durations
293 between notes. However, as we saw in the Bach Suite above, many modern
294 scores adhere to the durations with mathematical precision, which leads
295 to poor results. In the next example a motif is printed twice: the
296 first time using exact mathematical spacing, and the second with
297 corrections. Which do you prefer?
299 @cindex optical spacing
320 \override NoteSpacing.stem-spacing-correction = #0.0
321 \override NoteSpacing.same-direction-correction = #0.0
322 \override StaffSpacing.stem-spacing-correction = #0.0
348 \override NoteSpacing.stem-spacing-correction = #0.6
354 @cindex regular rhythms
355 @cindex regular spacing
356 @cindex spacing, regular
358 Each bar in the fragment only uses notes that are played in a constant
359 rhythm. The spacing should reflect that. Unfortunately, the eye deceives
360 us a little; not only does it notice the distance between note heads, it
361 also takes into account the distance between consecutive stems. As a
362 result, the notes of an up-stem/@/down-stem combination should be put
363 farther apart, and the notes of a down-stem/@/up-stem combination should
364 be put closer together, all depending on the combined vertical positions
365 of the notes. The lower two measures are printed with this correction,
366 the upper two measures, however, form down-stem/@/up-stem clumps of
367 notes. A master engraver would adjust the spacing as needed to please
370 The spacing algorithms in LilyPond even take the barlines into account,
371 which is why the final up-stem in the properly spaced example has been
372 given a little more space before the barline to keep it from looking
373 crowded. A down-stem would not need this adjustment.
376 @unnumberedsubsec Ledger lines
381 Ledger lines present a typographical challenge: they make it more
382 difficult to space musical symbols close together and they must be clear
383 enough to identify the pitch at a glance. In the example below, we see
384 that ledger lines should be thicker than normal staff lines and that an
385 expert engraver will shorten a ledger line to allow closer spacing with
386 accidentals. We have included this feature in LilyPond's engraving.
388 @multitable @columnfractions .25 .25 .25 .25
392 @sourceimage{baer-ledger,3cm,,}
395 @sourceimage{baer-ledger,,,png}
401 @sourceimage{lily-ledger,3cm,,}
404 @sourceimage{lily-ledger,,,png}
411 @unnumberedsubsec Optical sizing
413 Music may need to be printed in a range of sizes. Originally, this was
414 accomplished by creating punching dies in each of the required sizes,
415 which meant that each die was designed to look its best at that size.
416 With the advent of digital fonts, a single outline can be mathematically
417 scaled to any size, which is very convenient, but at the smaller sizes
418 the glyphs will appear very light.
420 In LilyPond, we have created fonts in a range of weights, corresponding
421 to a range of music sizes. This is a LilyPond engraving at staff size
426 @sourceimage{pdf/size26,,23mm,}
429 @sourceimage{size26,,,png}
434 and this is the same engraving set at staff size 11, then
435 magnified by 236% to print at the same size as the previous example:
439 @sourceimage{pdf/size11,,23mm,}
442 @sourceimage{size11,,,png}
447 At smaller sizes, LilyPond uses proportionally heavier lines so the
448 music will still read well.
450 This also allows staves of different sizes to coexist peacefully when
451 used together on the same page:
453 @c Grieg's Violin Sonata Op. 45
454 @lilypond[indent=1.5\cm]
467 \set Staff.instrumentName = #"Violin"
468 c'8.(\f^> b16 c d) ees8.(^> d16 c b)
469 g8.(^> b16 c ees) g8-.^> r r
473 \set PianoStaff.instrumentName = #"Piano"
474 \new Staff \relative {
477 s4. s8 r8 r16 <c' f aes c>
478 <c f aes c>4.^> <c ees g>8 r r
480 \new Staff \relative {
485 \once \override DynamicText.X-offset = #-3
487 <ees g c>4.~ <ees g c>8
490 <c g c,>4.~ <c g c,>8
493 r8 r16 <f, c' aes'>16
494 <f c' aes'>4.-> <c' g'>8 r r
502 @node Why work so hard?
503 @unnumberedsubsec Why work so hard?
505 Musicians are usually more absorbed with performing than with studying
506 the looks of a piece of music, so nitpicking typographical details may
507 seem academic. But it is not. Sheet music is performance material:
508 everything is done to aid the musician in letting her perform better,
509 and anything that is unclear or unpleasant to read is a hindrance.
511 Traditionally engraved music uses bold symbols on heavy staff to create
512 a strong, well-balanced look that stands out well when the music is far
513 away from the reader: for example, if it is on a music stand. A careful
514 distribution of white space allows music to be set very tightly without
515 crowding symbols together. The result minimizes the number of page
516 turns, which is a great advantage.
518 This is a common characteristic of typography. Layout should be pretty,
519 not only for its own sake, but especially because it helps the reader in
520 his task. For sheet music this is of double importance because musicians
521 have a limited amount of attention. The less attention they need for
522 reading, the more they can focus on playing the music. In other words,
523 better typography translates to better performances.
525 These examples demonstrate that music typography is an art that is
526 subtle and complex, and that producing it requires considerable
527 expertise, which musicians usually do not have. LilyPond is our
528 effort to bring the graphical excellence of hand-engraved music to
529 the computer age, and make it available to normal musicians. We
530 have tuned our algorithms, font-designs, and program settings to
531 produce prints that match the quality of the old editions we love
532 to see and love to play from.
535 @node Automated engraving
536 @section Automated engraving
538 @cindex engraving, automated
539 @cindex automated engraving
541 Here we describe what is required to create software that can mimic the
542 layout of engraved scores: a method of describing good layouts to the
543 computer and a lot of detailed comparisons with real engravings.
547 * Improvement by benchmarking::
548 * Getting things right::
551 @node Beauty contests
552 @unnumberedsubsec Beauty contests
554 How do we actually make formatting decisions? In other words, which
555 of the three configurations should we choose for the following slur?
560 \once \override Slur.positions = #'(1.5 . 1)
561 e8[( f] g[ a b d,)] r4
562 \once \override Slur.positions = #'(2 . 3)
563 e8[( f] g[ a b d,)] r4
564 e8[( f] g[ a b d,)] r4
568 There are a few books on the art of music engraving
569 available. Unfortunately, they contain simple rules of thumb and some
570 examples. Such rules can be instructive, but they are a far cry from
571 an algorithm that we could readily implement in a computer. Following
572 the instructions from literature leads to algorithms with lots of
573 hand-coded exceptions. Doing all this case analysis is a lot of work,
574 and often not all cases are covered completely:
578 @sourceimage{ross-beam-scan,7cm,,}
581 @sourceimage{ross-beam-scan,,,.jpg}
585 (Image source: Ted Ross, @emph{The Art of Music Engraving})
587 Rather than trying to write detailed layout rules for every possible
588 scenario, we only have to describe the objectives well enough that
589 LilyPond can judge the attractiveness of several alternatives. Then,
590 for each possible configuration we compute an ugliness score and we
591 choose the least ugly configuration.
593 For example, here are three possible slur configurations, and LilyPond
594 has given each one a score in @q{ugly points}. The first example gets
595 15.39 points for grazing one of the noteheads:
600 \once \override Slur.positions = #'(1.5 . 1)
601 e8[(_"15.39" f] g[ a b d,)] r4
606 second one is nicer, but the slur doesn't start or end on the note heads.
607 It gets 1.71 points for the left side and 9.37 points for the right
608 side, plus another 2 points because the slur ascends while the melody
609 descends for a total of 13.08 ugly points:
614 \once \override Slur.positions = #'(2 . 3)
615 e8[(_"13.08" f] g[ a b d,)] r4
619 The final slur gets 10.04
620 points for the gap on the right and 2 points for the upward slope, but
621 it is the most attractive of the three configurations, so LilyPond
627 e8[(_"12.04" f] g[ a b d,)] r4
631 This technique is quite general, and is used to make optimal decisions
632 for beam configurations, ties and dots in chords, line breaks, and page
633 breaks. The results of these decisions can be judged by comparison to
636 @node Improvement by benchmarking
637 @unnumberedsubsec Improvement by benchmarking
639 LilyPond's output has improved gradually over time, and it continues to
640 improve by comparing its output to hand-engraved scores.
642 For example, here is one line of a benchmark piece from a
643 hand-engraved edition (Bärenreiter BA320):
646 @sourceimage{baer-sarabande-hires,16cm,,}
649 @sourceimage{baer-sarabande,,,png}
653 and the same quotation as engraved by a very old version of LilyPond
654 (version 1.4, May 2001):
657 @sourceimage{pdf/lily14-sarabande,16cm,,}
660 @sourceimage{lily14-sarabande,,,png}
663 @noindent The LilyPond 1.4 output is certainly readable, but close
664 comparison with the hand-engraved score showed a lot of errors in the
668 @sourceimage{lily14-sarabande-annotated-hires,16cm,,}
671 @sourceimage{lily14-sarabande-annotated,,,png}
675 @item there is too much space before the time signature
676 @item the stems of the beamed notes are too long
677 @item the second and fourth measures are too narrow
678 @item the slur is awkward-looking
679 @item the trill marks are too big
680 @item the stems are too thin
684 (There were also two missing note heads, several missing editorial
685 annotations, and an incorrect pitch!)
687 By adjusting the layout rules and font design, the output has improved
688 considerably. Compare the same reference score and the output
689 from the current version of LilyPond (@version{}):
692 @sourceimage{baer-sarabande-hires,16cm,,}
695 @sourceimage{baer-sarabande,,,png}
698 @lilypond[staffsize=17.5,line-width=15.9\cm]
703 \mergeDifferentlyDottedOn
705 { \slurDashed d8.-\flageolet( e16) e4.-\trill( d16 e) }
710 <f' a, d,>4. e8( d c)
712 bes8 g' f e16( f g_1 a_2 bes_3 d,_2)
714 cis4.-\trill b8_3( a g)
716 { \slurDashed d'8.( e16) e4.-\trill( d16 e) }
724 The current output is not a clone of the reference edition, but it is
725 much closer to publication quality that the earlier output.
727 @node Getting things right
728 @unnumberedsubsec Getting things right
730 We can also measure LilyPond's ability to make music engraving decisions
731 automatically by comparing LilyPond's output to the output of a
732 commercial software product. In this case we have chosen Finale 2008,
733 which is one of the most popular commercial score writers, particularly
734 in North America. Sibelius is its major rival and appears to be
735 especially strong in the European market.
737 For our comparison we selected Bach's Fugue in G minor from the
738 Well-Tempered Clavier, Book I, BWV 861, whose opening subject is
745 r8 a16 bes c8 bes16 a bes8
751 We made our comparison by engraving the last seven measures of the piece
752 (28--34) in Finale and LilyPond. This is the point in the piece where
753 the subject returns in a three-part stretto and leads into the closing
754 section. In the Finale version, we have resisted the temptation to make
755 any adjustments to the default output because we are trying to show the
756 things that each software package gets right without assistance. The
757 only major edits that we made were adjusting the page size to match this
758 essay and forcing the music onto two systems to make the comparison
759 easier. By default Finale would have engraved two systems of three
760 measures each and a final, full-width system containing a single
763 Many of the differences between the two engravings are visible in
764 measures 28--29, as shown here with Finale first and LilyPond second:
767 @sourceimage{pdf/bwv861mm28-29,14cm,,}
770 @sourceimage{bwv861mm28-29,,,png}
773 @lilypond[staffsize=19.5,line-width=14\cm]
774 global = { \key g \minor }
778 fis'8 d' ees g, fis4 g
779 r8 a16 bes c8 bes16 a d8 r r4
784 d'4 r4 r8 d'16 c bes8 c16 d
788 partIII = \relative {
790 r2 r8 d' ees g, fis4 g r8 a16 bes c8 bes16 a
801 % \set Score.barNumberVisibility = #all-bar-numbers-visible
803 \set Score.currentBarNumber = #28
808 \new Voice = "voiceI" { \partI }
809 \new Voice = "voiceII" { \partII }
815 \new Voice = "voiceIII" { \partIII }
816 \new Voice = "voiceIV" { \partIV }
823 \remove "Time_signature_engraver"
827 \override StaffGrouper.staff-staff-spacing.padding = #1
833 Some shortcomings in the unedited Finale output include:
835 @item Most of the beams extend too far off the staff. A beam that points
836 towards the center of the staff should have a length of about one
837 octave, but engravers shorten this when the beam points away from the
838 staff in multi-voice music. The Finale beaming can easily be improved
839 with their Patterson Beams plug-in, but we elected to skip that step for
841 @item Finale doesn't adjust the positions of interlocking note heads,
842 which makes the music extremely difficult to read when the upper and
843 lower voices exchange positions temporarily:
846 collide = \once \override NoteColumn.force-hshift = #0
850 \new Voice = "sample" \relative c''{
853 { \voiceOne g4 \collide g4 }
854 \new Voice { \voiceTwo bes \collide bes }
857 \new Lyrics \lyricsto "sample" { "good " " bad" }
862 @item Finale has placed all of the rests at fixed heights on the staff.
863 The user is free to adjust them as needed, but the software makes no
864 attempt to consider the content of the other voice. As luck would have
865 it, there are no true collisions between notes and rests in this example,
866 but that has more to do with the positions of the notes than the rest.
867 In other words, Bach deserves more credit for avoiding a complete
868 collision than Finale does.
872 This example is not intended to suggest that Finale cannot be used to
873 produce publication-quality output. On the contrary, in the hands of a
874 skilled user it can and does, but it requires skill and time. One of the
875 fundamental differences between LilyPond and commercial scorewriters is
876 that LilyPond hopes to reduce the amount of human intervention to an
877 absolute minimum, while other packages try to provide an attractive
878 interface in which to make these types of edits.
880 One particularly glaring omission we found from Finale is a missing flat
885 @sourceimage{pdf/bwv861mm33-34-annotate,7.93cm,,}
888 @sourceimage{bwv861mm33-34-annotate,,,png}
893 The flat symbol is required to cancel out the natural in the same
894 measure, but Finale misses it because it occurred in a different voice.
895 So in addition to running a beaming plug-in and checking the spacing on
896 the noteheads and rests, the user must also check each measure for
897 cross-voice accidentals to avoid interrupting a rehearsal over an
900 If you are interested in examining these examples in more detail, the
901 full seven-measure excerpt can be found at the end of this essay along
902 with four different published engravings. Close examination reveals that
903 there is some acceptable variation among the hand-engravings, but that
904 LilyPond compares reasonably well to that acceptable range. There are
905 still some shortcomings in the LilyPond output, for example, it appears
906 a bit too aggressive in shortening some of the stems, so there is room
907 for further development and fine-tuning.
909 Of course, typography relies on human judgment of appearance, so people
910 cannot be replaced completely. However, much of the dull work can be
911 automated. If LilyPond solves most of the common situations correctly,
912 this will be a huge improvement over existing software. Over the course
913 of years, the software can be refined to do more and more things
914 automatically, so manual overrides are less and less necessary. Where
915 manual adjustments are needed, LilyPond's structure has been designed
916 with that flexibility in mind.
918 @node Building software
919 @section Building software
921 This section describes some of the programming decisions that we made
922 when designing LilyPond.
925 * Music representation::
926 * What symbols to engrave?::
927 * Flexible architecture::
931 @node Music representation
932 @unnumberedsubsec Music representation
935 @cindex recursive structures
937 Ideally, the input format for any high-level formatting system is
938 an abstract description of the content. In this case, that would
939 be the music itself. This poses a formidable problem: how can we
940 define what music really is? Instead of trying to find an answer,
941 we have reversed the question. We write a program capable of
942 producing sheet music, and adjust the format to be as lean as
943 possible. When the format can no longer be trimmed down, by
944 definition we are left with content itself. Our program serves as
945 a formal definition of a music document.
947 The syntax is also the user-interface for LilyPond, hence it is
957 to create a quarter note on middle C (C1) and an eighth note on
958 the D above middle C (D1).
966 On a microscopic scale, such syntax is easy to use. On a larger
967 scale, syntax also needs structure. How else can you enter
968 complex pieces like symphonies and operas? The structure is
969 formed by the concept of music expressions: by combining small
970 fragments of music into larger ones, more complex music can be
971 expressed. For example
973 @lilypond[quote,verbatim,fragment]
978 Simultaneous notes can be constructed by enclosing them with
979 @code{<<} and @code{>>}:
985 @lilypond[quote,fragment,relative=1]
986 \new Voice { <<c4 d4 e>> }
990 This expression is put in sequence by enclosing it in curly braces
991 @code{@{@tie{}@dots{}@tie{}@}}:
994 @{ f4 <<c4 d4 e4>> @}
997 @lilypond[quote,relative=1,fragment]
1002 The above is also an expression, and so it may be combined again
1003 with another simultaneous expression (a half note) using
1004 @code{<<}, @code{\\}, and @code{>>}:
1007 << g2 \\ @{ f4 <<c4 d4 e4>> @} >>
1010 @lilypond[quote,fragment,relative=2]
1011 \new Voice { << g2 \\ { f4 <<c d e>> } >> }
1014 Such recursive structures can be specified neatly and formally in
1015 a context-free grammar. The parsing code is also generated from
1016 this grammar. In other words, the syntax of LilyPond is clearly
1017 and unambiguously defined.
1019 User-interfaces and syntax are what people see and deal with most.
1020 They are partly a matter of taste, and also the subject of much
1021 discussion. Although discussions on taste do have their merit,
1022 they are not very productive. In the larger picture of LilyPond,
1023 the importance of input syntax is small: inventing neat syntax is
1024 easy, while writing decent formatting code is much harder. This
1025 is also illustrated by the line-counts for the respective
1026 components: parsing and representation take up less than 10% of
1029 When designing the structures used in LilyPond, we made some different
1030 decisions than are apparent in other software. Consider the hierarchical
1031 nature of music notation:
1033 @lilypond[quote,fragment]
1035 \new Staff \relative {
1038 d''4 g,8 a b c d4 g, g
1040 \new Staff \relative {
1048 In this case, there are pitches grouped into chords that belong to
1049 measures, which belong to staves. This resembles a tidy structure of
1054 @sourceimage{pdf/nestedboxes,,4cm,}
1057 @sourceimage{nestedboxes,,,png}
1061 Unfortunately, the structure is tidy because it is based on some
1062 excessively restrictive assumptions. This becomes apparent if we
1063 consider a more complicated musical example:
1069 \remove "Timing_translator"
1070 \remove "Default_bar_line_engraver"
1074 \consists "Timing_translator"
1075 \consists "Default_bar_line_engraver"
1080 \new Staff = "RH" <<
1081 \new Voice = "I" \relative {
1084 \tuplet 7/6 { g''8 g g g g g g }
1086 r4 <b,, fis' g bes> r4\fermata
1088 \new Voice = "II" \relative {
1093 \change Staff = "LH" \oneVoice
1096 \override Stem.cross-staff = ##t
1097 \override Stem.length = #12
1101 \new Staff = "LH" <<
1102 \new Voice = "III" \relative {
1112 In this example, staves start and stop at will, voices jump around
1113 between staves, and the staves have different time signatures. Many
1114 software packages would struggle with reproducing this example because
1115 they are built on the nested box structure. With LilyPond, on the other
1116 hand, we have tried to keep the input format and the structure as
1117 flexible as possible.
1119 @node What symbols to engrave?
1120 @unnumberedsubsec What symbols to engrave?
1127 The formatting process decides where to place symbols. However,
1128 this can only be done once it is decided @emph{what} symbols
1129 should be printed -- in other words, what notation to use.
1131 Common music notation is a system of recording music that has
1132 evolved over the past 1000 years. The form that is now in common
1133 use dates from the early Renaissance. Although the basic form
1134 (i.e., note heads on a 5-line staff) has not changed, the details
1135 still evolve to express the innovations of contemporary notation.
1136 Hence, common music notation encompasses some 500 years of music.
1137 Its applications range from monophonic melodies to monstrous
1138 counterpoints for a large orchestra.
1140 How can we get a grip on such a seven-headed beast, and force it
1141 into the confines of a computer program? Our solution is to break
1142 up the problem of notation (as opposed to engraving, i.e.,
1143 typography) into digestible and programmable chunks: every type of
1144 symbol is handled by a separate module, a so-called plug-in. Each
1145 plug-in is completely modular and independent, so each can be
1146 developed and improved separately. Such plug-ins are called
1147 @code{engraver}s, by analogy with craftsmen who translate musical
1148 ideas to graphic symbols.
1150 In the following example, we start out with a plug-in for note
1151 heads, the @code{Note_heads_engraver}.
1153 @lilypond[quote,ragged-right]
1154 \include "engraver-example.ily"
1161 \remove "Stem_engraver"
1162 \remove "Phrasing_slur_engraver"
1163 \remove "Slur_engraver"
1164 \remove "Script_engraver"
1165 \remove "New_fingering_engraver"
1166 \remove "Beam_engraver"
1167 \remove "Auto_beam_engraver"
1171 \remove "Accidental_engraver"
1172 \remove "Key_engraver"
1173 \remove "Clef_engraver"
1174 \remove "Bar_engraver"
1175 \remove "Time_signature_engraver"
1176 \remove "Staff_symbol_engraver"
1177 \consists "Pitch_squash_engraver"
1184 Then a @code{Staff_symbol_engraver} adds the staff,
1186 @lilypond[quote,ragged-right]
1187 \include "engraver-example.ily"
1194 \remove "Stem_engraver"
1195 \remove "Phrasing_slur_engraver"
1196 \remove "Slur_engraver"
1197 \remove "Script_engraver"
1198 \remove "New_fingering_engraver"
1199 \remove "Beam_engraver"
1200 \remove "Auto_beam_engraver"
1204 \remove "Accidental_engraver"
1205 \remove "Key_engraver"
1206 \remove "Clef_engraver"
1207 \remove "Bar_engraver"
1208 \consists "Pitch_squash_engraver"
1209 \remove "Time_signature_engraver"
1216 the @code{Clef_engraver} defines a reference point for the staff,
1218 @lilypond[quote,ragged-right]
1219 \include "engraver-example.ily"
1226 \remove "Stem_engraver"
1227 \remove "Phrasing_slur_engraver"
1228 \remove "Slur_engraver"
1229 \remove "Script_engraver"
1230 \remove "New_fingering_engraver"
1231 \remove "Beam_engraver"
1232 \remove "Auto_beam_engraver"
1236 \remove "Accidental_engraver"
1237 \remove "Key_engraver"
1238 \remove "Bar_engraver"
1239 \remove "Time_signature_engraver"
1246 and the @code{Stem_engraver} adds stems.
1248 @lilypond[quote,ragged-right]
1249 \include "engraver-example.ily"
1256 \remove "Phrasing_slur_engraver"
1257 \remove "Slur_engraver"
1258 \remove "Script_engraver"
1259 \remove "New_fingering_engraver"
1260 \remove "Beam_engraver"
1261 \remove "Auto_beam_engraver"
1265 \remove "Accidental_engraver"
1266 \remove "Key_engraver"
1267 \remove "Bar_engraver"
1268 \remove "Time_signature_engraver"
1275 The @code{Stem_engraver} is notified of any note head coming
1276 along. Every time one (or more, for a chord) note head is seen, a
1277 stem object is created and connected to the note head. By adding
1278 engravers for beams, slurs, accents, accidentals, bar lines, time
1279 signature, and key signature, we get a complete piece of notation.
1281 @lilypond[quote,ragged-right]
1282 \include "engraver-example.ily"
1283 \score { \topVoice }
1287 @cindex engraving multiple voices
1290 This system works well for monophonic music, but what about
1291 polyphony? In polyphonic notation, many voices can share a staff.
1293 @lilypond[quote,ragged-right]
1294 \include "engraver-example.ily"
1295 \new Staff << \topVoice \\ \botVoice >>
1298 In this situation, the accidentals and staff are shared, but the
1299 stems, slurs, beams, etc., are private to each voice. Hence,
1300 engravers should be grouped. The engravers for note heads, stems,
1301 slurs, etc., go into a group called @q{Voice context}, while the
1302 engravers for key, accidental, bar, etc., go into a group called
1303 @q{Staff context}. In the case of polyphony, a single Staff
1304 context contains more than one Voice context. Similarly, multiple
1305 Staff contexts can be put into a single Score context. The Score
1306 context is the top level notation context.
1308 @lilypond[quote,ragged-right]
1309 \include "engraver-example.ily"
1312 \new Staff << \topVoice \\ \botVoice >>
1313 \new Staff << \pah \\ \hoom >>
1319 Internals Reference: @rinternals{Contexts}.
1321 @node Flexible architecture
1322 @unnumberedsubsec Flexible architecture
1324 When we started, we wrote the LilyPond program entirely in the C++
1325 programming language; the program's functionality was set in stone
1326 by the developers. That proved to be unsatisfactory for a number
1331 @item When LilyPond makes mistakes, users need to override
1332 formatting decisions. Therefore, the user must have access to the
1333 formatting engine. Hence, rules and settings cannot be fixed by
1334 us at compile-time but must be accessible for users at run-time.
1336 @item Engraving is a matter of visual judgment, and therefore a
1337 matter of taste. As knowledgeable as we are, users can disagree
1338 with our personal decisions. Therefore, the definitions of
1339 typographical style must also be accessible to the user.
1341 @item Finally, we continually refine the formatting algorithms, so
1342 we need a flexible approach to rules. The C++ language forces a
1343 certain method of grouping rules that cannot readily be applied to
1344 formatting music notation.
1348 @cindex Scheme programming language
1350 These problems have been addressed by integrating an interpreter
1351 for the Scheme programming language and rewriting parts of
1352 LilyPond in Scheme. The current formatting architecture is built
1353 around the notion of graphical objects, described by Scheme
1354 variables and functions. This architecture encompasses formatting
1355 rules, typographical style and individual formatting decisions.
1356 The user has direct access to most of these controls.
1358 Scheme variables control layout decisions. For example, many
1359 graphical objects have a direction variable that encodes the
1360 choice between up and down (or left and right). Here you see two
1361 chords, with accents and arpeggios. In the first chord, the
1362 graphical objects have all directions down (or left). The second
1363 chord has all directions up (right).
1365 @lilypond[quote,ragged-right]
1368 \stemDown <e' g b>4_>-\arpeggio
1369 \override Arpeggio.direction = #RIGHT
1370 \stemUp <e g b>4^>-\arpeggio
1375 \override SpacingSpanner.spacing-increment = #3
1382 @cindex score formatting
1383 @cindex formatting a score
1384 @cindex formatting rules
1387 The process of formatting a score consists of reading and writing
1388 the variables of graphical objects. Some variables have a preset
1389 value. For example, the thickness of many lines -- a
1390 characteristic of typographical style -- is a variable with a
1391 preset value. You are free to alter this value, giving your score
1392 a different typographical impression.
1394 @lilypond[quote,ragged-right]
1397 c'4-~ c'16 as g f e16 g bes c' des'4
1400 \new Staff \fragment
1402 \override Beam.beam-thickness = #0.3
1403 \override Stem.thickness = #0.5
1404 \override Bar.thickness = #3.6
1405 \override Tie.thickness = #2.2
1406 \override StaffSymbol.thickness = #3.0
1407 \override Tie.extra-offset = #'(0 . 0.3)
1413 Formatting rules are also preset variables: each object has
1414 variables containing procedures. These procedures perform the
1415 actual formatting, and by substituting different ones, we can
1416 change the appearance of objects. In the following example, the
1417 rule governing which note head objects are used to produce the
1418 note head symbol is changed during the music fragment.
1420 @lilypond[quote,ragged-right]
1421 #(set-global-staff-size 30)
1423 #(define (mc-squared grob orig current)
1424 (let* ((interfaces (ly:grob-interfaces grob))
1425 (pos (ly:grob-property grob 'staff-position)))
1426 (if (memq 'note-head-interface interfaces)
1428 (ly:grob-set-property! grob 'stencil
1429 (grob-interpret-markup grob
1430 (make-lower-markup 0.5
1434 ((-2) (make-smaller-markup (make-bold-markup "2")))
1435 (else "bla")))))))))
1437 \new Voice \relative {
1439 \set autoBeaming = ##f
1442 \once \override NoteHead.stencil = #note-head::brew-ez-stencil
1443 \once \override NoteHead.font-size = #-7
1444 \once \override NoteHead.font-family = #'sans
1445 \once \override NoteHead.font-series = #'bold
1447 \once \override NoteHead.style = #'cross
1449 \applyOutput Voice #mc-squared
1452 { d8[ es-( fis^^ g] fis2-) }
1453 \repeat unfold 5 { \applyOutput Voice #mc-squared s8 }
1460 @node Putting LilyPond to work
1461 @section Putting LilyPond to work
1463 @cindex simple examples
1464 @cindex examples, simple
1466 We have written LilyPond as an experiment of how to condense the
1467 art of music engraving into a computer program. Thanks to all
1468 that hard work, the program can now be used to perform useful
1469 tasks. The simplest application is printing notes.
1471 @lilypond[quote,relative=1]
1479 By adding chord names and lyrics we obtain a lead sheet.
1481 @lilypond[quote,ragged-right]
1483 \chords { c2 c f2 c }
1489 \addlyrics { twin -- kle twin -- kle lit -- tle star }
1493 Polyphonic notation and piano music can also be printed. The
1494 following example combines some more exotic constructs.
1496 @lilypond[quote,line-width=15.9\cm]
1498 title = "Screech and boink"
1499 subtitle = "Random complex notation"
1500 composer = "Han-Wen Nienhuys"
1504 \context PianoStaff <<
1509 \revert Stem.direction
1510 \change Staff = down
1511 \set subdivideBeams = ##t
1515 \change Staff = down
1519 \change Staff = down
1523 \set followVoice = ##t
1524 c'''32([ b''16 a''16 gis''16 g''32)]
1526 s4 \tuplet 3/2 { d'16[ f' g'] } as'32[ b''32 e'' d'']
1528 s4 \autoBeamOff d''8.. f''32
1534 \new Staff = "down" {
1537 \set subdivideBeams = ##f
1538 \override Stem.french-beaming = ##t
1539 \override Beam.beam-thickness = #0.3
1540 \override Stem.thickness = #4.0
1541 g'16[ b16 fis16 g16]
1547 \override Staff.Arpeggio.arpeggio-direction =#down
1548 <cis, e, gis, b, cis>4\arpeggio
1558 \consists "Horizontal_bracket_engraver"
1564 The fragments shown above have all been written by hand, but that
1565 is not a requirement. Since the formatting engine is mostly
1566 automatic, it can serve as an output means for other programs that
1567 manipulate music. For example, it can also be used to convert
1568 databases of musical fragments to images for use on websites and
1569 multimedia presentations.
1571 This manual also shows an application: the input format is text, and can
1572 therefore be easily embedded in other text-based formats such as
1573 @LaTeX{}, HTML, or in the case of this manual, Texinfo. Using the
1574 @command{lilypond-book} program, included with LilyPond, the input
1575 fragments can be replaced by music images in the resulting PDF or HTML
1576 output files. Another example is the third-party OOoLilyPond extension
1577 for OpenOffice.org or LibreOffice, which makes it extremely easy to
1578 embed musical examples in documents.
1580 For more examples of LilyPond in action, full documentation, and the
1581 software itself, see our main website: www.lilypond.org.
1584 @node Engraved examples (BWV 861)
1585 @section Engraved examples (BWV 861)
1587 This section contains four reference engravings and two
1588 software-engraved versions of Bach's Fugue in G minor from the
1589 Well-Tempered Clavier, Book I, BWV 861 (the last seven measures).
1592 Bärenreiter BA5070 (Neue Ausgabe Sämtlicher Werke, Serie V, Band 6.1,
1596 @sourceimage{bwv861-baer,16cm,,}
1599 @sourceimage{bwv861-baer-small,,,png}
1603 Bärenreiter BA5070 (Neue Ausgabe Sämtlicher Werke, Serie V, Band 6.1,
1604 1989), an alternate musical source. Aside from the textual differences,
1605 this demonstrates slight variations in the engraving decisions, even
1606 from the same publisher and edition:
1609 @sourceimage{bwv861-baer-alt,16cm,,}
1612 @sourceimage{bwv861-baer-alt-small,,,png}
1616 Breitkopf & Härtel, edited by Ferruccio Busoni (Wiesbaden, 1894), also
1617 available from the Petrucci Music Library (IMSLP #22081). The editorial
1618 markings (fingerings, articulations, etc.) have been removed for clearer
1619 comparison with the other editions here:
1622 @sourceimage{bwv861-breitkopf,16cm,,}
1625 @sourceimage{bwv861-breitkopf-small,,,png}
1629 Bach-Gesellschaft edition (Leipzig, 1866), available from the Petrucci
1630 Music Library (IMSPL #02221):
1633 @sourceimage{bwv861-gessellschaft,16cm,,}
1636 @sourceimage{bwv861-gessellschaft-small,,,png}
1643 @sourceimage{pdf/bwv861-finale2008a,,,}
1646 @sourceimage{bwv861-finale2008a,,,png}
1651 LilyPond, version @version{}:
1653 @lilypond[staffsize=14.3,line-width=15.9\cm]
1654 global = {\key g \minor}
1658 fis'8 d' ees g, fis4 g
1659 r8 a16 bes c8 bes16 a d8 r r4
1660 r2 r8 d16 ees f8 ees16 d
1661 ees4 ~ 16 d c bes a4 r8 ees'16 d
1662 c8 d16 ees d8 e16 fis g8 fis16 g a4 ~
1663 8 d, g f ees d c bes
1664 a2 g\fermata \bar "|."
1667 partII = \relative {
1669 d'4 r4 r8 d'16 c bes8 c16 d
1670 ees8 d c ees a, r r4
1671 r8 fis16 g a8 g16 fis g2 ~
1673 fis4 g r8 a16 bes c8 bes16 a
1674 bes4. <g b>8 <a c> r <d, g> r
1677 partIII = \relative {
1679 r2 r8 d' ees g, fis4 g r8 a16 bes c8 bes16 a
1680 bes2 ~ 8 b16 a g8 a16 b
1684 r8 a16 bes c8 bes16 a b2
1686 partIV = \relative {
1690 d,8 d'16 c bes8 c16 d ees2 ~
1691 8 ees16 d c8 d16 ees fis,8 a16 g fis8 g16 a
1692 d,8 d'16 c bes8 c16 d ees8 c a fis'
1699 % \set Score.barNumberVisibility = #all-bar-numbers-visible
1701 \set Score.currentBarNumber = #28
1704 \new Staff = "RH" <<
1706 \new Voice = "voiceI" { \partI }
1707 \new Voice = "voiceII" { \partII }
1710 \new Staff = "LH" <<
1713 \new Voice = "voiceIII" { \partIII }
1714 \new Voice = "voiceIV" { \partIV }
1721 \remove "Time_signature_engraver"
1725 \override StaffGrouper.staff-staff-spacing.padding = #1