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