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