1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
6 When revising a translation, copy the HEAD committish of the
7 version that you are working on. See TRANSLATION for details.
12 @node Fundamental concepts
13 @chapter Fundamental concepts
15 You've seen in the Tutorial how to produce beautifully printed
16 music from a simple text file. This section introduces the
17 concepts and techniques required to produce equally beautiful
18 but more complex scores.
21 * How LilyPond input files work::
22 * Voices contain music::
23 * Contexts and engravers::
24 * Extending the templates::
28 @node How LilyPond input files work
29 @section How LilyPond input files work
31 The LilyPond input format is quite free-form, giving experienced
32 users a lot of flexibility to structure their files however they
33 wish. But this flexibility can make things confusing for new
34 users. This section will explain some of this structure, but may
35 gloss over some details in favor of simplicity. For a complete
36 description of the input format, see @ruser{File structure}.
39 * Introduction to the LilyPond file structure::
40 * Score is a (single) compound musical expression::
41 * Nesting music expressions::
42 * On the un-nestedness of brackets and ties::
45 @node Introduction to the LilyPond file structure
46 @subsection Introduction to the LilyPond file structure
49 @cindex file structure
51 A basic example of a LilyPond input file is
54 \version @w{"@version{}"}
57 @var{...compound music expression...} % all the music goes here!
64 There are many variations of this basic pattern, but this
65 example serves as a useful starting place.
72 Up to this point none of the examples you have seen has used a
73 @code{\score@{@}} command. This is because LilyPond automatically
74 adds the extra commands which are needed when you give it simple
75 input. LilyPond treats input like this:
84 as shorthand for this:
101 In other words, if the input contains a single music expression,
102 LilyPond will interpret the file as though the music expression
103 was wrapped up inside the commands shown above.
105 @cindex implicit contexts
107 @strong{A word of warning!} Many of the examples in the LilyPond
108 documentation will omit the @code{\new Staff} and @code{\new Voice}
109 commands, leaving them to be created implicitly. For simple
110 examples this works well, but for more complex examples, especially
111 when additional commands are used, the implicit creation of contexts
112 can give surprising results, maybe creating extra unwanted staves.
113 The way to create contexts explicitly is explained in
114 @ref{Contexts and engravers}.
116 @warning{When entering more than a few lines of music it is
117 advisable to always create staves and voices explicitly.}
119 For now, though, let us return to the first example and examine the
120 @code{\score} command, leaving the others to default.
122 A @code{\score} block must always contain just one music expression,
123 and this must appear immediately after the @code{\score} command.
124 Remember that a music expression could be anything from a single
125 note to a huge compound expression like
130 @var{...insert the whole score of a Wagner opera in here...}
136 Since everything is inside @code{@{ ... @}}, it counts
137 as one music expression.
139 As we saw previously, the @code{\score} block can contain other
159 Note that these three commands -- @code{\header}, @code{\layout} and
160 @code{\midi} -- are special: unlike many other commands which begin
161 with a backward slash (@code{\}) they are @emph{not} music expressions
162 and are not part of any music expression. So they may be placed
163 inside a @code{\score} block or outside it. In fact, these commands
164 are commonly placed outside the @code{\score} block -- for example,
165 @code{\header} is often placed above the @code{\score} command, as the
166 example at the beginning of this section shows.
168 Two more commands you have not previously seen are
169 @code{\layout @{ @}} and @code{\midi @{@}}. If these appear as
170 shown they will cause LilyPond to produce a printed output and a
171 MIDI output respectively. They are described fully in the
172 Notation Reference -- @ruser{Score layout}, and
173 @ruser{Creating MIDI files}.
175 @cindex scores, multiple
177 You may code multiple @code{\score} blocks. Each will be
178 treated as a separate score, but they will be all combined into
179 a single output file. A @code{\book} command is not necessary
180 -- one will be implicitly created. However, if you would like
181 separate output files from one @code{.ly} file then the
182 @code{\book} command should be used to separate the different
183 sections: each @code{\book} block will produce a
184 separate output file.
188 Every @code{\book} block creates a separate output file (e.g., a
189 PDF file). If you haven't explicitly added one, LilyPond wraps
190 your entire input code in a @code{\book} block implicitly.
192 Every @code{\score} block is a separate chunk of music within a
195 Every @code{\layout} block affects the @code{\score} or
196 @code{\book} block in which it appears -- i.e., a @code{\layout}
197 block inside a @code{\score} block affects only that @code{\score}
198 block, but a @code{\layout} block outside of a @code{\score} block
199 (and thus in a @code{\book} block, either explicitly or
200 implicitly) will affect every @code{\score} in that @code{\book}.
202 Every @code{\context} block will affect the named context (e.g.,
203 @code{\StaffGroup}) throughout the block (@code{\score} or
204 @code{\book}) in which it appears.
206 For details see @ruser{Multiple scores in a book}.
210 Another great shorthand is the ability to define variables. All
211 the templates use this
214 melody = \relative c' @{
223 When LilyPond looks at this file, it takes the value of
224 @code{melody} (everything after the equals sign) and inserts it
225 whenever it sees @code{\melody}. There's nothing special about
226 the names -- it could be @code{melody}, @code{global},
228 @code{pianorighthand}, or @code{foofoobarbaz}. For more details,
229 see @ref{Saving typing with variables and functions}.
230 Remember that you can use almost any name you like as long
231 as it contains just alphabetic characters and is distinct from
232 LilyPond command names. The exact
233 limitations on variable names are detailed in
234 @ruser{File structure}.
240 For a complete definition of the input format, see
241 @ruser{File structure}.
243 @node Score is a (single) compound musical expression
244 @subsection Score is a (single) compound musical expression
248 @cindex Compound music expression
249 @cindex Music expression, compound
251 We saw the general organization of LilyPond input files in the
252 previous section, @ref{How LilyPond input files work}. But we seemed to
253 skip over the most important part: how do we figure out what to
254 write after @code{\score}?
256 We didn't skip over it at all. The big mystery is simply that
257 there @emph{is} no mystery. This line explains it all:
260 @emph{A @code{\score} block must begin with a compound music expression.}
264 To understand what is meant by a music expression and a compound
265 music expression, you may find it useful to review the tutorial,
266 @ref{Music expressions explained}. In that section, we saw how to
267 build big music expressions from small pieces -- we started from
268 notes, then chords, etc. Now we're going to start from a big
269 music expression and work our way down.
273 @{ % this brace begins the overall compound music expression
275 @var{...insert the whole score of a Wagner opera in here...}
277 @} % this brace ends the overall compound music expression
282 A whole Wagner opera would easily double the length of this
283 manual, so let's just add a singer and piano. We don't need a
284 @code{GrandStaff} for this ensemble, which simply groups a number
285 of staves together with a brace at the left, so we shall remove
286 it. We @emph{do} need a singer and a piano, though.
291 \new Staff = "singer" <<
293 \new PianoStaff = piano <<
300 Remember that we use @code{<< ... >>} instead of @code{@{ ... @}} to
301 show simultaneous music. And we definitely want to show the vocal
302 part and piano part at the same time, not one after the other! Note
303 that the @code{<< ... >>} construct is not really necessary for the
304 Singer staff, as it contains only one sequential music expression;
305 however, using @code{<< ... >>} instead of braces is still necessary
306 if the music in the Staff is made of two simultaneous expressions,
307 e.g. two simultaneous Voices, or a Voice with lyrics. We'll add some
308 real music later; for now let's just put in some dummy notes and
311 @lilypond[verbatim,quote,ragged-right]
314 \new Staff = "singer" <<
315 \new Voice = "vocal" { c'1 }
318 \new PianoStaff = "piano" <<
319 \new Staff = "upper" { c'1 }
320 \new Staff = "lower" { c'1 }
327 Now we have a lot more details. We have the singer's staff: it
328 contains a @code{Voice} (in LilyPond, this term refers to a set of
329 notes, not necessarily vocal notes -- for example, a violin
330 generally plays one voice) and some lyrics. We also have a piano
331 staff: it contains an upper staff (right hand) and a lower staff
334 At this stage, we could start filling in notes. Inside the curly
335 braces next to @code{\new Voice = vocal}, we could start writing
343 But if we did that, the @code{\score} section would get pretty
344 long, and it would be harder to understand what was happening. So
345 let's use variables instead. These were introduced at the end
346 of the previous section, remember? So, adding a few notes, we
347 now have a piece of real music:
349 @lilypond[verbatim,quote,ragged-right]
350 melody = \relative c'' { r4 d8\noBeam g, c4 r }
351 text = \lyricmode { And God said, }
352 upper = \relative c'' { <g d g,>2~ <g d g,> }
353 lower = \relative c { b2 e2 }
357 \new Staff = "singer" <<
358 \new Voice = "vocal" { \melody }
361 \new PianoStaff = "piano" <<
362 \new Staff = "upper" { \upper }
363 \new Staff = "lower" {
374 Be careful about the difference between notes, which are introduced
375 with @code{\relative} or which are directly included in a music
376 expression, and lyrics, which are introduced with
377 @code{\lyricmode}. These are essential to tell LilyPond
378 to interpret the following content as music and text
381 When writing (or reading) a @code{\score} section, just take it
382 slowly and carefully. Start with the outer level, then work on
383 each smaller level. It also really helps to be strict with
384 indentation -- make sure that each item on the same level starts
385 on the same horizontal position in your text editor.
388 @node Nesting music expressions
389 @subsection Nesting music expressions
391 @cindex staves, temporary
394 It is not essential to declare all staves at the beginning; they may
395 be introduced temporarily at any point. This is particularly useful
396 for creating ossia sections -- see @rglos{ossia}. Here is a simple
397 example showing how to introduce a new staff temporarily for the
398 duration of three notes:
400 @lilypond[verbatim,quote,ragged-right]
417 Note that the size of the clef is the same as a clef printed
418 following a clef change -- slightly smaller than the clef
419 at the beginning of the line. This is usual for clefs printed
420 in the middle of a line.
422 @cindex staff, positioning
424 The ossia section may be placed above the staff
427 @lilypond[verbatim,quote,ragged-right]
435 alignAboveContext = "main" }
443 This example uses @code{\with}, which will be explained more
444 fully later. It is a means of modifying the default behavior
445 of a single Staff. Here it says that the new staff should be
446 placed above the staff called @qq{main} instead of the default
447 position which is below.
449 Ossia are often written without clef and without
450 time signature and are usually in a smaller font.
451 These require further commands which
452 have not yet been introduced. See @ref{Size of objects}.
454 @node On the un-nestedness of brackets and ties
455 @subsection On the un-nestedness of brackets and ties
457 @cindex brackets, nesting
459 You have already met a number of different types of bracket in
460 writing the input file to LilyPond. These obey different rules
461 which can be confusing at first. Before we explain the rules
462 let's first review the different types of bracket.
464 @c attempt to force this onto a new page
466 @multitable @columnfractions .3 .7
467 @headitem Bracket Type
469 @item @code{@{ .. @}}
470 @tab Encloses a sequential segment of music
472 @tab Encloses the notes of a chord
473 @item @code{<< .. >>}
474 @tab Encloses simultaneous music expressions
476 @tab Marks the start and end of a slur
477 @item @code{\( .. \)}
478 @tab Marks the start and end of a phrasing slur
480 @tab Marks the start and end of a manual beam
483 To these we should add other constructs which generate lines
484 between or across notes: ties (marked by a tilde, @code{~}),
485 tuplets written as @code{\times x/y @{..@}}, and grace notes
486 written as @code{\grace@{..@}}.
488 Outside LilyPond, the conventional use of brackets requires
489 the different types to be properly nested, like this,
490 @code{<< [ @{ ( .. ) @} ] >>}, with the closing brackets being
491 encountered in exactly the opposite order to the opening
492 brackets. This @strong{is} a requirement for the three types of
493 bracket described by the word @q{Encloses} in the table above --
494 they must nest properly.
495 However, the remaining brackets, described with the word
496 @q{Marks} in the table above together with ties and tuplets,
497 do @strong{not} have to nest
498 properly with any of the brackets. In fact, these are not
499 brackets in the sense that
500 they enclose something -- they are simply markers to indicate
501 where something starts and ends.
503 So, for example, a phrasing slur can start before a manually
504 inserted beam and end before the end of the beam -- not very
505 musical, perhaps, but possible:
507 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
508 { g8\( a b[ c b\) a] }
511 In general, different kinds of brackets, and those implied by
512 tuplets, ties and grace notes, may be mixed freely.
513 This example shows a beam extending into a tuplet (line 1),
514 a slur extending into a tuplet (line 2),
515 a beam and a slur extending into a tuplet, a tie crossing
516 two tuplets, and a phrasing slur extending out of a tuplet
519 @lilypond[quote,verbatim,fragment,ragged-right]
521 r16[ g16 \times 2/3 {r16 e'8] }
522 g16( a \times 2/3 {b d) e' }
523 g8[( a \times 2/3 {b d') e'~]}
524 \times 4/5 {e'32\( a b d' e'} a'4.\)
529 @node Voices contain music
530 @section Voices contain music
532 Singers need voices to sing, and so does LilyPond.
533 The actual music for all instruments in a score
534 is contained in Voices -- the most fundamental
535 of all LilyPond's concepts.
538 * I'm hearing Voices::
539 * Explicitly instantiating voices::
540 * Voices and vocals::
543 @node I'm hearing Voices
544 @subsection I'm hearing Voices
548 @cindex multiple voices
549 @cindex Voice context
550 @cindex context, Voice
551 @cindex simultaneous music
552 @cindex concurrent music
554 The lowest, most fundamental or innermost layers in a LilyPond
555 score are called @q{Voice contexts} or just @q{Voices} for short.
556 Voices are sometimes called @q{layers} in other notation
559 In fact, a Voice layer or context is the only one which can
560 contain music. If a Voice context is not explicitly declared
561 one is created automatically, as we saw at the beginning of
562 this chapter. Some instruments such as an
563 Oboe can play only one note at a time. Music written for
564 such instruments is monophonic and requires just a single
565 voice. Instruments which can play more than one note at a
566 time like the piano will often require multiple voices to
567 encode the different concurrent notes and rhythms they are
570 A single voice can contain many notes in a chord, of course,
571 so when exactly are multiple voices needed? Look first at
572 this example of four chords:
574 @lilypond[quote,verbatim,fragment,ragged-right,relative=1]
576 <d g>4 <d fis> <d a'> <d g>
579 This can be expressed using just the single angle bracket chord
580 symbols, @code{< ... >}, and for this just a single voice is
581 needed. But suppose the F-sharp were actually an eighth-note
582 followed by an eighth-note G, a passing note on the way to the A?
583 Now we have two notes which start at the same time but have
584 different durations: the quarter-note D and the eighth-note
585 F-sharp. How are these to be coded? They cannot be written as
586 a chord because all the notes in a chord must have the same
587 duration. And they cannot be written as two sequential notes
588 as they need to start at the same time. This is when two
591 Let us see how this is done in LilyPond input syntax.
596 The easiest way to enter fragments with more than one voice on a
597 staff is to enter each voice as a sequence (with @code{@{...@}}),
598 and combine them simultaneously with angle brackets, @code{<<...>>}.
599 The fragments must also be separated with double backward slashes,
600 @code{\\}, to place them in separate voices. Without these, the
601 notes would be entered into a single voice, which would usually
602 cause errors. This technique is particularly suited to pieces of
603 music which are largely monophonic with occasional short sections
606 Here's how we split the chords above into two voices and add both
607 the passing note and a slur:
609 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
611 % Voice "1" Voice "2"
612 << { g4 fis8( g) a4 g } \\ { d4 d d d } >> |
615 Notice how the stems of the second voice now point down.
617 Here's another simple example:
619 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
621 % Voice "1" Voice "2"
622 << { r4 g g4. a8 } \\ { d,2 d4 g } >> |
623 << { bes4 bes c bes } \\ { g4 g g8( a) g4 } >> |
624 << { a2. r4 } \\ { fis2. s4 } >> |
627 It is not necessary to use a separate @code{<< \\ >>} construct
628 for each bar. For music with few notes in each bar this layout
629 can help the legibility of the code, but if there are many
630 notes in each bar it may be better to split out each voice
631 separately, like this:
633 @lilypond[quote,verbatim,fragment,ragged-right,relative=2]
649 This example has just two voices, but the same construct may be
650 used to encode three or more voices by adding more back-slash
653 The Voice contexts bear the names @code{"1"}, @code{"2"}, etc.
654 In each of these contexts, the vertical direction of slurs,
655 stems, ties, dynamics etc., is set appropriately.
657 @lilypond[quote,verbatim,fragment]
658 \new Staff \relative c' {
661 % Voice "1" Voice "2" Voice "3"
662 << { g4 f e } \\ { r8 e4 d c8 ~ } >> |
663 << { d2 e2 } \\ { c8 b16 a b8 g ~ g2 } \\ { s4 b4 c2 } >> |
667 These voices are all separate from the main voice that contains
668 the notes just outside the @code{<< .. >>} construct. Let's call
669 this the @emph{simultaneous construct}. Slurs and ties may only
670 connect notes within the same voice, so slurs and ties cannot go
671 into or out of a simultaneous construct. Conversely,
672 parallel voices from separate simultaneous constructs on the same
673 staff are the same voice. Other voice-related properties also
674 carry across simultaneous constructs. Here is the same example,
675 with different colors and note heads for each voice. Note that
676 changes in one voice do not affect other voices, but they do
677 persist in the same voice later. Note also that tied notes may be
678 split across the same voices in two constructs, shown here in the
681 @lilypond[quote,verbatim]
682 \new Staff \relative c' {
701 { c8 b16 a b8 g ~ g2 }
711 @funindex \voiceOneStyle
712 @funindex \voiceTwoStyle
713 @funindex \voiceThreeStyle
714 @funindex \voiceFourStyle
715 @funindex \voiceNeutralStyle
717 The commands @code{\voiceXXXStyle} are mainly intended for use in
718 educational documents such as this one. They modify the color
719 of the note head, the stem and the beams, and the style of the
720 note head, so that the voices may be easily distinguished.
721 Voice one is set to red diamonds, voice two to blue triangles,
722 voice three to green crossed circles, and voice four (not used
723 here) to magenta crosses; @code{\voiceNeutralStyle} (also not
724 used here) reverts the style back to the default.
725 We shall see later how commands like these may be created by the
727 See @ref{Visibility and color of objects} and
728 @ref{Using variables for tweaks}.
730 Polyphony does not change the relationship of notes within a
731 @code{\relative @{ @}} block. Each note is still calculated
732 relative to the note immediately preceding it, or to the first
733 note of the preceding chord. So in
736 \relative c' @{ noteA << < noteB noteC > \\ noteD >> noteE @}
740 @code{noteB} is relative to @code{noteA} @*
741 @code{noteC} is relative to @code{noteB}, not @code{noteA}; @*
742 @code{noteD} is relative to @code{noteB}, not @code{noteA} or
744 @code{noteE} is relative to @code{noteD}, not @code{noteA}.
746 An alternative way, which may be clearer if the notes in the
747 voices are widely separated, is to place a @code{\relative}
748 command at the start of each voice:
751 \relative c' @{ noteA ... @}
753 \relative c'' @{ < noteB noteC > ... @}
755 \relative g' @{ noteD ... @}
757 \relative c' @{ noteE ... @}
760 Let us finally analyze the voices in a more complex piece of
762 the notes from the first two bars of the second of Chopin's
763 Deux Nocturnes, Op 32. This example will be used at later
764 stages in this and the next chapter to illustrate several
765 techniques for producing notation, so please ignore for now
766 anything in the underlying code which looks mysterious and
767 concentrate just on the music and the voices -- the
768 complications will all be explained in later sections.
770 @c The following should appear as music without code
771 @lilypond[quote,ragged-right]
772 \new Staff \relative c'' {
781 % Ignore these for now - they are explained in Ch 4
782 \once \override NoteColumn #'force-hshift = #0
784 \once \override NoteColumn #'force-hshift = #0.5
792 The direction of the stems is often used to indicate the
793 continuity of two simultaneous melodic lines. Here the
794 stems of the highest notes are all pointing up and the
795 stems of the lower notes are all pointing down.
796 This is the first indication that more than one voice
799 But the real need for multiple voices arises when notes
800 which start at the same time have different durations.
801 Look at the notes which start at beat three in the first
802 bar. The A-flat is a dotted quarter note, the F is a
803 quarter note and the D-flat is a half note. These
804 cannot be written as a chord as all the notes in a chord
805 must have the same duration. Neither can they be written
806 as sequential notes, as they must start at the same time.
807 This section of the bar requires three voices, and the
808 normal practice would be to write the whole bar as three
809 voices, as shown below, where we have used different note heads
810 and colors for the three voices. Again, the code behind this
811 example will be explained later, so ignore anything you do
814 @c The following should appear as music without code
815 @c The three voice styles should be defined in -init
816 @lilypond[quote,ragged-right]
817 \new Staff \relative c'' {
828 \\ % No Voice three (we want stems down)
831 % Ignore these for now - they are explained in Ch 4
832 \once \override NoteColumn #'force-hshift = #0
834 \once \override NoteColumn #'force-hshift = #0.5
843 Let us try to encode this music from scratch. As we
844 shall see, this encounters some difficulties. We begin as
845 we have learnt, using the @code{<< \\ >>} construct to
846 enter the music of the first bar in three voices:
848 @lilypond[quote,verbatim,fragment,ragged-right]
849 \new Staff \relative c'' {
852 { c2 aes4. bes8 } \\ { aes2 f4 fes } \\ { <ees c>2 des2 }
861 The stem directions are automatically assigned with the
862 odd-numbered voices taking upward stems and the even-numbered
863 voices downward ones. The stems for voices 1 and 2 are right,
864 but the stems in voice 3 should go down in this particular piece
865 of music. We can correct this simply by missing out voice three
866 and placing the music in voice four:
868 @lilypond[quote,verbatim,fragment,ragged-right]
869 \new Staff \relative c'' {
875 \\ % Omit Voice three
884 We see that this fixes the stem direction, but exposes a problem
885 sometimes encountered with multiple voices -- the stems of the notes
886 in one voice can collide with the note heads in other voices. In
887 laying out the notes, LilyPond allows the notes or chords from two
888 voices to occupy the same vertical note column provided the stems are
889 in opposite directions, but the notes from the third and fourth voices
890 are displaced, if necessary, to avoid the note heads colliding. This
891 usually works well, but in this example the notes of the lowest voice
892 are clearly not well placed by default. LilyPond provides several ways
893 to adjust the horizontal placing of notes. We are not quite ready yet
894 to see how to correct this, so we shall leave this problem until a
895 later section --- see the @code{force-hshift} property in @ref{Fixing
896 overlapping notation}.
899 @node Explicitly instantiating voices
900 @subsection Explicitly instantiating voices
904 @funindex \voiceThree
908 @cindex Voice contexts, creating
910 Voice contexts can also be created manually
911 inside a @code{<< >>} block to create polyphonic music, using
912 @code{\voiceOne} ... @code{\voiceFour} to indicate the required
913 directions of stems, slurs, etc. In longer scores this method
914 is clearer, as it permits the voices to be separated and to be
915 given more descriptive names.
917 Specifically, the construct @code{<< \\ >>} which we used in
918 the previous section:
923 << @{ e4 f g a @} \\ @{ c,4 d e f @} >>
933 \new Voice = "1" @{ \voiceOne \relative c' @{ e4 f g a @} @}
934 \new Voice = "2" @{ \voiceTwo \relative c' @{ c4 d e f @} @}
938 Both of the above would produce
940 @c The following example should not display the code
941 @lilypond[ragged-right,quote]
943 \new Voice = "1" { \voiceOne \relative c' { e4 f g a } }
944 \new Voice = "2" { \voiceTwo \relative c' { c4 d e f } }
948 The @code{\voiceXXX} commands set the direction of stems, slurs,
949 ties, articulations, text annotations, augmentation dots of dotted
950 notes, and fingerings. @code{\voiceOne} and @code{\voiceThree}
951 make these objects point upwards, while @code{\voiceTwo} and
952 @code{\voiceFour} make them point downwards. These commands also
953 generate a horizontal shift for each voice when this is required
954 to avoid clashes of note heads. The command @code{\oneVoice}
955 reverts the settings back to the normal values for a single voice.
957 Let us see in some simple examples exactly what effect
958 @code{\oneVoice}, @code{\voiceOne} and @code{voiceTwo} have on
959 markup, ties, slurs, and dynamics:
961 @lilypond[quote,ragged-right,verbatim]
963 % Default behavior or behavior after \oneVoice
964 c d8 ~ d e4 ( f g a ) b-> c
968 @lilypond[quote,ragged-right,verbatim]
971 c d8 ~ d e4 ( f g a ) b-> c
973 c, d8 ~ d e4 ( f g a ) b-> c
977 @lilypond[quote,ragged-right,verbatim]
980 c d8 ~ d e4 ( f g a ) b-> c
982 c, d8 ~ d e4 ( f g a ) b-> c
986 Now let's look at three different ways to notate the same passage
987 of polyphonic music, each of which is advantageous in different
988 circumstances, using the example from the previous section.
990 An expression that appears directly inside a @code{<< >>} belongs
991 to the main voice (but, note, @strong{not} in a @code{<< \\ >>}
992 construct). This is useful when extra voices appear while the
993 main voice is playing. Here is a more correct rendition of our
994 example. The red diamond-shaped notes
995 demonstrate that the main melody is now in a single voice context,
996 permitting a phrasing slur to be drawn over them.
998 @lilypond[quote,ragged-right,verbatim]
999 \new Staff \relative c' {
1001 % The following notes are monophonic
1003 % Start simultaneous section of three voices
1005 % Continue the main voice in parallel
1007 % Initiate second voice
1009 % Set stems, etc, down
1011 r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
1013 % Initiate third voice
1015 % Set stems, etc, up
1023 @cindex nesting music expressions
1024 @cindex nesting simultaneous constructs
1026 @cindex voices, temporary
1027 @cindex voices, nesting
1029 More deeply nested polyphony constructs are possible, and if a
1030 voice appears only briefly this might be a more natural way to
1033 @lilypond[quote,ragged-right,verbatim]
1034 \new Staff \relative c' {
1042 {c8 b16 a b8 g ~ g2}
1053 @cindex spacing notes
1055 This method of nesting new voices briefly is useful
1056 when only small sections of the music
1057 are polyphonic, but when the whole staff is largely polyphonic
1058 it can be clearer to use multiple voices throughout, using
1059 spacing notes to step over sections where the voice is silent,
1062 @lilypond[quote,ragged-right,verbatim]
1063 \new Staff \relative c' <<
1064 % Initiate first voice
1067 c16^( d e f g4 f e | d2 e2) |
1069 % Initiate second voice
1071 % Set stems, etc, down
1073 s4 r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2 |
1075 % Initiate third voice
1077 % Set stems, etc, up
1084 @subsubheading Note columns
1087 @cindex shift commands
1091 @funindex \shiftOnnn
1093 Closely spaced notes in a chord, or notes occurring at the same
1094 time in different voices, are arranged in two, occasionally more,
1095 columns to prevent the note heads overlapping. These are called
1096 note columns. There are separate columns for each voice, and
1097 the currently specified voice-dependent shift is applied to the
1098 note column if there would otherwise be a collision. This can
1099 be seen in the example above. In bar 2 the C in voice two is
1100 shifted to the right relative to the D in voice one, and in the
1101 final chord the C in voice three is also shifted to the right
1102 relative to the other notes.
1104 The @code{\shiftOn}, @code{\shiftOnn}, @code{\shiftOnnn}, and
1105 @code{\shiftOff} commands specify the degree to which notes and
1106 chords of the voice should be shifted if a collision
1107 would otherwise occur. By default, the outer voices (normally
1108 voices one and two) have @code{\shiftOff} specified, while the
1109 inner voices (three and four) have @code{\shiftOn} specified.
1110 When a shift is applied, Voices one and three are shifted to
1111 the right and voices two and four to the left.
1113 @code{\shiftOnn} and @code{\shiftOnnn} define further shift
1114 levels which may be specified temporarily to resolve collisions
1115 in complex situations -- see @ref{Real music example}.
1117 A note column can contain just one note (or chord) from a voice
1118 with stems up and one note (or chord) from a voice with stems
1119 down. If notes from two voices which have their stems in the
1120 same direction are placed at the same position and both voices
1121 have no shift or the same shift specified, the error message
1122 @qq{Too many clashing note columns} will be produced.
1124 @node Voices and vocals
1125 @subsection Voices and vocals
1127 Vocal music presents a special difficulty: we need to combine two
1128 expressions -- notes and lyrics.
1130 @funindex \new Lyrics
1132 @cindex Lyrics context, creating
1133 @cindex lyrics, linking to voice
1135 You have already seen the @code{\addlyrics@{@}} command, which
1136 handles simple scores well. However, this technique is
1137 quite limited. For more complex music, you must introduce the
1138 lyrics in a @code{Lyrics} context using @code{\new Lyrics} and
1140 the lyrics to the notes with @code{\lyricsto@{@}}, using the
1141 name assigned to the Voice.
1143 @lilypond[quote,verbatim,fragment]
1145 \new Voice = "one" \relative c'' {
1148 c4 b8. a16 g4. f8 e4 d c2
1150 \new Lyrics \lyricsto "one" {
1151 No more let sins and sor -- rows grow.
1156 Note that the lyrics must be linked to a @code{Voice} context,
1157 @emph{not} a @code{Staff} context. This is a case where it is
1158 necessary to create @code{Staff} and @code{Voice} contexts
1161 @cindex lyrics and beaming
1162 @cindex beaming and lyrics
1163 @funindex \autoBeamOff
1165 The automatic beaming which LilyPond uses by default works well
1166 for instrumental music, but not so well for music with lyrics,
1167 where beaming is either not required at all or is used to indicate
1168 melismata in the lyrics. In the example above we use the command
1169 @code{\autoBeamOff} to turn off the automatic beaming.
1171 @funindex \new ChoirStaff
1172 @funindex \lyricmode
1173 @cindex vocal score structure
1175 Let us reuse the earlier example from Judas Maccabæus to
1176 illustrate this more flexible technique. We first recast
1177 it to use variables so the music and lyrics can be separated
1178 from the staff structure. We also introduce a ChoirStaff
1179 bracket. The lyrics themselves must be introduced with
1180 @code{\lyricmode} to ensure they are interpreted as lyrics
1183 @lilypond[quote,verbatim]
1184 global = { \time 6/8 \partial 8 \key f \major}
1185 SopOneMusic = \relative c'' {
1186 c8 | c([ bes)] a a([ g)] f | f'4. b, | c4.~ c4 }
1187 SopTwoMusic = \relative c' {
1188 r8 | r4. r4 c8 | a'([ g)] f f([ e)] d | e([ d)] c bes' }
1189 SopOneLyrics = \lyricmode {
1190 Let | flee -- cy flocks the | hills a -- dorn, __ }
1191 SopTwoLyrics = \lyricmode {
1192 Let | flee -- cy flocks the | hills a -- dorn, }
1197 \new Voice = "SopOne" {
1201 \new Lyrics \lyricsto "SopOne" {
1206 \new Voice = "SopTwo" {
1210 \new Lyrics \lyricsto "SopTwo" {
1218 This is the basic structure of all vocal scores. More staves may
1219 be added as required, more voices may be added to the staves,
1220 more verses may be added to the lyrics,
1221 and the variables containing the music can easily be placed
1222 in separate files should they become too long.
1224 @cindex hymn structure
1226 Here is a example of the first line of a hymn with four
1227 verses, set for SATB. In this case the words for all four
1228 parts are the same. Note how we use variables to separate the
1229 music notation and words from the staff structure. See too
1230 how a variable, which we have chosen to call @q{TimeKey}, is used
1231 to hold several commands for use within the two staves. In other
1232 examples this is often called @q{global}.
1234 @lilypond[quote,verbatim]
1235 TimeKey = { \time 4/4 \partial 4 \key c \major}
1236 SopMusic = \relative c' { c4 | e4. e8 g4 g | a a g }
1237 AltoMusic = \relative c' { c4 | c4. c8 e4 e | f f e }
1238 TenorMusic = \relative c { e4 | g4. g8 c4. b8 | a8 b c d e4 }
1239 BassMusic = \relative c { c4 | c4. c8 c4 c | f8 g a b c4 }
1240 VerseOne = \lyricmode {
1241 E -- | ter -- nal fa -- ther, | strong to save, }
1242 VerseTwo = \lyricmode {
1243 O | Christ, whose voice the | wa -- ters heard, }
1244 VerseThree = \lyricmode {
1245 O | Ho -- ly Spi -- rit, | who didst brood }
1246 VerseFour = \lyricmode {
1247 O | Tri -- ni -- ty of | love and pow'r }
1253 \new Voice = "Sop" { \voiceOne \TimeKey \SopMusic }
1254 \new Voice = "Alto" { \voiceTwo \AltoMusic }
1255 \new Lyrics \lyricsto "Sop" { \VerseOne }
1256 \new Lyrics \lyricsto "Sop" { \VerseTwo }
1257 \new Lyrics \lyricsto "Sop" { \VerseThree }
1258 \new Lyrics \lyricsto "Sop" { \VerseFour }
1262 \new Voice = "Tenor" { \voiceOne \TimeKey \TenorMusic }
1263 \new Voice = "Bass" { \voiceTwo \BassMusic }
1269 @cindex verse and refrain
1271 We end with an example to show how we might code a solo verse which
1272 continues into a two-part refrain in two staves. The
1273 positioning of the sequential and simultaneous sections to achieve
1274 this within a single score is quite tricky, so follow the
1275 explanation carefully!
1277 Let's start with a score block containing a @code{ChoirStaff}, as
1278 we would like the brace to appear at the start of the chorus.
1279 Normally you would need angle brackets after @code{\new ChoirStaff}
1280 to bring in all the staves in parallel, but here we want to
1281 defer the parallelism during the solo so we use braces, although
1282 angle brackets here wouldn't hurt. Inside the @code{ChoirStaff} we
1283 want first the staff which will contain the verse. This must
1284 contain notes and lyrics in parallel, so here we need angle
1285 brackets around the @code{\new Voice} and @code{\new Lyrics} to
1286 start them at the same time:
1288 @lilypond[quote,verbatim,ragged-right]
1289 versenotes = \relative c'' {
1292 \time 3/4 g g g b b b
1294 versewords = \lyricmode {
1295 One two three four five six
1300 \new Voice = "verse" {
1303 \new Lyrics \lyricsto verse {
1311 That gives the verse line.
1313 Now we want to continue with refrainA on the same staff while a
1314 second staff is introduced in parallel with it for refrainB, so
1315 this is a parallel section which must be positioned immediately
1316 following the @code{\break} in the verse Voice. Yes, @emph{within}
1317 the verse Voice! Here's that parallel section. More staves
1318 could be introduced here in the same way.
1323 \new Lyrics \lyricsto verse @{
1327 \new Voice = "refrainB" @{
1330 \new Lyrics \lyricsto "refrainB" @{
1337 Here's the final result with two staves in the chorus showing
1338 how the parallel section is positioned within the verse Voice:
1340 @lilypond[quote,verbatim, ragged-right]
1341 versenotes = \relative c'' {
1344 \time 3/4 g g g b b b
1346 refrainnotesA = \relative c'' {
1350 refrainnotesB = \relative c {
1355 versewords = \lyricmode {
1356 One two three four five six
1358 refrainwordsA = \lyricmode {
1361 refrainwordsB = \lyricmode {
1367 \new Voice = "verse" {
1371 \new Lyrics \lyricsto "verse" {
1375 \new Voice = "refrainB" {
1378 \new Lyrics \lyricsto "refrainB" {
1384 \new Lyrics \lyricsto "verse" {
1392 @cindex book, example of using
1395 However, although this is an interesting and useful exercise to
1396 help you to understand how sequential and simultaneous blocks work,
1397 in practice one would perhaps choose to code this as two
1398 @code{\score} blocks within an implicit @code{\book} block, as
1401 @lilypond[quote,verbatim,ragged-right]
1402 versenotes = \relative c'' {
1405 \time 3/4 g g g b b b
1407 refrainnotesA = \relative c'' {
1411 refrainnotesB = \relative c {
1416 versewords = \lyricmode {
1417 One two three four five six
1419 refrainwordsA = \lyricmode {
1422 refrainwordsB = \lyricmode {
1427 \new Voice = "verse" {
1430 \new Lyrics \lyricsto "verse" {
1439 \new Voice = "refrainA" {
1442 \new Lyrics \lyricsto "refrainA" {
1447 \new Voice = "refrainB" {
1450 \new Lyrics \lyricsto "refrainB" {
1458 @node Contexts and engravers
1459 @section Contexts and engravers
1461 Contexts and engravers have been mentioned informally
1462 in earlier sections; we now must look at
1463 these concepts in more detail, as they are important
1464 in the fine-tuning of LilyPond output.
1468 * Contexts explained::
1469 * Creating contexts::
1470 * Engravers explained::
1471 * Modifying context properties::
1472 * Adding and removing engravers::
1475 @node Contexts explained
1476 @subsection Contexts explained
1478 @cindex contexts explained
1480 When music is printed, many notational elements which do not
1481 appear explicitly in the input file must be added to the
1482 output. For example, compare the input and output of the
1485 @lilypond[quote,verbatim,relative=2,fragment]
1489 The input is rather sparse, but in the output, bar lines,
1490 accidentals, clef, and time signature have been added. When
1491 LilyPond @emph{interprets} the input the musical information
1492 is inspected in time order, similar to reading a score from left
1493 to right. While reading the input, the program remembers where
1494 measure boundaries are, and which pitches require explicit
1495 accidentals. This information must be held on several levels.
1496 For example, the effect of an accidental is limited
1497 to a single staff, while a bar line must be synchronized across
1500 Within LilyPond, these rules and bits of information are grouped
1501 in @emph{Contexts}. We have already met the
1502 @code{Voice} context.
1503 Others are the @code{Staff} and @code{Score} contexts.
1504 Contexts are hierarchical to reflect the hierarchical nature of
1506 For example: a @code{Staff} context can contain many
1507 @code{Voice} contexts, and a @code{Score} context can
1508 contain many @code{Staff} contexts.
1511 @sourceimage{context-example,5cm,,}
1514 Each context has the responsibility for enforcing some notation rules,
1515 creating some notation objects and maintaining the associated
1516 properties. For example, the @code{Voice} context may introduce an
1517 accidental and then the @code{Staff} context maintains the rule to
1518 show or suppress the accidental for the remainder of the measure.
1520 As another example, the synchronization of bar lines is, by default,
1521 handled in the @code{Score} context.
1522 However, in some music we may not want the bar lines to be
1523 synchronized -- consider a polymetric score in 4/4 and 3/4 time.
1524 In such cases, we must modify the default settings of the
1525 @code{Score} and @code{Staff} contexts.
1527 For very simple scores, contexts are created implicitly, and you need
1528 not be aware of them. For larger pieces, such as anything with more
1529 than one staff, they must be
1530 created explicitly to make sure that you get as many staves as you
1531 need, and that they are in the correct order. For typesetting pieces
1532 with specialized notation, it is usual to modify existing, or
1533 even to define totally new, contexts.
1535 In addition to the @code{Score,} @code{Staff} and
1536 @code{Voice} contexts there are contexts which fit between
1537 the score and staff levels to control staff groups, such as the
1538 @code{PianoStaff} and @code{ChoirStaff} contexts. There
1539 are also alternative staff and voice contexts, and contexts for
1540 lyrics, percussion, fret boards, figured bass, etc.
1542 The names of all context types are formed from one or more
1543 words, each word being capitalized and joined immediately to the
1544 preceding word with no hyphen or underscore, e.g.,
1545 @code{GregorianTranscriptionStaff}.
1547 @node Creating contexts
1548 @subsection Creating contexts
1551 @cindex new contexts
1552 @cindex creating contexts
1553 @cindex contexts, creating
1555 There can be only one top level context: the
1557 context. This is created with the @code{\score} command,
1558 or, in simple scores, it is created automatically.
1560 For scores with only one voice and one staff, the
1561 @code{Voice} and @code{Staff} contexts may be left to be
1562 created automatically, but for more complex scores it is
1563 necessary to create them by hand.
1564 The simplest command that does this is @code{\new}.
1565 It is prepended to a music expression, for example
1568 \new @var{type} @var{music-expression}
1572 where @var{type} is a context name (like @code{Staff} or
1573 @code{Voice}). This command creates a new context, and starts
1574 interpreting the @var{music-expression} within that context.
1576 Note that there is no @code{\new Score} command;
1577 the single top-level @code{Score} context is introduced
1580 You have seen many practical examples which created new
1581 @code{Staff} and @code{Voice} contexts in earlier sections, but
1582 to remind you how these commands are used in practice, here's an
1583 annotated real-music example:
1585 @lilypond[quote,verbatim,ragged-right]
1586 \score { % start of single compound music expression
1587 << % start of simultaneous staves section
1589 \new Staff { % create RH staff
1592 \new Voice { % create voice for RH notes
1593 \relative c'' { % start of RH notes
1599 \new Staff << % create LH staff; needs two simultaneous voices
1602 \new Voice { % create LH voice one
1604 \relative g { % start of LH voice one notes
1605 g8 <bes d> ees, <g c> |
1606 g8 <bes d> ees, <g c> |
1607 } % end of LH voice one notes
1608 } % end of LH voice one
1609 \new Voice { % create LH voice two
1611 \relative g { % start of LH voice two notes
1614 } % end of LH voice two notes
1615 } % end of LH voice two
1616 >> % end of LH staff
1617 >> % end of simultaneous staves section
1618 } % end of single compound music expression
1621 (Note how all the statements which open a block with either a
1622 curly bracket, @code{@{}, or double angle brackets, @code{<<},
1623 are indented by two further spaces, and the corresponding
1624 closing bracket is indented by exactly the same amount. While
1625 this is not required, following this practice will greatly
1626 reduce the number of @q{unmatched bracket} errors, and is
1627 strongly recommended. It enables the structure of the music to
1628 be seen at a glance, and any unmatched brackets will be obvious.
1629 Note too how the LH staff is created using double angle brackets
1630 because it requires two voices for its music, whereas the RH staff
1631 is created with a single music expression surrounded by curly
1632 brackets because it requires only one voice.)
1634 @cindex contexts, naming
1635 @cindex naming contexts
1637 The @code{\new} command may also give a identifying name to the
1638 context to distinguish it from other contexts of the same type,
1641 \new @var{type} = @var{id} @var{music-expression}
1644 Note the distinction between the name of the context type,
1645 @code{Staff}, @code{Voice}, etc, and the identifying name of a
1646 particular instance of that type, which can be any sequence of letters
1647 and digits invented by the user. The identifying name is used to
1648 refer back to that particular instance of a context. We saw this in
1649 use in the section on lyrics in @ref{Voices and vocals}.
1652 @node Engravers explained
1653 @subsection Engravers explained
1657 Every mark on the printed output of a score produced by LilyPond
1658 is produced by an @code{Engraver}. Thus there is an engraver
1659 to print staves, one to print note heads, one for stems, one for
1660 beams, etc, etc. In total there are over 120 such engravers!
1661 Fortunately, for most scores it is not necessary to know about
1662 more than a few, and for simple scores you do not need to know
1665 Engravers live and operate in Contexts.
1666 Engravers such as the @code{Metronome_mark_engraver}, whose
1667 action and output applies to the score as a whole, operate in
1668 the highest level context -- the @code{Score} context.
1670 The @code{Clef_engraver} and @code{Key_engraver} are to be
1671 found in every Staff Context, as different staves may require
1672 different clefs and keys.
1674 The @code{Note_heads_engraver} and @code{Stem_engraver} live
1675 in every @code{Voice} context, the lowest level context of all.
1677 Each engraver processes the particular objects associated
1678 with its function, and maintains the properties that relate
1679 to that function. These properties, like the properties
1680 associated with contexts, may be modified to change the
1681 operation of the engraver or the appearance of those elements
1682 in the printed score.
1684 Engravers all have compound names formed from words which
1685 describe their function. Just the first word is capitalized,
1686 and the remainder are joined to it with underscores. Thus
1687 the @code{Staff_symbol_engraver} is responsible for creating the
1688 lines of the staff, the @code{Clef_engraver} determines and sets
1689 the pitch reference point on the staff by drawing a clef symbol.
1691 Here are some of the most common engravers together with their
1692 function. You will see it is usually easy to guess the function
1693 from the name, or vice versa.
1695 @multitable @columnfractions .3 .7
1698 @item Accidental_engraver
1699 @tab Makes accidentals, cautionary and suggested accidentals
1704 @item Completion_heads_engraver
1705 @tab Splits notes which cross bar lines
1706 @c The old Dynamic_engraver is deprecated. -jm
1707 @item New_dynamic_engraver
1708 @tab Creates hairpins and dynamic texts
1709 @item Forbid_line_break_engraver
1710 @tab Prevents line breaks if a musical element is still active
1712 @tab Creates the key signature
1713 @item Metronome_mark_engraver
1714 @tab Engraves metronome marking
1715 @item Note_heads_engraver
1716 @tab Engraves note heads
1719 @item Staff_symbol_engraver
1720 @tab Engraves the five (by default) lines of the staff
1722 @tab Creates stems and single-stem tremolos
1723 @item Time_signature_engraver
1724 @tab Creates time signatures
1729 We shall see later how the output of LilyPond can be changed
1730 by modifying the action of Engravers.
1733 @node Modifying context properties
1734 @subsection Modifying context properties
1736 @cindex context properties
1737 @cindex context properties, modifying
1738 @cindex modifying context properties
1742 Contexts are responsible for holding the values of a number of
1743 context @emph{properties}. Many of them can be changed to
1744 influence the interpretation of the input and so change the
1745 appearance of the output. They are changed by the
1746 @code{\set} command. This takes the form
1749 \set @emph{ContextName}.@emph{propertyName} = #@emph{value}
1752 Where the @emph{ContextName} is usually @code{Score},
1753 @code{Staff} or @code{Voice}. It may be omitted,
1754 in which case @code{Voice} is assumed.
1756 The names of context properties consist of words joined
1757 together with no hyphens or underscores, all except the
1758 first having a capital letter. Here are a few examples
1759 of some commonly used ones. There are many more.
1761 @c attempt to force this onto a new page
1763 @multitable @columnfractions .25 .15 .45 .15
1764 @headitem propertyName
1770 @tab If true, set extra natural signs before accidentals
1771 @tab @code{#t}, @code{#f}
1772 @item currentBarNumber
1774 @tab Set the current bar number
1778 @tab If true, print slurs both above and below notes
1779 @tab @code{#t}, @code{#f}
1780 @item instrumentName
1782 @tab Set the name to be placed at the start of the staff
1783 @tab @code{"Cello I"}
1786 @tab Increase or decrease the font size
1790 @tab Set the text to print before the start of a verse
1795 where a Boolean is either True (@code{#t}) or False (@code{#f}),
1796 an Integer is a positive whole number, a Real is a positive
1797 or negative decimal number, and text is enclosed in double
1798 apostrophes. Note the occurrence of hash signs,
1799 (@code{#}), in two different places -- as part of the Boolean
1800 value before the @code{t} or @code{f}, and before @emph{value}
1801 in the @code{\set} statement. So when a Boolean is being
1802 entered you need to code two hash signs, e.g., @code{##t}.
1804 Before we can set any of these properties we need to know
1805 in which context they operate. Sometimes this is obvious,
1806 but occasionally it can be tricky. If the wrong context
1807 is specified, no error message is produced, but the expected
1808 action will not take place. For example, the
1809 @code{instrumentName} clearly lives in the Staff context, since
1810 it is the staff that is to be named.
1811 In this example the first staff is labelled, but not the second,
1812 because we omitted the context name.
1814 @lilypond[quote,verbatim,ragged-right]
1816 \new Staff \relative c'' {
1817 \set Staff.instrumentName = #"Soprano"
1820 \new Staff \relative c' {
1821 \set instrumentName = #"Alto" % Wrong!
1827 Remember the default context name is Voice, so the second
1828 @code{\set} command set the property @code{instrumentName} in the
1829 Voice context to @qq{Alto}, but as LilyPond does not look
1830 for any such property in the @code{Voice} context, no
1831 further action took place. This is not an error, and no error
1832 message is logged in the log file.
1834 Similarly, if the property name is mis-spelt no error message is
1835 produced, and clearly the expected action cannot be performed. If
1836 fact, you can set any (fictitious) @q{property} using any name you
1837 like in any context that exists by using the @code{\set} command. But
1838 if the name is not known to LilyPond it will not cause any action to
1839 be taken. Some text editors with special support for LilyPond input
1840 files document property names with bullets when you hover them with
1841 the mouse, like JEdit with LilyPondTool, or highlight unknown property
1842 names differently, like ConTEXT. If you do not use an editor with
1843 such features, it is recommended to check the property name in the
1844 Internals Reference: see @rinternals{Tunable context properties}, or
1845 @rinternals{Contexts}.
1847 The @code{instrumentName} property will take effect only
1848 if it is set in the @code{Staff} context, but
1849 some properties can be set in more than one context.
1850 For example, the property @code{extraNatural} is by
1851 default set to ##t (true) for all staves.
1852 If it is set to ##f (false) in one particular @code{Staff}
1853 context it applies just to the accidentals on that staff.
1854 If it is set to false in the @code{Score} context
1855 it applies to all staves.
1857 So this turns off extra naturals in one staff:
1859 @lilypond[quote,verbatim,ragged-right]
1861 \new Staff \relative c'' {
1864 \new Staff \relative c'' {
1865 \set Staff.extraNatural = ##f
1872 and this turns them off in all staves:
1874 @lilypond[quote,verbatim,ragged-right]
1876 \new Staff \relative c'' {
1879 \new Staff \relative c'' {
1880 \set Score.extraNatural = ##f
1886 As another example, if @code{clefOctavation} is set in
1887 the @code{Score} context this immediately changes the value
1888 of the octavation in all current staves and sets a new default
1889 value which will be applied to all staves.
1891 The opposite command, @code{\unset}, effectively removes the
1892 property from the context, which causes most properties to
1893 revert to their default value. Usually @code{\unset} is not
1894 required as a new @code{\set} command will achieve what is
1897 The @code{\set} and @code{\unset} commands can appear anywhere
1898 in the input file and will take effect from the time they are
1899 encountered until the end of the score or until the property is
1900 @code{\set} or @code{\unset} again. Let's try changing the
1901 font size, which affects the size of the note heads (among
1902 other things) several times. The change is from the default
1903 value, not the most recently set value.
1905 @lilypond[quote,verbatim,ragged-right,relative=1,fragment]
1907 % make note heads smaller
1910 % make note heads larger
1911 \set fontSize = #2.5
1913 % return to default size
1918 We have now seen how to set the values of several different types of
1919 property. Note that integers and numbers are always preceded by a
1920 hash sign, @code{#}, while a true or false value is specified by
1921 @code{##t} and @code{##f}, with two hash signs. A text property
1922 should be enclosed in double quotation signs, as above, although we
1923 shall see later that text can actually be specified in a much more
1924 general way by using the very powerful @code{markup} command.
1928 @cindex context properties, setting with \with
1930 Context properties may also be set at the time the context is
1931 created. Sometimes this is a clearer way of specifying a
1932 property value if it is to remain fixed for the duration of
1933 the context. When a context is created with a @code{\new}
1934 command it may be followed immediately by a
1935 @code{\with @{ .. @}} block in which the property values are
1936 set. For example, if we wish to suppress the printing of
1937 extra naturals for the duration of a staff we would write:
1940 \new Staff \with @{ extraNatural = ##f @}
1946 @lilypond[quote,verbatim,ragged-right]
1952 \new Staff \with { extraNatural = ##f }
1959 Properties set in this way may still be changed dynamically using
1960 @code{\set} and returned to their default value with @code{\unset}.
1962 The @code{fontSize} property is treated differently. If this is
1963 set in a @code{\with} clause it effectively resets the default
1964 value of the font size. If it is later changed with @code{\set},
1965 this new default value may be restored with the
1966 @code{\unset fontSize} command.
1969 @node Adding and removing engravers
1970 @subsection Adding and removing engravers
1972 @cindex engravers, adding
1973 @cindex engravers, removing
1978 We have seen that contexts each contain several engravers, each
1979 of which is responsible for producing a particular part of the
1980 output, like bar lines, staves, note heads, stems, etc. If an
1981 engraver is removed from a context, it can no longer produce its
1982 output. This is a crude way of modifying the output, but it
1983 can sometimes be useful.
1985 @subsubheading Changing a single context
1987 To remove an engraver from a single context we use the
1988 @code{\with} command placed immediately after the context creation
1989 command, as in the previous section.
1991 As an illustration, let's repeat an example from the previous section
1992 with the staff lines removed. Remember that the staff lines are
1993 produced by the @code{Staff_symbol_engraver}.
1995 @lilypond[quote,verbatim,ragged-right]
1997 \remove Staff_symbol_engraver
2001 \set fontSize = #-4 % make note heads smaller
2003 \set fontSize = #2.5 % make note heads larger
2005 \unset fontSize % return to default size
2010 @cindex ambitus engraver
2012 Engravers can also be added to individual contexts.
2013 The command to do this is
2015 @code{\consists @var{Engraver_name}},
2018 placed inside a @code{\with} block. Some vocal scores have an ambitus
2019 placed at the beginning of a staff to indicate the range of notes in
2020 that staff -- see @rglos{ambitus}. The ambitus is produced by the
2021 @code{Ambitus_engraver}, which is not normally included in any
2022 context. If we add it to the @code{Voice} context, it calculates the
2023 range from that voice only:
2025 @lilypond[quote,verbatim,ragged-right]
2028 \consists Ambitus_engraver
2043 but if we add the ambitus engraver to the
2044 @code{Staff} context, it calculates the range from all
2045 the notes in all the voices on that staff:
2047 @lilypond[quote,verbatim,ragged-right]
2049 \consists Ambitus_engraver
2065 @subsubheading Changing all contexts of the same type
2069 The examples above show how to remove or add engravers to
2070 individual contexts. It is also possible to remove or add
2071 engravers to every context of a specific type by placing the
2072 commands in the appropriate context in a @code{\layout}
2073 block. For example, if we wanted to show an ambitus for every
2074 staff in a four-staff score, we could write
2076 @lilypond[quote,verbatim,ragged-right]
2080 \relative c'' { c a b g }
2083 \relative c' { c a b g }
2087 \relative c' { c a b g }
2091 \relative c { c a b g }
2097 \consists Ambitus_engraver
2104 The values of context properties may also be set
2105 for all contexts of a particular type by including the
2106 @code{\set} command in a @code{\context} block in the
2110 @node Extending the templates
2111 @section Extending the templates
2113 You've read the tutorial, you know how to write music, you
2114 understand the fundamental concepts. But how can you
2115 get the staves that you want? Well, you can find lots of
2116 templates (see @ref{Templates}) which may give you a start.
2117 But what if you want something that isn't covered there? Read on.
2120 * Soprano and cello::
2121 * Four-part SATB vocal score::
2122 * Building a score from scratch::
2125 @node Soprano and cello
2126 @subsection Soprano and cello
2128 @cindex template, modifying
2130 Start off with the template that seems closest to what you want to end
2131 up with. Let's say that you want to write something for soprano and
2132 cello. In this case, we would start with @q{Notes and lyrics} (for the
2136 \version @w{"@version{}"}
2137 melody = \relative c' @{
2144 text = \lyricmode @{
2150 \new Voice = "one" @{
2154 \new Lyrics \lyricsto "one" \text
2161 Now we want to add a cello part. Let's look at the @q{Notes only} example:
2164 \version @w{"@version{}"}
2165 melody = \relative c' @{
2179 We don't need two @code{\version} commands. We'll need the
2180 @code{melody} section. We don't want two @code{\score} sections
2181 -- if we had two @code{\score}s, we'd get the two parts separately.
2182 We want them together, as a duet. Within the @code{\score}
2183 section, we don't need two @code{\layout} or @code{\midi}.
2185 If we simply cut and paste the @code{melody} section, we would
2186 end up with two @code{melody} definitions. This would not generate
2187 an error, but the second one would be used for both melodies.
2188 So let's rename them to make them distinct. We'll call the
2189 section for the soprano @code{sopranoMusic} and the section for
2190 the cello @code{celloMusic}. While we're doing this, let's rename
2191 @code{text} to be @code{sopranoLyrics}. Remember to rename both
2192 instances of all these names -- both the initial definition (the
2193 @code{melody = \relative c' @{ } part) and the name's use (in the
2194 @code{\score} section).
2196 While we're doing this, let's change the cello part's staff --
2197 celli normally use bass clef. We'll also give the cello some
2201 \version @w{"@version{}"}
2202 sopranoMusic = \relative c' @{
2209 sopranoLyrics = \lyricmode @{
2213 celloMusic = \relative c @{
2222 \new Voice = "one" @{
2226 \new Lyrics \lyricsto "one" \sopranoLyrics
2233 This is looking promising, but the cello part won't appear in the
2234 score -- we haven't used it in the @code{\score} section. If we
2235 want the cello part to appear under the soprano part, we need to add
2238 \new Staff \celloMusic
2242 underneath the soprano stuff. We also need to add @code{<<} and
2243 @code{>>} around the music -- that tells LilyPond that there's
2244 more than one thing (in this case, two @code{Staves}) happening
2245 at once. The @code{\score} looks like this now:
2247 @c Indentation in this example is deliberately poor
2252 \new Voice = "one" @{
2256 \new Lyrics \lyricsto "one" \sopranoLyrics
2258 \new Staff \celloMusic
2266 This looks a bit messy; the indentation is messed up now. That is
2267 easily fixed. Here's the complete soprano and cello template.
2269 @lilypond[quote,verbatim,ragged-right,addversion]
2270 sopranoMusic = \relative c' {
2277 sopranoLyrics = \lyricmode {
2281 celloMusic = \relative c {
2291 \new Voice = "one" {
2295 \new Lyrics \lyricsto "one" \sopranoLyrics
2297 \new Staff \celloMusic
2306 The starting templates can be found in the @q{Templates} appendix,
2307 see @ref{Single staff}.
2310 @node Four-part SATB vocal score
2311 @subsection Four-part SATB vocal score
2313 @cindex template, SATB
2314 @cindex SATB template
2316 Most vocal scores of music written for four-part mixed choir
2317 with orchestral accompaniment such as Mendelssohn's Elijah or
2318 Handel's Messiah have the choral music and words on four
2319 staves, one for each of SATB, with a piano reduction of the
2320 orchestral accompaniment underneath. Here's an example
2321 from Handel's Messiah:
2323 @c The following should appear as music without code
2324 @lilypond[quote,ragged-right]
2325 global = { \key d \major \time 4/4 }
2326 sopranoMusic = \relative c'' {
2328 r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
2330 sopranoWords = \lyricmode {
2331 Wor -- thy is the lamb that was slain
2333 altoMusic = \relative a' {
2335 r4 a2 a4 | fis4. fis8 a2 | g4 fis e2 |
2337 altoWords = \sopranoWords
2338 tenorMusic = \relative c' {
2340 r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
2342 tenorWords = \sopranoWords
2343 bassMusic = \relative c' {
2345 r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
2347 bassWords = \sopranoWords
2348 upper = \relative a' {
2351 r4 <a d fis>2 <a e' a>4 |
2352 <d fis d'>4. <d fis d'>8 <a d a'>2 |
2353 <g cis g'>4 <a d fis> <a cis e>2 |
2355 lower = \relative c, {
2358 <d d'>4 <d d'>2 <cis cis'>4 |
2359 <b b'>4. <b' b'>8 <fis fis'>2 |
2360 <e e'>4 <d d'> <a' a'>2 |
2364 << % combine ChoirStaff and PianoStaff in parallel
2366 \new Staff = "sopranos" <<
2367 \set Staff.instrumentName = "Soprano"
2368 \new Voice = "sopranos" { \global \sopranoMusic }
2370 \new Lyrics \lyricsto "sopranos" { \sopranoWords }
2371 \new Staff = "altos" <<
2372 \set Staff.instrumentName = "Alto"
2373 \new Voice = "altos" { \global \altoMusic }
2375 \new Lyrics \lyricsto "altos" { \altoWords }
2376 \new Staff = "tenors" <<
2377 \set Staff.instrumentName = "Tenor"
2378 \new Voice = "tenors" { \global \tenorMusic }
2380 \new Lyrics \lyricsto "tenors" { \tenorWords }
2381 \new Staff = "basses" <<
2382 \set Staff.instrumentName = "Bass"
2383 \new Voice = "basses" { \global \bassMusic }
2385 \new Lyrics \lyricsto "basses" { \bassWords }
2389 \set PianoStaff.instrumentName = "Piano"
2390 \new Staff = "upper" \upper
2391 \new Staff = "lower" \lower
2397 None of the templates provides this layout exactly. The nearest is
2398 @q{SATB vocal score and automatic piano reduction} -- see @ref{Vocal
2399 ensembles} -- but we need to change the layout and add a piano
2400 accompaniment which is not derived automatically from the vocal parts.
2401 The variables holding the music and words for the vocal parts are
2402 fine, but we shall need to add variables for the piano reduction.
2404 The order in which the contexts appear in the ChoirStaff of
2405 the template do not correspond with the order in the vocal
2406 score shown above. We need to rearrange them so there are
2407 four staves with the words written directly underneath the
2408 notes for each part.
2409 All the voices should be @code{\voiceOne}, which is
2410 the default, so the @code{\voiceXXX} commands should be removed.
2411 We also need to specify the tenor clef for the tenors.
2412 The way in which lyrics are specified in the template has not yet
2413 been encountered so we need to use the method with which we are
2414 familiar. We should also add the names of each staff.
2416 Doing this gives for our ChoirStaff:
2420 \new Staff = "sopranos" <<
2421 \set Staff.instrumentName = "Soprano"
2422 \new Voice = "sopranos" @{ \global \sopranoMusic @}
2424 \new Lyrics \lyricsto "sopranos" @{ \sopranoWords @}
2425 \new Staff = "altos" <<
2426 \set Staff.instrumentName = "Alto"
2427 \new Voice = "altos" @{ \global \altoMusic @}
2429 \new Lyrics \lyricsto "altos" @{ \altoWords @}
2430 \new Staff = "tenors" <<
2431 \set Staff.instrumentName = "Tenor"
2432 \new Voice = "tenors" @{ \global \tenorMusic @}
2434 \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
2435 \new Staff = "basses" <<
2436 \set Staff.instrumentName = "Bass"
2437 \new Voice = "basses" @{ \global \bassMusic @}
2439 \new Lyrics \lyricsto "basses" @{ \bassWords @}
2443 Next we must work out the piano part. This is
2444 easy - we just pull out the piano part from the
2445 @q{Solo piano} template:
2449 \set PianoStaff.instrumentName = "Piano "
2450 \new Staff = "upper" \upper
2451 \new Staff = "lower" \lower
2455 and add the variable definitions for @code{upper}
2458 The ChoirStaff and PianoStaff must be combined
2459 using angle brackets as we want them to be
2460 stacked one above the other:
2463 << % combine ChoirStaff and PianoStaff one above the other
2465 \new Staff = "sopranos" <<
2466 \new Voice = "sopranos" @{ \global \sopranoMusic @}
2468 \new Lyrics \lyricsto "sopranos" @{ \sopranoWords @}
2469 \new Staff = "altos" <<
2470 \new Voice = "altos" @{ \global \altoMusic @}
2472 \new Lyrics \lyricsto "altos" @{ \altoWords @}
2473 \new Staff = "tenors" <<
2474 \clef "G_8" % tenor clef
2475 \new Voice = "tenors" @{ \global \tenorMusic @}
2477 \new Lyrics \lyricsto "tenors" @{ \tenorWords @}
2478 \new Staff = "basses" <<
2480 \new Voice = "basses" @{ \global \bassMusic @}
2482 \new Lyrics \lyricsto "basses" @{ \bassWords @}
2486 \set PianoStaff.instrumentName = "Piano"
2487 \new Staff = "upper" \upper
2488 \new Staff = "lower" \lower
2493 Combining all these together and adding the music
2494 for the three bars of the example above gives:
2496 @lilypond[quote,verbatim,ragged-right,addversion]
2497 global = { \key d \major \time 4/4 }
2498 sopranoMusic = \relative c'' {
2500 r4 d2 a4 | d4. d8 a2 | cis4 d cis2 |
2502 sopranoWords = \lyricmode {
2503 Wor -- thy is the lamb that was slain
2505 altoMusic = \relative a' {
2507 r4 a2 a4 | fis4. fis8 a2 | g4 fis fis2 |
2509 altoWords = \sopranoWords
2510 tenorMusic = \relative c' {
2512 r4 fis2 e4 | d4. d8 d2 | e4 a, cis2 |
2514 tenorWords = \sopranoWords
2515 bassMusic = \relative c' {
2517 r4 d2 cis4 | b4. b8 fis2 | e4 d a'2 |
2519 bassWords = \sopranoWords
2520 upper = \relative a' {
2523 r4 <a d fis>2 <a e' a>4 |
2524 <d fis d'>4. <d fis d'>8 <a d a'>2 |
2525 <g cis g'>4 <a d fis> <a cis e>2 |
2527 lower = \relative c, {
2530 <d d'>4 <d d'>2 <cis cis'>4 |
2531 <b b'>4. <b' b'>8 <fis fis'>2 |
2532 <e e'>4 <d d'> <a' a'>2 |
2536 << % combine ChoirStaff and PianoStaff in parallel
2538 \new Staff = "sopranos" <<
2539 \set Staff.instrumentName = "Soprano"
2540 \new Voice = "sopranos" { \global \sopranoMusic }
2542 \new Lyrics \lyricsto "sopranos" { \sopranoWords }
2543 \new Staff = "altos" <<
2544 \set Staff.instrumentName = "Alto"
2545 \new Voice = "altos" { \global \altoMusic }
2547 \new Lyrics \lyricsto "altos" { \altoWords }
2548 \new Staff = "tenors" <<
2549 \set Staff.instrumentName = "Tenor"
2550 \new Voice = "tenors" { \global \tenorMusic }
2552 \new Lyrics \lyricsto "tenors" { \tenorWords }
2553 \new Staff = "basses" <<
2554 \set Staff.instrumentName = "Bass"
2555 \new Voice = "basses" { \global \bassMusic }
2557 \new Lyrics \lyricsto "basses" { \bassWords }
2561 \set PianoStaff.instrumentName = "Piano "
2562 \new Staff = "upper" \upper
2563 \new Staff = "lower" \lower
2570 @node Building a score from scratch
2571 @subsection Building a score from scratch
2573 @cindex template, writing your own
2575 After gaining some facility with writing LilyPond code, you
2576 may find that it is easier to build a score from scratch
2577 rather than modifying one of the templates. You can also
2578 develop your own style this way to suit the sort of music you
2579 like. Let's see how to put together the score for an organ
2580 prelude as an example.
2582 We begin with a header section. Here go the title, name
2583 of composer, etc, then come any variable definitions, and
2584 finally the score block. Let's start with these in outline
2585 and fill in the details later.
2587 We'll use the first two bars of Bach's prelude
2588 based on @emph{Jesu, meine Freude} which is written for two
2589 manuals and pedal organ. You can see these two bars of music
2590 at the bottom of this section. The top manual part has two voices,
2591 the lower and pedal organ one each. So we need four
2592 music definitions and one to define the time signature
2596 \version @w{"@version{}"}
2598 title = "Jesu, meine Freude"
2599 composer = "J S Bach"
2601 TimeKey = @{ \time 4/4 \key c \minor @}
2602 ManualOneVoiceOneMusic = @{s1@}
2603 ManualOneVoiceTwoMusic = @{s1@}
2604 ManualTwoMusic = @{s1@}
2605 PedalOrganMusic = @{s1@}
2611 For now we've just used a spacer note, @code{s1},
2612 instead of the real music. We'll add that later.
2614 Next let's see what should go in the score block.
2615 We simply mirror the staff structure we want.
2616 Organ music is usually written on three staves,
2617 one for each manual and one for the pedals. The
2618 manual staves should be bracketed together, so we
2619 need to use a PianoStaff for them. The first
2620 manual part needs two voices and the second manual
2625 \new Staff = "ManualOne" <<
2626 \new Voice @{ \ManualOneVoiceOneMusic @}
2627 \new Voice @{ \ManualOneVoiceTwoMusic @}
2628 >> % end ManualOne Staff context
2629 \new Staff = "ManualTwo" <<
2630 \new Voice @{ \ManualTwoMusic @}
2631 >> % end ManualTwo Staff context
2632 >> % end PianoStaff context
2635 Next we need to add a staff for the pedal organ.
2636 This goes underneath the PianoStaff, but it must
2637 be simultaneous with it, so we need angle brackets
2638 around the two. Missing these out would generate
2639 an error in the log file. It's a common mistake
2640 which you'll make sooner or later! Try copying
2641 the final example at the end of this section,
2642 remove these angle brackets, and compile it to
2643 see what errors it generates.
2646 << % PianoStaff and Pedal Staff must be simultaneous
2648 \new Staff = "ManualOne" <<
2649 \new Voice @{ \ManualOneVoiceOneMusic @}
2650 \new Voice @{ \ManualOneVoiceTwoMusic @}
2651 >> % end ManualOne Staff context
2652 \new Staff = "ManualTwo" <<
2653 \new Voice @{ \ManualTwoMusic @}
2654 >> % end ManualTwo Staff context
2655 >> % end PianoStaff context
2656 \new Staff = "PedalOrgan" <<
2657 \new Voice @{ \PedalOrganMusic @}
2662 It is not necessary to use the simultaneous construct
2663 @code{<< .. >>} for the manual two staff and the pedal organ staff,
2664 since they contain only one music expression, but it does no harm,
2665 and always using angle brackets after @code{\new Staff} is a good
2666 habit to cultivate in case there are multiple voices. The opposite
2667 is true for Voices: these should habitually be followed by braces
2668 @code{@{ .. @}} in case your music is coded in several variables
2669 which need to run consecutively.
2671 Let's add this structure to the score block, and adjust the indenting.
2672 We also add the appropriate clefs, ensure stems, ties and slurs in
2673 each voice on the upper staff point to the right direction with
2674 @code{\voiceOne} and @code{\voiceTwo}, and enter the time signature
2675 and key to each staff using our predefined variable, @code{\TimeKey}.
2679 << % PianoStaff and Pedal Staff must be simultaneous
2681 \new Staff = "ManualOne" <<
2682 \TimeKey % set time signature and key
2684 \new Voice @{ \voiceOne \ManualOneVoiceOneMusic @}
2685 \new Voice @{ \voiceTwo \ManualOneVoiceTwoMusic @}
2686 >> % end ManualOne Staff context
2687 \new Staff = "ManualTwo" <<
2690 \new Voice @{ \ManualTwoMusic @}
2691 >> % end ManualTwo Staff context
2692 >> % end PianoStaff context
2693 \new Staff = "PedalOrgan" <<
2696 \new Voice @{ \PedalOrganMusic @}
2697 >> % end PedalOrgan Staff
2699 @} % end Score context
2702 That completes the structure. Any three-staff organ music
2703 will have a similar structure, although the number of voices
2704 may vary. All that remains now
2705 is to add the music, and combine all the parts together.
2707 @lilypond[quote,verbatim,ragged-right,addversion]
2709 title = "Jesu, meine Freude"
2710 composer = "J S Bach"
2712 TimeKey = { \time 4/4 \key c \minor }
2713 ManualOneVoiceOneMusic = \relative g' {
2714 g4 g f ees | d2 c2 |
2716 ManualOneVoiceTwoMusic = \relative c' {
2717 ees16 d ees8~ ees16 f ees d c8 d~ d c~ |
2718 c c4 b8 c8. g16 c b c d |
2720 ManualTwoMusic = \relative c' {
2721 c16 b c8~ c16 b c g a8 g~ g16 g aes ees |
2722 f ees f d g aes g f ees d e8~ ees16 f ees d |
2724 PedalOrganMusic = \relative c {
2725 r8 c16 d ees d ees8~ ees16 a, b g c b c8 |
2726 r16 g ees f g f g8 c,2 |
2730 << % PianoStaff and Pedal Staff must be simultaneous
2732 \new Staff = "ManualOne" <<
2733 \TimeKey % set time signature and key
2735 \new Voice { \voiceOne \ManualOneVoiceOneMusic }
2736 \new Voice { \voiceTwo \ManualOneVoiceTwoMusic }
2737 >> % end ManualOne Staff context
2738 \new Staff = "ManualTwo" <<
2741 \new Voice { \ManualTwoMusic }
2742 >> % end ManualTwo Staff context
2743 >> % end PianoStaff context
2744 \new Staff = "PedalOrgan" <<
2747 \new Voice { \PedalOrganMusic }
2748 >> % end PedalOrgan Staff context
2750 } % end Score context