]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/essay/engraving.itely
Doc-es: Oops; fix bad xref.
[lilypond.git] / Documentation / essay / engraving.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
3 @ignore
4     Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5
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..
9 @end ignore
10
11 @c \version "2.17.30"
12
13 @node Music engraving
14 @chapter Music engraving
15
16 This essay describes why LilyPond was created and how it can produce
17 such beautiful sheet music.
18
19 @c TODO:
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
23
24 @menu
25 * The LilyPond story::
26 * Engraving details::
27 * Automated engraving::
28 * Building software::
29 * Putting LilyPond to work::
30 * Engraved examples (BWV 861)::
31 @end menu
32
33 @node The LilyPond story
34 @section The LilyPond story
35
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:
40
41 @quotation
42 Why does most computer output fail to achieve the beauty and balance of
43 a hand-engraved score?
44 @end quotation
45
46 @noindent
47 Some of the answers can be found by examining the two scores
48 @ifnottex
49 below.
50 @end ifnottex
51 @iftex
52 on the following pages.
53 @end iftex
54 The first score is a beautiful hand-engraved score from 1950 and the
55 second is a modern, computer-engraved edition.
56
57 @ifnottex
58 @quotation
59 @noindent
60 Bärenreiter BA 320, @copyright{}1950:
61
62 @sourceimage{baer-suite1-fullpage,,,png}
63 @end quotation
64
65 @quotation
66 @noindent
67 Henle no. 666, @copyright{}2000:
68
69 @sourceimage{henle-suite1-fullpage,,,png}
70 @end quotation
71 @end ifnottex
72
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.
76 @ifnottex
77 (The PDF version of this manual has high-resolution images suitable for
78 printing.)
79 @end ifnottex
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.
84
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.
90
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
97 (right):
98
99 @quotation
100 @iftex
101 @sourceimage{pdf/page-layout-comparison,,,}
102 @end iftex
103 @ifnottex
104 @sourceimage{page-layout-comparison,,,png}
105 @end ifnottex
106 @end quotation
107 @noindent
108
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.
112
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
119 page.
120
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.
124
125 @iftex
126 @page
127 @noindent
128 Bärenreiter BA 320, @copyright{}1950:
129
130 @sourceimage{baer-suite1-fullpage-hires,16cm,,}
131 @page
132 @noindent
133 Henle no. 666, @copyright{}2000:
134 @sp 3
135 @sourceimage{henle-suite1-fullpage-hires,16cm,,}
136 @page
137 @end iftex
138
139 @node Engraving details
140 @section Engraving details
141
142 @cindex engraving
143 @cindex typography, music
144 @cindex music typography
145 @cindex plate engraving
146 @cindex music engraving
147
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.
168
169 @quotation
170 @iftex
171 @sourceimage{hader-slaan,,7cm,}
172 @end iftex
173 @ifnottex
174 @sourceimage{hader-slaan,,,jpg}
175 @end ifnottex
176 @end quotation
177
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
185 LilyPond.
186
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.
193
194 @menu
195 * Music fonts::
196 * Optical spacing::
197 * Ledger lines::
198 * Optical sizing::
199 * Why work so hard?::
200 @end menu
201
202 @node Music fonts
203 @unnumberedsubsec Music fonts
204
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
213 sharp corners.
214
215 @multitable @columnfractions .25 .25 .25 .25
216 @item @tab
217 @ifnotinfo
218 @iftex
219 @sourceimage{baer-flat-gray,,4cm,}
220 @end iftex
221 @ifnottex
222 @sourceimage{baer-flat-gray,,,png}
223 @end ifnottex
224
225 @tab
226 @iftex
227 @sourceimage{henle-flat-gray,,4cm,}
228 @end iftex
229 @ifnottex
230 @sourceimage{henle-flat-gray,,,png}
231 @end ifnottex
232
233 @end ifnotinfo
234 @ifinfo
235 @sourceimage{henle-flat-bw,,,png}
236 @sourceimage{baer-flat-bw,,,png}
237 @sourceimage{lily-flat-bw,,,png}
238 @end ifinfo
239
240
241 @item @tab
242 Bärenreiter (1950)
243 @tab
244 Henle (2000)
245
246 @end multitable
247
248 @cindex musical symbols
249 @cindex font
250 @cindex blackness
251 @cindex balance
252
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.
260
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.
264
265 @quotation
266 @iftex
267 @sourceimage{pdf/OpusAndFeta,,,}
268 @end iftex
269 @ifnottex
270 @sourceimage{OpusAndFeta,,,png}
271 @end ifnottex
272 @end quotation
273
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
281 overall impression.
282
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.
288
289 @node Optical spacing
290 @unnumberedsubsec Optical spacing
291
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?
298
299 @cindex optical spacing
300
301 @lilypond
302 \paper {
303   ragged-right = ##t
304   indent = #0.0
305 }
306
307 music = {
308    c'4 e''4 e'4 b'4
309    \stemDown
310    b'8[ e'' a' e'']
311    \stemNeutral
312    e'8[ e'8 e'8 e'8]
313 }
314 \score
315 {
316   \music
317   \layout {
318     \context {
319       \Staff
320       \override NoteSpacing.stem-spacing-correction = #0.0
321       \override NoteSpacing.same-direction-correction = #0.0
322       \override StaffSpacing.stem-spacing-correction = #0.0
323     }
324   }
325 }
326 @end lilypond
327
328 @lilypond
329 \paper {
330   ragged-right = ##t
331   indent = #0.0
332 }
333
334 music = {
335    c'4 e''4 e'4 b'4 |
336    \stemDown
337    b'8[ e'' a' e'']
338    \stemNeutral
339    e'8[ e'8 e'8 e'8]
340 }
341
342 \score
343 {
344   \music
345   \layout {
346     \context {
347       \Staff
348       \override NoteSpacing.stem-spacing-correction = #0.6
349     }
350   }
351 }
352 @end lilypond
353
354 @cindex regular rhythms
355 @cindex regular spacing
356 @cindex spacing, regular
357
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
368 the eye.
369
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.
374
375 @node Ledger lines
376 @unnumberedsubsec Ledger lines
377
378 @cindex ledger lines
379 @cindex collisions
380
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.
387
388 @multitable @columnfractions .25 .25 .25 .25
389 @item @tab
390
391 @iftex
392 @sourceimage{baer-ledger,3cm,,}
393 @end iftex
394 @ifnottex
395 @sourceimage{baer-ledger,,,png}
396 @end ifnottex
397
398 @tab
399
400 @iftex
401 @sourceimage{lily-ledger,3cm,,}
402 @end iftex
403 @ifnottex
404 @sourceimage{lily-ledger,,,png}
405 @end ifnottex
406
407 @end multitable
408
409
410 @node Optical sizing
411 @unnumberedsubsec Optical sizing
412
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.
419
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
422 26:
423
424 @quotation
425 @iftex
426 @sourceimage{pdf/size26,,23mm,}
427 @end iftex
428 @ifnottex
429 @sourceimage{size26,,,png}
430 @end ifnottex
431 @end quotation
432
433 @noindent
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:
436
437 @quotation
438 @iftex
439 @sourceimage{pdf/size11,,23mm,}
440 @end iftex
441 @ifnottex
442 @sourceimage{size11,,,png}
443 @end ifnottex
444 @end quotation
445
446 @noindent
447 At smaller sizes, LilyPond uses proportionally heavier lines so the
448 music will still read well.
449
450 @ignore
451 This also allows staves of different sizes to coexist peacefully when
452 used together on the same page:
453
454 @c TODO: are the stems in this example the right thickness? How should
455 @c line weights be scaled for small staves?
456
457 @c Grieg's Violin Sonata Op. 45
458 @lilypond[indent=1.5cm]
459 global = {
460   \time 6/8
461   \key c \minor
462 }
463
464 \score {
465   <<
466     \new Staff \with {
467       fontSize = #-4
468       \override StaffSymbol.staff-space = #(magstep -4)
469       \override StaffSymbol.thickness = #(magstep -3)
470     }
471     \relative c' {
472       \global
473       \set Staff.instrumentName = #"Violin"
474       c8.(\f^> b16 c d) ees8.(^> d16 c b)
475       g8.(^> b16 c ees) g8-.^> r r
476       R2.
477     }
478     \new PianoStaff <<
479       \set PianoStaff.instrumentName = #"Piano"
480       \new Staff \relative c' {
481         \global
482         s2.
483         s4. s8 r8 r16 <c f aes c>
484         <c f aes c>4.^> <c ees g>8 r r
485       }
486       \new Staff \relative c {
487         \global
488         \clef "bass"
489         <<
490         {
491           \once \override DynamicText.X-offset = #-3
492           <ees g c>2.~->^\f
493           <ees g c>4.~ <ees g c>8
494         } \\ {
495           <c g c,>2.~
496           <c g c,>4.~ <c g c,>8
497         }
498         >>
499         r8 r16 <f, c' aes'>16
500         <f c' aes'>4.-> <c' g'>8 r r
501       }
502     >>
503   >>
504 }
505 @end lilypond
506 @end ignore
507
508 @node Why work so hard?
509 @unnumberedsubsec Why work so hard?
510
511 Musicians are usually more absorbed with performing than with studying
512 the looks of a piece of music, so nitpicking typographical details may
513 seem academic.  But it is not.  Sheet music is performance material:
514 everything is done to aid the musician in letting her perform better,
515 and anything that is unclear or unpleasant to read is a hindrance.
516
517 Traditionally engraved music uses bold symbols on heavy staff to create
518 a strong, well-balanced look that stands out well when the music is far
519 away from the reader: for example, if it is on a music stand.  A careful
520 distribution of white space allows music to be set very tightly without
521 crowding symbols together.  The result minimizes the number of page
522 turns, which is a great advantage.
523
524 This is a common characteristic of typography.  Layout should be pretty,
525 not only for its own sake, but especially because it helps the reader in
526 his task.  For sheet music this is of double importance because musicians
527 have a limited amount of attention.  The less attention they need for
528 reading, the more they can focus on playing the music.  In other words,
529 better typography translates to better performances.
530
531 These examples demonstrate that music typography is an art that is
532 subtle and complex, and that producing it requires considerable
533 expertise, which musicians usually do not have.  LilyPond is our
534 effort to bring the graphical excellence of hand-engraved music to
535 the computer age, and make it available to normal musicians.  We
536 have tuned our algorithms, font-designs, and program settings to
537 produce prints that match the quality of the old editions we love
538 to see and love to play from.
539
540
541 @node Automated engraving
542 @section Automated engraving
543
544 @cindex engraving, automated
545 @cindex automated engraving
546
547 Here we describe what is required to create software that can mimic the
548 layout of engraved scores: a method of describing good layouts to the
549 computer and a lot of detailed comparisons with real engravings.
550
551 @menu
552 * Beauty contests::
553 * Improvement by benchmarking::
554 * Getting things right::
555 @end menu
556
557 @node Beauty contests
558 @unnumberedsubsec Beauty contests
559
560 How do we actually make formatting decisions?  In other words, which
561 of the three configurations should we choose for the following slur?
562
563 @lilypond
564 \relative c {
565   \clef bass
566   \once \override Slur.positions = #'(1.5 . 1)
567   e8[( f] g[ a b d,)] r4
568   \once \override Slur.positions = #'(2 . 3)
569   e8[( f] g[ a b d,)] r4
570   e8[( f] g[ a b d,)] r4
571 }
572 @end lilypond
573
574 There are a few books on the art of music engraving
575 available.  Unfortunately, they contain simple rules of thumb and some
576 examples.  Such rules can be instructive, but they are a far cry from
577 an algorithm that we could readily implement in a computer.  Following
578 the instructions from literature leads to algorithms with lots of
579 hand-coded exceptions.  Doing all this case analysis is a lot of work,
580 and often not all cases are covered completely:
581
582 @quotation
583 @iftex
584 @sourceimage{ross-beam-scan,7cm,,}
585 @end iftex
586 @ifnottex
587 @sourceimage{ross-beam-scan,,,.jpg}
588 @end ifnottex
589 @end quotation
590
591 (Image source: Ted Ross, @emph{The Art of Music Engraving})
592
593 Rather than trying to write detailed layout rules for every possible
594 scenario, we only have to describe the objectives well enough that
595 LilyPond can judge the attractiveness of several alternatives.  Then,
596 for each possible configuration we compute an ugliness score and we
597 choose the least ugly configuration.
598
599 For example, here are three possible slur configurations, and LilyPond
600 has given each one a score in @q{ugly points}.  The first example gets
601 15.39 points for grazing one of the noteheads:
602
603 @lilypond
604 \relative c {
605   \clef bass
606   \once \override Slur.positions = #'(1.5 . 1)
607   e8[(_"15.39" f] g[ a b d,)] r4
608 }
609 @end lilypond
610
611 The
612 second one is nicer, but the slur doesn't start or end on the note heads.
613 It gets 1.71 points for the left side and 9.37 points for the right
614 side, plus another 2 points because the slur ascends while the melody
615 descends for a total of 13.08 ugly points:
616
617 @lilypond
618 \relative c {
619   \clef bass
620   \once \override Slur.positions = #'(2 . 3)
621   e8[(_"13.08" f] g[ a b d,)] r4
622 }
623 @end lilypond
624
625 The final slur gets 10.04
626 points for the gap on the right and 2 points for the upward slope, but
627 it is the most attractive of the three configurations, so LilyPond
628 selects this one:
629
630 @lilypond
631 \relative c {
632   \clef bass
633   e8[(_"12.04" f] g[ a b d,)] r4
634 }
635 @end lilypond
636
637 This technique is quite general, and is used to make optimal decisions
638 for beam configurations, ties and dots in chords, line breaks, and page
639 breaks.  The results of these decisions can be judged by comparison to
640 real engravings.
641
642 @node Improvement by benchmarking
643 @unnumberedsubsec Improvement by benchmarking
644
645 LilyPond's output has improved gradually over time, and it continues to
646 improve by comparing its output to hand-engraved scores.
647
648 For example, here is one line of a benchmark piece from a
649 hand-engraved edition (Bärenreiter BA320):
650
651 @iftex
652 @sourceimage{baer-sarabande-hires,16cm,,}
653 @end iftex
654 @ifnottex
655 @sourceimage{baer-sarabande,,,png}
656 @end ifnottex
657
658 @noindent
659 and the same quotation as engraved by a very old version of LilyPond
660 (version 1.4, May 2001):
661
662 @iftex
663 @sourceimage{pdf/lily14-sarabande,16cm,,}
664 @end iftex
665 @ifnottex
666 @sourceimage{lily14-sarabande,,,png}
667 @end ifnottex
668
669 @noindent The LilyPond 1.4 output is certainly readable, but close
670 comparison with the hand-engraved score showed a lot of errors in the
671 formatting details:
672
673 @iftex
674 @sourceimage{lily14-sarabande-annotated-hires,16cm,,}
675 @end iftex
676 @ifnottex
677 @sourceimage{lily14-sarabande-annotated,,,png}
678 @end ifnottex
679
680 @itemize @bullet
681 @item there is too much space before the time signature
682 @item the stems of the beamed notes are too long
683 @item the second and fourth measures are too narrow
684 @item the slur is awkward-looking
685 @item the trill marks are too big
686 @item the stems are too thin
687 @end itemize
688
689 @noindent
690 (There were also two missing note heads, several missing editorial
691 annotations, and an incorrect pitch!)
692
693 By adjusting the layout rules and font design, the output has improved
694 considerably.  Compare the same reference score and the output
695 from the current version of LilyPond (@version{}):
696
697 @iftex
698 @sourceimage{baer-sarabande-hires,16cm,,}
699 @end iftex
700 @ifnottex
701 @sourceimage{baer-sarabande,,,png}
702 @end ifnottex
703
704 @lilypond[staffsize=17.5,line-width=15.9\cm]
705 \relative c {
706   \clef "bass"
707   \key d \minor
708   \time 3/4
709   \mergeDifferentlyDottedOn
710   <<
711     { \slurDashed d8.-\flageolet( e16) e4.-\trill( d16 e) }
712      \\
713     { d4_2 a2 }
714   >>
715   \slurDashed
716   <f' a, d,>4. e8( d c)
717   \slurSolid
718   bes8 g' f e16( f g_1 a_2 bes_3 d,_2)
719   \slurDashed
720   cis4.-\trill b8_3( a g)
721   <<
722     { \slurDashed d'8.( e16) e4.-\trill( d16 e) }
723      \\
724     { <f, a>4 a2 }
725   >>
726 }
727 @end lilypond
728
729 @noindent
730 The current output is not a clone of the reference edition, but it is
731 much closer to publication quality that the earlier output.
732
733 @node Getting things right
734 @unnumberedsubsec Getting things right
735
736 We can also measure LilyPond's ability to make music engraving decisions
737 automatically by comparing LilyPond's output to the output of a
738 commercial software product.  In this case we have chosen Finale 2008,
739 which is one of the most popular commercial score writers, particularly
740 in North America.  Sibelius is its major rival and appears to be
741 especially strong in the European market.
742
743 For our comparison we selected Bach's Fugue in G minor from the
744 Well-Tempered Clavier, Book I, BWV 861, whose opening subject is
745
746 @lilypond
747 \relative c' {
748   \key g \minor
749   \clef "treble_8"
750   r8 d ees g, fis4 g
751   r8 a16 bes c8 bes16 a bes8
752 }
753 @end lilypond
754
755 @noindent
756
757 We made our comparison by engraving the last seven measures of the piece
758 (28--34) in Finale and LilyPond.  This is the point in the piece where
759 the subject returns in a three-part stretto and leads into the closing
760 section.  In the Finale version, we have resisted the temptation to make
761 any adjustments to the default output because we are trying to show the
762 things that each software package gets right without assistance.  The
763 only major edits that we made were adjusting the page size to match this
764 essay and forcing the music onto two systems to make the comparison
765 easier.  By default Finale would have engraved two systems of three
766 measures each and a final, full-width system containing a single
767 measure.
768
769 Many of the differences between the two engravings are visible in
770 measures 28--29, as shown here with Finale first and LilyPond second:
771
772 @iftex
773 @sourceimage{pdf/bwv861mm28-29,14cm,,}
774 @end iftex
775 @ifnottex
776 @sourceimage{bwv861mm28-29,,,png}
777 @end ifnottex
778
779 @lilypond[staffsize=19.5,line-width=14\cm]
780 global = { \key g \minor }
781
782 partI = \relative c' {
783   \voiceOne
784   fis8 d' ees g, fis4 g
785   r8 a16 bes c8 bes16 a d8 r r4
786 }
787
788 partII = \relative c' {
789   \voiceTwo
790   d4 r4 r8 d'16 c bes8 c16 d
791   ees8 d c ees a, r r4
792 }
793
794 partIII = \relative c' {
795   \voiceOne
796   r2 r8 d ees g, fis4 g r8 a16 bes c8 bes16 a
797 }
798
799 partIV = \relative c {
800   \voiceTwo
801   d4 r r2
802   r8 d ees g, fis4 a
803 }
804
805 \score {
806   <<
807     % \set Score.barNumberVisibility = #all-bar-numbers-visible
808     % required in 2.13
809     \set Score.currentBarNumber = #28
810     \bar ""
811     \new PianoStaff <<
812       \new Staff = "RH" <<
813         \global
814         \new Voice = "voiceI" { \partI }
815         \new Voice = "voiceII" { \partII }
816       >>
817       \new Staff = "LH"
818       <<
819         \clef "bass"
820         \global
821         \new Voice = "voiceIII" { \partIII }
822         \new Voice = "voiceIV" { \partIV }
823       >>
824     >>
825   >>
826   \layout {
827     \context {
828       \Staff
829       \remove "Time_signature_engraver"
830     }
831     \context {
832       \PianoStaff
833       \override StaffGrouper.staff-staff-spacing.padding = #1
834     }
835   }
836 }
837 @end lilypond
838
839 Some shortcomings in the unedited Finale output include:
840 @itemize @bullet
841 @item Most of the beams extend too far off the staff.  A beam that points
842 towards the center of the staff should have a length of about one
843 octave, but engravers shorten this when the beam points away from the
844 staff in multi-voice music.  The Finale beaming can easily be improved
845 with their Patterson Beams plug-in, but we elected to skip that step for
846 this example.
847 @item Finale doesn't adjust the positions of interlocking note heads,
848 which makes the music extremely difficult to read when the upper and
849 lower voices exchange positions temporarily:
850
851 @lilypond
852 collide = \once \override NoteColumn.force-hshift = #0
853
854 \score {
855   <<
856     \new Voice = "sample" \relative c''{
857       \key g \minor
858       <<
859         { \voiceOne g4 \collide g4 }
860         \new Voice { \voiceTwo bes \collide bes }
861       >>
862     }
863     \new Lyrics \lyricsto "sample" \lyricmode { "good " " bad" }
864   >>
865 }
866 @end lilypond
867
868 @item Finale has placed all of the rests at fixed heights on the staff.
869 The user is free to adjust them as needed, but the software makes no
870 attempt to consider the content of the other voice.  As luck would have
871 it, there are no true collisions between notes and rests in this example,
872 but that has more to do with the positions of the notes than the rest.
873 In other words, Bach deserves more credit for avoiding a complete
874 collision than Finale does.
875
876 @end itemize
877
878 This example is not intended to suggest that Finale cannot be used to
879 produce publication-quality output.  On the contrary, in the hands of a
880 skilled user it can and does, but it requires skill and time.  One of the
881 fundamental differences between LilyPond and commercial scorewriters is
882 that LilyPond hopes to reduce the amount of human intervention to an
883 absolute minimum, while other packages try to provide an attractive
884 interface in which to make these types of edits.
885
886 One particularly glaring omission we found from Finale is a missing flat
887 in measure 33:
888
889 @quotation
890 @iftex
891 @sourceimage{pdf/bwv861mm33-34-annotate,7.93cm,,}
892 @end iftex
893 @ifnottex
894 @sourceimage{bwv861mm33-34-annotate,,,png}
895 @end ifnottex
896 @end quotation
897
898 @noindent
899 The flat symbol is required to cancel out the natural in the same
900 measure, but Finale misses it because it occurred in a different voice.
901 So in addition to running a beaming plug-in and checking the spacing on
902 the noteheads and rests, the user must also check each measure for
903 cross-voice accidentals to avoid interrupting a rehearsal over an
904 engraving error.
905
906 If you are interested in examining these examples in more detail, the
907 full seven-measure excerpt can be found at the end of this essay along
908 with four different published engravings.  Close examination reveals that
909 there is some acceptable variation among the hand-engravings, but that
910 LilyPond compares reasonably well to that acceptable range.  There are
911 still some shortcomings in the LilyPond output, for example, it appears
912 a bit too aggressive in shortening some of the stems, so there is room
913 for further development and fine-tuning.
914
915 Of course, typography relies on human judgment of appearance, so people
916 cannot be replaced completely.  However, much of the dull work can be
917 automated.  If LilyPond solves most of the common situations correctly,
918 this will be a huge improvement over existing software.  Over the course
919 of years, the software can be refined to do more and more things
920 automatically, so manual overrides are less and less necessary.  Where
921 manual adjustments are needed, LilyPond's structure has been designed
922 with that flexibility in mind.
923
924 @node Building software
925 @section Building software
926
927 This section describes some of the programming decisions that we made
928 when designing LilyPond.
929
930 @menu
931 * Music representation::
932 * What symbols to engrave?::
933 * Flexible architecture::
934 @end menu
935
936
937 @node Music representation
938 @unnumberedsubsec Music representation
939
940 @cindex syntax
941 @cindex recursive structures
942
943 Ideally, the input format for any high-level formatting system is
944 an abstract description of the content.  In this case, that would
945 be the music itself.  This poses a formidable problem: how can we
946 define what music really is? Instead of trying to find an answer,
947 we have reversed the question.  We write a program capable of
948 producing sheet music, and adjust the format to be as lean as
949 possible.  When the format can no longer be trimmed down, by
950 definition we are left with content itself.  Our program serves as
951 a formal definition of a music document.
952
953 The syntax is also the user-interface for LilyPond, hence it is
954 easy to type:
955
956 @example
957 @{
958   c'4 d'8
959 @}
960 @end example
961
962 @noindent
963 to create a quarter note on middle C (C1) and an eighth note on
964 the D above middle C (D1).
965
966 @lilypond[quote]
967 {
968   c'4 d'8
969 }
970 @end lilypond
971
972 On a microscopic scale, such syntax is easy to use.  On a larger
973 scale, syntax also needs structure.  How else can you enter
974 complex pieces like symphonies and operas?  The structure is
975 formed by the concept of music expressions: by combining small
976 fragments of music into larger ones, more complex music can be
977 expressed.  For example
978
979 @lilypond[quote,verbatim,fragment,relative=1]
980 f4
981 @end lilypond
982
983 @noindent
984 Simultaneous notes can be constructed by enclosing them with
985 @code{<<} and @code{>>}:
986
987 @example
988 <<c4 d4 e4>>
989 @end example
990
991 @lilypond[quote,fragment,relative=1]
992 \new Voice { <<c4 d4 e>> }
993 @end lilypond
994
995 @noindent
996 This expression is put in sequence by enclosing it in curly braces
997 @code{@{@tie{}@dots{}@tie{}@}}:
998
999 @example
1000 @{ f4 <<c4 d4 e4>> @}
1001 @end example
1002
1003 @lilypond[quote,relative=1,fragment]
1004 { f4 <<c d e4>> }
1005 @end lilypond
1006
1007 @noindent
1008 The above is also an expression, and so it may be combined again
1009 with another simultaneous expression (a half note) using
1010 @code{<<}, @code{\\}, and @code{>>}:
1011
1012 @example
1013 << g2 \\ @{ f4 <<c4 d4 e4>> @} >>
1014 @end example
1015
1016 @lilypond[quote,fragment,relative=2]
1017 \new Voice { << g2 \\ { f4 <<c d e>> } >> }
1018 @end lilypond
1019
1020 Such recursive structures can be specified neatly and formally in
1021 a context-free grammar.  The parsing code is also generated from
1022 this grammar.  In other words, the syntax of LilyPond is clearly
1023 and unambiguously defined.
1024
1025 User-interfaces and syntax are what people see and deal with most.
1026 They are partly a matter of taste, and also the subject of much
1027 discussion.  Although discussions on taste do have their merit,
1028 they are not very productive.  In the larger picture of LilyPond,
1029 the importance of input syntax is small: inventing neat syntax is
1030 easy, while writing decent formatting code is much harder.  This
1031 is also illustrated by the line-counts for the respective
1032 components: parsing and representation take up less than 10% of
1033 the source code.
1034
1035 When designing the structures used in LilyPond, we made some different
1036 decisions than are apparent in other software.  Consider the hierarchical
1037 nature of music notation:
1038
1039 @lilypond[quote,fragment]
1040 <<
1041   \new Staff \relative c'' {
1042     \key g \major
1043     \time 3/4
1044     d4 g,8 a b c d4 g, g
1045   }
1046   \new Staff \relative c' {
1047     \clef "bass"
1048     \key g \major
1049     <g b d>2 a4 b2.
1050   }
1051 >>
1052 @end lilypond
1053
1054 In this case, there are pitches grouped into chords that belong to
1055 measures, which belong to staves.  This resembles a tidy structure of
1056 nested boxes:
1057
1058 @quotation
1059 @iftex
1060 @sourceimage{pdf/nestedboxes,,4cm,}
1061 @end iftex
1062 @ifnottex
1063 @sourceimage{nestedboxes,,,png}
1064 @end ifnottex
1065 @end quotation
1066
1067 Unfortunately, the structure is tidy because it is based on some
1068 excessively restrictive assumptions.  This becomes apparent if we
1069 consider a more complicated musical example:
1070
1071 @lilypond[quote]
1072 \layout {
1073   \context {
1074     \Score
1075     \remove "Timing_translator"
1076     \remove "Default_bar_line_engraver"
1077   }
1078   \context {
1079     \Staff
1080     \consists "Timing_translator"
1081     \consists "Default_bar_line_engraver"
1082   }
1083 }
1084
1085 \new PianoStaff <<
1086   \new Staff = "RH" <<
1087     \new Voice = "I" \relative c''' {
1088       \time 3/4
1089       \voiceOne
1090       \tuplet 7/6 { g8 g g g g g g }
1091       \oneVoice
1092       r4 <b,, fis' g bes> r4\fermata
1093     }
1094     \new Voice = "II" \relative c' {
1095       \voiceTwo
1096       c4
1097       \tuplet 5/4 {
1098         <c ees>8 f g
1099         \change Staff = "LH" \oneVoice
1100         \stemUp g,( c}
1101       r4
1102       \override Stem.cross-staff = ##t
1103       \override Stem.length = #12
1104       <fis, b>) r\fermata
1105     }
1106   >>
1107   \new Staff = "LH" <<
1108     \new Voice = "III" \relative c' {
1109       \time 2/4
1110       \clef "bass"
1111       g4 \stopStaff s
1112       \startStaff s2*2
1113     }
1114   >>
1115 >>
1116 @end lilypond
1117
1118 In this example, staves start and stop at will, voices jump around
1119 between staves, and the staves have different time signatures.  Many
1120 software packages would struggle with reproducing this example because
1121 they are built on the nested box structure.  With LilyPond, on the other
1122 hand, we have tried to keep the input format and the structure as
1123 flexible as possible.
1124
1125 @node What symbols to engrave?
1126 @unnumberedsubsec What symbols to engrave?
1127
1128 @cindex engraving
1129 @cindex typography
1130 @cindex engraver
1131 @cindex plug-in
1132
1133 The formatting process decides where to place symbols.  However,
1134 this can only be done once it is decided @emph{what} symbols
1135 should be printed -- in other words, what notation to use.
1136
1137 Common music notation is a system of recording music that has
1138 evolved over the past 1000 years.  The form that is now in common
1139 use dates from the early Renaissance.  Although the basic form
1140 (i.e., note heads on a 5-line staff) has not changed, the details
1141 still evolve to express the innovations of contemporary notation.
1142 Hence, common music notation encompasses some 500 years of music.
1143 Its applications range from monophonic melodies to monstrous
1144 counterpoints for a large orchestra.
1145
1146 How can we get a grip on such a seven-headed beast, and force it
1147 into the confines of a computer program?  Our solution is to break
1148 up the problem of notation (as opposed to engraving, i.e.,
1149 typography) into digestible and programmable chunks: every type of
1150 symbol is handled by a separate module, a so-called plug-in.  Each
1151 plug-in is completely modular and independent, so each can be
1152 developed and improved separately.  Such plug-ins are called
1153 @code{engraver}s, by analogy with craftsmen who translate musical
1154 ideas to graphic symbols.
1155
1156 In the following example, we start out with a plug-in for note
1157 heads, the @code{Note_heads_engraver}.
1158
1159 @lilypond[quote,ragged-right]
1160 \include "engraver-example.ily"
1161
1162 \score {
1163   \topVoice
1164   \layout {
1165     \context {
1166       \Voice
1167       \remove "Stem_engraver"
1168       \remove "Phrasing_slur_engraver"
1169       \remove "Slur_engraver"
1170       \remove "Script_engraver"
1171       \remove "New_fingering_engraver"
1172       \remove "Beam_engraver"
1173       \remove "Auto_beam_engraver"
1174     }
1175     \context {
1176       \Staff
1177       \remove "Accidental_engraver"
1178       \remove "Key_engraver"
1179       \remove "Clef_engraver"
1180       \remove "Bar_engraver"
1181       \remove "Time_signature_engraver"
1182       \remove "Staff_symbol_engraver"
1183       \consists "Pitch_squash_engraver"
1184     }
1185   }
1186 }
1187 @end lilypond
1188
1189 @noindent
1190 Then a @code{Staff_symbol_engraver} adds the staff,
1191
1192 @lilypond[quote,ragged-right]
1193 \include "engraver-example.ily"
1194
1195 \score {
1196   \topVoice
1197   \layout {
1198     \context {
1199       \Voice
1200       \remove "Stem_engraver"
1201       \remove "Phrasing_slur_engraver"
1202       \remove "Slur_engraver"
1203       \remove "Script_engraver"
1204       \remove "New_fingering_engraver"
1205       \remove "Beam_engraver"
1206       \remove "Auto_beam_engraver"
1207     }
1208     \context {
1209       \Staff
1210       \remove "Accidental_engraver"
1211       \remove "Key_engraver"
1212       \remove "Clef_engraver"
1213       \remove "Bar_engraver"
1214       \consists "Pitch_squash_engraver"
1215       \remove "Time_signature_engraver"
1216     }
1217   }
1218 }
1219 @end lilypond
1220
1221 @noindent
1222 the @code{Clef_engraver} defines a reference point for the staff,
1223
1224 @lilypond[quote,ragged-right]
1225 \include "engraver-example.ily"
1226
1227 \score {
1228   \topVoice
1229   \layout {
1230     \context {
1231       \Voice
1232       \remove "Stem_engraver"
1233       \remove "Phrasing_slur_engraver"
1234       \remove "Slur_engraver"
1235       \remove "Script_engraver"
1236       \remove "New_fingering_engraver"
1237       \remove "Beam_engraver"
1238       \remove "Auto_beam_engraver"
1239     }
1240     \context {
1241       \Staff
1242       \remove "Accidental_engraver"
1243       \remove "Key_engraver"
1244       \remove "Bar_engraver"
1245       \remove "Time_signature_engraver"
1246     }
1247   }
1248 }
1249 @end lilypond
1250
1251 @noindent
1252 and the @code{Stem_engraver} adds stems.
1253
1254 @lilypond[quote,ragged-right]
1255 \include "engraver-example.ily"
1256
1257 \score {
1258   \topVoice
1259   \layout {
1260     \context {
1261       \Voice
1262       \remove "Phrasing_slur_engraver"
1263       \remove "Slur_engraver"
1264       \remove "Script_engraver"
1265       \remove "New_fingering_engraver"
1266       \remove "Beam_engraver"
1267       \remove "Auto_beam_engraver"
1268     }
1269     \context {
1270       \Staff
1271       \remove "Accidental_engraver"
1272       \remove "Key_engraver"
1273       \remove "Bar_engraver"
1274       \remove "Time_signature_engraver"
1275     }
1276   }
1277 }
1278 @end lilypond
1279
1280 @noindent
1281 The @code{Stem_engraver} is notified of any note head coming
1282 along.  Every time one (or more, for a chord) note head is seen, a
1283 stem object is created and connected to the note head.  By adding
1284 engravers for beams, slurs, accents, accidentals, bar lines, time
1285 signature, and key signature, we get a complete piece of notation.
1286
1287 @lilypond[quote,ragged-right]
1288 \include "engraver-example.ily"
1289 \score { \topVoice }
1290 @end lilypond
1291
1292 @cindex polyphony
1293 @cindex engraving multiple voices
1294 @cindex contexts
1295
1296 This system works well for monophonic music, but what about
1297 polyphony?  In polyphonic notation, many voices can share a staff.
1298
1299 @lilypond[quote,ragged-right]
1300 \include "engraver-example.ily"
1301 \new Staff << \topVoice \\ \botVoice >>
1302 @end lilypond
1303
1304 In this situation, the accidentals and staff are shared, but the
1305 stems, slurs, beams, etc., are private to each voice.  Hence,
1306 engravers should be grouped.  The engravers for note heads, stems,
1307 slurs, etc., go into a group called @q{Voice context}, while the
1308 engravers for key, accidental, bar, etc., go into a group called
1309 @q{Staff context}.  In the case of polyphony, a single Staff
1310 context contains more than one Voice context.  Similarly, multiple
1311 Staff contexts can be put into a single Score context.  The Score
1312 context is the top level notation context.
1313
1314 @lilypond[quote,ragged-right]
1315 \include "engraver-example.ily"
1316 \score {
1317    <<
1318       \new Staff << \topVoice \\ \botVoice >>
1319       \new Staff << \pah \\ \hoom >>
1320    >>
1321 }
1322 @end lilypond
1323
1324 @seealso
1325 Internals Reference: @rinternals{Contexts}.
1326
1327 @node Flexible architecture
1328 @unnumberedsubsec Flexible architecture
1329
1330 When we started, we wrote the LilyPond program entirely in the C++
1331 programming language; the program's functionality was set in stone
1332 by the developers.  That proved to be unsatisfactory for a number
1333 of reasons:
1334
1335 @itemize
1336
1337 @item When LilyPond makes mistakes, users need to override
1338 formatting decisions.  Therefore, the user must have access to the
1339 formatting engine.  Hence, rules and settings cannot be fixed by
1340 us at compile-time but must be accessible for users at run-time.
1341
1342 @item Engraving is a matter of visual judgment, and therefore a
1343 matter of taste.  As knowledgeable as we are, users can disagree
1344 with our personal decisions.  Therefore, the definitions of
1345 typographical style must also be accessible to the user.
1346
1347 @item Finally, we continually refine the formatting algorithms, so
1348 we need a flexible approach to rules.  The C++ language forces a
1349 certain method of grouping rules that cannot readily be applied to
1350 formatting music notation.
1351
1352 @end itemize
1353
1354 @cindex Scheme programming language
1355
1356 These problems have been addressed by integrating an interpreter
1357 for the Scheme programming language and rewriting parts of
1358 LilyPond in Scheme.  The current formatting architecture is built
1359 around the notion of graphical objects, described by Scheme
1360 variables and functions.  This architecture encompasses formatting
1361 rules, typographical style and individual formatting decisions.
1362 The user has direct access to most of these controls.
1363
1364 Scheme variables control layout decisions.  For example, many
1365 graphical objects have a direction variable that encodes the
1366 choice between up and down (or left and right).  Here you see two
1367 chords, with accents and arpeggios.  In the first chord, the
1368 graphical objects have all directions down (or left).  The second
1369 chord has all directions up (right).
1370
1371 @lilypond[quote,ragged-right]
1372 \score {
1373   \relative c' {
1374     \stemDown <e g b>4_>-\arpeggio
1375     \override Arpeggio.direction = #RIGHT
1376     \stemUp <e g b>4^>-\arpeggio
1377   }
1378   \layout {
1379     \context {
1380       \Score
1381       \override SpacingSpanner.spacing-increment = #3
1382       \hide TimeSignature
1383     }
1384   }
1385 }
1386 @end lilypond
1387
1388 @cindex score formatting
1389 @cindex formatting a score
1390 @cindex formatting rules
1391
1392 @noindent
1393 The process of formatting a score consists of reading and writing
1394 the variables of graphical objects.  Some variables have a preset
1395 value.  For example, the thickness of many lines -- a
1396 characteristic of typographical style -- is a variable with a
1397 preset value.  You are free to alter this value, giving your score
1398 a different typographical impression.
1399
1400 @lilypond[quote,ragged-right]
1401 fragment = {
1402    \clef bass f8 as8
1403    c'4-~ c'16 as g f e16 g bes c' des'4
1404 }
1405 <<
1406    \new Staff \fragment
1407    \new Staff \with {
1408       \override Beam.beam-thickness = #0.3
1409       \override Stem.thickness = #0.5
1410       \override Bar.thickness = #3.6
1411       \override Tie.thickness = #2.2
1412       \override StaffSymbol.thickness = #3.0
1413       \override Tie.extra-offset = #'(0 .  0.3)
1414       }
1415       \fragment
1416 >>
1417 @end lilypond
1418
1419 Formatting rules are also preset variables: each object has
1420 variables containing procedures.  These procedures perform the
1421 actual formatting, and by substituting different ones, we can
1422 change the appearance of objects.  In the following example, the
1423 rule governing which note head objects are used to produce the
1424 note head symbol is changed during the music fragment.
1425
1426 @lilypond[quote,ragged-right]
1427 #(set-global-staff-size 30)
1428
1429 #(define (mc-squared grob orig current)
1430   (let* ((interfaces (ly:grob-interfaces grob))
1431          (pos (ly:grob-property grob 'staff-position)))
1432     (if (memq 'note-head-interface interfaces)
1433         (begin
1434           (ly:grob-set-property! grob 'stencil
1435             (grob-interpret-markup grob
1436               (make-lower-markup 0.5
1437                 (case pos
1438                   ((-5) "m")
1439                   ((-3) "c ")
1440                   ((-2) (make-smaller-markup (make-bold-markup "2")))
1441                   (else "bla")))))))))
1442
1443 \new Voice \relative c' {
1444   \stemUp
1445   \set autoBeaming = ##f
1446   \time 2/4
1447   <d f g>4
1448   \once \override NoteHead.stencil = #note-head::brew-ez-stencil
1449   \once \override NoteHead.font-size = #-7
1450   \once \override NoteHead.font-family = #'sans
1451   \once \override NoteHead.font-series = #'bold
1452   <d f g>4
1453   \once \override NoteHead.style = #'cross
1454   <d f g>4
1455   \applyOutput #'Voice #mc-squared
1456   <d f g>4
1457   <<
1458     { d8[ es-( fis^^ g] fis2-) }
1459     \repeat unfold 5 { \applyOutput #'Voice #mc-squared s8 }
1460   >>
1461 }
1462 @end lilypond
1463
1464
1465
1466 @node Putting LilyPond to work
1467 @section Putting LilyPond to work
1468
1469 @cindex simple examples
1470 @cindex examples, simple
1471
1472 We have written LilyPond as an experiment of how to condense the
1473 art of music engraving into a computer program.  Thanks to all
1474 that hard work, the program can now be used to perform useful
1475 tasks.  The simplest application is printing notes.
1476
1477 @lilypond[quote,relative=1]
1478 {
1479   \time 2/4
1480   c4 c g'4 g a4 a g2
1481 }
1482 @end lilypond
1483
1484 @noindent
1485 By adding chord names and lyrics we obtain a lead sheet.
1486
1487 @lilypond[quote,ragged-right]
1488 <<
1489    \chords { c2 c f2 c }
1490    \new Staff
1491    \relative c' {
1492      \time 2/4
1493      c4 c g' g a a g2
1494    }
1495    \addlyrics { twin -- kle twin -- kle lit -- tle star }
1496 >>
1497 @end lilypond
1498
1499 Polyphonic notation and piano music can also be printed.  The
1500 following example combines some more exotic constructs.
1501
1502 @lilypond[quote,line-width=15.9\cm]
1503 \header {
1504   title = "Screech and boink"
1505   subtitle = "Random complex notation"
1506   composer = "Han-Wen Nienhuys"
1507 }
1508
1509 \score {
1510   \context PianoStaff <<
1511     \new Staff = "up" {
1512       \time 4/8
1513       \key c \minor
1514       << {
1515         \revert Stem.direction
1516         \change Staff = down
1517         \set subdivideBeams = ##t
1518         g16.[
1519           \change Staff = up
1520           c'''32
1521           \change Staff = down
1522           g32
1523           \change Staff = up
1524           c'''32
1525           \change Staff = down
1526           g16]
1527         \change Staff = up
1528         \stemUp
1529         \set followVoice = ##t
1530         c'''32([ b''16 a''16 gis''16 g''32)]
1531       } \\ {
1532         s4 \tuplet 3/2 { d'16[ f' g'] } as'32[ b''32 e'' d'']
1533       } \\ {
1534         s4 \autoBeamOff d''8.. f''32
1535       } \\ {
1536         s4 es''4
1537       } >>
1538     }
1539
1540     \new Staff = "down" {
1541       \clef bass
1542       \key c \minor
1543       \set subdivideBeams = ##f
1544       \override Stem.french-beaming = ##t
1545       \override Beam.beam-thickness = #0.3
1546       \override Stem.thickness = #4.0
1547       g'16[ b16 fis16 g16]
1548       << \makeClusters {
1549         as16 <as b>
1550         <g b>
1551         <g cis>
1552       } \\ {
1553         \override Staff.Arpeggio.arpeggio-direction =#down
1554         <cis, e, gis, b, cis>4\arpeggio
1555       }
1556     >> }
1557   >>
1558   \midi {
1559     \tempo 8 = 60
1560   }
1561   \layout {
1562     \context {
1563       \Staff
1564       \consists "Horizontal_bracket_engraver"
1565     }
1566   }
1567 }
1568 @end lilypond
1569
1570 The fragments shown above have all been written by hand, but that
1571 is not a requirement.  Since the formatting engine is mostly
1572 automatic, it can serve as an output means for other programs that
1573 manipulate music.  For example, it can also be used to convert
1574 databases of musical fragments to images for use on websites and
1575 multimedia presentations.
1576
1577 This manual also shows an application: the input format is text, and can
1578 therefore be easily embedded in other text-based formats such as
1579 @LaTeX{}, HTML, or in the case of this manual, Texinfo.  Using the
1580 @command{lilypond-book} program, included with LilyPond, the input
1581 fragments can be replaced by music images in the resulting PDF or HTML
1582 output files.  Another example is the third-party OOoLilyPond extension
1583 for OpenOffice.org or LibreOffice, which makes it extremely easy to
1584 embed musical examples in documents.
1585
1586 For more examples of LilyPond in action, full documentation, and the
1587 software itself, see our main website: www.lilypond.org.
1588
1589 @page
1590 @node Engraved examples (BWV 861)
1591 @section Engraved examples (BWV 861)
1592
1593 This section contains four reference engravings and two
1594 software-engraved versions of Bach's Fugue in G minor from the
1595 Well-Tempered Clavier, Book I, BWV 861 (the last seven measures).
1596
1597 @noindent
1598 Bärenreiter BA5070 (Neue Ausgabe Sämtlicher Werke, Serie V, Band 6.1,
1599 1989):
1600
1601 @iftex
1602 @sourceimage{bwv861-baer,16cm,,}
1603 @end iftex
1604 @ifnottex
1605 @sourceimage{bwv861-baer-small,,,png}
1606 @end ifnottex
1607
1608 @noindent
1609 Bärenreiter BA5070 (Neue Ausgabe Sämtlicher Werke, Serie V, Band 6.1,
1610 1989), an alternate musical source.  Aside from the textual differences,
1611 this demonstrates slight variations in the engraving decisions, even
1612 from the same publisher and edition:
1613
1614 @iftex
1615 @sourceimage{bwv861-baer-alt,16cm,,}
1616 @end iftex
1617 @ifnottex
1618 @sourceimage{bwv861-baer-alt-small,,,png}
1619 @end ifnottex
1620
1621 @noindent
1622 Breitkopf & Härtel, edited by Ferruccio Busoni (Wiesbaden, 1894), also
1623 available from the Petrucci Music Library (IMSLP #22081).  The editorial
1624 markings (fingerings, articulations, etc.) have been removed for clearer
1625 comparison with the other editions here:
1626
1627 @iftex
1628 @sourceimage{bwv861-breitkopf,16cm,,}
1629 @end iftex
1630 @ifnottex
1631 @sourceimage{bwv861-breitkopf-small,,,png}
1632 @end ifnottex
1633
1634 @noindent
1635 Bach-Gesellschaft edition (Leipzig, 1866), available from the Petrucci
1636 Music Library (IMSPL #02221):
1637
1638 @iftex
1639 @sourceimage{bwv861-gessellschaft,16cm,,}
1640 @end iftex
1641 @ifnottex
1642 @sourceimage{bwv861-gessellschaft-small,,,png}
1643 @end ifnottex
1644
1645 @noindent
1646 Finale 2008:
1647
1648 @iftex
1649 @sourceimage{pdf/bwv861-finale2008a,,,}
1650 @end iftex
1651 @ifnottex
1652 @sourceimage{bwv861-finale2008a,,,png}
1653 @end ifnottex
1654
1655 @sp 4
1656 @noindent
1657 LilyPond, version @version{}:
1658
1659 @lilypond[staffsize=14.3,line-width=15.9\cm]
1660 global = {\key g \minor}
1661
1662 partI = \relative c' {
1663   \voiceOne
1664   fis8 d' ees g, fis4 g
1665   r8 a16 bes c8 bes16 a d8 r r4
1666   r2 r8 d16 ees f8 ees16 d
1667   ees4 ~ ees16 d c bes a4 r8 ees'16 d
1668   c8 d16 ees d8 e16 fis g8 fis16 g a4 ~
1669   a8 d, g f ees d c bes
1670   a2 g\fermata \bar "|."
1671 }
1672
1673 partII = \relative c' {
1674   \voiceTwo
1675   d4 r4 r8 d'16 c bes8 c16 d
1676   ees8 d c ees a, r r4
1677   r8 fis16 g a8 g16 fis g2 ~
1678   g2 r8 d' ees g,
1679   fis4 g r8 a16 bes c8 bes16 a
1680   bes4. <g b>8 <a c> r <d, g> r
1681   <ees g>4 <d fis> d2
1682 }
1683 partIII = \relative c' {
1684   \voiceOne
1685   r2 r8 d ees g, fis4 g r8 a16 bes c8 bes16 a
1686   bes2 ~ bes8 b16 a g8 a16 b
1687   c4 r r2
1688   R1
1689   r8 d ees g, fis4 g
1690   r8 a16 bes c8 bes16 a b2
1691 }
1692 partIV = \relative c {
1693   \voiceTwo
1694   d4 r r2
1695   r8 d ees g, fis4 a
1696   d,8 d'16 c bes8 c16 d ees2 ~
1697   ees8 ees16 d c8 d16 ees fis,8 a16 g fis8 g16 a
1698   d,8 d'16 c bes8 c16 d ees8 c a fis'
1699   g f ees d c bes a g
1700   c a d d, g2\fermata
1701 }
1702
1703 \score {
1704   <<
1705     % \set Score.barNumberVisibility = #all-bar-numbers-visible
1706     % required in 2.13
1707     \set Score.currentBarNumber = #28
1708     \bar ""
1709     \new PianoStaff <<
1710       \new Staff = "RH" <<
1711         \global
1712         \new Voice = "voiceI" { \partI }
1713         \new Voice = "voiceII" { \partII }
1714       >>
1715
1716       \new Staff = "LH" <<
1717         \clef "bass"
1718         \global
1719         \new Voice = "voiceIII" { \partIII }
1720         \new Voice = "voiceIV" { \partIV }
1721       >>
1722     >>
1723   >>
1724   \layout {
1725     \context {
1726       \Staff
1727       \remove "Time_signature_engraver"
1728     }
1729     \context {
1730       \PianoStaff
1731       \override StaffGrouper.staff-staff-spacing.padding = #1
1732     }
1733   }
1734 }
1735 @end lilypond