]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/essay/engraving.itely
Doc: compile fix.
[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.  See TRANSLATION for details.
8 @end ignore
9
10 @c \version "2.13.4"
11
12 @node Music engraving
13 @chapter Music engraving
14
15 This section covers the overall goals and architecture of
16 LilyPond.
17
18 @c TODO:
19 @c update LP 1.4 example with PDF
20 @c annotate typography errors in old LilyPond
21 @c add Schubert
22 @c add 'tweaked' versions of LP & Finale BWV 861
23 @c find a good value for #'between-staff-spacing #'padding
24 @c remove 3mm eps bounding box left padding for Sarabande (This will
25 @c     require adding a new snippet option to lilypond-book.py
26
27 @c Notes:
28 @c Incorrect beaming in the Sarabande is a known bug.
29
30 @menu
31 * The LilyPond story::          
32 * Engraving details::           
33 * Automated engraving::         
34 * What symbols to engrave?::    
35 * Music representation::        
36 * Example applications::        
37 * Appendix::                    
38 @end menu
39
40 @node The LilyPond story
41 @unnumberedsec The LilyPond story
42
43 Before LilyPond had a community of users around the world, before it had
44 been used to produce university course notes or world-premier opera
45 performance scores, before there was an essay on music engraving or any
46 computer code or even an organized team of developers, LilyPond began
47 with a question:
48
49 @quotation
50 Why does most computer output fail to achieve the beauty and balance of
51 a hand-engraved score?
52 @end quotation
53
54 Some of the answers can be found by examining the two scores
55 @ifnottex
56 below.
57 @end ifnottex
58 @iftex
59 on the following pages.
60 @end iftex
61 The first score is a beautiful hand-engraved score from 1950 and the
62 second is a modern, computer-engraved edition.
63
64 @ifnottex
65 @noindent
66 Bärenreiter BA 320, @copyright{}1950:
67
68 @sourceimage{baer-suite1-fullpage,,,png}
69
70 @noindent
71 Henle no. 666, @copyright{}2000:
72
73 @sourceimage{henle-suite1-fullpage,,,png}
74 @end ifnottex
75
76 The notes here are identical, taken from Bach's first Suite for solo
77 cello, but the appearance is different, especially if you print them out
78 and veiw them from a distance.
79 @ifnottex
80 (The PDF version of this manual has high-resolution images suitable for
81 printing.)
82 @end ifnottex
83 Try reading or playing from each of the scores and you will find that
84 the hand-engraved score is more enjoyable to use. It has flowing lines
85 and movement, and it feels like a living, breathing piece of music,
86 while the newer edition seems cold and mechanical.
87
88 It is kind of hard to immediately see what makes the difference with the
89 newer edition. Everything looks neat and tiny, possibly even ``better''
90 because it looks more computerized and uniform. This really puzzled us
91 for quite a while. We wanted to improve computer notation, but we first
92 had to figure out what was wrong with it.
93
94 The answer lies in the precise, mathematical uniformity of the newer
95 edition. Find the barline in the middle of each line: in the
96 hand-engraved score the position of these barlines has some natural
97 natural variation, while in the newer version they line up almost
98 perfectly. This is shown in these simplified page layout diagrams,
99 traced from the hand-engraved (left) and computer-generated music
100 (right):
101
102 @quotation
103 @iftex
104 @sourceimage{pdf/page-layout-comparison,,,}
105 @end iftex
106 @ifnottex
107 @sourceimage{page-layout-comparison,,,png}
108 @end ifnottex
109 @end quotation
110 @noindent
111
112 In the computer-generated output, even the individual noteheads are
113 aligned in vertical columns, making the contour of the melody disappear
114 into a rigid grid of musical markings.
115
116 There are other differences as well: in the hand-engraved edition the
117 vertical lines are all stronger, the slurs lie closer to the noteheads,
118 and there is more visual variety in the placement of the beams. Although
119 such details may seem like nitpicking, the result is a score that is
120 easier to read. In the computer-generated output, each line is nearly
121 identical and if the musician looks away for a moment, she will be lost
122 on the page.
123
124 LilyPond was designed to solve the problems we found in existing
125 software and to create beautiful music that mimics the finest
126 hand-engraved scores. Along the way, we have learned a great deal about
127 the work that goes into a well-engraved score. In this essay we describe
128 several of those aspects that we have tried to imitate in LilyPond.
129
130 @iftex
131 @page
132 @noindent
133 Bärenreiter BA 320, @copyright{}1950:
134
135 @sourceimage{baer-suite1-fullpage,16cm,,}
136 @page
137 @noindent
138 Henle no. 666, @copyright{}2000:
139 @sp 3
140 @sourceimage{henle-suite1-fullpage,16cm,,}
141 @page
142 @end iftex
143
144 @node Engraving details
145 @unnumberedsec Engraving details
146
147 @cindex engraving
148 @cindex typography, music
149 @cindex music typography
150 @cindex plate engraving
151 @cindex music engraving
152
153 The art of music typography is called @emph{(plate) engraving}, a term
154 that derives from the manual process of music printing@footnote{Early
155 european printers explored several processes, including hand-carved
156 wooden blocks, movable type, and engraved sheets of thin metal.
157 Typesetting had the advantage of being more easily corrected and
158 facilitating the inclusion of text and lyrics, but only engraving
159 offered the ability to do unimpeded layout and unanticipated notation.
160 In the end, hand-engraved scores became the standard for all printed
161 music, with the exception of some hymnals and songbooks where
162 typesetting was justified by its ease and economy, even into the
163 twentieth century.}. Just a few decades ago, sheet music was made by
164 cutting and stamping the music into a zinc or pewter plate in mirror
165 image. The plate would be inked, and the depressions caused by the
166 cutting and stamping would hold ink. An image was formed by pressing
167 paper to the plate. The stamping and cutting was done completely by hand
168 and making a correction was cumbersome, so the engraving had to be
169 nearly perfect in one go. Engraving was a highly specialized skill; a
170 craftsman had to complete around five years of training before earning
171 the title of master engraver, and another five years of experience were
172 necessary to become truly skilled.
173
174 @quotation
175 @iftex
176 @sourceimage{hader-slaan,,7cm,}
177 @end iftex
178 @ifnottex
179 @sourceimage{hader-slaan,,,jpg}
180 @end ifnottex
181 @end quotation
182
183 @c Now all newly printed music is produced with computers. This has
184 @c obvious advantages: prints are cheaper to make, editorial work can be
185 @c delivered by email, and the original data can be easily stored.
186 @c Unfortunately, computer-generated scores rarely match the quality of
187 @c hand-engraved scores. Instead, computer printouts have a bland,
188 @c mechanical look, which makes them unpleasant to play from.
189
190 @menu
191 * Music fonts::                 
192 * Optical spacing::             
193 * Ledger lines::                
194 * Optical sizing::              
195 * Why work so hard?::           
196 @end menu
197
198 @node Music fonts
199 @unnumberedsubsec Music fonts
200
201 The images below illustrate some differences between traditional
202 engraving and typical computer output. The left picture shows a scan of
203 a flat symbol from a hand-engraved Bärenreiter edition, while the right
204 picture depicts a symbol from an edition of the same music published in
205 2000. Although both images are printed in the same shade of ink, the
206 earlier version looks darker: the staff lines are heavier, and the
207 Bärenreiter flat has a bold, almost voluptuous rounded look. The right
208 scan, on the other hand, has thinner lines and a straight layout with
209 sharp corners.
210
211 @multitable @columnfractions .25 .25 .25 .25
212 @item @tab
213 @ifnotinfo
214 @iftex
215 @sourceimage{baer-flat-gray,,4cm,}
216 @end iftex
217 @ifnottex
218 @sourceimage{baer-flat-gray,,,png}
219 @end ifnottex
220
221 @tab
222 @iftex
223 @sourceimage{henle-flat-gray,,4cm,}
224 @end iftex
225 @ifnottex
226 @sourceimage{henle-flat-gray,,,png}
227 @end ifnottex
228
229 @end ifnotinfo
230 @ifinfo
231 @sourceimage{henle-flat-bw,,,png}
232 @sourceimage{baer-flat-bw,,,png}
233 @sourceimage{lily-flat-bw,,,png}
234 @end ifinfo
235
236
237 @item @tab
238 Bärenreiter (1950)
239 @tab
240 Henle (2000)
241
242 @end multitable
243
244 @cindex musical symbols
245 @cindex font
246 @cindex blackness
247 @cindex balance
248
249 When we wanted to write a computer program to create music typography,
250 there were no musical fonts freely available that could match the
251 elegance of our favorite scores. Undeterred, we created a font of
252 musical symbols, relying on nice printouts of hand-engraved music. The
253 experience helped develop a typographical taste, and it made us
254 appreciate subtle design details. Without that experience, we would not
255 have realized how ugly the fonts were that we admired at first.
256
257 Below is a sample of two music fonts: the upper set is the default font
258 in the Sibelius software (the @emph{Opus} font), and the lower set is
259 our own LilyPond font.
260
261 @quotation
262 @iftex
263 @sourceimage{pdf/OpusAndFeta,,,}
264 @end iftex
265 @ifnottex
266 @sourceimage{OpusAndFeta,,,png}
267 @end ifnottex
268 @end quotation
269
270 The LilyPond symbols are heavier and their weight is more consistent,
271 which makes them easier to read. Fine endings, such as the ones on the
272 sides of the quarter rest, should not end in sharp points, but rather
273 in rounded shapes. This is because sharp corners of the punching dies
274 are fragile and quickly wear out when stamping in metal. Taken together,
275 the blackness of the font must be carefully tuned together with the
276 thickness of lines, beams and slurs to give a strong yet balanced
277 overall impression.
278
279 Also, notice that our half-notehead is not elliptic but slightly diamond
280 shaped. The vertical stem of a flat symbol is slightly brushed, becoming
281 wider at the top. The sharp and the natural are easier to distinguish
282 from a distance because their angled lines have different slopes and the
283 vertical strokes are heavier.
284
285 @node Optical spacing
286 @unnumberedsubsec Optical spacing
287
288 In spacing, the distribution of space should reflect the durations
289 between notes. However, as we saw in the Bach Suite above, many modern
290 scores adhere to the durations with mathematical precision, which leads
291 to poor results. In the next example a motive is printed twice: the
292 first time using exact mathematical spacing, and the second with
293 corrections. Which do you prefer?
294
295 @cindex optical spacing
296
297 @lilypond
298 \paper {
299   ragged-right = ##t
300   indent = #0.0
301 }
302
303 music = {
304    c'4 e''4 e'4 b'4 |
305    \stemDown
306    b'8[ e'' a' e'']
307    \stemNeutral
308    e'8[ e'8 e'8 e'8]
309 }
310 \score
311 {
312   \music
313   \layout {
314     \context {
315       \Staff
316       \override NoteSpacing #'stem-spacing-correction = #0.0
317       \override NoteSpacing #'same-direction-correction = #0.0
318       \override StaffSpacing #'stem-spacing-correction = #0.0
319     }
320   }
321 }
322 @end lilypond
323
324 @lilypond
325 \paper {
326   ragged-right = ##t
327   indent = #0.0
328 }
329
330 music = {
331    c'4 e''4 e'4 b'4 |
332    \stemDown
333    b'8[ e'' a' e'']
334    \stemNeutral
335    e'8[ e'8 e'8 e'8]
336 }
337
338 \score
339 {
340   \music
341   \layout {
342     \context {
343       \Staff
344       \override NoteSpacing #'stem-spacing-correction = #0.6
345     }
346   }
347 }
348 @end lilypond
349
350 @cindex regular rhythms
351 @cindex regular spacing
352 @cindex spacing, regular
353
354 Each bar in the fragment only uses notes that are played in a constant
355 rhythm. The spacing should reflect that. Unfortunately, the eye deceives
356 us a little; not only does it notice the distance between note heads, it
357 also takes into account the distance between consecutive stems. As a
358 result, the notes of an up-stem/@/down-stem combination should be put
359 farther apart, and the notes of a down-stem/@/up-stem combination should
360 be put closer together, all depending on the combined vertical positions
361 of the notes. The lower two measures are printed with this correction,
362 the upper two measures, however, form down-stem/@/up-stem clumps of
363 notes. A master engraver would adjust the spacing as needed to please
364 the eye.
365
366 Another example of optical spacing is the visual interplay between the
367 stems and the bar lines. When an up-stem precedes the bar line, a little
368 more space is needed to keep it from feeling crowded:
369
370 @lilypond
371 \paper {
372   ragged-right = ##t
373 }
374
375 \score {
376   {
377     c''8 c'' c'' c'' c'' c'' c'' c'' \break
378     a' a' a' a' a' a' a' a'
379   }
380   \layout {
381     \context {
382       \Staff
383       \remove "Time_signature_engraver"
384       \override NoteSpacing #'stem-spacing-correction = #0.7
385     }
386     \context {
387       \Score
388       \remove "Bar_number_engraver"
389     }
390   }
391 }
392 @end lilypond
393
394 @node Ledger lines
395 @unnumberedsubsec Ledger lines
396
397 @cindex ledger lines
398 @cindex collisions
399
400 Ledger lines present a typographical challenge: they make it more
401 difficult to space musical symbols close together and they must be clear
402 enough to identify the pitch at a glance. In the example below, we see
403 that ledger lines should be thicker than normal staff lines and that an
404 expert engraver will shorten a ledger line to allow closer spacing with
405 accidentals. We have included this feature in LilyPond's engraving.
406
407 @multitable @columnfractions .25 .25 .25 .25
408 @item @tab
409
410 @iftex
411 @sourceimage{baer-ledger,3cm,,}
412 @end iftex
413 @ifnottex
414 @sourceimage{baer-ledger,,,png}
415 @end ifnottex
416
417 @tab
418
419 @iftex
420 @sourceimage{lily-ledger,3cm,,}
421 @end iftex
422 @ifnottex
423 @sourceimage{lily-ledger,,,png}
424 @end ifnottex
425
426 @end multitable
427
428
429 @node Optical sizing
430 @unnumberedsubsec Optical sizing
431
432 Music may need to be printed in a range of sizes. Originally, this was
433 accomplished by creating punching dies in each of the required sizes,
434 which meant that each die was designed to look its best at that size.
435 With the advent of digital fonts, a single outline can be mathematically
436 scaled to any size, which is very convenient, but at the smaller sizes
437 the glyphs will appear very light.
438
439 In LilyPond, we have created fonts in a range of weights, corresponding
440 to a range of music sizes. This is a LilyPond engraving at staff size
441 26:
442
443 @quotation
444 @iftex
445 @sourceimage{pdf/size26,,23mm,}
446 @end iftex
447 @ifnottex
448 @sourceimage{size26,,,png}
449 @end ifnottex
450 @end quotation
451
452 @noindent
453 and this is the same engraving set at staff size 11, then
454 magnified by 236% to print at the same size as the previous example:
455
456 @quotation
457 @iftex
458 @sourceimage{pdf/size11,,23mm,}
459 @end iftex
460 @ifnottex
461 @sourceimage{size11,,,png}
462 @end ifnottex
463 @end quotation
464
465 At smaller sizes, LilyPond uses proportionally heavier lines and so the
466 music will still read well. This also allows staves of different
467 sizes to coexist peacefully when used together on the same page:
468
469 @c Grieg's Violin Sonata Op. 45
470 @lilypond[indent=1.5cm]
471 global = {
472   \time 6/8
473   \key c \minor
474 }
475
476 \new Score <<
477   \new Staff \with {
478       fontSize = #-4
479       \override StaffSymbol #'staff-space = #(magstep -4)
480       \override StaffSymbol #'thickness = #(magstep -3)
481     }
482     \relative c' {
483       \global
484       \set Staff.instrumentName = #"Violin"
485       c8.(\f^> b16 c d) ees8.(^> d16 c b)
486       g8.(^> b16 c ees) g8-.^> r r
487       R2.
488     }
489   \new PianoStaff <<
490     \set PianoStaff.instrumentName = #"Piano"
491     \new Staff \relative c' {
492       \global
493       s2.
494       s4. s8 r8 r16 <c f aes c>
495       <c f aes c>4.^> <c ees g>8 r r
496     }
497     \new Staff \relative c {
498       \global
499       \clef "bass"
500       << {
501         \once \override DynamicText #'X-offset = #-3
502         <ees g c>2.~->^\f
503         <ees g c>4.~ <ees g c>8
504       } \\ {
505         <c g c,>2.~
506         <c g c,>4.~ <c g c,>8
507       } >>
508       r8 r16 <f, c' aes'>16
509       <f c' aes'>4.-> <c' g'>8 r r
510     }
511   >>
512 >>
513 @end lilypond
514
515 @node Why work so hard?
516 @unnumberedsubsec Why work so hard?
517
518 Musicians are usually more absorbed with performing than with studying
519 the looks of a piece of music, so nitpicking typographical details may
520 seem academic. But it is not. Sheet music is performance material:
521 everything is done to aid the musician in letting her perform better,
522 and anything that is unclear or unpleasant to read is a hinderance.
523
524 Traditionally engraved music uses bold symbols on heavy staff to create
525 a strong, well-balanced look that stands out well when the music is far
526 away from the reader: for example, if it is on a music stand. A careful
527 distribution of white space allows music to be set very tightly without
528 crowding symbols together. The result minimizes the number of page
529 turns, which is a great advantage.
530
531 This is a common characteristic of typography. Layout should be pretty,
532 not only for its own sake, but especially because it helps the reader in
533 his task. For sheet music this is of double importance because musicians
534 have a limited amount of attention. The less attention they need for
535 reading, the more they can focus on playing the music. In other words,
536 better typography translates to better performances.
537
538 These examples demonstrate that music typography is an art that is
539 subtle and complex, and that producing it requires considerable
540 expertise, which musicians usually do not have.  LilyPond is our
541 effort to bring the graphical excellence of hand-engraved music to
542 the computer age, and make it available to normal musicians.  We
543 have tuned our algorithms, font-designs, and program settings to
544 produce prints that match the quality of the old editions we love
545 to see and love to play from.
546
547
548 @node Automated engraving
549 @unnumberedsec Automated engraving
550
551 @cindex engraving, automated
552 @cindex automated engraving
553
554 @menu
555 * Beauty contests::             
556 * Notation benchmarking::       
557 * Flexible architecture::       
558 @end menu
559
560 @node Beauty contests
561 @unnumberedsubsec Beauty contests
562
563 Rather than trying to write rules that describe how to draw an
564 attractive slur for every possible scenario, we have written rules that
565 describe an attractive slur and LilyPond finds the nicest possibility.
566 For example, here are three possible slur configurations, and LilyPond
567 has given each one a score in `ugly points':
568
569 @lilypond
570 %\layout { #(define debug-slur-scoring #t) }
571
572 \relative c {
573     \clef bass
574     \once \override Slur #'positions = #'(1.5 . 1)
575     e8[(_"15.39" f] g[ a b d,)] r4
576     \once \override Slur #'positions = #'(2 . 3)
577     e8[(_"13.08" f] g[ a b d,)] r4
578     e8[(_"12.04" f] g[ a b d,)] r4
579 }
580 @end lilypond
581
582 @noindent
583
584 The first example gets 15.39 points for grazing one of the notes. The
585 second one is nicer, but the slur doesn't start or end on the noteheads.
586 It gets 1.71 points for the left side and 9.37 points for the right
587 side, plus another 2 points because the slur ascends while the melody
588 descends for a total of 13.08 ulgy points. The final slur gets 10.04
589 points for the gap on the right and 2 points for the upward slope, but
590 it is the most attractive of the three configurations, so LilyPond
591 selects that one.
592
593 This technique is quite general, and is used to select beam
594 configurations, ties and dots in chords, line breaks, and page breaks.
595
596 @node Notation benchmarking
597 @unnumberedsubsec Notation benchmarking
598
599 We have tuned LilyPond's layout rules by comparing its output to
600 hand-engraved scores. Here is one line of a benchmark piece from a
601 hand-engraved edition (Bärenreiter BA320), and as engraved by an old
602 version of LilyPond (version 1.4, released May 2001):
603
604 @iftex
605 @sourceimage{baer-sarabande-hires,16cm,,}
606 @end iftex
607 @ifnottex
608 @sourceimage{baer-sarabande,,,png}
609 @end ifnottex
610
611 @iftex
612 @sourceimage{lily14-sarabande,16cm,,}
613 @end iftex
614 @ifnottex
615 @sourceimage{lily14-sarabande,,,png}
616 @end ifnottex
617
618 @noindent The LilyPond 1.4 output is certainly readable, but close
619 comparison with the hand-engraved score showed a lot of errors in the
620 formatting details:
621
622 @itemize @bullet
623 @item most of the stems are too long
624 @item the trill marks are too big
625 @item the second and fourth measures are too narrow
626 @item the slur is awkward-looking
627 @item the stems are too thin
628 @item there was too much space before the time signature
629 @end itemize
630
631 @noindent
632 (There were also two missing notes, and one wrong one!)
633
634 By adjusting the layout rules and font design, the output has improved
635 considerably. This is the same musical quotation compared to the output
636 from the current version of LilyPond (@version{}):
637
638 @iftex
639 @sourceimage{baer-sarabande-hires,16cm,,}
640 @end iftex
641 @ifnottex
642 @sourceimage{baer-sarabande,,,png}
643 @end ifnottex
644
645 @lilypond[staffsize=17.5,line-width=15.9\cm]
646 \relative c {
647   \clef "bass"
648   \key d \minor
649   \time 3/4
650   \mergeDifferentlyDottedOn
651   << {\slurDashed d8.-\flageolet( e16) e4.-\trill( d16 e)}
652      \\ {d4_2 a2}
653   >>
654   \slurDashed
655   <f' a, d,>4. e8( d c)
656   \slurSolid
657   bes g' f e16( f g_1 a_2 bes_3 d,_2)
658   \slurDashed
659   cis4.-\trill b8_3( a g)
660   << {\slurDashed d'8.( e16) e4.-\trill( d16 e)}
661      \\ {<f, a>4 a2}
662   >>
663 }
664 @end lilypond
665
666 @noindent
667 The current output is not a clone of the reference edition, but it is
668 much closer to publication quality that the earlier output.
669
670 [@strong{Andrew H:} This comparison is not finished. I promise not to
671 leave it in such a mess!]
672
673 Another valuable form of benchmarking is comparing LilyPond's output to
674 the output of a commercial software product. In this case we have chosen
675 Finale 2008, which is one of the most popular commercial scorewriters,
676 particularly in North America. Sibelius is their major rival and they
677 appear to be especially strong in the European market.
678
679 We have selected the last seven measures of Bach's Fugue in G minor from
680 the Well-Tempered Clavier, Book I, BWV 861. In the final draft there
681 will be pitcures here, but for now please consult the appendix
682 (currently pp. 12--14) for the output from both programs and images from
683 four reference engravings.
684
685 My findings so far:
686
687 @itemize @bullet
688 @item Finale rests are always at the same heights (in v1/v2 situations).
689 @item Finale doesn't interlock notes nicely (mm. 28--29).
690 @item Finale misses the B-flat in mm. 33!
691 @item Finale's beamed stems are almost always too long when they extend
692 off the staff.
693 @item LilyPond 2.13.5 has a vertical spacing problem (no padding).
694 @item LilyPond could use a little more space before the first note of
695 mm. 30, 33--34.
696 @item LilyPond's ties to beat 1 of mm. 31 are shorter than any of the
697 reference scores, and Finale's are even worse.
698 @item LilyPond's stems are often shorter than any of the references,
699 especially RH mm. 31.
700 @item In some locations, the Bärenreiter engraver(s) place rests
701 further from the staff than strictly necessary in order to keep them
702 vertically close to the subsequent element in that voice (e.g. mm. 28).
703 @item There is some acceptable variation within the reference scores,
704 although some scores are more attractive than others.
705 @end itemize
706
707 @c Han-Wen said once:
708 @c A lot of readers misinterpret the Finale example. Finale is a
709 @c powerful package, and in the hands of a good engraver — which is
710 @c something different from a good musician — it can produce very good
711 @c scores. However, the friendly GUI is misleading: you need a lot of
712 @c time and expertise to get decent scores from Finale.
713
714 @c How do we go about implementing typography?  If craftsmen need
715 @c over ten years to become true masters, how could we simple hackers
716 @c ever write a program to take over their jobs?
717
718 @c The answer is: we cannot.
719
720 The last two points lead into this concluding/transition paragraph:
721
722 Typography relies on human judgment of
723 appearance, so people cannot be replaced completely.  However,
724 much of the dull work can be automated.  If LilyPond solves most
725 of the common situations correctly, this will be a huge
726 improvement over existing software.  The remaining cases can be
727 tuned by hand.  Over the course of years, the software can be
728 refined to do more and more things automatically, so manual
729 overrides are less and less necessary. Where manual adjustments are
730 needed, LilyPond's structure must be ...
731
732 @node Flexible architecture
733 @unnumberedsubsec Flexible architecture
734
735 When we started, we wrote the LilyPond program entirely in the C++
736 programming language; the program's functionality was set in stone
737 by the developers.  That proved to be unsatisfactory for a number
738 of reasons:
739
740 @itemize
741
742 @item When LilyPond makes mistakes, users need to override
743 formatting decisions.  Therefore, the user must have access to the
744 formatting engine.  Hence, rules and settings cannot be fixed by
745 us at compile-time but must be accessible for users at run-time.
746
747 @item Engraving is a matter of visual judgment, and therefore a
748 matter of taste.  As knowledgeable as we are, users can disagree
749 with our personal decisions.  Therefore, the definitions of
750 typographical style must also be accessible to the user.
751
752 @item Finally, we continually refine the formatting algorithms, so
753 we need a flexible approach to rules.  The C++ language forces a
754 certain method of grouping rules that cannot readily be applied to
755 formatting music notation.
756
757 @end itemize
758
759 @cindex Scheme programming language
760
761 These problems have been addressed by integrating an interpreter
762 for the Scheme programming language and rewriting parts of
763 LilyPond in Scheme.  The current formatting architecture is built
764 around the notion of graphical objects, described by Scheme
765 variables and functions.  This architecture encompasses formatting
766 rules, typographical style and individual formatting decisions.
767 The user has direct access to most of these controls.
768
769 Scheme variables control layout decisions.  For example, many
770 graphical objects have a direction variable that encodes the
771 choice between up and down (or left and right).  Here you see two
772 chords, with accents and arpeggios.  In the first chord, the
773 graphical objects have all directions down (or left).  The second
774 chord has all directions up (right).
775
776 @lilypond[quote,ragged-right]
777 \new Score \with {
778    \override SpacingSpanner #'spacing-increment = #3
779    \override TimeSignature #'transparent = ##t
780 } \relative c' {
781    \stemDown <e g b>4_>-\arpeggio
782    \override Arpeggio #'direction = #RIGHT
783    \stemUp <e g b>4^>-\arpeggio
784 }
785 @end lilypond
786
787 @cindex score formatting
788 @cindex formatting a score
789 @cindex formatting rules
790
791 @noindent
792 The process of formatting a score consists of reading and writing
793 the variables of graphical objects.  Some variables have a preset
794 value.  For example, the thickness of many lines -- a
795 characteristic of typographical style -- is a variable with a
796 preset value.  You are free to alter this value, giving your score
797 a different typographical impression.
798
799 @lilypond[quote,ragged-right]
800 fragment = {
801    \clef bass f8 as8
802    c'4-~ c'16 as g f e16 g bes c' des'4
803 }
804 <<
805    \new Staff \fragment
806    \new Staff \with {
807       \override Beam #'beam-thickness = #0.3
808       \override Stem #'thickness = #0.5
809       \override Bar #'thickness = #3.6
810       \override Tie #'thickness = #2.2
811       \override StaffSymbol #'thickness = #3.0
812       \override Tie #'extra-offset = #'(0 .  0.3)
813       }
814       \fragment
815 >>
816 @end lilypond
817
818 Formatting rules are also preset variables: each object has
819 variables containing procedures.  These procedures perform the
820 actual formatting, and by substituting different ones, we can
821 change the appearance of objects.  In the following example, the
822 rule governing which note head objects are used to produce the
823 note head symbol is changed during the music fragment.
824
825 @lilypond[quote,ragged-right]
826 #(set-global-staff-size 30)
827
828 #(define (mc-squared grob orig current)
829   (let* ((interfaces (ly:grob-interfaces grob))
830          (pos (ly:grob-property grob 'staff-position)))
831     (if (memq 'note-head-interface interfaces)
832         (begin
833           (ly:grob-set-property! grob 'stencil
834             (grob-interpret-markup grob
835               (make-lower-markup 0.5
836                 (case pos
837                   ((-5) "m")
838                   ((-3) "c ")
839                   ((-2) (make-smaller-markup (make-bold-markup "2")))
840                   (else "bla")))))))))
841
842 \new Voice \relative c' {
843   \stemUp
844   \set autoBeaming = ##f
845   \time 2/4
846   <d f g>4
847   \once \override NoteHead #'stencil = #note-head::brew-ez-stencil
848   \once \override NoteHead #'font-size = #-7
849   \once \override NoteHead #'font-family = #'sans
850   \once \override NoteHead #'font-series = #'bold
851   <d f g>4
852   \once \override NoteHead #'style = #'cross
853   <d f g>4
854   \applyOutput #'Voice #mc-squared
855   <d f g>4
856   <<
857     { d8[ es-( fis^^ g] fis2-) }
858     \repeat unfold 5 { \applyOutput #'Voice #mc-squared s8 }
859   >>
860 }
861 @end lilypond
862
863
864 @node What symbols to engrave?
865 @unnumberedsec What symbols to engrave?
866
867 @cindex engraving
868 @cindex typography
869 @cindex engraver
870 @cindex plug-in
871
872 The formatting process decides where to place symbols.  However,
873 this can only be done once it is decided @emph{what} symbols
874 should be printed -- in other words, what notation to use.
875
876 Common music notation is a system of recording music that has
877 evolved over the past 1000 years.  The form that is now in common
878 use dates from the early Renaissance.  Although the basic form
879 (i.e., note heads on a 5-line staff) has not changed, the details
880 still evolve to express the innovations of contemporary notation.
881 Hence, common music notation encompasses some 500 years of music.
882 Its applications range from monophonic melodies to monstrous
883 counterpoints for a large orchestra.
884
885 How can we get a grip on such a seven-headed beast, and force it
886 into the confines of a computer program?  Our solution is to break
887 up the problem of notation (as opposed to engraving, i.e.,
888 typography) into digestible and programmable chunks: every type of
889 symbol is handled by a separate module, a so-called plug-in.  Each
890 plug-in is completely modular and independent, so each can be
891 developed and improved separately.  Such plug-ins are called
892 @code{engraver}s, by analogy with craftsmen who translate musical
893 ideas to graphic symbols.
894
895 In the following example, we start out with a plug-in for note
896 heads, the @code{Note_heads_engraver}.
897
898 @lilypond[quote,ragged-right]
899 \include "engraver-example.ily"
900
901 \score {
902   \topVoice
903   \layout {
904     \context {
905       \Voice
906       \remove "Stem_engraver"
907       \remove "Phrasing_slur_engraver"
908       \remove "Slur_engraver"
909       \remove "Script_engraver"
910       \remove "Beam_engraver"
911       \remove "Auto_beam_engraver"
912     }
913     \context {
914       \Staff
915       \remove "Accidental_engraver"
916       \remove "Key_engraver"
917       \remove "Clef_engraver"
918       \remove "Bar_engraver"
919       \remove "Time_signature_engraver"
920       \remove "Staff_symbol_engraver"
921       \consists "Pitch_squash_engraver"
922     }
923   }
924 }
925 @end lilypond
926
927 @noindent
928 Then a @code{Staff_symbol_engraver} adds the staff,
929
930 @lilypond[quote,ragged-right]
931 \include "engraver-example.ily"
932
933 \score {
934   \topVoice
935   \layout {
936     \context {
937       \Voice
938       \remove "Stem_engraver"
939       \remove "Phrasing_slur_engraver"
940       \remove "Slur_engraver"
941       \remove "Script_engraver"
942       \remove "Beam_engraver"
943       \remove "Auto_beam_engraver"
944     }
945     \context {
946       \Staff
947       \remove "Accidental_engraver"
948       \remove "Key_engraver"
949       \remove "Clef_engraver"
950       \remove "Bar_engraver"
951       \consists "Pitch_squash_engraver"
952       \remove "Time_signature_engraver"
953     }
954   }
955 }
956 @end lilypond
957
958 @noindent
959 the @code{Clef_engraver} defines a reference point for the staff,
960
961 @lilypond[quote,ragged-right]
962 \include "engraver-example.ily"
963
964 \score {
965   \topVoice
966   \layout {
967     \context {
968       \Voice
969       \remove "Stem_engraver"
970       \remove "Phrasing_slur_engraver"
971       \remove "Slur_engraver"
972       \remove "Script_engraver"
973       \remove "Beam_engraver"
974       \remove "Auto_beam_engraver"
975     }
976     \context {
977       \Staff
978       \remove "Accidental_engraver"
979       \remove "Key_engraver"
980       \remove "Bar_engraver"
981       \remove "Time_signature_engraver"
982     }
983   }
984 }
985 @end lilypond
986
987 @noindent
988 and the @code{Stem_engraver} adds stems.
989
990 @lilypond[quote,ragged-right]
991 \include "engraver-example.ily"
992
993 \score {
994   \topVoice
995   \layout {
996     \context {
997       \Voice
998       \remove "Phrasing_slur_engraver"
999       \remove "Slur_engraver"
1000       \remove "Script_engraver"
1001       \remove "Beam_engraver"
1002       \remove "Auto_beam_engraver"
1003     }
1004     \context {
1005       \Staff
1006       \remove "Accidental_engraver"
1007       \remove "Key_engraver"
1008       \remove "Bar_engraver"
1009       \remove "Time_signature_engraver"
1010     }
1011   }
1012 }
1013 @end lilypond
1014
1015 @noindent
1016 The @code{Stem_engraver} is notified of any note head coming
1017 along.  Every time one (or more, for a chord) note head is seen, a
1018 stem object is created and connected to the note head.  By adding
1019 engravers for beams, slurs, accents, accidentals, bar lines, time
1020 signature, and key signature, we get a complete piece of notation.
1021
1022 @lilypond[quote,ragged-right]
1023 \include "engraver-example.ily"
1024 \score { \topVoice }
1025 @end lilypond
1026
1027 @cindex polyphony
1028 @cindex engraving multiple voices
1029 @cindex contexts
1030
1031 This system works well for monophonic music, but what about
1032 polyphony?  In polyphonic notation, many voices can share a staff.
1033
1034 @lilypond[quote,ragged-right]
1035 \include "engraver-example.ily"
1036 \new Staff << \topVoice \\ \botVoice >>
1037 @end lilypond
1038
1039 In this situation, the accidentals and staff are shared, but the
1040 stems, slurs, beams, etc., are private to each voice.  Hence,
1041 engravers should be grouped.  The engravers for note heads, stems,
1042 slurs, etc., go into a group called @q{Voice context,} while the
1043 engravers for key, accidental, bar, etc., go into a group called
1044 @q{Staff context.}  In the case of polyphony, a single Staff
1045 context contains more than one Voice context.  Similarly, multiple
1046 Staff contexts can be put into a single Score context.  The Score
1047 context is the top level notation context.
1048
1049
1050 @seealso
1051 Internals Reference: @rinternals{Contexts}.
1052
1053
1054 @lilypond[quote,ragged-right]
1055 \include "engraver-example.ily"
1056 \score {
1057    <<
1058       \new Staff << \topVoice \\ \botVoice >>
1059       \new Staff << \pah \\ \hoom >>
1060    >>
1061 }
1062 @end lilypond
1063
1064
1065 @node Music representation
1066 @unnumberedsec Music representation
1067
1068 @cindex syntax
1069 @cindex recursive structures
1070
1071 Ideally, the input format for any high-level formatting system is
1072 an abstract description of the content.  In this case, that would
1073 be the music itself.  This poses a formidable problem: how can we
1074 define what music really is? Instead of trying to find an answer,
1075 we have reversed the question.  We write a program capable of
1076 producing sheet music, and adjust the format to be as lean as
1077 possible.  When the format can no longer be trimmed down, by
1078 definition we are left with content itself.  Our program serves as
1079 a formal definition of a music document.
1080
1081 The syntax is also the user-interface for LilyPond, hence it is
1082 easy to type:
1083
1084 @example
1085 @{
1086   c'4 d'8
1087 @}
1088 @end example
1089
1090 @noindent
1091 to create a quarter note on middle C (C1) and an eighth note on
1092 the D above middle C (D1).
1093
1094 @lilypond[quote]
1095 {
1096   c'4 d'8
1097 }
1098 @end lilypond
1099
1100 On a microscopic scale, such syntax is easy to use.  On a larger
1101 scale, syntax also needs structure.  How else can you enter
1102 complex pieces like symphonies and operas?  The structure is
1103 formed by the concept of music expressions: by combining small
1104 fragments of music into larger ones, more complex music can be
1105 expressed.  For example
1106
1107 @lilypond[quote,verbatim,fragment,relative=1]
1108 f4
1109 @end lilypond
1110
1111 @noindent
1112 Simultaneous notes can be constructed by enclosing them with
1113 @code{<<} and @code{>>}:
1114
1115 @example
1116 <<c4 d4 e4>>
1117 @end example
1118
1119 @lilypond[quote,fragment,relative=1]
1120 \new Voice { <<c4 d4 e>> }
1121 @end lilypond
1122
1123 @noindent
1124 This expression is put in sequence by enclosing it in curly braces
1125 @code{@{@tie{}@dots{}@tie{}@}}:
1126
1127 @example
1128 @{ f4 <<c4 d4 e4>> @}
1129 @end example
1130
1131 @lilypond[quote,relative=1,fragment]
1132 { f4 <<c d e4>> }
1133 @end lilypond
1134
1135 @noindent
1136 The above is also an expression, and so it may be combined again
1137 with another simultaneous expression (a half note) using
1138 @code{<<}, @code{\\}, and @code{>>}:
1139
1140 @example
1141 << g2 \\ @{ f4 <<c4 d4 e4>> @} >>
1142 @end example
1143
1144 @lilypond[quote,fragment,relative=2]
1145 \new Voice { << g2 \\ { f4 <<c d e>> } >> }
1146 @end lilypond
1147
1148 Such recursive structures can be specified neatly and formally in
1149 a context-free grammar.  The parsing code is also generated from
1150 this grammar.  In other words, the syntax of LilyPond is clearly
1151 and unambiguously defined.
1152
1153 User-interfaces and syntax are what people see and deal with most.
1154 They are partly a matter of taste, and also the subject of much
1155 discussion.  Although discussions on taste do have their merit,
1156 they are not very productive.  In the larger picture of LilyPond,
1157 the importance of input syntax is small: inventing neat syntax is
1158 easy, while writing decent formatting code is much harder.  This
1159 is also illustrated by the line-counts for the respective
1160 components: parsing and representation take up less than 10% of
1161 the source code.
1162
1163
1164 @node Example applications
1165 @unnumberedsec Example applications
1166
1167 @cindex simple examples
1168 @cindex examples, simple
1169
1170 We have written LilyPond as an experiment of how to condense the
1171 art of music engraving into a computer program.  Thanks to all
1172 that hard work, the program can now be used to perform useful
1173 tasks.  The simplest application is printing notes.
1174
1175 @lilypond[quote,relative=1]
1176 {
1177   \time 2/4
1178   c4 c g'4 g a4 a g2
1179 }
1180 @end lilypond
1181
1182 @noindent
1183 By adding chord names and lyrics we obtain a lead sheet.
1184
1185 @lilypond[quote,ragged-right]
1186 <<
1187    \chords { c2 c f2 c }
1188    \new Staff
1189    \relative c' {
1190      \time 2/4
1191      c4 c g' g a a g2
1192    }
1193    \addlyrics { twin -- kle twin -- kle lit -- tle star }
1194 >>
1195 @end lilypond
1196
1197 Polyphonic notation and piano music can also be printed.  The
1198 following example combines some more exotic constructs.
1199
1200 @lilypond[quote]
1201 \header {
1202   title = "Screech and boink"
1203   subtitle = "Random complex notation"
1204   composer = "Han-Wen Nienhuys"
1205 }
1206
1207 \score {
1208   \context PianoStaff <<
1209     \new Staff = "up" {
1210       \time 4/8
1211       \key c \minor
1212       << {
1213         \revert Stem #'direction
1214         \change Staff = down
1215         \set subdivideBeams = ##t
1216         g16.[
1217           \change Staff = up
1218           c'''32
1219           \change Staff = down
1220           g32
1221           \change Staff = up
1222           c'''32
1223           \change Staff = down
1224           g16]
1225         \change Staff = up
1226         \stemUp
1227         \set followVoice = ##t
1228         c'''32([ b''16 a''16 gis''16 g''32)]
1229       } \\ {
1230         s4 \times 2/3 { d'16[ f' g'] } as'32[ b''32 e'' d'']
1231       } \\ {
1232         s4 \autoBeamOff d''8.. f''32
1233       } \\ {
1234         s4 es''4
1235       } >>
1236     }
1237
1238     \new Staff = "down" {
1239       \clef bass
1240       \key c \minor
1241       \set subdivideBeams = ##f
1242       \override Stem  #'french-beaming = ##t
1243       \override Beam  #'beam-thickness = #0.3
1244       \override Stem  #'thickness = #4.0
1245       g'16[ b16 fis16 g16]
1246       << \makeClusters {
1247         as16 <as b>
1248         <g b>
1249         <g cis>
1250       } \\ {
1251         \override Staff.Arpeggio  #'arpeggio-direction =#down
1252         <cis, e, gis, b, cis>4\arpeggio
1253       }
1254     >> }
1255   >>
1256   \midi {
1257     \context {
1258       \Score
1259       tempoWholesPerMinute = #(ly:make-moment 60 8)
1260     }
1261   }
1262   \layout {
1263     \context {
1264       \Staff
1265       \consists Horizontal_bracket_engraver
1266     }
1267   }
1268 }
1269 @end lilypond
1270
1271 The fragments shown above have all been written by hand, but that
1272 is not a requirement.  Since the formatting engine is mostly
1273 automatic, it can serve as an output means for other programs that
1274 manipulate music.  For example, it can also be used to convert
1275 databases of musical fragments to images for use on websites and
1276 multimedia presentations.
1277
1278 This manual also shows an application: the input format is text,
1279 and can therefore be easily embedded in other text-based formats
1280 such as @LaTeX{}, HTML, or in the case of this manual, Texinfo.
1281 By means of a special program, the input fragments can be replaced
1282 by music images in the resulting PDF or HTML output files.  This
1283 makes it easy to mix music and text in documents.
1284
1285
1286 TODO: add extra chapter for computer aesthetics?
1287
1288 @page
1289 @node Appendix
1290 @unnumberedsec Appendix
1291
1292 This appendix contaions four reference engravings and two
1293 software-engraved versions of Bach's Fugue in G minor from the
1294 Well-Tempered Clavier, Book I, BWV 861 (the last seven measures).
1295
1296 @noindent
1297 Bärenreiter BA5070 (Neue Ausgabe Sämtlicher Werke, Serie V, Band 6.1,
1298 1989):
1299
1300 @iftex
1301 @sourceimage{bwv861-baer,16cm,,}
1302 @end iftex
1303 @ifnottex
1304 @sourceimage{bwv861-baer-small,,,png}
1305 @end ifnottex
1306
1307 @noindent
1308 Bärenreiter BA5070 (Neue Ausgabe Sämtlicher Werke, Serie V, Band 6.1,
1309 1989), an alternate musical source. Aside from the textual differences,
1310 this demsontrates slight variations in the engraving decisions, even
1311 from the same publisher and edition:
1312
1313 @iftex
1314 @sourceimage{bwv861-baer-alt,16cm,,}
1315 @end iftex
1316 @ifnottex
1317 @sourceimage{bwv861-baer-alt-small,,,png}
1318 @end ifnottex
1319
1320 @noindent
1321 Breitkopf & Härtel, edited by Ferruccio Busoni (Wiesbaden, 1894), also
1322 available from the Petrucci Music Library (IMSLP #22081). The editorial
1323 markings (fingerings, articulations, etc.) have been removed for clearer
1324 comparison with the other editions here:
1325
1326 @iftex
1327 @sourceimage{bwv861-breitkopf,16cm,,}
1328 @end iftex
1329 @ifnottex
1330 @sourceimage{bwv861-breitkopf-small,,,png}
1331 @end ifnottex
1332
1333 @noindent
1334 Bach-Gessellschaft edition (Leipzig, 1866), available from the Petrucci
1335 Music Library (IMSPL #02221):
1336
1337 @iftex
1338 @sourceimage{bwv861-gessellschaft,16cm,,}
1339 @end iftex
1340 @ifnottex
1341 @sourceimage{bwv861-gessellschaft-small,,,png}
1342 @end ifnottex
1343
1344 @noindent
1345 Finale 2008:
1346
1347 @iftex
1348 @sourceimage{pdf/bwv861-finale2008a,,,}
1349 @end iftex
1350 @ifnottex
1351 @sourceimage{bwv861-finale2008a,,,png}
1352 @end ifnottex
1353
1354 @noindent
1355 LilyPond, version @version{}:
1356
1357 @lilypond[staffsize=14.3,line-width=15.9\cm]
1358 global = {\key g \minor}
1359
1360 partI = \relative c' {
1361   \voiceOne
1362   fis8 d' ees g, fis4 g
1363   r8 a16 bes c8 bes16 a d8 r r4
1364   r2 r8 d16 ees f8 ees16 d
1365   ees4 ~ ees16 d c bes a4 r8 ees'16 d
1366   c8 d16 ees d8 e16 fis g8 fis16 g a4 ~
1367   a8 d, g f ees d c bes
1368   a2 g\fermata \bar "|."
1369 }
1370
1371 partII = \relative c' {
1372   \voiceTwo
1373   d4 r4 r8 d'16 c bes8 c16 d
1374   ees8 d c ees a, r r4
1375   r8 fis16 g a8 g16 fis g2 ~
1376   g2 r8 d' ees g,
1377   fis4 g r8 a16 bes c8 bes16 a
1378   bes4. <g b>8 <a c> r <d, g> r
1379   <ees g>4 <d fis> d2
1380 }
1381 partIII = \relative c' {
1382   \voiceOne
1383   r2 r8 d ees g, fis4 g r8 a16 bes c8 bes16 a
1384   bes2 ~ bes8 b16 a g8 a16 b
1385   c4 r r2
1386   R1
1387   r8 d ees g, fis4 g
1388   r8 a16 bes c8 bes16 a b2
1389 }
1390 partIV = \relative c {
1391   \voiceTwo
1392   d4 r r2
1393   r8 d ees g, fis4 a
1394   d,8 d'16 c bes8 c16 d ees2 ~
1395   ees8 ees16 d c8 d16 ees fis,8 a16 g fis8 g16 a
1396   d,8 d'16 c bes8 c16 d ees8 c a fis'
1397   g f ees d c bes a g
1398   c a d d, g2\fermata
1399 }
1400
1401 \score {
1402   <<
1403     % \set Score.barNumberVisibility = #all-bar-numbers-visible
1404     % required in 2.13
1405     \set Score.currentBarNumber = #28
1406     \bar ""
1407     \new PianoStaff <<
1408       \new Staff = "RH" <<
1409         \global
1410         \new Voice = "voiceI" { \partI }
1411         \new Voice = "voiceII" { \partII }
1412       >>
1413
1414       \new Staff = "LH" <<
1415         \clef "bass"
1416         \global
1417         \new Voice = "voiceIII" { \partIII }
1418         \new Voice = "voiceIV" { \partIV }
1419       >>
1420     >>
1421   >>
1422   \layout {
1423     \context {
1424       \Staff
1425       \remove "Time_signature_engraver"
1426     }
1427   }
1428 }
1429 @end lilypond