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