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