]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
Finish LM 3 'Fundamental concepts' proofreading
[lilypond.git] / Documentation / user / fundamental.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
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.11.51"
11
12 @node Fundamental concepts
13 @chapter Fundamental concepts
14
15 You've seen in the Tutorial how to produce beautifully printed
16 music from a simple text file.  This section introduces the
17 concepts and techniques required to produce equally beautiful
18 but more complex scores.
19
20 @menu
21 * How LilyPond input files work::
22 * Voices contain music::
23 * Contexts and engravers::
24 * Extending the templates::
25 @end menu
26
27
28 @node How LilyPond input files work
29 @section How LilyPond input files work
30
31 The LilyPond input format is quite free-form, giving experienced
32 users a lot of flexibility to structure their files however they
33 wish.  But this flexibility can make things confusing for new
34 users.  This section will explain some of this structure, but may
35 gloss over some details in favor of simplicity.  For a complete
36 description of the input format, see @ruser{File structure}.
37
38 @menu
39 * Introduction to the LilyPond file structure::
40 * Score is a (single) compound musical expression::
41 * Nesting music expressions::
42 * On the un-nestedness of brackets and ties::
43 @end menu
44
45 @node Introduction to the LilyPond file structure
46 @subsection Introduction to the LilyPond file structure
47
48 @cindex input format
49 @cindex file structure
50
51 A basic example of a LilyPond input file is
52
53 @example
54 \version @w{"@version{}"}
55 \header @{ @}
56 \score @{
57   @var{...compound music expression...}  % all the music goes here!
58   \layout @{ @}
59   \midi @{ @}
60 @}
61 @end example
62
63 @noindent
64 There are many variations of this basic pattern, but this
65 example serves as a useful starting place.
66
67 @funindex \book
68 @funindex \score
69 @cindex book
70 @cindex score
71
72 Up to this point none of the examples you have seen has used a
73 @code{\score@{@}} command.  This is because LilyPond automatically
74 adds the extra commands which are needed when you give it simple
75 input.  LilyPond treats input like this:
76
77 @example
78 \relative c'' @{
79   c4 a d c
80 @}
81 @end example
82
83 @noindent
84 as shorthand for this:
85
86 @example
87 \book @{
88   \score @{
89     \new Staff @{
90       \new Voice @{
91         \relative c'' @{
92           c4 a b c
93         @}
94       @}
95     @}
96     \layout @{ @}
97   @}
98 @}
99 @end example
100
101 In other words, if the input contains a single music expression,
102 LilyPond will interpret the file as though the music expression
103 was wrapped up inside the commands shown above.
104
105 @cindex implicit contexts
106
107 @strong{A word of warning!}  Many of the examples in the LilyPond
108 documentation will omit the @code{\new Staff} and @code{\new Voice}
109 commands, leaving them to be created implicitly.  For simple
110 examples this works well, but for more complex examples, especially
111 when additional commands are used, the implicit creation of contexts
112 can give surprising results, maybe creating extra unwanted staves.
113 The way to create contexts explicitly is explained in 
114 @ref{Contexts and engravers}.
115
116 @warning{When entering more than a few lines of music it is
117 advisable to always create staves and voices explicitly.}
118
119 For now, though, let us return to the first example and examine the
120 @code{\score} command, leaving the others to default.
121
122 A @code{\score} block must always contain just one music expression,
123 and this must appear immediately after the @code{\score} command.
124 Remember that a music expression could be anything from a single
125 note to a huge compound expression like
126
127 @example
128 @{
129   \new GrandStaff <<
130     @var{...insert the whole score of a Wagner opera in here...}
131   >>
132 @}
133 @end example
134
135 @noindent
136 Since everything is inside @code{@{ ... @}}, it counts
137 as one music expression.
138
139 As we saw previously, the @code{\score} block can contain other
140 things, such as
141
142 @example
143 \score @{
144   @{ c'4 a b c' @}
145   \header @{ @}
146   \layout @{ @}
147   \midi @{ @}
148 @}
149 @end example
150
151 @funindex \header
152 @funindex \layout
153 @funindex \midi
154 @cindex header
155 @cindex layout
156 @cindex midi
157
158 @noindent
159 Note that these three commands -- @code{\header}, @code{\layout} and
160 @code{\midi} -- are special: unlike many other commands which begin
161 with a backward slash (@code{\}) they are @emph{not} music expressions
162 and are not part of any music expression.  So they may be placed
163 inside a @code{\score} block or outside it.  In fact, these commands
164 are commonly placed outside the @code{\score} block -- for example,
165 @code{\header} is often placed above the @code{\score} command, as the
166 example at the beginning of this section shows.
167
168 Two more commands you have not previously seen are
169 @code{\layout @{ @}} and @code{\midi @{@}}.  If these appear as
170 shown they will cause LilyPond to produce a printed output and a
171 MIDI output respectively.  They are described fully in the
172 Notation Reference -- @ruser{Score layout}, and
173 @ruser{Creating MIDI files}.
174
175 @cindex scores, multiple
176
177 You may code multiple @code{\score} blocks.  Each will be
178 treated as a separate score, but they will be all combined into
179 a single output file.  A @code{\book} command is not necessary
180 -- one will be implicitly created.  However, if you would like
181 separate output files from one @code{.ly} file then the
182 @code{\book} command should be used to separate the different
183 sections: each @code{\book} block will produce a
184 separate output file.
185
186 In summary:
187
188 Every @code{\book} block creates a separate output file (e.g., a
189 PDF file).  If you haven't explicitly added one, LilyPond wraps
190 your entire input code in a @code{\book} block implicitly.
191
192 Every @code{\score} block is a separate chunk of music within a
193 @code{\book} block.
194
195 Every @code{\layout} block affects the @code{\score} or
196 @code{\book} block in which it appears -- i.e., a @code{\layout}
197 block inside a @code{\score} block affects only that @code{\score}
198 block, but a @code{\layout} block outside of a @code{\score} block
199 (and thus in a @code{\book} block, either explicitly or
200 implicitly) will affect every @code{\score} in that @code{\book}.
201
202 Every @code{\context} block will affect the named context (e.g.,
203 @code{\StaffGroup}) throughout the block (@code{\score} or
204 @code{\book}) in which it appears.
205
206 For details see @ruser{Multiple scores in a book}.
207
208 @cindex variables
209
210 Another great shorthand is the ability to define variables.  All
211 the templates use this
212
213 @example
214 melody = \relative c' @{
215   c4 a b c
216 @}
217
218 \score @{
219   \melody
220 @}
221 @end example
222
223 When LilyPond looks at this file, it takes the value of
224 @code{melody} (everything after the equals sign) and inserts it
225 whenever it sees @code{\melody}.  There's nothing special about
226 the names -- it could be @code{melody}, @code{global},
227 @code{TimeKey},
228 @code{pianorighthand}, or @code{foofoobarbaz}.  For more details,
229 see @ref{Saving typing with variables and functions}.
230 Remember that you can use almost any name you like as long
231 as it contains just alphabetic characters and is distinct from
232 LilyPond command names.  The exact
233 limitations on variable names are detailed in
234 @ruser{File structure}.
235
236
237
238 @seealso
239
240 For a complete definition of the input format, see
241 @ruser{File structure}.
242
243 @node Score is a (single) compound musical expression
244 @subsection Score is a (single) compound musical expression
245
246 @funindex \score
247 @cindex score
248 @cindex Compound music expression
249 @cindex Music expression, compound
250
251 We saw the general organization of LilyPond input files in the
252 previous section, @ref{How LilyPond input files work}.  But we seemed to
253 skip over the most important part: how do we figure out what to
254 write after @code{\score}?
255
256 We didn't skip over it at all.  The big mystery is simply that
257 there @emph{is} no mystery.  This line explains it all:
258
259 @quotation
260 @emph{A @code{\score} block must begin with a compound music expression.}
261 @end quotation
262
263 @noindent
264 To understand what is meant by a music expression and a compound
265 music expression, you may find it useful to review the tutorial,
266 @ref{Music expressions explained}.  In that section, we saw how to
267 build big music expressions from small pieces -- we started from
268 notes, then chords, etc.  Now we're going to start from a big
269 music expression and work our way down.
270
271 @example
272 \score @{
273   @{ % this brace begins the overall compound music expression
274     \new GrandStaff <<
275       @var{...insert the whole score of a Wagner opera in here...}
276     >>
277   @} % this brace ends the overall compound music expression
278   \layout @{ @}
279 @}
280 @end example
281
282 A whole Wagner opera would easily double the length of this
283 manual, so let's just add a singer and piano.  We don't need a
284 @code{GrandStaff} for this ensemble, which simply groups a number
285 of staves together with a brace at the left, so we shall remove
286 it.  We @emph{do} need a singer and a piano, though.
287
288 @example
289 \score @{
290   <<
291     \new Staff = "singer" <<
292     >>
293     \new PianoStaff = piano <<
294     >>
295   >>
296   \layout @{ @}
297 @}
298 @end example
299
300 Remember that we use @code{<< ... >>} instead of @code{@{ ... @}} to
301 show simultaneous music.  And we definitely want to show the vocal
302 part and piano part at the same time, not one after the other!  Note
303 that the @code{<< ... >>} construct is not really necessary for the
304 Singer staff, as it contains only one sequential music expression;
305 however, using @code{<< ... >>} instead of braces is still necessary
306 if the music in the Staff is made of two simultaneous expressions,
307 e.g. two simultaneous Voices, or a Voice with lyrics.  We'll add some
308 real music later; for now let's just put in some dummy notes and
309 lyrics.
310
311 @lilypond[verbatim,quote,ragged-right]
312 \score {
313   <<
314     \new Staff = "singer" <<
315       \new Voice = "vocal" { c'1 }
316       \addlyrics { And }
317     >>
318     \new PianoStaff = "piano" <<
319       \new Staff = "upper" { c'1 }
320       \new Staff = "lower" { c'1 }
321     >>
322   >>
323   \layout { }
324 }
325 @end lilypond
326
327 Now we have a lot more details.  We have the singer's staff: it
328 contains a @code{Voice} (in LilyPond, this term refers to a set of
329 notes, not necessarily vocal notes -- for example, a violin
330 generally plays one voice) and some lyrics.  We also have a piano
331 staff: it contains an upper staff (right hand) and a lower staff
332 (left hand).
333
334 At this stage, we could start filling in notes.  Inside the curly
335 braces next to @code{\new Voice = vocal}, we could start writing
336
337 @example
338 \relative c'' @{
339   r4 d8\noBeam g, c4 r
340 @}
341 @end example
342
343 But if we did that, the @code{\score} section would get pretty
344 long, and it would be harder to understand what was happening.  So
345 let's use variables instead.  These were introduced at the end
346 of the previous section, remember?  So, adding a few notes, we
347 now have a piece of real music:
348
349 @lilypond[verbatim,quote,ragged-right]
350 melody = \relative c'' { r4 d8\noBeam g, c4 r }
351 text   = \lyricmode { And God said, }
352 upper  = \relative c'' { <g d g,>2~ <g d g,> }
353 lower  = \relative c { b2 e2 }
354
355 \score {
356   <<
357     \new Staff = "singer" <<
358       \new Voice = "vocal" { \melody }
359       \addlyrics { \text }
360     >>
361     \new PianoStaff = "piano" <<
362       \new Staff = "upper" { \upper }
363       \new Staff = "lower" {
364         \clef "bass"
365         \lower
366       }
367     >>
368   >>
369   \layout { }
370 }
371 @end lilypond
372
373
374 Be careful about the difference between notes, which are introduced
375 with @code{\relative} or which are directly included in a music
376 expression, and lyrics, which are introduced with
377 @code{\lyricmode}.  These are essential to tell LilyPond
378 to interpret the following content as music and text
379 respectively.
380
381 When writing (or reading) a @code{\score} section, just take it
382 slowly and carefully.  Start with the outer level, then work on
383 each smaller level.  It also really helps to be strict with
384 indentation -- make sure that each item on the same level starts
385 on the same horizontal position in your text editor.
386
387
388 @node Nesting music expressions
389 @subsection Nesting music expressions
390
391 @cindex staves, temporary
392 @cindex ossias
393
394 It is not essential to declare all staves at the beginning; they may
395 be introduced temporarily at any point.  This is particularly useful
396 for creating ossia sections -- see @rglos{ossia}.  Here is a simple
397 example showing how to introduce a new staff temporarily for the
398 duration of three notes:
399
400 @lilypond[verbatim,quote,ragged-right]
401 \new Staff {
402   \relative g' {
403     r4 g8 g c4 c8 d |
404     e4 r8
405     <<
406       { f c c }
407       \new Staff {
408         f8 f c
409       }
410     >>
411     r4 |
412   }
413 }
414 @end lilypond
415
416 @noindent
417 Note that the size of the clef is the same as a clef printed
418 following a clef change -- slightly smaller than the clef
419 at the beginning of the line.  This is usual for clefs printed
420 in the middle of a line.
421
422 @cindex staff, positioning
423
424 The ossia section may be placed above the staff
425 as follows:
426
427 @lilypond[verbatim,quote,ragged-right]
428 \new Staff ="main" {
429   \relative g' {
430     r4 g8 g c4 c8 d |
431     e4 r8
432     <<
433       { f c c }
434       \new Staff \with {
435         alignAboveContext = "main" }
436       { f8 f c }
437     >>
438     r4 |
439   }
440 }
441 @end lilypond
442
443 This example uses @code{\with}, which will be explained more
444 fully later.  It is a means of modifying the default behavior
445 of a single Staff.  Here it says that the new staff should be
446 placed above the staff called @qq{main} instead of the default
447 position which is below.
448
449 Ossia are often written without clef and without
450 time signature and are usually in a smaller font.
451 These require further commands which
452 have not yet been introduced.  See @ref{Size of objects}.
453
454 @node On the un-nestedness of brackets and ties
455 @subsection On the un-nestedness of brackets and ties
456
457 @cindex brackets, nesting
458
459 You have already met a number of different types of bracket in
460 writing the input file to LilyPond.  These obey different rules
461 which can be confusing at first.  Before we explain the rules
462 let's first review the different types of bracket.
463
464 @c attempt to force this onto a new page
465 @need 50
466 @multitable @columnfractions .3 .7
467 @headitem Bracket Type
468   @tab Function
469 @item @code{@{ .. @}}
470   @tab Encloses a sequential segment of music
471 @item @code{< .. >}
472   @tab Encloses the notes of a chord
473 @item @code{<< .. >>}
474   @tab Encloses simultaneous music expressions
475 @item @code{( .. )}
476   @tab Marks the start and end of a slur
477 @item @code{\( .. \)}
478   @tab Marks the start and end of a phrasing slur
479 @item @code{[ .. ]}
480   @tab Marks the start and end of a manual beam
481 @end multitable
482
483 To these we should add other constructs which generate lines
484 between or across notes: ties (marked by a tilde, @code{~}),
485 tuplets written as @code{\times x/y @{..@}}, and grace notes
486 written as @code{\grace@{..@}}.
487
488 Outside LilyPond, the conventional use of brackets requires
489 the different types to be properly nested, like this,
490 @code{<< [ @{ ( .. ) @} ] >>}, with the closing brackets being
491 encountered in exactly the opposite order to the opening
492 brackets.  This @strong{is} a requirement for the three types of
493 bracket described by the word @q{Encloses} in the table above --
494 they must nest properly.
495 However, the remaining brackets, described with the word
496 @q{Marks} in the table above together with ties and tuplets,
497 do @strong{not} have to nest
498 properly with any of the brackets.  In fact, these are not
499 brackets in the sense that
500 they enclose something -- they are simply markers to indicate
501 where something starts and ends.
502
503 So, for example, a phrasing slur can start before a manually
504 inserted beam and end before the end of the beam -- not very
505 musical, perhaps, but possible:
506
507 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
508  { g8\( a b[ c b\) a] }
509 @end lilypond
510
511 In general, different kinds of brackets, and those implied by
512 tuplets, ties and grace notes, may be mixed freely.
513 This example shows a beam extending into a tuplet (line 1),
514 a slur extending into a tuplet (line 2),
515 a beam and a slur extending into a tuplet, a tie crossing
516 two tuplets, and a phrasing slur extending out of a tuplet
517 (lines 3 and 4).
518
519 @lilypond[quote,verbatim,fragment,ragged-right]
520 {
521   r16[ g16 \times 2/3 {r16 e'8] }
522   g16( a \times 2/3 {b d) e' }
523   g8[( a \times 2/3 {b d') e'~]}
524   \times 4/5 {e'32\( a b d' e'} a'4.\)
525 }
526 @end lilypond
527
528
529 @node Voices contain music
530 @section Voices contain music
531
532 Singers need voices to sing, and so does LilyPond.
533 The actual music for all instruments in a score
534 is contained in Voices -- the most fundamental
535 of all LilyPond's concepts.
536
537 @menu
538 * I'm hearing Voices::
539 * Explicitly instantiating voices::
540 * Voices and vocals::
541 @end menu
542
543 @node I'm hearing Voices
544 @subsection I'm hearing Voices
545
546 @cindex polyphony
547 @cindex layers
548 @cindex multiple voices
549 @cindex Voice context
550 @cindex context, Voice
551 @cindex simultaneous music
552 @cindex concurrent music
553
554 The lowest, most fundamental or innermost layers in a LilyPond
555 score are called @q{Voice contexts} or just @q{Voices} for short.
556 Voices are sometimes called @q{layers} in other notation
557 packages.
558
559 In fact, a Voice layer or context is the only one which can
560 contain music.  If a Voice context is not explicitly declared
561 one is created automatically, as we saw at the beginning of
562 this chapter.  Some instruments such as an
563 Oboe can play only one note at a time.  Music written for
564 such instruments is monophonic and requires just a single
565 voice.  Instruments which can play more than one note at a
566 time like the piano will often require multiple voices to
567 encode the different concurrent notes and rhythms they are
568 capable of playing.
569
570 A single voice can contain many notes in a chord, of course,
571 so when exactly are multiple voices needed?  Look first at
572 this example of four chords:
573
574 @lilypond[quote,verbatim,fragment,ragged-right,relative=1]
575 \key g \major
576 <d g>4 <d fis> <d a'> <d g>
577 @end lilypond
578
579 This can be expressed using just the single angle bracket chord
580 symbols, @code{< ... >}, and for this just a single voice is
581 needed.  But suppose the F-sharp were actually an eighth-note
582 followed by an eighth-note G, a passing note on the way to the A?
583 Now we have two notes which start at the same time but have
584 different durations: the quarter-note D and the eighth-note
585 F-sharp.  How are these to be coded?  They cannot be written as
586 a chord because all the notes in a chord must have the same
587 duration.  And they cannot be written as two sequential notes
588 as they need to start at the same time.  This is when two
589 voices are required.
590
591 Let us see how this is done in LilyPond input syntax.
592
593 @funindex << \\ >>
594 @funindex \\
595
596 The easiest way to enter fragments with more than one voice on a
597 staff is to enter each voice as a sequence (with @code{@{...@}}),
598 and combine them simultaneously with angle brackets, @code{<<...>>}.
599 The fragments must also be separated with double backward slashes,
600 @code{\\}, to place them in separate voices.  Without these, the
601 notes would be entered into a single voice, which would usually
602 cause errors.  This technique is particularly suited to pieces of
603 music which are largely monophonic with occasional short sections
604 of polyphony.
605
606 Here's how we split the chords above into two voices and add both
607 the passing note and a slur:
608
609 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
610 \key g \major
611 %    Voice "1"                  Voice "2"
612 << { g4 fis8( g) a4 g }    \\ { d4 d d d }  >> |
613 @end lilypond
614
615 Notice how the stems of the second voice now point down.
616
617 Here's another simple example:
618
619 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
620 \key d \minor
621 %    Voice "1"             Voice "2"
622 << { r4 g g4. a8 }    \\ { d,2 d4 g }       >> |
623 << { bes4 bes c bes } \\ { g4 g g8( a) g4 } >> |
624 << { a2. r4 }         \\ { fis2. s4 }       >> |
625 @end lilypond
626
627 It is not necessary to use a separate @code{<< \\ >>} construct
628 for each bar. For music with few notes in each bar this layout
629 can help the legibility of the code, but if there are many
630 notes in each bar it may be better to split out each voice
631 separately, like this:
632
633 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
634 \key d \minor
635 << {
636   % Voice "1"
637   r4 g g4. a8 |
638   bes4 bes c bes |
639   a2. r4 |
640 } \\ {
641   % Voice "2"
642   d,2 d4 g |
643   g4 g g8( a) g4 |
644   fis2. s4 |
645 } >>
646 @end lilypond
647
648
649 This example has just two voices, but the same construct may be
650 used to encode three or more voices by adding more back-slash
651 separators.
652
653 The Voice contexts bear the names @code{"1"}, @code{"2"}, etc.
654 In each of these contexts, the vertical direction of slurs,
655 stems, ties, dynamics etc., is set appropriately.
656
657 @lilypond[quote,verbatim,fragment]
658 \new Staff \relative c' {
659   % Main voice
660   c16 d e f
661   %    Voice "1"     Voice "2"                 Voice "3"
662   << { g4 f e } \\ { r8 e4 d c8 ~ } >> |
663   << { d2 e2 }  \\ { c8 b16 a b8 g ~ g2 } \\ { s4 b4 c2 } >> |
664 }
665 @end lilypond
666
667 These voices are all separate from the main voice that contains
668 the notes just outside the @code{<< .. >>} construct.  Let's call
669 this the @emph{simultaneous construct}.  Slurs and ties may only
670 connect notes within the same voice, so slurs and ties cannot go
671 into or out of a simultaneous construct.  Conversely,
672 parallel voices from separate simultaneous constructs on the same
673 staff are the same voice.  Other voice-related properties also
674 carry across simultaneous constructs.  Here is the same example,
675 with different colors and note heads for each voice.  Note that
676 changes in one voice do not affect other voices, but they do
677 persist in the same voice later.  Note also that tied notes may be
678 split across the same voices in two constructs, shown here in the
679 blue triangle voice.
680
681 @lilypond[quote,verbatim]
682 \new Staff \relative c' {
683   % Main voice
684   c16 d e f
685   << % Bar 1
686     {
687       \voiceOneStyle
688       g4 f e
689     }
690   \\
691     {
692       \voiceTwoStyle
693       r8 e4 d c8 ~
694     }
695   >>
696   << % Bar 2
697      % Voice 1 continues
698     { d2 e2 }
699   \\
700      % Voice 2 continues
701     { c8 b16 a b8 g ~ g2 }
702   \\
703     {
704       \voiceThreeStyle
705       s4 b4 c2
706     }
707   >>
708 }
709 @end lilypond
710
711 @funindex \voiceOneStyle
712 @funindex \voiceTwoStyle
713 @funindex \voiceThreeStyle
714 @funindex \voiceFourStyle
715 @funindex \voiceNeutralStyle
716
717 The commands @code{\voiceXXXStyle} are mainly intended for use in
718 educational documents such as this one.  They modify the color
719 of the note head, the stem and the beams, and the style of the
720 note head, so that the voices may be easily distinguished.
721 Voice one is set to red diamonds, voice two to blue triangles,
722 voice three to green crossed circles, and voice four (not used
723 here) to magenta crosses;  @code{\voiceNeutralStyle} (also not
724 used here) reverts the style back to the default.  
725 We shall see later how commands like these may be created by the
726 user.
727 See @ref{Visibility and color of objects} and
728 @ref{Using variables for tweaks}.
729
730 Polyphony does not change the relationship of notes within a
731 @code{\relative @{ @}} block.  Each note is still calculated
732 relative to the note immediately preceding it, or to the first
733 note of the preceding chord.  So in
734
735 @example
736 \relative c' @{ noteA << < noteB noteC > \\ noteD >> noteE @}
737 @end example
738
739 @noindent
740 @code{noteB} is relative to @code{noteA}                      @*
741 @code{noteC} is relative to @code{noteB}, not @code{noteA};   @*
742 @code{noteD} is relative to @code{noteB}, not @code{noteA} or
743 @code{noteC};                                                 @*
744 @code{noteE} is relative to @code{noteD}, not @code{noteA}.
745
746 An alternative way, which may be clearer if the notes in the
747 voices are widely separated, is to place a @code{\relative}
748 command at the start of each voice:
749
750 @example
751 \relative c' @{ noteA ... @}
752 <<
753   \relative c'' @{ < noteB noteC > ... @}
754 \\
755   \relative g' @{ noteD ... @}
756 >>
757 \relative c' @{ noteE ... @}
758 @end example
759
760 Let us finally analyze the voices in a more complex piece of
761 music.  Here are
762 the notes from the first two bars of the second of Chopin's
763 Deux Nocturnes, Op 32.  This example will be used at later
764 stages in this and the next chapter to illustrate several
765 techniques for producing notation, so please ignore for now
766 anything in the underlying code which looks mysterious and
767 concentrate just on the music and the voices -- the
768 complications will all be explained in later sections.
769
770 @c The following should appear as music without code
771 @lilypond[quote,ragged-right]
772 \new Staff \relative c'' {
773   \key aes \major
774   << % Voice one
775     { c2 aes4. bes8 }
776   \\ % Voice two
777     { aes2 f4 fes }
778   \\ % No voice three
779   \\ % Voice four
780     {
781       % Ignore these for now - they are explained in Ch 4
782       \once \override NoteColumn #'force-hshift = #0
783       <ees c>2
784       \once \override NoteColumn #'force-hshift = #0.5
785       des2
786     }
787   >> |
788   <c ees aes c>1 |
789 }
790 @end lilypond
791
792 The direction of the stems is often used to indicate the
793 continuity of two simultaneous melodic lines.  Here the
794 stems of the highest notes are all pointing up and the
795 stems of the lower notes are all pointing down.
796 This is the first indication that more than one voice
797 is required.
798
799 But the real need for multiple voices arises when notes
800 which start at the same time have different durations.
801 Look at the notes which start at beat three in the first
802 bar.  The A-flat is a dotted quarter note, the F is a
803 quarter note and the D-flat is a half note.  These
804 cannot be written as a chord as all the notes in a chord
805 must have the same duration.  Neither can they be written
806 as sequential notes, as they must start at the same time.
807 This section of the bar requires three voices, and the
808 normal practice would be to write the whole bar as three
809 voices, as shown below, where we have used different note heads
810 and colors for the three voices.  Again, the code behind this
811 example will be explained later, so ignore anything you do
812 not understand.
813
814 @c The following should appear as music without code
815 @c The three voice styles should be defined in -init
816 @lilypond[quote,ragged-right]
817 \new Staff \relative c'' {
818   \key aes \major
819   <<
820     { % Voice one
821       \voiceOneStyle
822       c2 aes4. bes8
823     }
824   \\  % Voice two
825     { \voiceTwoStyle
826       aes2 f4 fes
827     }
828   \\  % No Voice three (we want stems down)
829   \\  % Voice four
830     { \voiceThreeStyle
831       % Ignore these for now - they are explained in Ch 4
832       \once \override NoteColumn #'force-hshift = #0
833       <ees c>2
834       \once \override NoteColumn #'force-hshift = #0.5
835       des2
836     }
837   >> |
838   <c ees aes c>1 |
839 }
840 @end lilypond
841
842
843 Let us try to encode this music from scratch.  As we
844 shall see, this encounters some difficulties.  We begin as
845 we have learnt, using the @code{<< \\  >>} construct to
846 enter the music of the first bar in three voices:
847
848 @lilypond[quote,verbatim,fragment,ragged-right]
849 \new Staff \relative c'' {
850   \key aes \major
851   <<
852     { c2 aes4. bes8 } \\ { aes2 f4 fes } \\ { <ees c>2 des2 }
853   >>
854   <c ees aes c>1
855 }
856 @end lilypond
857
858 @cindex stem down
859 @cindex stem up
860
861 The stem directions are automatically assigned with the
862 odd-numbered voices taking upward stems and the even-numbered
863 voices downward ones.  The stems for voices 1 and 2 are right,
864 but the stems in voice 3 should go down in this particular piece
865 of music.  We can correct this simply by missing out voice three
866 and placing the music in voice four:
867
868 @lilypond[quote,verbatim,fragment,ragged-right]
869 \new Staff \relative c'' {
870   \key aes \major
871   << % Voice one
872     { c2 aes4. bes8 }
873   \\ % Voice two
874     { aes2 f4 fes   }
875   \\ % Omit Voice three
876   \\ % Voice four
877     { <ees c>2 des2 }
878   >> |
879   <c ees aes c>1 |
880 }
881 @end lilypond
882
883 @noindent
884 We see that this fixes the stem direction, but exposes a problem
885 sometimes encountered with multiple voices -- the stems of the notes
886 in one voice can collide with the note heads in other voices.  In
887 laying out the notes, LilyPond allows the notes or chords from two
888 voices to occupy the same vertical note column provided the stems are
889 in opposite directions, but the notes from the third and fourth voices
890 are displaced, if necessary, to avoid the note heads colliding.  This
891 usually works well, but in this example the notes of the lowest voice
892 are clearly not well placed by default. LilyPond provides several ways
893 to adjust the horizontal placing of notes.  We are not quite ready yet
894 to see how to correct this, so we shall leave this problem until a
895 later section --- see the @code{force-hshift} property in @ref{Fixing
896 overlapping notation}.
897
898
899 @node Explicitly instantiating voices
900 @subsection Explicitly instantiating voices
901
902 @funindex \voiceOne
903 @funindex \voiceTwo
904 @funindex \voiceThree
905 @funindex \voiceFour
906 @funindex \oneVoice
907 @funindex \new Voice
908 @cindex Voice contexts, creating
909
910 Voice contexts can also be created manually
911 inside a @code{<< >>} block to create polyphonic music, using
912 @code{\voiceOne} ... @code{\voiceFour} to indicate the required
913 directions of stems, slurs, etc.  In longer scores this method
914 is clearer, as it permits the voices to be separated and to be
915 given more descriptive names.
916
917 Specifically, the construct @code{<< \\ >>} which we used in
918 the previous section:
919
920 @example
921 \new Staff @{
922   \relative c' @{
923     << @{ e4 f g a @} \\ @{ c,4 d e f @} >>
924   @}
925 @}
926 @end example
927
928 @noindent
929 is equivalent to
930
931 @example
932 \new Staff <<
933   \new Voice = "1" @{ \voiceOne \relative c' @{ e4 f g a @} @}
934   \new Voice = "2" @{ \voiceTwo \relative c' @{ c4 d e f @} @}
935 >>
936 @end example
937
938 Both of the above would produce
939
940 @c The following example should not display the code
941 @lilypond[ragged-right,quote]
942 \new Staff <<
943   \new Voice = "1" { \voiceOne \relative c' { e4 f g a } }
944   \new Voice = "2" { \voiceTwo \relative c' { c4 d e f } }
945 >>
946 @end lilypond
947
948 The @code{\voiceXXX} commands set the direction of stems, slurs,
949 ties, articulations, text annotations, augmentation dots of dotted
950 notes, and fingerings.  @code{\voiceOne} and @code{\voiceThree}
951 make these objects point upwards, while @code{\voiceTwo} and
952 @code{\voiceFour} make them point downwards.  These commands also
953 generate a horizontal shift for each voice when this is required
954 to avoid clashes of note heads.  The command @code{\oneVoice}
955 reverts the settings back to the normal values for a single voice.
956
957 Let us see in some simple examples exactly what effect
958 @code{\oneVoice}, @code{\voiceOne} and @code{voiceTwo} have on
959 markup, ties, slurs, and dynamics:
960
961 @lilypond[quote,ragged-right,verbatim]
962 \relative c'{
963   % Default behavior or behavior after \oneVoice
964   c d8 ~ d e4 ( f g a ) b-> c
965 }
966 @end lilypond
967
968 @lilypond[quote,ragged-right,verbatim]
969 \relative c'{
970   \voiceOne
971   c d8 ~ d e4 ( f g a ) b-> c
972   \oneVoice
973   c, d8 ~ d e4 ( f g a ) b-> c
974 }
975 @end lilypond
976
977 @lilypond[quote,ragged-right,verbatim]
978 \relative c'{
979   \voiceTwo
980   c d8 ~ d e4 ( f g a ) b-> c
981   \oneVoice
982   c, d8 ~ d e4 ( f g a ) b-> c
983 }
984 @end lilypond
985
986 Now let's look at three different ways to notate the same passage
987 of polyphonic music, each of which is advantageous in different
988 circumstances, using the example from the previous section.
989
990 An expression that appears directly inside a @code{<< >>} belongs
991 to the main voice (but, note, @strong{not} in a @code{<< \\ >>}
992 construct).  This is useful when extra voices appear while the
993 main voice is playing.  Here is a more correct rendition of our
994 example.  The red diamond-shaped notes
995 demonstrate that the main melody is now in a single voice context,
996 permitting a phrasing slur to be drawn over them.
997
998 @lilypond[quote,ragged-right,verbatim]
999 \new Staff \relative c' {
1000   \voiceOneStyle
1001   % The following notes are monophonic
1002   c16^( d e f
1003   % Start simultaneous section of three voices
1004   <<
1005     % Continue the main voice in parallel
1006     { g4 f e | d2 e2) }
1007     % Initiate second voice
1008     \new Voice {
1009       % Set stems, etc, down
1010       \voiceTwo
1011       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
1012     }
1013     % Initiate third voice
1014     \new Voice {
1015       % Set stems, etc, up
1016       \voiceThree
1017       s2. | s4 b4 c2
1018     }
1019   >>
1020 }
1021 @end lilypond
1022
1023 @cindex nesting music expressions
1024 @cindex nesting simultaneous constructs
1025
1026 @cindex voices, temporary
1027 @cindex voices, nesting
1028
1029 More deeply nested polyphony constructs are possible, and if a
1030 voice appears only briefly this might be a more natural way to
1031 typeset the music:
1032
1033 @lilypond[quote,ragged-right,verbatim]
1034 \new Staff \relative c' {
1035   c16^( d e f
1036   <<
1037     { g4 f e | d2 e2) }
1038     \new Voice {
1039       \voiceTwo
1040       r8 e4 d c8 ~ |
1041       <<
1042         {c8 b16 a b8 g ~ g2}
1043         \new Voice {
1044           \voiceThree
1045           s4 b4 c2
1046         }
1047       >>
1048     }
1049   >>
1050 }
1051 @end lilypond
1052
1053 @cindex spacing notes
1054
1055 This method of nesting new voices briefly is useful
1056 when only small sections of the music
1057 are polyphonic, but when the whole staff is largely polyphonic
1058 it can be clearer to use multiple voices throughout, using
1059 spacing notes to step over sections where the voice is silent,
1060 as here:
1061
1062 @lilypond[quote,ragged-right,verbatim]
1063 \new Staff \relative c' <<
1064   % Initiate first voice
1065   \new Voice {
1066     \voiceOne
1067     c16^( d e f g4 f e | d2 e2) |
1068   }
1069   % Initiate second voice
1070   \new Voice {
1071     % Set stems, etc, down
1072     \voiceTwo
1073     s4 r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2 |
1074   }
1075   % Initiate third voice
1076   \new Voice {
1077     % Set stems, etc, up
1078     \voiceThree
1079     s1 | s4 b4 c2 |
1080   }
1081 >>
1082 @end lilypond
1083
1084 @subsubheading Note columns
1085
1086 @cindex note column
1087 @cindex shift commands
1088 @funindex \shiftOff
1089 @funindex \shiftOn
1090 @funindex \shiftOnn
1091 @funindex \shiftOnnn
1092
1093 Closely spaced notes in a chord, or notes occurring at the same
1094 time in different voices, are arranged in two, occasionally more,
1095 columns to prevent the note heads overlapping.  These are called
1096 note columns.  There are separate columns for each voice, and
1097 the currently specified voice-dependent shift is applied to the
1098 note column if there would otherwise be a collision.  This can
1099 be seen in the example above.  In bar 2 the C in voice two is
1100 shifted to the right relative to the D in voice one, and in the
1101 final chord the C in voice three is also shifted to the right
1102 relative to the other notes.
1103
1104 The @code{\shiftOn}, @code{\shiftOnn}, @code{\shiftOnnn}, and
1105 @code{\shiftOff} commands specify the degree to which notes and
1106 chords of the voice should be shifted if a collision
1107 would otherwise occur. By default, the outer voices (normally
1108 voices one and two) have @code{\shiftOff} specified, while the
1109 inner voices (three and four) have @code{\shiftOn} specified.
1110 When a shift is applied, Voices one and three are shifted to
1111 the right and voices two and four to the left.
1112
1113 @code{\shiftOnn} and @code{\shiftOnnn} define further shift
1114 levels which may be specified temporarily to resolve collisions
1115 in complex situations -- see @ref{Real music example}.
1116
1117 A note column can contain just one note (or chord) from a voice
1118 with stems up and one note (or chord) from a voice with stems
1119 down.  If notes from two voices which have their stems in the
1120 same direction are placed at the same position and both voices
1121 have no shift or the same shift specified, the error message
1122 @qq{Too many clashing note columns} will be produced.
1123
1124 @node Voices and vocals
1125 @subsection Voices and vocals
1126
1127 Vocal music presents a special difficulty: we need to combine two
1128 expressions -- notes and lyrics.
1129
1130 @funindex \new Lyrics
1131 @funindex \lyricsto
1132 @cindex Lyrics context, creating
1133 @cindex lyrics, linking to voice
1134
1135 You have already seen the @code{\addlyrics@{@}} command, which
1136 handles simple scores well.  However, this technique is
1137 quite limited.  For more complex music, you must introduce the
1138 lyrics in a @code{Lyrics} context using @code{\new Lyrics} and
1139 explicitly link
1140 the lyrics to the notes with @code{\lyricsto@{@}}, using the
1141 name assigned to the Voice.
1142
1143 @lilypond[quote,verbatim,fragment]
1144 <<
1145   \new Voice = "one" \relative c'' {
1146     \autoBeamOff
1147     \time 2/4
1148     c4 b8. a16 g4. f8 e4 d c2
1149   }
1150   \new Lyrics \lyricsto "one" {
1151     No more let sins and sor -- rows grow.
1152   }
1153 >>
1154 @end lilypond
1155
1156 Note that the lyrics must be linked to a @code{Voice} context,
1157 @emph{not} a @code{Staff} context.  This is a case where it is
1158 necessary to create @code{Staff} and @code{Voice} contexts
1159 explicitly.
1160
1161 @cindex lyrics and beaming
1162 @cindex beaming and lyrics
1163 @funindex \autoBeamOff
1164
1165 The automatic beaming which LilyPond uses by default works well
1166 for instrumental music, but not so well for music with lyrics,
1167 where beaming is either not required at all or is used to indicate
1168 melismata in the lyrics.  In the example above we use the command
1169 @code{\autoBeamOff} to turn off the automatic beaming.
1170
1171 @funindex \new ChoirStaff
1172 @funindex \lyricmode
1173 @cindex vocal score structure
1174
1175 Let us reuse the earlier example from Judas Maccabæus to
1176 illustrate this more flexible technique.  We first recast
1177 it to use variables so the music and lyrics can be separated
1178 from the staff structure.  We also introduce a ChoirStaff
1179 bracket.  The lyrics themselves must be introduced with
1180 @code{\lyricmode} to ensure they are interpreted as lyrics
1181 rather than music.
1182
1183 @lilypond[quote,verbatim]
1184 global = { \time 6/8 \partial 8 \key f \major}
1185 SopOneMusic = \relative c'' {
1186   c8 | c([ bes)] a a([ g)] f | f'4. b, | c4.~ c4 }
1187 SopTwoMusic = \relative c' {
1188   r8 | r4. r4 c8 | a'([ g)] f f([ e)] d | e([ d)] c bes' }
1189 SopOneLyrics = \lyricmode {
1190   Let | flee -- cy flocks the | hills a -- dorn, __ }
1191 SopTwoLyrics = \lyricmode {
1192   Let | flee -- cy flocks the | hills a -- dorn, }
1193
1194 \score {
1195   \new ChoirStaff <<
1196     \new Staff <<
1197       \new Voice = "SopOne" {
1198         \global
1199         \SopOneMusic
1200       }
1201       \new Lyrics \lyricsto "SopOne" {
1202         \SopOneLyrics
1203       }
1204     >>
1205     \new Staff <<
1206       \new Voice = "SopTwo" {
1207         \global
1208         \SopTwoMusic
1209       }
1210       \new Lyrics \lyricsto "SopTwo" {
1211         \SopTwoLyrics
1212       }
1213     >>
1214   >>
1215 }
1216 @end lilypond
1217
1218 This is the basic structure of all vocal scores.  More staves may
1219 be added as required, more voices may be added to the staves,
1220 more verses may be added to the lyrics,
1221 and the variables containing the music can easily be placed
1222 in separate files should they become too long.
1223
1224 @cindex hymn structure
1225
1226 Here is a example of the first line of a hymn with four
1227 verses, set for SATB.  In this case the words for all four
1228 parts are the same.  Note how we use variables to separate the
1229 music notation and words from the staff structure.  See too
1230 how a variable, which we have chosen to call @q{TimeKey}, is used
1231 to hold several commands for use within the two staves.  In other
1232 examples this is often called @q{global}.
1233
1234 @lilypond[quote,verbatim]
1235 TimeKey = { \time 4/4 \partial 4 \key c \major}
1236 SopMusic   = \relative c' { c4 | e4. e8 g4  g  | a a g }
1237 AltoMusic  = \relative c' { c4 | c4. c8 e4  e  | f f e }
1238 TenorMusic = \relative c  { e4 | g4. g8 c4. b8 | a8 b c d e4 }
1239 BassMusic  = \relative c  { c4 | c4. c8 c4  c  | f8 g a b c4 }
1240 VerseOne   = \lyricmode {
1241   E -- | ter -- nal fa -- ther, | strong to save, }
1242 VerseTwo   = \lyricmode {
1243   O | Christ, whose voice the | wa -- ters heard, }
1244 VerseThree = \lyricmode {
1245   O | Ho -- ly Spi -- rit, | who didst brood }
1246 VerseFour  = \lyricmode {
1247   O | Tri -- ni -- ty of | love and pow'r }
1248
1249 \score {
1250   \new ChoirStaff <<
1251     \new Staff <<
1252       \clef "treble"
1253       \new Voice = "Sop"  { \voiceOne \TimeKey \SopMusic }
1254       \new Voice = "Alto" { \voiceTwo \AltoMusic }
1255       \new Lyrics \lyricsto "Sop" { \VerseOne   }
1256       \new Lyrics \lyricsto "Sop" { \VerseTwo   }
1257       \new Lyrics \lyricsto "Sop" { \VerseThree }
1258       \new Lyrics \lyricsto "Sop" { \VerseFour  }
1259     >>
1260     \new Staff <<
1261       \clef "bass"
1262       \new Voice = "Tenor" { \voiceOne \TimeKey \TenorMusic }
1263       \new Voice = "Bass"  { \voiceTwo \BassMusic  }
1264     >>
1265   >>
1266 }
1267 @end lilypond
1268
1269 @cindex verse and refrain
1270
1271 We end with an example to show how we might code a solo verse which
1272 continues into a two-part refrain in two staves.  The
1273 positioning of the sequential and simultaneous sections to achieve
1274 this within a single score is quite tricky, so follow the
1275 explanation carefully!
1276
1277 Let's start with a score block containing a @code{ChoirStaff}, as
1278 we would like the brace to appear at the start of the chorus.
1279 Normally you would need angle brackets after @code{\new ChoirStaff}
1280 to bring in all the staves in parallel, but here we want to
1281 defer the parallelism during the solo so we use braces, although
1282 angle brackets here wouldn't hurt.  Inside the @code{ChoirStaff} we
1283 want first the staff which will contain the verse.  This must
1284 contain notes and lyrics in parallel, so here we need angle
1285 brackets around the @code{\new Voice} and @code{\new Lyrics} to
1286 start them at the same time:
1287
1288 @lilypond[quote,verbatim,ragged-right]
1289 versenotes = \relative c'' {
1290   \clef "treble"
1291   \key g \major
1292   \time 3/4 g g g b b b
1293 }
1294 versewords = \lyricmode {
1295   One two three four five six
1296 }
1297 \score {
1298   \new Choirstaff {
1299     \new Staff <<
1300       \new Voice = "verse" {
1301         \versenotes \break
1302       }
1303       \new Lyrics \lyricsto verse {
1304         \versewords
1305       }
1306     >>
1307   }
1308 }
1309 @end lilypond
1310
1311 That gives the verse line.
1312
1313 Now we want to continue with refrainA on the same staff while a
1314 second staff is introduced in parallel with it for refrainB, so
1315 this is a parallel section which must be positioned immediately
1316 following the @code{\break} in the verse Voice.  Yes, @emph{within}
1317 the verse Voice!  Here's that parallel section.  More staves
1318 could be introduced here in the same way.
1319
1320 @example
1321 <<
1322   \refrainnotesA
1323   \new Lyrics \lyricsto verse @{
1324     \refrainwordsA
1325   @}
1326   \new Staff <<
1327     \new Voice = "refrainB" @{
1328       \refrainnotesB
1329     @}
1330     \new Lyrics \lyricsto "refrainB" @{
1331       \refrainwordsB
1332     @}
1333   >>
1334 >>
1335 @end example
1336
1337 Here's the final result with two staves in the chorus showing
1338 how the parallel section is positioned within the verse Voice:
1339
1340 @lilypond[quote,verbatim, ragged-right]
1341 versenotes = \relative c'' {
1342   \clef "treble"
1343   \key g \major
1344   \time 3/4 g g g b b b
1345 }
1346 refrainnotesA = \relative c'' {
1347   \time 2/4
1348   c c g g \bar "|."
1349 }
1350 refrainnotesB = \relative c {
1351   \clef "bass"
1352   \key g \major
1353   c e d d
1354 }
1355 versewords = \lyricmode {
1356   One two three four five six
1357 }
1358 refrainwordsA = \lyricmode {
1359   la la la la
1360 }
1361 refrainwordsB = \lyricmode {
1362   dum dum dum dum
1363 }
1364 \score {
1365   \new ChoirStaff {
1366     \new Staff <<
1367       \new Voice = "verse" {
1368         \versenotes \break
1369         <<
1370           \refrainnotesA
1371           \new Lyrics \lyricsto "verse" {
1372             \refrainwordsA
1373           }
1374           \new Staff <<
1375             \new Voice = "refrainB" {
1376               \refrainnotesB
1377             }
1378             \new Lyrics \lyricsto "refrainB" {
1379               \refrainwordsB
1380             }
1381           >>
1382         >>
1383       }
1384       \new Lyrics \lyricsto "verse" {
1385         \versewords
1386       }
1387     >>
1388   }
1389 }
1390 @end lilypond
1391
1392 @cindex book, example of using
1393 @funindex \book
1394
1395 However, although this is an interesting and useful exercise to
1396 help you to understand how sequential and simultaneous blocks work,
1397 in practice one would perhaps choose to code this as two
1398 @code{\score} blocks within an implicit @code{\book} block, as
1399 follows:
1400
1401 @lilypond[quote,verbatim,ragged-right]
1402 versenotes = \relative c'' {
1403   \clef "treble"
1404   \key g \major
1405   \time 3/4 g g g b b b
1406 }
1407 refrainnotesA = \relative c'' {
1408   \time 2/4
1409   c c g g \bar "|."
1410 }
1411 refrainnotesB = \relative c {
1412   \clef "bass"
1413   \key g \major
1414   c e d d
1415 }
1416 versewords = \lyricmode {
1417   One two three four five six
1418 }
1419 refrainwordsA = \lyricmode {
1420   la la la la
1421 }
1422 refrainwordsB = \lyricmode {
1423   dum dum dum dum
1424 }
1425 \score {
1426   \new Staff <<
1427     \new Voice = "verse" {
1428       \versenotes
1429     }
1430     \new Lyrics \lyricsto "verse" {
1431       \versewords
1432     }
1433   >>
1434 }
1435
1436 \score {
1437   \new ChoirStaff <<
1438     \new Staff <<
1439       \new Voice = "refrainA" {
1440         \refrainnotesA
1441       }
1442       \new Lyrics \lyricsto "refrainA" {
1443         \refrainwordsA
1444       }
1445     >>
1446     \new Staff <<
1447       \new Voice = "refrainB" {
1448         \refrainnotesB
1449       }
1450       \new Lyrics \lyricsto "refrainB" {
1451         \refrainwordsB
1452       }
1453     >>
1454   >>
1455 }
1456 @end lilypond
1457
1458 @node Contexts and engravers
1459 @section Contexts and engravers
1460
1461 Contexts and engravers have been mentioned informally
1462 in earlier sections; we now must look at
1463 these concepts in more detail, as they are important
1464 in the fine-tuning of LilyPond output.
1465
1466
1467 @menu
1468 * Contexts explained::
1469 * Creating contexts::
1470 * Engravers explained::
1471 * Modifying context properties::
1472 * Adding and removing engravers::
1473 @end menu
1474
1475 @node Contexts explained
1476 @subsection Contexts explained
1477
1478 @cindex contexts explained
1479
1480 When music is printed, many notational elements which do not
1481 appear explicitly in the input file must be added to the
1482 output.  For example, compare the input and output of the
1483 following example:
1484
1485 @lilypond[quote,verbatim,relative=2,fragment]
1486 cis4 cis2. g4
1487 @end lilypond
1488
1489 The input is rather sparse, but in the output, bar lines,
1490 accidentals, clef, and time signature have been added.  When
1491 LilyPond @emph{interprets} the input the musical information
1492 is inspected in time order, similar to reading a score from left
1493 to right.  While reading the input, the program remembers where
1494 measure boundaries are, and which pitches require explicit
1495 accidentals.  This information must be held on several levels.
1496 For example, the effect of an accidental is limited
1497 to a single staff, while a bar line must be synchronized across
1498 the entire score.
1499
1500 Within LilyPond, these rules and bits of information are grouped
1501 in @emph{Contexts}.  We have already met the
1502 @code{Voice} context.
1503 Others are the @code{Staff} and @code{Score} contexts.
1504 Contexts are hierarchical to reflect the hierarchical nature of
1505 a musical score.
1506 For example: a @code{Staff} context can contain many
1507 @code{Voice} contexts, and a @code{Score} context can
1508 contain many @code{Staff} contexts.
1509
1510 @quotation
1511 @sourceimage{context-example,5cm,,}
1512 @end quotation
1513
1514 Each context has the responsibility for enforcing some notation rules,
1515 creating some notation objects and maintaining the associated
1516 properties.  For example, the @code{Voice} context may introduce an
1517 accidental and then the @code{Staff} context maintains the rule to
1518 show or suppress the accidental for the remainder of the measure.
1519
1520 As another example, the synchronization of bar lines is, by default,
1521 handled in the @code{Score} context.
1522 However, in some music we may not want the bar lines to be
1523 synchronized -- consider a polymetric score in 4/4 and 3/4 time.
1524 In such cases, we must modify the default settings of the
1525 @code{Score} and @code{Staff} contexts.
1526
1527 For very simple scores, contexts are created implicitly, and you need
1528 not be aware of them.  For larger pieces, such as anything with more
1529 than one staff, they must be
1530 created explicitly to make sure that you get as many staves as you
1531 need, and that they are in the correct order.  For typesetting pieces
1532 with specialized notation, it is usual to modify existing, or
1533 even to define totally new, contexts.
1534
1535 In addition to the @code{Score,} @code{Staff} and
1536 @code{Voice} contexts there are contexts which fit between
1537 the score and staff levels to control staff groups, such as the
1538 @code{PianoStaff} and @code{ChoirStaff} contexts.  There
1539 are also alternative staff and voice contexts, and contexts for
1540 lyrics, percussion, fret boards, figured bass, etc.
1541
1542 The names of all context types are formed from one or more
1543 words, each word being capitalized and joined immediately to the
1544 preceding word with no hyphen or underscore, e.g.,
1545 @code{GregorianTranscriptionStaff}.
1546
1547 @node Creating contexts
1548 @subsection Creating contexts
1549
1550 @funindex \new
1551 @cindex new contexts
1552 @cindex creating contexts
1553 @cindex contexts, creating
1554
1555 There can be only one top level context: the
1556 @code{Score}
1557 context.  This is created with the @code{\score} command,
1558 or, in simple scores, it is created automatically.
1559
1560 For scores with only one voice and one staff, the
1561 @code{Voice} and @code{Staff} contexts may be left to be
1562 created automatically, but for more complex scores it is
1563 necessary to create them by hand.
1564 The simplest command that does this is @code{\new}.
1565 It is prepended to a music expression, for example
1566
1567 @example
1568 \new @var{type} @var{music-expression}
1569 @end example
1570
1571 @noindent
1572 where @var{type} is a context name (like @code{Staff} or
1573 @code{Voice}).  This command creates a new context, and starts
1574 interpreting the @var{music-expression} within that context.
1575
1576 Note that there is no @code{\new Score} command;
1577 the single top-level @code{Score} context is introduced
1578 with @code{\score}.
1579
1580 You have seen many practical examples which created new
1581 @code{Staff} and @code{Voice} contexts in earlier sections, but
1582 to remind you how these commands are used in practice, here's an
1583 annotated real-music example:
1584
1585 @lilypond[quote,verbatim,ragged-right]
1586 \score {  % start of single compound music expression
1587   <<  % start of simultaneous staves section
1588     \time 2/4
1589     \new Staff {  % create RH staff
1590       \key g \minor
1591       \clef "treble"
1592       \new Voice {  % create voice for RH notes
1593         \relative c'' {  % start of RH notes
1594           d4 ees16 c8. |
1595           d4 ees16 c8. |
1596         }  % end of RH notes
1597       }  % end of RH voice
1598     }  % end of RH staff
1599     \new Staff <<  % create LH staff; needs two simultaneous voices
1600       \key g \minor
1601       \clef "bass"
1602       \new Voice {  % create LH voice one
1603         \voiceOne
1604         \relative g {  % start of LH voice one notes
1605           g8 <bes d> ees, <g c> |
1606           g8 <bes d> ees, <g c> |
1607         }  % end of LH voice one notes
1608       }  % end of LH voice one
1609       \new Voice {  % create LH voice two
1610         \voiceTwo
1611         \relative g {  % start of LH voice two notes
1612           g4 ees |
1613           g4 ees |
1614         }  % end of LH voice two notes
1615       }  % end of LH voice two
1616     >>  % end of LH staff
1617   >>  % end of simultaneous staves section
1618 }  % end of single compound music expression
1619 @end lilypond
1620
1621 (Note how all the statements which open a block with either a
1622 curly bracket, @code{@{}, or double angle brackets, @code{<<},
1623 are indented by two further spaces, and the corresponding
1624 closing bracket is indented by exactly the same amount.  While
1625 this is not required, following this practice will greatly
1626 reduce the number of @q{unmatched bracket} errors, and is
1627 strongly recommended.  It enables the structure of the music to
1628 be seen at a glance, and any unmatched brackets will be obvious.
1629 Note too how the LH staff is created using double angle brackets
1630 because it requires two voices for its music, whereas the RH staff
1631 is created with a single music expression surrounded by curly
1632 brackets because it requires only one voice.)
1633
1634 @cindex contexts, naming
1635 @cindex naming contexts
1636
1637 The @code{\new} command may also give a identifying name to the
1638 context to distinguish it from other contexts of the same type,
1639
1640 @example
1641 \new @var{type} = @var{id} @var{music-expression}
1642 @end example
1643
1644 Note the distinction between the name of the context type,
1645 @code{Staff}, @code{Voice}, etc, and the identifying name of a
1646 particular instance of that type, which can be any sequence of letters
1647 and digits invented by the user.  The identifying name is used to
1648 refer back to that particular instance of a context.  We saw this in
1649 use in the section on lyrics in @ref{Voices and vocals}.
1650
1651
1652 @node Engravers explained
1653 @subsection Engravers explained
1654
1655 @cindex engravers
1656
1657 Every mark on the printed output of a score produced by LilyPond
1658 is produced by an @code{Engraver}.  Thus there is an engraver
1659 to print staves, one to print note heads, one for stems, one for
1660 beams, etc, etc.  In total there are over 120 such engravers!
1661 Fortunately, for most scores it is not necessary to know about
1662 more than a few, and for simple scores you do not need to know
1663 about any.
1664
1665 Engravers live and operate in Contexts.
1666 Engravers such as the @code{Metronome_mark_engraver}, whose
1667 action and output applies to the score as a whole, operate in
1668 the highest level context -- the @code{Score} context.
1669
1670 The @code{Clef_engraver} and @code{Key_engraver} are to be
1671 found in every Staff Context, as different staves may require
1672 different clefs and keys.
1673
1674 The @code{Note_heads_engraver} and @code{Stem_engraver} live
1675 in every @code{Voice} context, the lowest level context of all.
1676
1677 Each engraver processes the particular objects associated
1678 with its function, and maintains the properties that relate
1679 to that function.  These properties, like the properties
1680 associated with contexts, may be modified to change the
1681 operation of the engraver or the appearance of those elements
1682 in the printed score.
1683
1684 Engravers all have compound names formed from words which
1685 describe their function.  Just the first word is capitalized,
1686 and the remainder are joined to it with underscores.  Thus
1687 the @code{Staff_symbol_engraver} is responsible for creating the
1688 lines of the staff, the @code{Clef_engraver} determines and sets
1689 the pitch reference point on the staff by drawing a clef symbol.
1690
1691 Here are some of the most common engravers together with their
1692 function.  You will see it is usually easy to guess the function
1693 from the name, or vice versa.
1694
1695 @multitable @columnfractions .3 .7
1696 @headitem Engraver
1697   @tab Function
1698 @item Accidental_engraver
1699   @tab Makes accidentals, cautionary and suggested accidentals
1700 @item Beam_engraver
1701   @tab Engraves beams
1702 @item Clef_engraver
1703   @tab Engraves clefs
1704 @item Completion_heads_engraver
1705   @tab Splits notes which cross bar lines
1706 @c The old Dynamic_engraver is deprecated. -jm
1707 @item New_dynamic_engraver
1708   @tab Creates hairpins and dynamic texts
1709 @item Forbid_line_break_engraver
1710   @tab Prevents line breaks if a musical element is still active
1711 @item Key_engraver
1712   @tab Creates the key signature
1713 @item Metronome_mark_engraver
1714   @tab Engraves metronome marking
1715 @item Note_heads_engraver
1716   @tab Engraves note heads
1717 @item Rest_engraver
1718   @tab Engraves rests
1719 @item Staff_symbol_engraver
1720   @tab Engraves the five (by default) lines of the staff
1721 @item Stem_engraver
1722   @tab Creates stems and single-stem tremolos
1723 @item Time_signature_engraver
1724   @tab Creates time signatures
1725 @end multitable
1726
1727 @smallspace
1728
1729 We shall see later how the output of LilyPond can be changed
1730 by modifying the action of Engravers.
1731
1732
1733 @node Modifying context properties
1734 @subsection Modifying context properties
1735
1736 @cindex context properties
1737 @cindex context properties, modifying
1738 @cindex modifying context properties
1739 @funindex \set
1740 @funindex \unset
1741
1742 Contexts are responsible for holding the values of a number of
1743 context @emph{properties}.  Many of them can be changed to
1744 influence the interpretation of the input and so change the
1745 appearance of the output.  They are changed by the
1746 @code{\set} command.  This takes the form
1747
1748 @example
1749 \set @emph{ContextName}.@emph{propertyName} = #@emph{value}
1750 @end example
1751
1752 Where the @emph{ContextName} is usually @code{Score},
1753 @code{Staff} or @code{Voice}.  It may be omitted,
1754 in which case @code{Voice} is assumed.
1755
1756 The names of context properties consist of words joined
1757 together with no hyphens or underscores, all except the
1758 first having a capital letter.  Here are a few examples
1759 of some commonly used ones.  There are many more.
1760
1761 @c attempt to force this onto a new page
1762 @need 50
1763 @multitable @columnfractions .25 .15 .45 .15
1764 @headitem propertyName
1765   @tab Type
1766   @tab Function
1767   @tab Example Value
1768 @item extraNatural
1769   @tab Boolean
1770   @tab If true, set extra natural signs before accidentals
1771   @tab @code{#t}, @code{#f}
1772 @item currentBarNumber
1773   @tab Integer
1774   @tab Set the current bar number
1775   @tab @code{50}
1776 @item doubleSlurs
1777   @tab Boolean
1778   @tab If true, print slurs both above and below notes
1779   @tab @code{#t}, @code{#f}
1780 @item instrumentName
1781   @tab Text
1782   @tab Set the name to be placed at the start of the staff
1783   @tab @code{"Cello I"}
1784 @item fontSize
1785   @tab Real
1786   @tab Increase or decrease the font size
1787   @tab @code{2.4}
1788 @item stanza
1789   @tab Text
1790   @tab Set the text to print before the start of a verse
1791   @tab @code{"2"}
1792 @end multitable
1793
1794 @noindent
1795 where a Boolean is either True (@code{#t}) or False (@code{#f}),
1796 an Integer is a positive whole number, a Real is a positive
1797 or negative decimal number, and text is enclosed in double
1798 apostrophes.  Note the occurrence of hash signs,
1799 (@code{#}), in two different places -- as part of the Boolean
1800 value before the @code{t} or @code{f}, and before @emph{value}
1801 in the @code{\set} statement.  So when a Boolean is being
1802 entered you need to code two hash signs, e.g., @code{##t}.
1803
1804 Before we can set any of these properties we need to know
1805 in which context they operate.  Sometimes this is obvious,
1806 but occasionally it can be tricky.  If the wrong context
1807 is specified, no error message is produced, but the expected
1808 action will not take place.  For example, the
1809 @code{instrumentName} clearly lives in the Staff context, since
1810 it is the staff that is to be named.
1811 In this example the first staff is labelled, but not the second,
1812 because we omitted the context name.
1813
1814 @lilypond[quote,verbatim,ragged-right]
1815 <<
1816   \new Staff \relative c'' {
1817     \set Staff.instrumentName = #"Soprano"
1818     c4 c
1819  }
1820   \new Staff \relative c' {
1821   \set instrumentName = #"Alto"  % Wrong!
1822   d4 d
1823  }
1824 >>
1825 @end lilypond
1826
1827 Remember the default context name is Voice, so the second
1828 @code{\set} command set the property @code{instrumentName} in the
1829 Voice context to @qq{Alto}, but as LilyPond does not look
1830 for any such property in the @code{Voice} context, no
1831 further action took place.  This is not an error, and no error
1832 message is logged in the log file.
1833
1834 Similarly, if the property name is mis-spelt no error message is
1835 produced, and clearly the expected action cannot be performed.  If
1836 fact, you can set any (fictitious) @q{property} using any name you
1837 like in any context that exists by using the @code{\set} command.  But
1838 if the name is not known to LilyPond it will not cause any action to
1839 be taken.  Some text editors with special support for LilyPond input
1840 files document property names with bullets when you hover them with
1841 the mouse, like JEdit with LilyPondTool, or highlight unknown property
1842 names differently, like ConTEXT.  If you do not use an editor with
1843 such features, it is recommended to check the property name in the
1844 Internals Reference: see @rinternals{Tunable context properties}, or
1845 @rinternals{Contexts}.
1846
1847 The @code{instrumentName} property will take effect only
1848 if it is set in the @code{Staff} context, but
1849 some properties can be set in more than one context.
1850 For example, the property @code{extraNatural} is by
1851 default set to ##t (true) for all staves.
1852 If it is set to ##f (false) in one particular @code{Staff}
1853 context it applies just to the accidentals on that staff.
1854 If it is set to false in the @code{Score} context
1855 it applies to all staves.
1856
1857 So this turns off extra naturals in one staff:
1858
1859 @lilypond[quote,verbatim,ragged-right]
1860 <<
1861   \new Staff \relative c'' {
1862     ais4 aes
1863  }
1864   \new Staff \relative c'' {
1865     \set Staff.extraNatural = ##f
1866     ais4 aes
1867  }
1868 >>
1869 @end lilypond
1870
1871 @noindent
1872 and this turns them off in all staves:
1873
1874 @lilypond[quote,verbatim,ragged-right]
1875 <<
1876   \new Staff \relative c'' {
1877     ais4 aes
1878  }
1879   \new Staff \relative c'' {
1880     \set Score.extraNatural = ##f
1881     ais4 aes
1882  }
1883 >>
1884 @end lilypond
1885
1886 As another example, if @code{clefOctavation} is set in
1887 the @code{Score} context this immediately changes the value
1888 of the octavation in all current staves and sets a new default
1889 value which will be applied to all staves.
1890
1891 The opposite command, @code{\unset}, effectively removes the
1892 property from the context, which causes most properties to
1893 revert to their default value.  Usually @code{\unset} is not
1894 required as a new @code{\set} command will achieve what is
1895 wanted.
1896
1897 The @code{\set} and @code{\unset} commands can appear anywhere
1898 in the input file and will take effect from the time they are
1899 encountered until the end of the score or until the property is
1900 @code{\set} or @code{\unset} again.  Let's try changing the
1901 font size, which affects the size of the note heads (among
1902 other things) several times.  The change is from the default
1903 value, not the most recently set value.
1904
1905 @lilypond[quote,verbatim,ragged-right,relative=1,fragment]
1906 c4
1907 % make note heads smaller
1908 \set fontSize = #-4
1909 d e
1910 % make note heads larger
1911 \set fontSize = #2.5
1912 f g
1913 % return to default size
1914 \unset fontSize
1915 a b
1916 @end lilypond
1917
1918 We have now seen how to set the values of several different types of
1919 property.  Note that integers and numbers are always preceded by a
1920 hash sign, @code{#}, while a true or false value is specified by
1921 @code{##t} and @code{##f}, with two hash signs.  A text property
1922 should be enclosed in double quotation signs, as above, although we
1923 shall see later that text can actually be specified in a much more
1924 general way by using the very powerful @code{markup} command.
1925
1926
1927 @funindex \with
1928 @cindex context properties, setting with \with
1929
1930 Context properties may also be set at the time the context is
1931 created.  Sometimes this is a clearer way of specifying a
1932 property value if it is to remain fixed for the duration of
1933 the context.  When a context is created with a @code{\new}
1934 command it may be followed immediately by a
1935 @code{\with @{ .. @}} block in which the property values are
1936 set.  For example, if we wish to suppress the printing of
1937 extra naturals for the duration of a staff we would write:
1938
1939 @example
1940 \new Staff \with @{ extraNatural = ##f @}
1941 @end example
1942
1943 @noindent
1944 like this:
1945
1946 @lilypond[quote,verbatim,ragged-right]
1947 <<
1948   \new Staff
1949   \relative c'' {
1950     gis ges aes ais
1951   }
1952   \new Staff \with { extraNatural = ##f }
1953   \relative c'' {
1954     gis ges aes ais
1955   }
1956 >>
1957 @end lilypond
1958
1959 Properties set in this way may still be changed dynamically using
1960 @code{\set} and returned to their default value with @code{\unset}.
1961
1962 The @code{fontSize} property is treated differently.  If this is
1963 set in a @code{\with} clause it effectively resets the default
1964 value of the font size.  If it is later changed with @code{\set},
1965 this new default value may be restored with the
1966 @code{\unset fontSize} command.
1967
1968
1969 @node Adding and removing engravers
1970 @subsection Adding and removing engravers
1971
1972 @cindex engravers, adding
1973 @cindex engravers, removing
1974
1975 @funindex \consists
1976 @funindex \remove
1977
1978 We have seen that contexts each contain several engravers, each
1979 of which is responsible for producing a particular part of the
1980 output, like bar lines, staves, note heads, stems, etc.  If an
1981 engraver is removed from a context, it can no longer produce its
1982 output.  This is a crude way of modifying the output, but it
1983 can sometimes be useful.
1984
1985 @subsubheading Changing a single context
1986
1987 To remove an engraver from a single context we use the
1988 @code{\with} command placed immediately after the context creation
1989 command, as in the previous section.
1990
1991 As an illustration, let's repeat an example from the previous section
1992 with the staff lines removed.  Remember that the staff lines are
1993 produced by the @code{Staff_symbol_engraver}.
1994
1995 @lilypond[quote,verbatim,ragged-right]
1996 \new Staff \with {
1997   \remove Staff_symbol_engraver
1998 }
1999 \relative c' {
2000   c4
2001   \set fontSize = #-4  % make note heads smaller
2002   d e
2003   \set fontSize = #2.5  % make note heads larger
2004   f g
2005   \unset fontSize  % return to default size
2006   a b
2007 }
2008 @end lilypond
2009
2010 @cindex ambitus engraver
2011
2012 Engravers can also be added to individual contexts.
2013 The command to do this is
2014
2015 @code{\consists @var{Engraver_name}},
2016
2017 @noindent
2018 placed inside a @code{\with} block.  Some vocal scores have an ambitus
2019 placed at the beginning of a staff to indicate the range of notes in
2020 that staff -- see @rglos{ambitus}.  The ambitus is produced by the
2021 @code{Ambitus_engraver}, which is not normally included in any
2022 context.  If we add it to the @code{Voice} context, it calculates the
2023 range from that voice only:
2024
2025 @lilypond[quote,verbatim,ragged-right]
2026 \new Staff <<
2027   \new Voice \with {
2028     \consists Ambitus_engraver
2029   }
2030   \relative c'' {
2031     \voiceOne
2032     c a b g
2033   }
2034   \new Voice
2035   \relative c' {
2036     \voiceTwo
2037     c e d f
2038   }
2039 >>
2040 @end lilypond
2041
2042 @noindent
2043 but if we add the ambitus engraver to the
2044 @code{Staff} context, it calculates the range from all
2045 the notes in all the voices on that staff:
2046
2047 @lilypond[quote,verbatim,ragged-right]
2048 \new Staff \with {
2049     \consists Ambitus_engraver
2050   }
2051   <<
2052   \new Voice
2053   \relative c'' {
2054     \voiceOne
2055     c a b g
2056   }
2057   \new Voice
2058   \relative c' {
2059     \voiceTwo
2060     c e d f
2061   }
2062 >>
2063 @end lilypond
2064
2065 @subsubheading Changing all contexts of the same type
2066
2067 @funindex \layout
2068
2069 The examples above show how to remove or add engravers to
2070 individual contexts.  It is also possible to remove or add
2071 engravers to every context of a specific type by placing the
2072 commands in the appropriate context in a @code{\layout}
2073 block.  For example, if we wanted to show an ambitus for every
2074 staff in a four-staff score, we could write
2075
2076 @lilypond[quote,verbatim,ragged-right]
2077 \score {
2078   <<
2079     \new Staff <<
2080       \relative c'' { c a b g }
2081     >>
2082     \new Staff <<
2083       \relative c' { c a b g }
2084     >>
2085     \new Staff <<
2086       \clef "G_8"
2087       \relative c' { c a b g }
2088     >>
2089     \new Staff <<
2090       \clef "bass"
2091       \relative c { c a b g }
2092     >>
2093   >>
2094   \layout {
2095     \context {
2096       \Staff
2097       \consists Ambitus_engraver
2098     }
2099   }
2100 }
2101 @end lilypond
2102
2103 @noindent
2104 The values of context properties may also be set
2105 for all contexts of a particular type by including the
2106 @code{\set} command in a @code{\context} block in the
2107 same way.
2108
2109
2110 @node Extending the templates
2111 @section Extending the templates
2112
2113 You've read the tutorial, you know how to write music, you
2114 understand the fundamental concepts.  But how can you
2115 get the staves that you want?  Well, you can find lots of
2116 templates (see @ref{Templates}) which may give you a start.
2117 But what if you want something that isn't covered there?  Read on.
2118
2119 @menu
2120 * Soprano and cello::
2121 * Four-part SATB vocal score::
2122 * Building a score from scratch::
2123 @end menu
2124
2125 @node Soprano and cello
2126 @subsection Soprano and cello
2127
2128 @cindex template, modifying
2129
2130 Start off with the template that seems closest to what you want to end
2131 up with.  Let's say that you want to write something for soprano and
2132 cello.  In this case, we would start with @q{Notes and lyrics} (for the
2133 soprano part).
2134
2135 @example
2136 \version @w{"@version{}"}
2137 melody = \relative c' @{
2138   \clef treble
2139   \key c \major
2140   \time 4/4
2141   a4 b c d
2142 @}
2143
2144 text = \lyricmode @{
2145   Aaa Bee Cee Dee
2146 @}
2147
2148 \score @{
2149   <<
2150     \new Voice = "one" @{
2151       \autoBeamOff
2152       \melody
2153     @}
2154     \new Lyrics \lyricsto "one" \text
2155   >>
2156   \layout @{ @}
2157   \midi @{ @}
2158 @}
2159 @end example
2160
2161 Now we want to add a cello part.  Let's look at the @q{Notes only} example:
2162
2163 @example
2164 \version @w{"@version{}"}
2165 melody = \relative c' @{
2166   \clef treble
2167   \key c \major
2168   \time 4/4
2169   a4 b c d
2170 @}
2171
2172 \score @{
2173   \new Staff \melody
2174   \layout @{ @}
2175   \midi @{ @}
2176 @}
2177 @end example
2178
2179 We don't need two @code{\version} commands.  We'll need the
2180 @code{melody} section.  We don't want two @code{\score} sections
2181 -- if we had two @code{\score}s, we'd get the two parts separately.
2182 We want them together, as a duet.  Within the @code{\score}
2183 section, we don't need two @code{\layout} or @code{\midi}.
2184
2185 If we simply cut and paste the @code{melody} section, we would
2186 end up with two @code{melody} definitions.  This would not generate
2187 an error, but the second one would be used for both melodies.
2188 So let's rename them to make them distinct.  We'll call the
2189 section for the soprano @code{sopranoMusic} and the section for
2190 the cello @code{celloMusic}.  While we're doing this, let's rename
2191 @code{text} to be @code{sopranoLyrics}.  Remember to rename both
2192 instances of all these names -- both the initial definition (the
2193 @code{melody = \relative c' @{ } part) and the name's use (in the
2194 @code{\score} section).
2195
2196 While we're doing this, let's change the cello part's staff --
2197 celli normally use bass clef.  We'll also give the cello some
2198 different notes.
2199
2200 @example
2201 \version @w{"@version{}"}
2202 sopranoMusic = \relative c' @{
2203   \clef treble
2204   \key c \major
2205   \time 4/4
2206   a4 b c d
2207 @}
2208
2209 sopranoLyrics = \lyricmode @{
2210   Aaa Bee Cee Dee
2211 @}
2212
2213 celloMusic = \relative c @{
2214   \clef bass
2215   \key c \major
2216   \time 4/4
2217   d4 g fis8 e d4
2218 @}
2219
2220 \score @{
2221   <<
2222     \new Voice = "one" @{
2223       \autoBeamOff
2224       \sopranoMusic
2225     @}
2226     \new Lyrics \lyricsto "one" \sopranoLyrics
2227   >>
2228   \layout @{ @}
2229   \midi @{ @}
2230 @}
2231 @end example
2232
2233 This is looking promising, but the cello part won't appear in the
2234 score -- we haven't used it in the @code{\score} section.  If we
2235 want the cello part to appear under the soprano part, we need to add
2236
2237 @example
2238 \new Staff \celloMusic
2239 @end example
2240
2241 @noindent
2242 underneath the soprano stuff.  We also need to add @code{<<} and
2243 @code{>>} around the music -- that tells LilyPond that there's
2244 more than one thing (in this case, two @code{Staves}) happening
2245 at once.  The @code{\score} looks like this now:
2246
2247 @c Indentation in this example is deliberately poor
2248 @example
2249 \score @{
2250   <<
2251   <<
2252     \new Voice = "one" @{
2253       \autoBeamOff
2254       \sopranoMusic
2255     @}
2256     \new Lyrics \lyricsto "one" \sopranoLyrics
2257   >>
2258   \new Staff \celloMusic
2259   >>
2260   \layout @{ @}
2261   \midi @{ @}
2262 @}
2263 @end example
2264
2265 @noindent
2266 This looks a bit messy; the indentation is messed up now.  That is
2267 easily fixed.  Here's the complete soprano and cello template.
2268
2269 @lilypond[quote,verbatim,ragged-right,addversion]
2270 sopranoMusic = \relative c' {
2271   \clef treble
2272   \key c \major
2273   \time 4/4
2274   a4 b c d
2275 }
2276
2277 sopranoLyrics = \lyricmode {
2278   Aaa Bee Cee Dee
2279 }
2280
2281 celloMusic = \relative c {
2282   \clef bass
2283   \key c \major
2284   \time 4/4
2285   d4 g fis8 e d4
2286 }
2287
2288 \score {
2289   <<
2290     <<
2291       \new Voice = "one" {
2292         \autoBeamOff
2293         \sopranoMusic
2294       }
2295       \new Lyrics \lyricsto "one" \sopranoLyrics
2296     >>
2297     \new Staff \celloMusic
2298   >>
2299   \layout { }
2300   \midi { }
2301 }
2302 @end lilypond
2303
2304 @seealso
2305
2306 The starting templates can be found in the @q{Templates} appendix,
2307 see @ref{Single staff}.
2308
2309
2310 @node Four-part SATB vocal score
2311 @subsection Four-part SATB vocal score
2312
2313 @cindex template, SATB
2314 @cindex SATB template
2315
2316 Most vocal scores of music written for four-part mixed choir
2317 with orchestral accompaniment such as Mendelssohn's Elijah or
2318 Handel's Messiah have the choral music and words on four
2319 staves, one for each of SATB, with a piano reduction of the
2320 orchestral accompaniment underneath.  Here's an example
2321 from Handel's Messiah:
2322
2323 @c The following should appear as music without code
2324 @lilypond[quote,ragged-right]
2325 global = { \key d \major \time 4/4 }
2326 sopranoMusic = \relative c'' {
2327   \clef "treble"
2328   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
2329 }
2330 sopranoWords = \lyricmode {
2331   Wor -- thy is the lamb that was slain
2332 }
2333 altoMusic = \relative a' {
2334   \clef "treble"
2335   r4 a2 a4 | fis4. fis8 a2 | g4 fis e2 |
2336 }
2337 altoWords = \sopranoWords
2338 tenorMusic = \relative c' {
2339   \clef "G_8"
2340   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
2341 }
2342 tenorWords = \sopranoWords
2343 bassMusic = \relative c' {
2344   \clef "bass"
2345   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
2346 }
2347 bassWords = \sopranoWords
2348 upper = \relative a' {
2349   \clef "treble"
2350   \global
2351   r4 <a d fis>2 <a e' a>4 |
2352   <d fis d'>4. <d fis d'>8 <a d a'>2 |
2353   <g cis g'>4 <a d fis> <a cis e>2 |
2354 }
2355 lower = \relative c, {
2356   \clef "bass"
2357   \global
2358   <d d'>4 <d d'>2 <cis cis'>4 |
2359   <b b'>4. <b' b'>8 <fis fis'>2 |
2360   <e e'>4 <d d'> <a' a'>2 |
2361 }
2362
2363 \score {
2364   <<  % combine ChoirStaff and PianoStaff in parallel
2365     \new ChoirStaff <<
2366       \new Staff = "sopranos" <<
2367         \set Staff.instrumentName = "Soprano"
2368         \new Voice = "sopranos" { \global \sopranoMusic }
2369       >>
2370       \new Lyrics \lyricsto "sopranos" { \sopranoWords }
2371       \new Staff = "altos" <<
2372         \set Staff.instrumentName = "Alto"
2373         \new Voice = "altos" { \global \altoMusic }
2374       >>
2375       \new Lyrics \lyricsto "altos" { \altoWords }
2376       \new Staff = "tenors" <<
2377         \set Staff.instrumentName = "Tenor"
2378         \new Voice = "tenors" { \global \tenorMusic }
2379       >>
2380       \new Lyrics \lyricsto "tenors" { \tenorWords }
2381       \new Staff = "basses" <<
2382         \set Staff.instrumentName = "Bass"
2383         \new Voice = "basses" { \global \bassMusic }
2384       >>
2385       \new Lyrics \lyricsto "basses" { \bassWords }
2386     >>  % end ChoirStaff
2387
2388     \new PianoStaff <<
2389       \set PianoStaff.instrumentName = "Piano"
2390       \new Staff = "upper" \upper
2391       \new Staff = "lower" \lower
2392     >>
2393   >>
2394 }
2395 @end lilypond
2396
2397 None of the templates provides this layout exactly.  The nearest is
2398 @q{SATB vocal score and automatic piano reduction} -- see @ref{Vocal
2399 ensembles} -- but we need to change the layout and add a piano
2400 accompaniment which is not derived automatically from the vocal parts.
2401 The variables holding the music and words for the vocal parts are
2402 fine, but we shall need to add variables for the piano reduction.
2403
2404 The order in which the contexts appear in the ChoirStaff of
2405 the template do not correspond with the order in the vocal
2406 score shown above.  We need to rearrange them so there are
2407 four staves with the words written directly underneath the
2408 notes for each part.
2409 All the voices should be @code{\voiceOne}, which is
2410 the default, so the @code{\voiceXXX} commands should be removed.
2411 We also need to specify the tenor clef for the tenors.
2412 The way in which lyrics are specified in the template has not yet
2413 been encountered so we need to use the method with which we are
2414 familiar.  We should also add the names of each staff.
2415
2416 Doing this gives for our ChoirStaff:
2417
2418 @example
2419     \new ChoirStaff <<
2420       \new Staff = "sopranos" <<
2421         \set Staff.instrumentName = "Soprano"
2422         \new Voice = "sopranos" @{ \global \sopranoMusic @}
2423       >>
2424       \new Lyrics \lyricsto "sopranos" @{ \sopranoWords @}
2425       \new Staff = "altos" <<
2426         \set Staff.instrumentName = "Alto"
2427         \new Voice = "altos" @{ \global \altoMusic @}
2428       >>
2429       \new Lyrics \lyricsto "altos" @{ \altoWords @}
2430       \new Staff = "tenors" <<
2431         \set Staff.instrumentName = "Tenor"
2432         \new Voice = "tenors" @{ \global \tenorMusic @}
2433       >>
2434       \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
2435       \new Staff = "basses" <<
2436         \set Staff.instrumentName = "Bass"
2437         \new Voice = "basses" @{ \global \bassMusic @}
2438       >>
2439       \new Lyrics \lyricsto "basses" @{ \bassWords @}
2440     >>  % end ChoirStaff
2441 @end example
2442
2443 Next we must work out the piano part.  This is
2444 easy - we just pull out the piano part from the
2445 @q{Solo piano} template:
2446
2447 @example
2448 \new PianoStaff <<
2449   \set PianoStaff.instrumentName = "Piano  "
2450   \new Staff = "upper" \upper
2451   \new Staff = "lower" \lower
2452 >>
2453 @end example
2454
2455 and add the variable definitions for @code{upper}
2456 and @code{lower}.
2457
2458 The ChoirStaff and PianoStaff must be combined
2459 using angle brackets as we want them to be
2460 stacked one above the other:
2461
2462 @example
2463 <<  % combine ChoirStaff and PianoStaff one above the other
2464   \new ChoirStaff <<
2465     \new Staff = "sopranos" <<
2466       \new Voice = "sopranos" @{ \global \sopranoMusic @}
2467     >>
2468     \new Lyrics \lyricsto "sopranos" @{ \sopranoWords @}
2469     \new Staff = "altos" <<
2470       \new Voice = "altos" @{ \global \altoMusic @}
2471     >>
2472     \new Lyrics \lyricsto "altos" @{ \altoWords @}
2473     \new Staff = "tenors" <<
2474       \clef "G_8"  % tenor clef
2475       \new Voice = "tenors" @{ \global \tenorMusic @}
2476     >>
2477     \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
2478     \new Staff = "basses" <<
2479       \clef "bass"
2480       \new Voice = "basses" @{ \global \bassMusic @}
2481     >>
2482     \new Lyrics \lyricsto "basses" @{ \bassWords @}
2483   >>  % end ChoirStaff
2484
2485   \new PianoStaff <<
2486     \set PianoStaff.instrumentName = "Piano"
2487     \new Staff = "upper" \upper
2488     \new Staff = "lower" \lower
2489   >>
2490 >>
2491 @end example
2492
2493 Combining all these together and adding the music
2494 for the three bars of the example above gives:
2495
2496 @lilypond[quote,verbatim,ragged-right,addversion]
2497 global = { \key d \major \time 4/4 }
2498 sopranoMusic = \relative c'' {
2499   \clef "treble"
2500   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
2501 }
2502 sopranoWords = \lyricmode {
2503   Wor -- thy is the lamb that was slain
2504 }
2505 altoMusic = \relative a' {
2506   \clef "treble"
2507   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
2508 }
2509 altoWords = \sopranoWords
2510 tenorMusic = \relative c' {
2511   \clef "G_8"
2512   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
2513 }
2514 tenorWords = \sopranoWords
2515 bassMusic = \relative c' {
2516   \clef "bass"
2517   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
2518 }
2519 bassWords = \sopranoWords
2520 upper = \relative a' {
2521   \clef "treble"
2522   \global
2523   r4 <a d fis>2 <a e' a>4 |
2524   <d fis d'>4. <d fis d'>8 <a d a'>2 |
2525   <g cis g'>4 <a d fis> <a cis e>2 |
2526 }
2527 lower = \relative c, {
2528   \clef "bass"
2529   \global
2530   <d d'>4 <d d'>2 <cis cis'>4 |
2531   <b b'>4. <b' b'>8 <fis fis'>2 |
2532   <e e'>4 <d d'> <a' a'>2 |
2533 }
2534
2535 \score {
2536   <<  % combine ChoirStaff and PianoStaff in parallel
2537     \new ChoirStaff <<
2538       \new Staff = "sopranos" <<
2539         \set Staff.instrumentName = "Soprano"
2540         \new Voice = "sopranos" { \global \sopranoMusic }
2541       >>
2542       \new Lyrics \lyricsto "sopranos" { \sopranoWords }
2543       \new Staff = "altos" <<
2544         \set Staff.instrumentName = "Alto"
2545         \new Voice = "altos" { \global \altoMusic }
2546       >>
2547       \new Lyrics \lyricsto "altos" { \altoWords }
2548       \new Staff = "tenors" <<
2549         \set Staff.instrumentName = "Tenor"
2550         \new Voice = "tenors" { \global \tenorMusic }
2551       >>
2552       \new Lyrics \lyricsto "tenors" { \tenorWords }
2553       \new Staff = "basses" <<
2554         \set Staff.instrumentName = "Bass"
2555         \new Voice = "basses" { \global \bassMusic }
2556       >>
2557       \new Lyrics \lyricsto "basses" { \bassWords }
2558     >>  % end ChoirStaff
2559
2560     \new PianoStaff <<
2561       \set PianoStaff.instrumentName = "Piano  "
2562       \new Staff = "upper" \upper
2563       \new Staff = "lower" \lower
2564     >>
2565   >>
2566 }
2567 @end lilypond
2568
2569
2570 @node Building a score from scratch
2571 @subsection Building a score from scratch
2572
2573 @cindex template, writing your own
2574
2575 After gaining some facility with writing LilyPond code, you
2576 may find that it is easier to build a score from scratch
2577 rather than modifying one of the templates.  You can also
2578 develop your own style this way to suit the sort of music you
2579 like.  Let's see how to put together the score for an organ
2580 prelude as an example.
2581
2582 We begin with a header section.  Here go the title, name
2583 of composer, etc, then come any variable definitions, and
2584 finally the score block.  Let's start with these in outline
2585 and fill in the details later.
2586
2587 We'll use the first two bars of Bach's prelude
2588 based on @emph{Jesu, meine Freude} which is written for two
2589 manuals and pedal organ.  You can see these two bars of music
2590 at the bottom of this section.  The top manual part has two voices,
2591 the lower and pedal organ one each.  So we need four
2592 music definitions and one to define the time signature
2593 and key:
2594
2595 @example
2596 \version @w{"@version{}"}
2597 \header @{
2598   title = "Jesu, meine Freude"
2599   composer = "J S Bach"
2600 @}
2601 TimeKey = @{ \time 4/4 \key c \minor @}
2602 ManualOneVoiceOneMusic = @{s1@}
2603 ManualOneVoiceTwoMusic = @{s1@}
2604 ManualTwoMusic = @{s1@}
2605 PedalOrganMusic = @{s1@}
2606
2607 \score @{
2608 @}
2609 @end example
2610
2611 For now we've just used a spacer note, @code{s1},
2612 instead of the real music.  We'll add that later.
2613
2614 Next let's see what should go in the score block.
2615 We simply mirror the staff structure we want.
2616 Organ music is usually written on three staves,
2617 one for each manual and one for the pedals.  The
2618 manual staves should be bracketed together, so we
2619 need to use a PianoStaff for them.  The first
2620 manual part needs two voices and the second manual
2621 part just one.
2622
2623 @example
2624   \new PianoStaff <<
2625     \new Staff = "ManualOne" <<
2626       \new Voice @{ \ManualOneVoiceOneMusic @}
2627       \new Voice @{ \ManualOneVoiceTwoMusic @}
2628     >>  % end ManualOne Staff context
2629     \new Staff = "ManualTwo" <<
2630       \new Voice @{ \ManualTwoMusic @}
2631     >>  % end ManualTwo Staff context
2632   >>  % end PianoStaff context
2633 @end example
2634
2635 Next we need to add a staff for the pedal organ.
2636 This goes underneath the PianoStaff, but it must
2637 be simultaneous with it, so we need angle brackets
2638 around the two.  Missing these out would generate
2639 an error in the log file.  It's a common mistake
2640 which you'll make sooner or later!  Try copying
2641 the final example at the end of this section,
2642 remove these angle brackets, and compile it to
2643 see what errors it generates.
2644
2645 @example
2646 <<  % PianoStaff and Pedal Staff must be simultaneous
2647   \new PianoStaff <<
2648     \new Staff = "ManualOne" <<
2649       \new Voice @{ \ManualOneVoiceOneMusic @}
2650       \new Voice @{ \ManualOneVoiceTwoMusic @}
2651     >>  % end ManualOne Staff context
2652     \new Staff = "ManualTwo" <<
2653       \new Voice @{ \ManualTwoMusic @}
2654     >>  % end ManualTwo Staff context
2655   >>  % end PianoStaff context
2656   \new Staff = "PedalOrgan" <<
2657     \new Voice @{ \PedalOrganMusic @}
2658   >>
2659 >>
2660 @end example
2661
2662 It is not necessary to use the simultaneous construct
2663 @code{<< .. >>} for the manual two staff and the pedal organ staff,
2664 since they contain only one music expression, but it does no harm,
2665 and always using angle brackets after @code{\new Staff} is a good
2666 habit to cultivate in case there are multiple voices.  The opposite
2667 is true for Voices: these should habitually be followed by braces
2668 @code{@{ .. @}} in case your music is coded in several variables
2669 which need to run consecutively.
2670
2671 Let's add this structure to the score block, and adjust the indenting.
2672 We also add the appropriate clefs, ensure stems, ties and slurs in
2673 each voice on the upper staff point to the right direction with
2674 @code{\voiceOne} and @code{\voiceTwo}, and enter the time signature
2675 and key to each staff using our predefined variable, @code{\TimeKey}.
2676
2677 @example
2678 \score @{
2679   <<  % PianoStaff and Pedal Staff must be simultaneous
2680     \new PianoStaff <<
2681       \new Staff = "ManualOne" <<
2682         \TimeKey  % set time signature and key
2683         \clef "treble"
2684         \new Voice @{ \voiceOne \ManualOneVoiceOneMusic @}
2685         \new Voice @{ \voiceTwo \ManualOneVoiceTwoMusic @}
2686       >>  % end ManualOne Staff context
2687       \new Staff = "ManualTwo" <<
2688         \TimeKey
2689         \clef "bass"
2690         \new Voice @{ \ManualTwoMusic @}
2691       >>  % end ManualTwo Staff context
2692     >>  % end PianoStaff context
2693     \new Staff = "PedalOrgan" <<
2694       \TimeKey
2695       \clef "bass"
2696       \new Voice @{ \PedalOrganMusic @}
2697     >>  % end PedalOrgan Staff
2698   >>
2699 @}  % end Score context
2700 @end example
2701
2702 That completes the structure.  Any three-staff organ music
2703 will have a similar structure, although the number of voices
2704 may vary.  All that remains now
2705 is to add the music, and combine all the parts together.
2706
2707 @lilypond[quote,verbatim,ragged-right,addversion]
2708 \header {
2709   title = "Jesu, meine Freude"
2710   composer = "J S Bach"
2711 }
2712 TimeKey = { \time 4/4 \key c \minor }
2713 ManualOneVoiceOneMusic = \relative g' {
2714   g4 g f ees | d2 c2 |
2715 }
2716 ManualOneVoiceTwoMusic = \relative c' {
2717   ees16 d ees8~ ees16 f ees d c8 d~ d c~ |
2718   c c4 b8 c8. g16 c b c d |
2719 }
2720 ManualTwoMusic = \relative c' {
2721   c16 b c8~ c16 b c g a8 g~ g16 g aes ees |
2722   f ees f d g aes g f ees d e8~ ees16 f ees d |
2723 }
2724 PedalOrganMusic = \relative c {
2725   r8 c16 d ees d ees8~ ees16 a, b g c b c8 |
2726   r16 g ees f g f g8 c,2 |
2727   }
2728
2729 \score {
2730   <<  % PianoStaff and Pedal Staff must be simultaneous
2731     \new PianoStaff <<
2732       \new Staff = "ManualOne" <<
2733         \TimeKey  % set time signature and key
2734         \clef "treble"
2735         \new Voice { \voiceOne \ManualOneVoiceOneMusic }
2736         \new Voice { \voiceTwo \ManualOneVoiceTwoMusic }
2737       >>  % end ManualOne Staff context
2738       \new Staff = "ManualTwo" <<
2739         \TimeKey
2740         \clef "bass"
2741         \new Voice { \ManualTwoMusic }
2742       >>  % end ManualTwo Staff context
2743     >>  % end PianoStaff context
2744     \new Staff = "PedalOrgan" <<
2745       \TimeKey
2746       \clef "bass"
2747       \new Voice { \PedalOrganMusic }
2748     >>  % end PedalOrgan Staff context
2749   >>
2750 }  % end Score context
2751 @end lilypond
2752
2753