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