]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
Final set of Trevor's fixes. (found and fixed the bug)
[lilypond.git] / Documentation / user / fundamental.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
3
4 @node Fundamental concepts
5 @chapter Fundamental concepts
6
7
8 @menu
9 * How LilyPond files work::     
10 * Voices contain music::        
11 * Contexts and engravers::      
12 * Extending the templates::     
13 @end menu
14
15
16 @node How LilyPond files work
17 @section How LilyPond files work
18
19 The LilyPond input format is quite free-form, giving experienced
20 users a lot of flexibility to structure their files however they
21 wish.  But this flexibility can make things confusing for new
22 users.  This section will explain some of this structure, but may
23 gloss over some details in favor of simplicity.  For a complete
24 description of the input format, see @ruser{File structure}.
25
26 @menu
27 * Introduction to the LilyPond file structure::  
28 * Score is a (single) compound musical expression::  
29 * Nesting Music Expressions::   
30 * On the un-nestedness of brackets and ties::  
31 @end menu
32
33 @node Introduction to the LilyPond file structure
34 @subsection Introduction to the LilyPond file structure
35
36 A basic example of a lilypond input file is
37
38 @example
39 \version "2.11.23"
40 \score @{
41   @var{...compound music expression...}  % all the music goes here!
42   \header @{ @}
43   \layout @{ @}
44   \midi @{ @}
45 @}
46 @end example
47
48 @noindent
49 There are many variations of this basic pattern, but this
50 example serves as a useful starting place.
51
52 At this point, you may be confused, since you have never seen a
53 @code{\score@{@}} before.  This is because LilyPond automatically
54 adds the extra commands when you give it simple input.  LilyPond
55 treats input like this:
56
57 @example
58 \relative c'' @{
59   c4 a d c
60 @}
61 @end example
62
63 @noindent
64 as shorthand for this:
65
66 @example
67 \score @{
68   \relative c'' @{
69     c4 a b c
70   @}
71   \layout @{ @}
72 @}
73 @end example
74
75 In other words, if the input contains a single music expression,
76 LilyPond will interpret the file as though the music expression
77 was wrapped up inside a @code{\score@{@}}.
78
79 @smallspace
80
81 A @code{\score} must begin with a compound music expression.
82 Remember that a music expression could be anything from a single
83 note to a huge
84
85 @example
86 @{
87   \new GrandStaff <<
88     @var{...insert the whole score of a Wagner opera in here...}
89   >>
90 @}
91 @end example
92
93 @noindent
94 Since everything is inside @code{@{ ... @}}, it counts
95 as one music expression.
96
97 As we saw previously, the @code{\score} can contain other things,
98 such as
99
100 @example
101 \score @{
102   @{ c'4 a b c' @}
103   \header @{ @}
104   \layout @{ @}
105   \midi @{ @}
106 @}
107 @end example
108
109 @noindent
110 Some people put some of those commands outside the @code{\score}
111 block -- for example, @code{\header} is often placed above the
112 @code{\score}.  That's just another shorthand that LilyPond
113 accepts.  Two more commands you have not previously seen are
114 @code{\layout @{ @}} and @code{\midi @{@}}.  If these appear as 
115 shown they will cause LilyPond to produce a printed output and a 
116 MIDI out respectively.  They are described fully in the
117 Notation Reference - @ruser{Score layout} and 
118 @ruser{Creating MIDI files}.  
119
120 @smallspace
121
122 @cindex variables
123
124 Another great shorthand is the ability to define variables.  All
125 the templates use this
126
127 @example
128 melody = \relative c' @{
129   c4 a b c
130 @}
131
132 \score @{
133   \melody
134 @}
135 @end example
136
137 When LilyPond looks at this file, it takes the value of
138 @code{melody} (everything after the equals sign) and inserts it
139 whenever it sees @code{\melody}.  There's nothing special about
140 the names -- it could be @code{melody}, @code{global},
141 @code{pianorighthand}, or @code{foofoobarbaz}.  You can use
142 whatever variable names you want as along as they contain just
143 alphabetic characters.  For more details, see
144 @ruser{Saving typing with variables and functions}.
145
146
147 @seealso
148
149 For a complete definition of the input format, see
150 @ruser{File structure}.
151
152 @node Score is a (single) compound musical expression
153 @subsection Score is a (single) compound musical expression
154
155 @cindex Compound music expression
156 @cindex Music expression, compound
157
158 We saw the general organization of LilyPond input files in the
159 previous section, @ref{How LilyPond files work}.  But we seemed to
160 skip over the most important part: how do we figure out what to
161 write after @code{\score}?
162
163 We didn't skip over it at all.  The big mystery is simply that
164 there @emph{is} no mystery.  This line explains it all:
165
166 @quotation
167 @emph{A @code{\score} must begin with a compound music expression.}
168 @end quotation
169
170 @noindent
171 You may find it useful to review
172 @ref{Music expressions explained}.  In that section, we saw how to
173 build big music expressions from small pieces -- we started from
174 notes, then chords, etc.  Now we're going to start from a big
175 music expression and work our way down.
176
177 @example
178 \score @{
179   @{ % this brace begins the overall compound music expression
180     \new GrandStaff <<
181       @var{...insert the whole score of a Wagner opera in here...}
182     >>
183   @} % this brace ends the overall compound music expression
184   \layout @{ @}
185 @}
186 @end example
187
188 A whole Wagner opera would easily double the length of this
189 manual, so let's just add a singer and piano.  We don't need a
190 @code{GrandStaff} for this ensemble, which simply groups a number
191 of staves together with a brace at the left, so we shall remove 
192 it.  We @emph{do} need a singer and a piano, though.
193
194 @example
195 \score @{
196   @{
197     <<
198       \new Staff = "singer" <<
199       >>
200       \new PianoStaff = piano <<
201       >>
202     >>
203   @}
204   \layout @{ @}
205 @}
206 @end example
207
208 Remember that we use @code{<<} and @code{>>} to show simultaneous
209 music.  And we definitely want to show the vocal part and piano
210 part at the same time, not one after the other!  However, the
211 @code{<< .. >>} construct is not really necessary for the Singer
212 staff, as it contains only one music expression, but Staves often
213 do require simultaneous Voices within them, so using @code{<< .. >>}
214 rather than braces is a good habit to adopt.
215
216 @example
217 \score @{
218   @{
219     <<
220       \new Staff = "singer" <<
221         \new Voice = "vocal" @{ @}
222         \addlyrics @{ @}
223       >>
224       \new PianoStaff = "piano" <<
225         \new Staff = "upper" @{ @}
226         \new Staff = "lower" @{ @}
227       >>
228     >>
229   @}
230   \layout @{ @}
231 @}
232 @end example
233
234 Now we have a lot more details.  We have the singer's staff: it
235 contains a @code{Voice} (in LilyPond, this term refers to a set of
236 notes, not necessarily vocal notes -- for example, a violin
237 generally plays one voice) and some lyrics.  We also have a piano
238 staff: it contains an upper staff (right hand) and a lower staff
239 (left hand).
240
241 At this stage, we could start filling in notes.  Inside the curly
242 braces next to @code{\new Voice = vocal}, we could start writing
243
244 @example
245 \relative c'' @{
246   a4 b c d
247 @}
248 @end example
249
250 But if we did that, the @code{\score} section would get pretty
251 long, and it would be harder to understand what was happening.  So
252 let's use variables instead.
253
254 @example
255 melody = \relative c'' @{ @}
256 text   = \lyricmode @{ @}
257 upper  = \relative c'' @{ @}
258 lower  = \relative c @{ @}
259 \score @{
260   @{
261     <<
262       \new Staff = "singer" <<
263         \new Voice = "vocal" @{ \melody @}
264         \addlyrics @{ \text @}
265       >>
266       \new PianoStaff = "piano" <<
267         \new Staff = "upper" @{ \upper @}
268         \new Staff = "lower" @{ \lower @}
269       >>
270     >>
271   @}
272   \layout @{ @}
273 @}
274 @end example
275
276 @noindent
277 Remember that you can use almost any name you like as long
278 as it contains just alphabetic characters.  The exact
279 limitations on variable names are detailed in
280 @ruser{File structure}.
281
282 Be careful about the difference between notes, which are introduced
283 with @code{\relative}, and lyrics, which are introduced with
284 @code{\lyricmode}.  These are essential to tell LilyPond
285 to interpret the following content as music and text
286 respectively.
287
288 When writing (or reading) a @code{\score} section, just take it
289 slowly and carefully.  Start with the outer layer, then work on
290 each smaller layer.  It also really helps to be strict with
291 indentation -- make sure that each item on the same layer starts
292 on the same horizontal position in your text editor.
293
294
295 @node Nesting Music Expressions
296 @subsection Nesting Music Expressions
297
298 It is not essential to declare all staves at the beginning; 
299 they may be introduced temporarily at any point.  This is
300 particularly useful for creating ossia sections 
301 (see @rglos{ossia}).  Here is a simple example showing how
302 to introduce a new staff temporarily for the duration of
303 three notes:
304
305 @lilypond[verbatim,quote,ragged-right]
306 \new Staff {
307   \relative g' {
308     r4 g8 g c4 c8 d |
309     e4 r8 
310     <<
311       { f c c }
312       \new Staff {
313         f8 f c
314       }
315     >>
316     r4 |
317   }
318 }
319 @end lilypond
320
321 The ossia section may be placed above the staff
322 as follows:
323
324 @lilypond[verbatim,quote,ragged-right]
325 \new Staff ="main" {
326   \relative g' {
327     r4 g8 g c4 c8 d |
328     e4 r8
329     <<
330       { f c c }
331       \new Staff \with {
332         alignAboveContext = "main" }
333       { f8 f c }
334     >>
335     r4 |
336   }
337 }
338 @end lilypond
339
340 This example uses @code{\with}, which will be explained more 
341 fully later.  It is a means of modifying the default behaviour
342 of a Staff.  Here it says that the new staff should be placed
343 above the staff called @qq{main} instead of the default position
344 which is below.
345
346 Ossia are often written without clef and without
347 time signature and are usually in a smaller font.
348 These require further commands which
349 have not yet been introduced.  See ...
350
351 TODO Add ref to tweaks section where this example should
352 be placed and explained.
353
354 @node On the un-nestedness of brackets and ties
355 @subsection On the un-nestedness of brackets and ties
356
357 You have already met a number of different types of bracket in
358 writing the input file to LilyPond.  These obey different rules
359 which can be confusing at first.  Before we explain the rules 
360 let's first review the different types of bracket.
361
362 @c attempt to force this onto a new page
363 @need 50
364 @multitable @columnfractions .3 .7 
365 @headitem Bracket Type
366   @tab Function
367 @item @code{@{ .. @}}
368   @tab Encloses a sequential segment of music
369 @item @code{< .. >}
370   @tab Encloses the notes of a chord
371 @item @code{<< .. >>}
372   @tab Encloses concurrent or simultaneous sections 
373 @item @code{( .. )}
374   @tab Marks the start and end of a slur
375 @item @code{\( .. \)}
376   @tab Marks the start and end of a phrase mark
377 @item @code{[ .. ]}
378   @tab Marks the start and end of a manual beam
379 @end multitable
380
381 To these we should add other constructs which generate lines
382 between or across notes: ties (marked by a tilde, @code{~}),
383 tuplets written as @code{\times x/y @{..@}}, and grace notes
384 written as @code{\grace@{..@}}.
385
386 Outside LilyPond, the conventional use of brackets requires 
387 the different types to be properly nested, like this, 
388 @code{<< [ @{ ( .. ) @} ] >>}, with the closing brackets being
389 encountered in exactly the opposite order to the opening 
390 brackets.  This @strong{is} a requirement for the three types of 
391 bracket described by the word @q{Encloses} in the table above - 
392 they must nest properly.
393 However, the remaining brackets, described with the word 
394 @q{Marks} in the table above together with ties and tuplets,
395 do @strong{not} have to nest
396 properly with any of the brackets.  In fact, these are not 
397 brackets in the sense that
398 they enclose something - they are simply markers to indicate
399 where something starts and ends.
400
401 So, for example, a phrasing slur can start before a manually
402 inserted beam and end before the end of the beam - not very
403 musical, perhaps, but possible:
404
405 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
406  { g8\( a b[ c b\) a] }
407 @end lilypond
408
409 In general, different kinds of brackets, and those implied by
410 tuplets, ties and grace notes, may be mixed freely.
411 This example shows a beam extending into a tuplet (line 1),
412 a slur extending into a tuplet (line 2),
413 a beam and a slur extending into a tuplet, a tie crossing
414 two tuplets, and a phrasing slur extending out of a tuplet
415 (lines 3 and 4).
416
417 @lilypond[quote,verbatim,fragment,ragged-right]
418 {
419   r16[ g16 \times 2/3 {r16 e'8] }
420   g16( a \times 2/3 {b d) e' }
421   g8[( a \times 2/3 {b d') e'~]}
422   \times 4/5 {e'32\( a b d' e'} a'4.\)
423 }
424 @end lilypond
425
426
427 @node Voices contain music
428 @section Voices contain music
429
430 Singers need voices to sing, and so does LilyPond.
431 The actual music for all instruments in a score 
432 is contained in Voices - the most fundamental 
433 of all LilyPond's concepts.
434
435 @menu
436 * I'm hearing Voices::          
437 * Explicitly instantiating voices::  
438 * Voices and vocals::           
439 @end menu
440
441 @node I'm hearing Voices
442 @subsection I'm hearing Voices
443
444 @cindex polyphony
445 @cindex layers
446 @cindex Voice context
447
448 The lowest, most fundamental or innermost layers in a LilyPond 
449 score are called @q{Voice contexts} or just @q{Voices} for short.
450 Voices are sometimes called @q{layers} in other notation 
451 packages.
452
453 In fact, a Voice layer or context is the only one which can
454 contain music.  If a Voice context is not explicitly declared
455 one is created automatically.  Some instruments such as an 
456 Oboe can play only one note at a time.  Music written for
457 such instruments is monophonic and requires just a single
458 voice.  Instruments which can play more than one note at a
459 time like the piano will often require multiple voices to
460 encode the different concurrent notes and rhythms they are
461 capable of playing.  
462
463 A single voice can contain many notes in a chord, of course,
464 so when exactly are multiple voices needed? 
465 Let us analyse a short piece of music to see how many voices
466 are required.  Here are the notes from the first two bars
467 of the second of Chopin's Deux Nocturnes, Op 32.
468
469 @c The following should appear as music without code
470 @lilypond[quote,ragged-right]
471 \new Staff \relative c'' {
472   \key aes \major
473   <<
474     { \voiceOne
475       c2 aes4. bes8 } \\
476     { \voiceTwo
477       aes2 f4 fes } \\
478     { \voiceFour
479       \once \override NoteColumn #'force-hshift = #0 <ees c>2
480       \once \override NoteColumn #'force-hshift = #0.5 des2 }
481   >> |
482   <c ees aes c>1 |
483 }
484 @end lilypond
485
486 The direction of the stems is often used to indicate the
487 continuity of two simultaneous melodic lines.  Here the
488 stems of the highest notes are all pointing up and the
489 stems of the lower notes are all pointing down.
490 This is the first indication that more than one voice
491 is required.
492
493 But the real need for multiple voices arises when notes
494 which start at the same time have different durations.
495 Look at the notes which start at beat three in the first 
496 bar.  The a-flat is a dotted quarter note, the f is a
497 quarter note and the d-flat is a half note.  These 
498 cannot be written as a chord as all the notes in a chord
499 must have the same duration.  Neither can they be written
500 as sequential notes, as they must start at the same time.
501 This section of the bar requires three voices, and the
502 normal practice would be to write the whole bar as three
503 voices, as shown here, where we have used different noteheads
504 and colors for the three voices.
505
506 @c The following should appear as music without code
507 @c The three voice styles should be defined in -init
508 @lilypond[quote,ragged-right]
509 \new Staff \relative c'' {
510   \key aes \major
511   <<
512     { \voiceOne
513       \voiceOneStyle 
514       c2 aes4. bes8 } \\
515     { \voiceTwo
516       \voiceTwoStyle
517       aes2 f4 fes } \\
518     { \voiceFour
519       \voiceThreeStyle
520       \once \override NoteColumn #'force-hshift = #0 <ees c>2
521       \once \override NoteColumn #'force-hshift = #0.5 des2 }
522   >> |
523   <c ees aes c>1 |
524 }
525 @end lilypond
526
527 Let us see how this is done.
528
529 @funindex \\
530
531 The easiest way to enter fragments with more than one voice on a
532 staff is to enter each voice as a sequence (with @code{@{...@}}),
533 and combine them simultaneously with angle brackets, @code{<<...>>}.
534 The fragments must also be separated with double backward slashes, 
535 @code{\\}, to place them in separate voices.  Without these, the 
536 notes would be entered into a single voice, which would usually 
537 cause errors.  This technique is particularly suited to pieces of
538 music which are largely monophonic with occasional short sections
539 of polyphony.  Here's a simple example:
540
541 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
542 \key d \minor
543 %    Voice "1"             Voice "2"
544 << { r4 g g4. a8 }    \\ { d,2 d4 g }       >> |
545 << { bes4 bes c bes } \\ { g4 g g8( a) g4 } >> |
546 << { a2. r4 }         \\ { fis2. s4 }       >>
547 @end lilypond
548
549 This example has just two voices, but the same contruct may be
550 used to encode three or more voices by adding more back-slash
551 separators.
552
553 The Voice contexts bear the names @code{"1"}, @code{"2"}, etc.  
554 In each of these contexts, the vertical direction of slurs, 
555 stems, ties, dynamics etc., is set appropriately.
556
557 @lilypond[quote,verbatim,fragment]
558 \new Staff \relative c' {
559   % Main voice
560   c16 d e f
561   %    Voice "1"     Voice "2"                 Voice "3"
562   << { g4 f e } \\ { r8 e4 d c8 ~ } >> |
563   << { d2 e2 }  \\ { c8 b16 a b8 g ~ g2 } \\ { s4 b4 c2 } >> |
564 }
565 @end lilypond
566
567 These voices are all separate from the main voice that contains
568 the notes just outside the @code{<< .. >>} construct.  Let's call
569 this the @emph{simultaneous construct}.  Slurs and ties may only
570 connect notes within the same voice, so slurs and ties cannot go
571 into or out of a simultaneous construct.  Conversely,
572 parallel voices from separate simultaneous constructs on the same
573 staff are the same voice.  Other voice-related properties also
574 carry across simultaneous constructs.  Here is the same example,
575 with different colors and noteheads for each voice.  Note that
576 changes in one Voice do not affect other voices, but they do
577 persist in the same Voice later.  Note also that tied notes may be
578 split across the same voices in two constructs, shown here in the
579 blue triangle voice.
580
581 @lilypond[quote,verbatim]
582 \new Staff \relative c' {
583   \voiceOneStyle
584   c16 d e f
585   << % Bar 1
586     { g4 f e } \\
587     { \voiceTwoStyle
588       r8 e4 d c8 ~ }
589   >> |
590   << % Bar 2
591     { d2 e2 } \\
592     { c8 b16 a b8 g ~ g2 } \\
593     { \voiceThreeStyle
594       s4 b4 c2 }
595   >>
596 }
597 @end lilypond
598
599 The commands @code{\voiceXXXStyle} are mainly intended for use in
600 educational documents such as this one.  They modify the color
601 of the notehead, the stem and the beams, and the style of the
602 notehead, so that the voices may be easily distinguished.
603 Voice one is set to red diamonds, voice two to blue triangles,
604 voice three to green crossed circles, and voice four (not used
605 here) to magenta crosses.  We shall see later how commands like 
606 these may be created by the user.
607 TODO: add ref to appropriate section in Tweaks
608
609 Polyphony does not change the relationship of notes within a
610 @code{\relative @{ @}} block.  Each note is still calculated 
611 relative to the note immediately preceding it, or to the first 
612 note of the preceding chord.  So in
613
614 @example
615 \relative c' @{ noteA << < noteB noteC > \\ noteD >> noteE @}
616 @end example
617
618 @noindent
619 @code{noteB} is relative to @code{noteA}                      @*
620 @code{noteC} is relative to @code{noteB}, not @code{noteA};   @*
621 @code{noteD} is relative to @code{noteB}, not @code{noteA} or
622 @code{noteC}.                                                 @*
623 @code{noteE} is relative to @code{noteD}, not @code{noteA}
624
625 We are now in a position to return to the first example from
626 the Chopin Nocturne to see how this might be encoded.  As we
627 shall see, this encounters some difficulties.  We begin as
628 we have learnt, using the @code{<< \\  >>} construct to 
629 enter the music of the first bar in three voices:
630
631 @lilypond[quote,verbatim,fragment,ragged-right]
632 \new Staff \relative c'' {
633   \key aes \major
634   << 
635     { c2 aes4. bes8 } \\ { aes2 f4 fes } \\ { <ees c>2 des2 }
636   >> |
637   <c ees aes c>1 |
638 }
639 @end lilypond
640
641 @cindex stem down
642 @cindex stem up
643 @funindex \voiceOne
644 @funindex \voiceTwo
645 @funindex \voiceThree
646 @funindex \voiceFour
647
648 The stem directions are automatically assigned with the
649 odd-numbered voices taking upward stems and the even-numbered
650 voices downward ones.  The stems for voices 1 and 2 are right,
651 but the stems in voice 3 should go down in this particular piece
652 of music.  We can correct this
653 by telling LilyPond that this third voice is really a fourth
654 voice, with stems going down, using the @code{\voiceFour} 
655 command.  There are also corresponding @code{\voiceOne},
656 @code{\voiceTwo}, and @code{voiceThree} 
657 commands.  This results in the following:
658
659 @lilypond[quote,verbatim,fragment,ragged-right]
660 \new Staff \relative c'' {
661   \key aes \major
662   << 
663     { c2 aes4. bes8 } \\ 
664     { aes2 f4 fes   } \\ 
665     { \voiceFour <ees c>2 des2 }
666   >> |
667   <c ees aes c>1 |
668 }
669 @end lilypond
670
671 @noindent
672 and exposes a problem commonly encountered with multiple
673 voices: the stems of notes can collide with noteheads
674 in other voices.  In laying out the notes, LilyPond allows the
675 notes or chords from two voices to occupy the same vertical
676 note column provided the stems are in opposite directions, but
677 the notes from a third voice are displaced to avoid the stems
678 colliding.  This often works well, but in this example the
679 notes of the third voice are clearly not well placed by default.
680 LilyPond provides several ways to adjust the horizontal placing
681 of notes.  We are not quite ready yet to see how to correct this,
682 so we shall leave this problem until a later section (see ... )
683
684 TODO link.
685
686 @node Explicitly instantiating voices
687 @subsection Explicitly instantiating voices
688
689 Voice contexts can also be created manually
690 inside a @code{<< >>} block to create polyphonic music, using
691 @code{\voiceOne} ... @code{\voiceFour} to indicate the required
692 directions of stems, slurs, etc.
693
694 Specifically, the construct @code{<< \\ >>} which we used in
695 the previous section:
696
697 @example
698 << \upper \\ \lower >>
699 @end example
700
701 @noindent
702 where @code{upper} and @code{lower} are user-defined variables 
703 containing the music for the two voices, is equivalent to 
704
705 @example
706 <<
707   \new Voice = "1" @{ \voiceOne \upper @}
708   \new Voice = "2" @{ \voiceTwo \lower @}
709 >>
710 @end example
711
712 The @code{\voiceXXX} commands set the direction of stems, slurs,
713 ties, articulations, text annotations, augmentation dots of dotted
714 notes, and fingerings.  @code{\voiceOne} and @code{\voiceThree}
715 make these objects point upwards, while @code{\voiceTwo} and
716 @code{\voiceFour} make them point downwards.  These commands also
717 generate a horizontal shift for each voice when this is required 
718 to avoid clashes of note heads.  The command @code{\oneVoice} 
719 reverts the settings back to the normal values for a single voice.
720
721 Let us see in some simple examples exactly what effect 
722 @code{\oneVoice}, @code{\voiceOne} and @code{voiceTwo} have on
723 markup, ties, slurs, and dynamics:
724
725 @lilypond[quote,ragged-right,verbatim]
726 \relative c'{ 
727   c-"default" d8 ~ d e4 ( f g a ) b-> c
728 }
729 @end lilypond
730
731 @lilypond[quote,ragged-right,verbatim]
732 \relative c'{
733   \voiceOne
734   c-"\\voiceOne" d8 ~ d e4 ( f g a ) b-> c
735   \oneVoice
736   c,-"\\oneVoice" d8 ~ d e4 ( f g a ) b-> c
737 }
738 @end lilypond
739
740 @lilypond[quote,ragged-right,verbatim]
741 \relative c'{
742   \voiceTwo
743   c-"\\voiceTwo" d8 ~ d e4 ( f g a ) b-> c
744   \oneVoice
745   c,-"\\oneVoice" d8 ~ d e4 ( f g a ) b-> c
746 }
747 @end lilypond
748
749 An expression that appears directly inside a @code{<< >>} belongs
750 to the main voice (but, note, @strong{not} in a @code{<< \\ >>}
751 construct).  This is useful when extra voices appear while the
752 main voice is playing.  Here is a more correct rendition of the
753 example from the previous section.  The red diamond-shaped notes
754 demonstrate that the main melody is now in a single voice context,
755 permitting a phrasing slur to be drawn over them.
756
757 @lilypond[quote,ragged-right,verbatim]
758 \new Staff \relative c' {
759   \voiceOneStyle
760   c16^( d e f  % These notes are monophonic
761   <<           % Start simultaneous section of three voices
762     { g4 f e | d2 e2) }  % Continue the main voice in parallel
763     \new Voice {         % Initiate second voice
764       \voiceTwo          % Set stems, etc, down
765       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
766     }
767     \new Voice {         % Initiate third voice
768       \voiceThree        % Set stems, etc, up
769       s2. | s4 b4 c2 
770     }
771   >>
772 }
773 @end lilypond
774
775 @cindex nesting music expressions
776 @cindex nesting simultaneous constructs
777
778 More deeply nested polyphony constructs are possible, and if a
779 voice appears only briefly this might be a more natural way to
780 typeset the music.
781
782 @lilypond[quote,ragged-right,verbatim]
783 \new Staff \relative c' {
784   c16^( d e f
785   <<
786     { g4 f e | d2 e2) }
787     \new Voice {
788       \voiceTwo
789       r8 e4 d c8 ~ |
790       <<
791         {c8 b16 a b8 g ~ g2}
792         \new Voice { 
793           \voiceThree
794           s4 b4 c2
795         }
796       >>
797     }
798   >>
799 }
800 @end lilypond
801
802
803 This method of nesting new voices briefly is useful 
804 when only small sections of the music
805 are polyphonic, but when the whole staff is largely polyphonic
806 it can be clearer to use multiple voices throughout, using 
807 spacing notes to step over sections where the voice is silent, 
808 as here:
809
810 @lilypond[quote,ragged-right,verbatim]
811 \new Staff \relative c' <<
812   \new Voice {   % Initiate first voice
813     \voiceOne
814     c16^( d e f g4 f e | d2 e2) |
815   }
816   \new Voice {   % Initiate second voice
817     \voiceTwo    % set stems, etc down
818     s4 r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2 |
819   }
820   \new Voice {   % Initiate third voice
821     \voiceThree  % set stems, etc up
822     s1 | s4 b4 c2 |
823   }
824 >>
825 @end lilypond
826
827
828 @node Voices and vocals
829 @subsection Voices and vocals
830
831 Vocal music presents a special difficulty: we need to combine two
832 expressions -- notes and lyrics.
833
834 You have already seen the @code{\addlyricsd@{@}} command, which
835 handles simple scores well.  However, this technique is
836 quite limited.  For more complex music, you must introduce the
837 lyrics in a @code{Lyrics} context using @code{\new Lyrics} and
838 explicitly link
839 the lyrics to the notes with @code{\lyricsto@{@}}, using the 
840 name assigned to the Voice.
841
842 @lilypond[quote,verbatim,fragment]
843 <<
844   \new Voice = "one" \relative c'' {
845     \autoBeamOff
846     \time 2/4
847     c4 b8. a16 g4. f8 e4 d c2
848   }
849   \new Lyrics \lyricsto "one" {
850     No more let sins and sor -- rows grow.
851   }
852 >>
853 @end lilypond
854
855 The automatic beaming which LilyPond uses by default works well
856 for instrumental music, but not so well for music with lyrics,
857 where beaming is either not required at all or is used to indicate
858 melismata in the lyrics.  In the example above we use the command
859 @code{\autoBeamOff} to turn off the automatic beaming.
860
861 Let us reuse the earlier example from Judas Maccabæus to 
862 illustrate this more flexible technique.  We first recast
863 it to use variables so the music and lyrics can be separated
864 from the staff structure.  We also introduce a ChoirStaff 
865 bracket.  The lyrics themselves must be introduced with 
866 @code{\lyricmode} to ensure they are interpreted as lyrics
867 rather than music.
868
869 @lilypond[quote,verbatim]
870 global = { \time 6/8 \partial 8 \key f \major}
871 SopOneMusic = \relative c'' {
872   c8 | c([ bes)] a a([ g)] f | f'4. b, | c4.~ c4 }
873 SopTwoMusic = \relative c' {
874   r8 | r4. r4 c8 | a'([ g)] f f([ e)] d | e([ d)] c bes' }
875 SopOneLyrics = \lyricmode {
876   Let | flee -- cy flocks the | hills a -- dorn, __ }
877 SopTwoLyrics = \lyricmode {
878   Let | flee -- cy flocks the | hills a -- dorn, }
879
880 \score {
881   \new ChoirStaff <<
882     \new Staff <<
883       \new Voice = "SopOne" {
884         \global
885         \SopOneMusic
886       }
887       \new Lyrics \lyricsto "SopOne" {
888         \SopOneLyrics
889       }
890     >>
891     \new Staff <<
892       \new Voice = "SopTwo" {
893         \SopTwoMusic
894       }
895       \new Lyrics \lyricsto "SopTwo" {
896         \SopTwoLyrics
897       }
898     >>
899   >>
900 }
901 @end lilypond
902
903 This is the basic structure of all vocal scores.  More staves may
904 be added as required, more voices may be added to the staves,
905 more verses may be added to the lyrics,
906 and the variables containing the music can easily be placed
907 in separate files should they become too long.
908
909 Here is a final example of the first line of a hymn with four
910 verses, set for SATB.  In this case the words for all four
911 parts are the same.
912
913 @lilypond[quote,verbatim]
914 global = { \time 4/4 \partial 4 \key c \major}
915 SopMusic   = \relative c' { c4 | e4. e8 g4  g  | a a g }
916 AltoMusic  = \relative c' { c4 | c4. c8 e4  e  | f f e }
917 TenorMusic = \relative c  { e4 | g4. g8 c4. b8 | a8 b c d e4 }
918 BassMusic  = \relative c  { c4 | c4. c8 c4  c  | f8 g a b c4 }
919 VerseOne   = \lyricmode { 
920   E -- | ter -- nal fa -- ther, | strong to save, }
921 VerseTwo   = \lyricmode { 
922   O | Christ, whose voice the | wa -- ters heard, }
923 VerseThree = \lyricmode { 
924   O | Ho -- ly Spi -- rit, | who didst brood }
925 VerseFour  = \lyricmode { 
926   O | Tri -- ni -- ty of | love and pow'r }
927
928 \score {
929   \new ChoirStaff <<
930     \new Staff <<
931       \clef "treble"
932       \new Voice = "Sop"  { \voiceOne \global \SopMusic }
933       \new Voice = "Alto" { \voiceTwo \AltoMusic }
934       \new Lyrics \lyricsto "Sop" { \VerseOne   }
935       \new Lyrics \lyricsto "Sop" { \VerseTwo   }
936       \new Lyrics \lyricsto "Sop" { \VerseThree }
937       \new Lyrics \lyricsto "Sop" { \VerseFour  }
938     >>
939     \new Staff <<
940       \clef "bass"
941       \new Voice = "Tenor" { \voiceOne \TenorMusic }
942       \new Voice = "Bass"  { \voiceTwo \BassMusic  }
943     >>
944   >>
945 }
946 @end lilypond
947
948 @node Contexts and engravers
949 @section Contexts and engravers
950
951 Contexts and engravers have been mentioned informally
952 in earlier sections; we now must look at 
953 these concepts in more detail, as they are important
954 in the fine-tuning of LilyPond output.
955
956
957 @menu
958 * Contexts explained::          
959 * Creating contexts::           
960 * Engravers::                   
961 * Modifying contexts::          
962 @end menu
963
964 @node Contexts explained
965 @subsection Contexts explained
966
967 When music is printed, many notational elements which do not 
968 appear explicitly in the input file must be added to the
969 output.  For example, compare the input and output of the 
970 following example:
971
972 @lilypond[quote,verbatim,relative=2,fragment]
973 cis4 cis2. g4
974 @end lilypond
975
976 The input is rather sparse, but in the output, bar lines, 
977 accidentals, clef, and time signature have been added.  When 
978 LilyPond @emph{interprets} the input the musical information 
979 is inspected in time order, similar to reading a score from left 
980 to right.  While reading the input, the program remembers where 
981 measure boundaries are, and which pitches require explicit 
982 accidentals.  This information must be held on several levels.  
983 For example, the effect of an accidental is limited
984 to a single staff, while a bar line must be synchronized across 
985 the entire score.
986
987 Within LilyPond, these rules and bits of information are grouped
988 in @emph{Contexts}.  We have already met the @context{Voice} 
989 context. 
990 Others are the @context{Staff} and @context{Score} contexts.  
991 Contexts are hierarchical to reflect the heirarchical nature of 
992 a musical score.  
993 For example: a @context{Staff} context can contain many 
994 @context{Voice} contexts, and a @context{Score} context can 
995 contain many @context{Staff} contexts.
996
997 @quotation
998 @image{context-example,5cm,,}
999 @end quotation
1000
1001 Each context has the responsibility for enforcing some notation rules,
1002 creating some notation objects and maintaining the associated
1003 properties.  For example, the @context{Voice} context may introduce an
1004 accidental and then the @context{Staff} context maintains the rule to
1005 show or suppress the accidental for the remainder of the measure.
1006
1007 As another example, the synchronization of bar lines is, by default, 
1008 handled in the @context{Score} context.
1009 However, in some music we may not want the bar lines to be
1010 synchronized -- consider a polymetric score in 4/4 and 3/4 time.
1011 In such cases, we must modify the default settings of the 
1012 @context{Score} and @context{Staff} contexts.
1013
1014 For very simple scores, contexts are created implicitly, and you need
1015 not be aware of them.  For larger pieces, such as anything with more
1016 than one staff, they must be
1017 created explicitly to make sure that you get as many staves as you
1018 need, and that they are in the correct order.  For typesetting pieces
1019 with specialized notation, it is usual to modify existing, or
1020 even to define totally new, contexts.
1021
1022 In addition to the @context{Score,} @context{Staff} and 
1023 @context{Voice} contexts there are contexts which fit between
1024 the score and staff levels to control staff groups, such as the
1025 @context{PianoStaff} and @context{ChoirStaff} contexts.  There
1026 are also alternative staff and voice contexts, and contexts for
1027 lyrics, percussion, fret boards, figured bass, etc.  A complete
1028 list is shown in the Notation Reference.
1029 TODO: Add link
1030
1031 The names of all context types are formed from one or more 
1032 words, each word being capitalised and joined immediately to the 
1033 preceding word with no hyphen or underscore, e.g., 
1034 @context{GregorianTranscriptionStaff}.
1035
1036 @node Creating contexts
1037 @subsection Creating contexts
1038
1039 There can be only one top level context: the @context{Score} 
1040 context.  This is created with the @code{\score} command, 
1041 or, in simple scores, it is created automatically.
1042
1043 For scores with only one voice and one staff, the 
1044 @context{Voice} and @context{Staff} contexts may be left to be 
1045 created automatically, but for more complex scores it is 
1046 necessary to create them by hand.  
1047 The simplest command that does this is @code{\new}.  
1048 It is prepended to a music expression, for example
1049
1050 @funindex \new
1051 @cindex new contexts
1052 @cindex Context, creating
1053
1054 @example
1055 \new @var{type} @var{music expression}
1056 @end example
1057
1058 @noindent
1059 where @var{type} is a context name (like @code{Staff} or
1060 @code{Voice}).  This command creates a new context, and starts
1061 interpreting the @var{music expression} within that context.
1062
1063 Note that there is no @code{\new Score % Invalid!} command;
1064 the single top-level @context{Score} context is introduced 
1065 with @code{\score}.  This is because there can be only one 
1066 @context{Score} context, whereas there may be multiple 
1067 @context{Staff} and @context{Voice} contexts - each created 
1068 by @code{\new}.
1069
1070 The @code{\new} command may also give a identifying name to the 
1071 context to distinguish it from other contexts of the same type,
1072
1073 @example
1074 \new @var{type} = @var{id} @var{music}
1075 @end example
1076
1077 Note the distinction between the name of the context type,
1078 @context{Staff}, @context{Voice}, etc, and
1079 the identifying name of a particular instance of that type,
1080 which can be any sequence of letters invented by the user.
1081 The identifying name is used to refer back to that particular
1082 instance of a context.  We saw this in use in the section on 
1083 lyrics in @ref{Voices and vocals}.
1084
1085
1086 @node Engravers
1087 @subsection Engravers
1088
1089 Every mark on the printed output of a score produced by LilyPond
1090 is produced by an @code{Engraver}.  Thus there is an engraver
1091 to print staves, one to print noteheads, one for stems, one for
1092 beams, etc, etc.  In total there are over 120 such engravers!
1093 Fortunately, for most scores it is not necessary to know about 
1094 more than a few, and for simple scores you do not need to know 
1095 about any.  
1096
1097 Engravers live and operate in Contexts.
1098 Engravers such as the @code{Metronome_mark_engraver}, whose
1099 action and output applies to the score as a whole, operate in
1100 the highest level context - the @context{Score} context.
1101
1102 The @code{Clef_engraver} and @code{Key_engraver} are to be
1103 found in every Staff Context, as different staves may require 
1104 different clefs and keys.
1105
1106 The @code{Note_heads_engraver} and @code{Stem_engraver} live
1107 in each @context{Voice} context, the lowest level context of all.
1108
1109 Each engraver processes the particular objects associated
1110 with its function, and maintains the properties that relate
1111 to that function.  These properties, like the properties
1112 associated with contexts, may be modified to change the
1113 operation of the engraver or the appearance of those elements
1114 in the printed score.
1115    
1116 Engravers all have compound names formed from words which
1117 describe their function.  Just the first word is capitalised, 
1118 and the remainder are joined to it with underscores.  Thus
1119 the @code{Staff_symbol_engraver} is responsible for creating the
1120 lines of the staff, the @code{Clef_engraver} determines and sets
1121 the pitch reference point on the staff by drawing a clef symbol.
1122
1123 Here are some of the most common engravers together with their
1124 function.  You will see it is easy to guess the function from 
1125 the name, or vice versa.
1126
1127 @multitable @columnfractions .3 .7 
1128 @headitem Engraver
1129   @tab Function
1130 @item Accidental_engraver
1131   @tab Makes accidentals, cautionary and suggested accidentals
1132 @item Beam_engraver
1133   @tab Engraves beams
1134 @item Clef_engraver
1135   @tab Engraves clefs
1136 @item Dynamic_engraver
1137   @tab Creates hairpins and dynamic texts
1138 @item Key_engraver
1139   @tab Creates the key signature
1140 @item Metronome_mark_engraver
1141   @tab Engraves metronome marking
1142 @item Note_heads_engraver
1143   @tab Engraves noteheads
1144 @item Rest_engraver
1145   @tab Engraves rests
1146 @item Staff_symbol_engraver
1147   @tab Engraves the five (by default) lines of the staff
1148 @item Stem_engraver
1149   @tab Creates stems and single-stem tremulos
1150 @end multitable
1151
1152 @smallspace
1153
1154 We shall see later how the output of LilyPond can be changed
1155 by modifying the action of Engravers.
1156   
1157
1158 @node Modifying contexts
1159 @subsection Modifying contexts
1160
1161 @cindex context properties
1162 @funindex \set
1163 @funindex \unset
1164
1165 Contexts are responsible for holding the values of a number of
1166 context @emph{properties}.  Many of them can be changed to
1167 influence the interpretation of the input and so change the
1168 appearance of the output.  They are changed by the 
1169 @code{\set} command.  This takes the form
1170
1171 @example
1172 \set @emph{ContextName}.@emph{propertyName} = @emph{value}
1173 @end example
1174
1175 Where the @emph{ContextName} is usually @context{Score},
1176 @context{Staff} or @context{Voice}.  It may be omitted,
1177 in which case @context{Voice} is assumed.
1178
1179 The names of context properties consist of words joined
1180 together with no hyphens or underscores, all except the
1181 first having a capital letter.  Here are a few examples
1182 of some commonly used ones.  There are many more.
1183
1184 @c attempt to force this onto a new page
1185 @need 50
1186 @multitable @columnfractions .2 .2 .6 
1187 @headitem propertyName
1188   @tab Value
1189   @tab Function
1190 @item extraNatural
1191   @tab ##t or ##f
1192   @tab If true (##t), set extra natural signs before accidentals
1193 @item currentBarNumber
1194   @tab Integer
1195   @tab Set the current bar number
1196 @item doubleSlurs
1197   @tab ##t or ##f
1198   @tab If true (##t), print slurs both above and below notes
1199 @item instrumentName
1200   @tab Text
1201   @tab Set the name to be placed at the start of the staff
1202 @item fontSize
1203   @tab Number
1204   @tab Increase or decrease the font size
1205 @item stanza
1206   @tab Text
1207   @tab Set the text to print before the start of a verse
1208 @end multitable
1209
1210 @smallspace   
1211
1212 Before we can set any of these properties we need to know
1213 which context they operate in.  Sometimes this is obvious,
1214 but occasionally it can be tricky.  If the wrong context
1215 is specified, no error message is produced, but the expected
1216 action will not be taken.  For example, the 
1217 @code{instrumentName} clearly lives in the Staff context, since
1218 it is the staff that is to be named.
1219 In this example the first staff is labelled, but the second,
1220 Alto, staff is not, because we omitted the context name.
1221
1222 @lilypond[quote,verbatim,ragged-right]
1223 <<
1224   \new Staff \relative c'' {
1225     \set Staff.instrumentName = "Soprano"
1226     c4 c
1227  }
1228   \new Staff \relative c' {
1229   \set instrumentName = "Alto"
1230   d4 d 
1231  }
1232 >>
1233 @end lilypond
1234
1235 Remember the default context name is Voice, so the second
1236 @code{\set} command set the property @code{instrumentName} in the
1237 Voice context to @qq{Alto}, but as LilyPond does not look
1238 for any such property in the @context{Voice} context, no 
1239 further action took place.  this is not an error, and no error 
1240 message is logged in the log file.
1241
1242 Similarly, if the property name is mis-spelt no error message 
1243 is produced, and clearly the expected action cannot be performed.
1244 If fact, you can set any (fictitious) @q{property} using any 
1245 name you like in any context that exists by using the 
1246 @code{\set} command.  But if the name is not
1247 known to LilyPond it will not cause any action to be taken.
1248
1249 The @code{instrumentName} property will take effect only
1250 if it is set in the @context{Staff} context, but
1251 some properties can be set in more than one context.
1252 For example, the property @code{extraNatural} is by
1253 default set to ##t (true) for all staves.  
1254 If it is set to ##f (false) in the @context{Staff} context 
1255 it applies just to the accidentals on that staff.  
1256 If it is set to false in the @context{Score} context
1257 it applies to all staves.
1258
1259 So this turns of extra naturals in one staff:
1260
1261 @lilypond[quote,verbatim,ragged-right]
1262 <<
1263   \new Staff \relative c'' {
1264     ais4 aes
1265  }
1266   \new Staff \relative c'' {
1267     \set Staff.extraNatural = ##f
1268     ais4 aes
1269  }
1270 >>
1271 @end lilypond
1272
1273 @noindent
1274 and this turns them off in all staves:
1275
1276 @lilypond[quote,verbatim,ragged-right]
1277 <<
1278   \new Staff \relative c'' {
1279     ais4 aes
1280  }
1281   \new Staff \relative c'' {
1282     \set Score.extraNatural = ##f
1283     ais4 aes
1284  }
1285 >>
1286 @end lilypond
1287
1288 The value of every property set in this way can be reset
1289 to its original value with the @code{\unset} command.
1290  
1291 The @code{\set} and @code{\unset} commands can appear anywhere
1292 in the input file and will take effect from the time they are
1293 encountered until the end of the score or until the property is 
1294 @code{\set} or @code{\unset} again.  Let's try changing the 
1295 font size, which affects the size of the note heads (among 
1296 other things) several times.
1297
1298 @lilypond[quote,verbatim,ragged-right,relative=1,fragment]
1299 c4 
1300 \set fontSize = #-4   % make noteheads smaller
1301 d e
1302 \set fontSize = #2.5  % make noteheads larger
1303 f g
1304 \unset fontSize       % return to original size
1305 a b
1306 @end lilypond
1307
1308 We have now seen how to set the values of several different
1309 types of property.  Note that integers and numbers are alway 
1310 preceded by a hash sign, @code{#}, while a true or false value 
1311 is specified by ##t and ##f, with two hash signs.  A text 
1312 property should be enclosed in double quotation signs, as above, 
1313 although we shall see later that text can actually be specified
1314 in a much more general way by using the very powerful 
1315 @code{markup} command. 
1316
1317
1318 @funindex \with
1319
1320 Context properties may also be set at the time the context is
1321 created.  Sometimes this is a clearer way of specifying a 
1322 property value if it is to remain fixed for the duration of
1323 the context.  When a context is created with a @code{\new}
1324 command it may be immediately followed by a 
1325 @code{\with @{ .. @}} block in which the property values are
1326 set.  For example, if we wish to suppress the printing of
1327 extra naturals for the duration of a staff we would write:
1328
1329 @lilypond[quote,verbatim,ragged-right]
1330 \new Staff \with {
1331   extraNatural = ##f
1332 }
1333 \relative c' {
1334   gis ges aes ais
1335 }
1336 @end lilypond
1337
1338 In effect this overrides the default value of the property.  It
1339 may still be changed dynamically using @code{\set} and 
1340 @code{\unset}.
1341
1342
1343 @cindex Engravers, adding
1344 @cindex Engravers, removing
1345
1346 @funindex \consists
1347 @funindex \remove
1348
1349 We have seen that contexts each contain several engravers, each
1350 of which is responsible for producing a particular part of the
1351 output, like barlines, staves, note heads, stems, etc.  If an
1352 engraver is removed from a context it can no longer produce its
1353 output.  This is a crude way of modifying the output, but it
1354 can sometimes be useful.  
1355
1356 To remove an engraver we can use the @code{\with} command placed
1357 immediately after the context creation command, as in the 
1358 previous section.
1359
1360 As an 
1361 illustration let's repeat an example from the previous 
1362 section with the staff lines removed.  Remember that the 
1363 staff lines are produced by the Staff_symbol_engraver.
1364
1365 @lilypond[quote,verbatim,ragged-right]
1366 \new Staff \with {
1367   \remove Staff_symbol_engraver
1368 }
1369 \relative c' {
1370   c4 
1371   \set fontSize = #-4  % make noteheads smaller
1372   d e
1373   \set fontSize = #2.5  % make noteheads larger
1374   f g
1375   \unset fontSize  % return to original size
1376   a b
1377 }
1378 @end lilypond
1379
1380 @cindex ambitus engraver
1381
1382 Engravers can also be added to contexts.  The command
1383 to do this is 
1384
1385 @code{\consists @emph{Engraver_name}},
1386
1387 placed inside a @code{\with} block.  Some vocal scores
1388 have an @rglos{ambitus} placed at the beginning of a
1389 staff to indicate the range of notes in that staff.
1390 The ambitus is produced by the @code{Ambitus_engraver},
1391 which is not normally included in any context.  If
1392 we add it to the @context{Voice} context it calculates
1393 the range from that voice only:
1394
1395 @lilypond[quote,verbatim,ragged-right]
1396 \new Staff <<
1397   \new Voice \with {
1398     \consists Ambitus_engraver
1399   }
1400   \relative c'' { 
1401     \voiceOne
1402     c a b g 
1403   }
1404   \new Voice
1405   \relative c' {
1406     \voiceTwo
1407     c e d f
1408   }
1409 >>
1410 @end lilypond
1411
1412 @noindent
1413 but if we add the Ambitus engraver to the 
1414 @context{Staff} context it calculates the range from all
1415 the notes in all the voices on that staff:
1416
1417 @lilypond[quote,verbatim,ragged-right]
1418 \new Staff \with {
1419     \consists Ambitus_engraver
1420   }
1421   <<
1422   \new Voice
1423   \relative c'' { 
1424     \voiceOne
1425     c a b g 
1426   }
1427   \new Voice
1428   \relative c' {
1429     \voiceTwo
1430     c e d f
1431   }
1432 >>
1433 @end lilypond
1434
1435
1436 @node Extending the templates
1437 @section Extending the templates
1438
1439 You've read the tutorial, you know how to write music, you 
1440 understand the fundamental concepts.  But how can you
1441 get the staves that you want?  Well, you can find lots of 
1442 templates (see @ref{Templates}) which may give you a start.  
1443 But what
1444 if you want something that isn't covered there?  Read on.
1445
1446 @menu
1447 * Soprano and cello::           
1448 * Four-part SATB vocal score::  
1449 * Building a score from scratch::  
1450 @end menu
1451
1452 @node Soprano and cello
1453 @subsection Soprano and cello
1454
1455 Start off with the template that seems closest to what you want to end
1456 up with.  Let's say that you want to write something for soprano and
1457 cello.  In this case, we would start with @q{Notes and lyrics} (for the
1458 soprano part).
1459
1460 @example
1461 \version "2.11.23"
1462 melody = \relative c' @{
1463   \clef treble
1464   \key c \major
1465   \time 4/4
1466
1467   a4 b c d
1468 @}
1469
1470 text = \lyricmode @{
1471   Aaa Bee Cee Dee
1472 @}
1473
1474 \score @{
1475   <<
1476     \new Voice = "one" @{
1477       \autoBeamOff
1478       \melody
1479     @}
1480     \new Lyrics \lyricsto "one" \text
1481   >>
1482   \layout @{ @}
1483   \midi @{ @}
1484 @}
1485 @end example
1486
1487 Now we want to add a cello part.  Let's look at the @q{Notes only} example:
1488
1489 @example
1490 \version "2.11.23"
1491 melody = \relative c' @{
1492   \clef treble
1493   \key c \major
1494   \time 4/4
1495
1496   a4 b c d
1497 @}
1498
1499 \score @{
1500   \new Staff \melody
1501   \layout @{ @}
1502   \midi @{ @}
1503 @}
1504 @end example
1505
1506 We don't need two @code{\version} commands.  We'll need the @code{melody}
1507 section.  We don't want two @code{\score} sections -- if we had two
1508 @code{\score}s, we'd get the two parts separately.  We want them together,
1509 as a duet.  Within the @code{\score} section, we don't need two
1510 @code{\layout} or @code{\midi}.
1511
1512 If we simply cut and paste the @code{melody} section, we would end up with
1513 two @code{melody} sections.  So let's rename them.  We'll call the section
1514 for the soprano @code{sopranoMusic} and the section for the cello
1515 @code{celloMusic}.  While we're doing this, let's rename @code{text}
1516 to be @code{sopranoLyrics}.  Remember to rename both instances of all
1517 these names -- both the initial definition (the
1518 @code{melody = relative c' @{ } part) and the name's use (in the
1519 @code{\score} section).
1520
1521 While we're doing this, let's change the cello part's staff -- celli
1522 normally use bass clef.  We'll also give the cello some different
1523 notes.
1524
1525 @example
1526 \version "2.11.23"
1527 sopranoMusic = \relative c' @{
1528   \clef treble
1529   \key c \major
1530   \time 4/4
1531
1532   a4 b c d
1533 @}
1534
1535 sopranoLyrics = \lyricmode @{
1536   Aaa Bee Cee Dee
1537 @}
1538
1539 celloMusic = \relative c @{
1540   \clef bass
1541   \key c \major
1542   \time 4/4
1543
1544   d4 g fis8 e d4
1545 @}
1546
1547 \score @{
1548   <<
1549     \new Voice = "one" @{
1550       \autoBeamOff
1551       \sopranoMusic
1552     @}
1553     \new Lyrics \lyricsto "one" \sopranoLyrics
1554   >>
1555   \layout @{ @}
1556   \midi @{ @}
1557 @}
1558 @end example
1559
1560 This is looking promising, but the cello part won't appear in the
1561 score -- we haven't used it in the @code{\score} section.  If we
1562 want the cello part to appear under the soprano part, we need to add
1563
1564 @example
1565 \new Staff \celloMusic
1566 @end example
1567
1568 @noindent
1569 underneath the soprano stuff.  We also need to add @code{<<} and
1570 @code{>>} around the music -- that tells LilyPond that there's
1571 more than one thing (in this case, two @code{Staves}) happening 
1572 at once.  The @code{\score} looks like this now
1573
1574 @c Indentation in this example is deliberately poor
1575 @example
1576 \score @{
1577   <<
1578   <<
1579     \new Voice = "one" @{
1580       \autoBeamOff
1581       \sopranoMusic
1582     @}
1583     \new Lyrics \lyricsto "one" \sopranoLyrics
1584   >>
1585   \new Staff \celloMusic
1586   >>
1587   \layout @{ @}
1588   \midi @{ @}
1589 @}
1590 @end example
1591
1592 @noindent
1593 This looks a bit messy; the indentation is messed up now.  That is
1594 easily fixed.  Here's the complete soprano and cello template.
1595
1596 @lilypond[quote,verbatim,ragged-right]
1597 \version "2.11.23"
1598 sopranoMusic = \relative c' {
1599   \clef treble
1600   \key c \major
1601   \time 4/4
1602
1603   a4 b c d
1604 }
1605
1606 sopranoLyrics = \lyricmode {
1607   Aaa Bee Cee Dee
1608 }
1609
1610 celloMusic = \relative c {
1611   \clef bass
1612   \key c \major
1613   \time 4/4
1614
1615   d4 g fis8 e d4
1616 }
1617
1618 \score {
1619   <<
1620     <<
1621       \new Voice = "one" {
1622         \autoBeamOff
1623         \sopranoMusic
1624       }
1625       \new Lyrics \lyricsto "one" \sopranoLyrics
1626     >>
1627     \new Staff \celloMusic
1628   >>
1629   \layout { }
1630   \midi { }
1631 }
1632 @end lilypond
1633
1634
1635 @node Four-part SATB vocal score
1636 @subsection Four-part SATB vocal score
1637
1638 Most vocal scores of music written for four-part mixed choir 
1639 with orchestral accompaniment such as Mendelssohn's Elijah or
1640 Handel's Messiah have the choral music and words on four
1641 staves, one for each of SATB, with a piano reduction of the
1642 orchestral accompaniment underneath.  Here's an example
1643 from Handel's Messiah:
1644
1645 @c The following should appear as music without code
1646 @lilypond[quote,ragged-right]
1647 \version "2.11.23"
1648 global = { \key d \major \time 4/4 }
1649 sopMusic = \relative c'' {
1650   \clef "treble"
1651   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
1652 }
1653 sopWords = \lyricmode {
1654   Wor -- thy is the lamb that was slain
1655 }
1656 altoMusic = \relative a' {
1657   \clef "treble"
1658   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
1659 }
1660 altoWords = \sopWords
1661 tenorMusic = \relative c' {
1662   \clef "G_8"
1663   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
1664 }
1665 tenorWords = \sopWords
1666 bassMusic = \relative c' {
1667   \clef "bass"
1668   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
1669 }
1670 bassWords = \sopWords
1671 upper = \relative a' {
1672   \clef "treble"
1673   \global
1674   r4 <a d fis>2 <a e' a>4 |
1675   <d fis d'>4. <d fis d'>8 <a d a'>2 |
1676   <g cis g'>4 <a d fis> <a cis e>2 |
1677 }
1678 lower = \relative c, {
1679   \clef "bass"
1680   \global
1681   <d d'>4 <d d'>2 <cis cis'>4 |
1682   <b b'>4. <b' b'>8 <fis fis'>2 |
1683   <e e'>4 <d d'> <a' a'>2 |
1684 }
1685
1686 \score {
1687   <<  % combine ChoirStaff and PianoStaff in parallel
1688     \new ChoirStaff <<
1689       \new Staff = "sopranos" <<
1690         \set Staff.instrumentName = "Soprano"
1691         \new Voice = "sopranos" { \global \sopMusic }
1692       >>
1693       \new Lyrics \lyricsto "sopranos" { \sopWords }
1694       \new Staff = "altos" <<
1695         \set Staff.instrumentName = "Alto"
1696         \new Voice = "altos" { \global \altoMusic }
1697       >>
1698       \new Lyrics \lyricsto "altos" { \altoWords }
1699       \new Staff = "tenors" <<
1700         \set Staff.instrumentName = "Tenor"
1701         \new Voice = "tenors" { \global \tenorMusic }
1702       >>
1703       \new Lyrics \lyricsto "tenors" { \tenorWords }
1704       \new Staff = "basses" <<
1705         \set Staff.instrumentName = "Bass"
1706         \new Voice = "basses" { \global \bassMusic }
1707       >>
1708       \new Lyrics \lyricsto "basses" { \bassWords }
1709     >>  % end ChoirStaff
1710
1711     \new PianoStaff <<
1712       \set PianoStaff.instrumentName = "Piano  "
1713       \new Staff = "upper" \upper
1714       \new Staff = "lower" \lower
1715     >>
1716   >>
1717 }
1718 @end lilypond
1719
1720 None of the templates provides this layout exactly.  The
1721 nearest is @q{SATB vocal score and automatic piano reduction},
1722 but we shall need to change the layout and add a piano
1723 accompaniment which is not derived automatically from the
1724 vocal parts.  The variables holding the music and words for
1725 the vocal parts are fine, but we shall need to add variables for
1726 the piano reduction.
1727
1728 The order in which the contexts appear in the ChoirStaff of
1729 the template do not correspond with the order in the vocal 
1730 score shown above.  We need to rearrange them so there are
1731 four staves with the words written directly underneath the
1732 notes for each part.
1733 All the voices should be @code{voiceOne}, which is
1734 the default, so the @code{\voiceXXX} commands can be removed.
1735 We also need to specify the tenor clef for the tenors.
1736 The way in which lyrics are specified has also been simplified
1737 as we have not yet encountered the method used in the template.
1738 We've also added the names of each staff.
1739
1740 Doing this gives for our ChoirStaff:
1741
1742 @example
1743     \new ChoirStaff <<
1744       \new Staff = "sopranos" <<
1745         \set Staff.instrumentName = "Soprano"
1746         \new Voice = "sopranos" @{ \global \sopMusic @}
1747       >>
1748       \new Lyrics \lyricsto "sopranos" @{ \sopWords @}
1749       \new Staff = "altos" <<
1750         \set Staff.instrumentName = "Alto"
1751         \new Voice = "altos" @{ \global \altoMusic @}
1752       >>
1753       \new Lyrics \lyricsto "altos" @{ \altoWords @}
1754       \new Staff = "tenors" <<
1755         \set Staff.instrumentName = "Tenor"
1756         \new Voice = "tenors" @{ \global \tenorMusic @}
1757       >>
1758       \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
1759       \new Staff = "basses" <<
1760         \set Staff.instrumentName = "Bass"
1761         \new Voice = "basses" @{ \global \bassMusic @}
1762       >>
1763       \new Lyrics \lyricsto "basses" @{ \bassWords @}
1764     >>  % end ChoirStaff
1765 @end example
1766
1767 Next we must work out the piano part.  This is
1768 easy - we just pull out the piano part from the
1769 @q{Solo piano} template:
1770
1771 @example
1772 \new PianoStaff <<
1773   \set PianoStaff.instrumentName = "Piano  "
1774   \new Staff = "upper" \upper
1775   \new Staff = "lower" \lower
1776 >>
1777 @end example
1778
1779 and add the variable definitions for @code{upper}
1780 and @code{lower}.
1781
1782 The ChoirStaff and PianoStaff must be combined
1783 using angle brackets as we want them to be
1784 stacked one above the other:
1785
1786 @example
1787 <<  % combine ChoirStaff and PianoStaff one above the other 
1788   \new ChoirStaff <<
1789     \new Staff = "sopranos" <<
1790       \new Voice = "sopranos" @{ \global \sopMusic @}
1791     >>
1792     \new Lyrics \lyricsto "sopranos" @{ \sopWords @}
1793     \new Staff = "altos" <<
1794       \new Voice = "altos" @{ \global \altoMusic @}
1795     >>
1796     \new Lyrics \lyricsto "altos" @{ \altoWords @}
1797     \new Staff = "tenors" <<
1798       \clef "G_8"  % tenor clef
1799       \new Voice = "tenors" @{ \global \tenorMusic @}
1800     >>
1801     \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
1802     \new Staff = "basses" <<
1803       \clef "bass"
1804       \new Voice = "basses" @{ \global \bassMusic @}
1805     >>
1806     \new Lyrics \lyricsto "basses" @{ bassWords @}   
1807   >>  % end ChoirStaff
1808
1809   \new PianoStaff <<
1810     \set PianoStaff.instrumentName = "Piano  "
1811     \new Staff = "upper" \upper
1812     \new Staff = "lower" \lower
1813   >>
1814 >>
1815 @end example
1816
1817 Combining all these together and adding the music
1818 for the three bars of the example above gives:
1819
1820 @lilypond[quote,verbatim,ragged-right]
1821 \version "2.11.23"
1822 global = { \key d \major \time 4/4 }
1823 sopMusic = \relative c'' {
1824   \clef "treble"
1825   r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
1826 }
1827 sopWords = \lyricmode {
1828   Wor -- thy is the lamb that was slain
1829 }
1830 altoMusic = \relative a' {
1831   \clef "treble"
1832   r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
1833 }
1834 altoWords = \sopWords
1835 tenorMusic = \relative c' {
1836   \clef "G_8"
1837   r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
1838 }
1839 tenorWords = \sopWords
1840 bassMusic = \relative c' {
1841   \clef "bass"
1842   r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
1843 }
1844 bassWords = \sopWords
1845 upper = \relative a' {
1846   \clef "treble"
1847   \global
1848   r4 <a d fis>2 <a e' a>4 |
1849   <d fis d'>4. <d fis d'>8 <a d a'>2 |
1850   <g cis g'>4 <a d fis> <a cis e>2 |
1851 }
1852 lower = \relative c, {
1853   \clef "bass"
1854   \global
1855   <d d'>4 <d d'>2 <cis cis'>4 |
1856   <b b'>4. <b' b'>8 <fis fis'>2 |
1857   <e e'>4 <d d'> <a' a'>2 |
1858 }
1859
1860 \score {
1861   <<  % combine ChoirStaff and PianoStaff in parallel
1862     \new ChoirStaff <<
1863       \new Staff = "sopranos" <<
1864         \set Staff.instrumentName = "Soprano"
1865         \new Voice = "sopranos" { \global \sopMusic }
1866       >>
1867       \new Lyrics \lyricsto "sopranos" { \sopWords }
1868       \new Staff = "altos" <<
1869         \set Staff.instrumentName = "Alto"
1870         \new Voice = "altos" { \global \altoMusic }
1871       >>
1872       \new Lyrics \lyricsto "altos" { \altoWords }
1873       \new Staff = "tenors" <<
1874         \set Staff.instrumentName = "Tenor"
1875         \new Voice = "tenors" { \global \tenorMusic }
1876       >>
1877       \new Lyrics \lyricsto "tenors" { \tenorWords }
1878       \new Staff = "basses" <<
1879         \set Staff.instrumentName = "Bass"
1880         \new Voice = "basses" { \global \bassMusic }
1881       >>
1882       \new Lyrics \lyricsto "basses" { \bassWords }
1883     >>  % end ChoirStaff
1884
1885     \new PianoStaff <<
1886       \set PianoStaff.instrumentName = "Piano  "
1887       \new Staff = "upper" \upper
1888       \new Staff = "lower" \lower
1889     >>
1890   >>
1891 }
1892 @end lilypond
1893   
1894
1895 @node Building a score from scratch
1896 @subsection Building a score from scratch
1897
1898 After gaining some facility with writing LilyPond code you
1899 may find that it is easier to build a score from scratch
1900 rather than modifying one of the templates.  You can also
1901 develop your own style this way to suit the sort of music you
1902 like.  Let's see how to put together the score for an organ 
1903 prelude as an example.
1904
1905 We begin with a header section.  Here go the title, name
1906 of composer, etc, then come any variable definitions, and
1907 finally the score block.  Let's start with these in outline
1908 and fill in the details later.
1909
1910 We'll use the first two bars of Bach's prelude
1911 based on @emph{Jesu, meine Freude} which is written for two
1912 manuals and pedal organ.  The top manual part has two voices,
1913 the lower and pedal organ one each.  So we need four
1914 music definitions and one to define the time signature
1915 and key:
1916
1917 @example
1918 \version "2.11.23"
1919 \header @{
1920   title = "Jesu, meine Freude"
1921   composer = "J S Bach"
1922 @}
1923 TimeKey = @{ \time 4/4 \key c \minor @}
1924 ManualOneVoiceOneMusic = @{s1@}
1925 ManualOneVoiceTwoMusic = @{s1@}
1926 ManualTwoMusic = @{s1@}
1927 PedalOrganMusic = @{s1@}
1928
1929 \score @{
1930 @}
1931 @end example
1932
1933 For now we've just used a spacer note, @code{s1},
1934 instead of the real music.  We'll add that later.
1935
1936 Next let's see what should go in the score block.
1937 We simply mirror the staff structure we want.
1938 Organ music is usually written on three staves,
1939 one for each manual and one for the pedals.  The
1940 manual staves should be bracketed together so we
1941 need to use a PianoStaff for them.  The first
1942 manual part needs two voices and the second manual
1943 part just one.
1944
1945 @example
1946   \new PianoStaff <<
1947     \new Staff = "ManualOne" <<
1948       \new Voice @{ \ManualOneVoiceOneMusic @}
1949       \new Voice @{ \ManualOneVoiceTwoMusic @}
1950     >>  % end ManualOne Staff context
1951     \new Staff = "ManualTwo" <<
1952       \new Voice @{ \ManualTwoMusic @}
1953     >>  % end ManualTwo Staff context
1954   >>  % end PianoStaff context
1955 @end example
1956
1957 Next we need to add a staff for the pedal organ.
1958 This goes underneath the PianoStaff, but it must
1959 be simultaneous with it, so we need angle brackets
1960 round the two.  Missing these out would generate
1961 an error in the log file.  It's a common mistake 
1962 which you'll make sooner or later!  Try copying
1963 the final example at the end of this section,
1964 remove these angle brackets, and compile it to
1965 see what errors it generates.
1966
1967 @example
1968 <<  % PianoStaff and Pedal Staff must be simultaneous
1969   \new PianoStaff <<
1970     \new Staff = "ManualOne" <<
1971       \new Voice @{ \ManualOneVoiceOneMusic @}
1972       \new Voice @{ \ManualOneVoiceTwoMusic @}
1973     >>  % end ManualOne Staff context
1974     \new Staff = "ManualTwo" <<
1975       \new Voice @{ \ManualTwoMusic @}
1976     >>  % end ManualTwo Staff context
1977   >>  % end PianoStaff context
1978   \new Staff = "PedalOrgan" <<
1979     \new Voice @{ \PedalOrganMusic @}
1980   >>
1981 >>
1982 @end example
1983
1984 It is not strictly necessary to use the simultaneous construct
1985 @code{<<  >>} for the manual two staff and the pedal organ staff,
1986 since they contain only one music expression, but it does no harm
1987 and always using angle brackets after @code{\new Staff} is a good
1988 habit to cultivate in case there are multiple voices.  
1989
1990 Let's add this structure to the score block, and adjust the
1991 indenting.  We also add the appropriate clefs, ensure the
1992 second voice stems point down with @code{\voiceTwo} and
1993 enter the time signature and key to each staff using our
1994 predefined variable, @code{\TimeKey}.
1995
1996 @example
1997 \score @{
1998   <<  % PianoStaff and Pedal Staff must be simultaneous
1999     \new PianoStaff <<
2000       \new Staff = "ManualOne" <<
2001         \TimeKey  % set time signature and key
2002         \clef "treble"
2003         \new Voice @{ \ManualOneVoiceOneMusic @}
2004         \new Voice @{ \voiceTwo \ManualOneVoiceTwoMusic @}
2005       >>  % end ManualOne Staff context
2006       \new Staff = "ManualTwo" <<
2007         \TimeKey
2008         \clef "bass"
2009         \new Voice @{ \ManualTwoMusic @}
2010       >>  % end ManualTwo Staff context
2011     >>  % end PianoStaff context
2012     \new Staff = "PedalOrgan" <<
2013       \TimeKey
2014       \clef "bass"
2015       \new Voice @{ \PedalOrganMusic @}
2016     >>  % end PedalOrgan Staff
2017   >>
2018 @}  % end Score context
2019 @end example
2020
2021 That completes the structure.  Any three-staff organ music
2022 will have a similar structure, although the number of voices
2023 may vary.  All that remains now 
2024 is to add the music, and combine all the parts together.
2025
2026 @lilypond[quote,verbatim,ragged-right]
2027 \version "2.11.23"
2028 \header {
2029   title = "Jesu, meine Freude"
2030   composer = "J S Bach"
2031 }
2032 TimeKey = { \time 4/4 \key c \minor }
2033 ManualOneVoiceOneMusic = \relative g' {
2034   g4 g f ees | d2 c2 |
2035 }
2036 ManualOneVoiceTwoMusic = \relative c' {
2037   ees16 d ees8~ ees16 f ees s c8 d~ d c~ |
2038   c c4 b8 c8. g16 c b c d |
2039 }
2040 ManualTwoMusic = \relative c' {
2041   c16 b c8~ c16 b c g a8 g~ g16 g aes ees |
2042   f ees f d g aes g f ees d e8~ ees16 f ees d |
2043 }
2044 PedalOrganMusic = \relative c {
2045   r8 c16 d ees d ees8~ ees16 a, b g c b c8 |
2046   r16 g ees f g f g8 c,2 |
2047   }
2048
2049 \score {
2050   <<  % PianoStaff and Pedal Staff must be simultaneous
2051     \new PianoStaff <<
2052       \new Staff = "ManualOne" <<
2053         \TimeKey  % set time signature and key
2054         \clef "treble"
2055         \new Voice { \ManualOneVoiceOneMusic }
2056         \new Voice { \voiceTwo \ManualOneVoiceTwoMusic }
2057       >>  % end ManualOne Staff context
2058       \new Staff = "ManualTwo" <<
2059         \TimeKey
2060         \clef "bass"
2061         \new Voice { \ManualTwoMusic }
2062       >>  % end ManualTwo Staff context
2063     >>  % end PianoStaff context
2064     \new Staff = "PedalOrgan" <<
2065       \TimeKey
2066       \clef "bass"
2067       \new Voice { \PedalOrganMusic }
2068     >>  % end PedalOrgan Staff
2069   >>
2070 }  % end Score context
2071 @end lilypond
2072
2073