]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/tex/tutorial.yo
03b1249e626d01848f025c3108b1a3ecb4fcb4bb
[lilypond.git] / Documentation / tex / tutorial.yo
1 COMMENT(-*-text-*-)
2
3 redef(var)(1)(whenlatex(latexcommand({\normalfont\scshape )ARG1+latexcommand(}))\
4     whenhtml(sc(ARG1)))
5
6 COMMENT( This document contains Mudela fragments.  You need at least
7 Yodl-1.30.18 to convert this to tex or html.
8
9 TODO
10
11 pipethrough(date) sucks.
12
13 paragraphs have too much space.
14
15 )
16
17 COMMENT(
18         Mainly written by Han-Wen Nienhuys, 
19
20         with help of (among others)
21
22         * Jan Nieuwenhuizen
23
24         * Lambert Meertens,
25
26         * Adrian Mariano
27
28         * Mats Bengtsson
29
30 )
31
32 htmlbodyopt(bgcolor)(white)
33 htmlcommand(<font color=black>)
34
35 latexlayoutcmds(
36     \topmargin -0.25in  
37     \textheight 53\baselineskip
38     \advance\textheight by \topskip
39     \marginparwidth 1 in        %   Width of marginal notes.
40     \oddsidemargin 0.25 in      %   Note that \oddsidemargin = \evensidemargin
41     \evensidemargin 0.25 in
42     \marginparwidth 0.75 in
43     \textwidth 5.875 in % Width of text line.
44     \input mudela-book
45 )
46
47 whenlatex(notableofcontents())
48 whentexinfo(notableofcontents())
49
50 article(Mudela -- Using LilyPond to typeset music)
51       (Han-Wen Nienhuys and Jan Nieuwenhuizen)
52       (nop()PIPETHROUGH(date "+%B %d, %Y")()()nop())
53
54
55 latexcommand(\def\interexample{})
56 latexcommand(\def\preexample{\par})
57 latexcommand(\def\postexample{\par\medskip})
58 latexcommand(\def\file#1{{code(#1)}})
59
60 whenhtml(
61 includefile(html-disclaimer.yo-urg)
62 )
63
64
65 sect(Introduction)
66 label(tutorial:introduction)
67 latexcommand(\parindent2pc)
68   
69 LilyPond is a program that can print music from a specification that
70 you, the user, supply.  Using LilyPond may be a bit quaint in the
71 beginning, because you have to give that specification using a
72 em(language).  This document is a gentle introduction to that
73 language, which is called Mudela, an abbreviation for Music Definition
74 Language.
75
76 We will demonstrate the working of Mudela by presenting  examples of
77 input alongside with the resulting output.  We will comment on these
78 examples using English terms for notation, so if you are not familiar
79 with these terms, you should consult the glossary that is distributed
80 with LilyPond: it contains a list of musical terms along with
81 explanations and translations in some other languages.
82
83 sect(The first tune)
84 label(sec:firsttune)
85
86 To demonstrate what LilyPond input looks like, we start off with a
87 full fledged, yet simple, example. It is somewhat convoluted version
88 of one of the menuets in bind(J.)bind(S.)Bach's em(Clavierbuchlein).
89
90 mudela(verbatim)(
91 % lines preceded by a percent are comments.
92 \include "paper16.ly"
93 \score {
94     \notes                      
95     \relative c'' {             
96         \key g;
97         \time 3/4;              
98
99         d4 [g,8 a b c] d4 g, g |
100         e'4 [c8 d e fis] g4 g, g |
101         c4 [d8( )c b a( ] )b4 [c8 b a g] |
102         a4 [b8 a g fis] g2.  |
103
104         \bar ":|";              
105
106         b'4 [g8 a b g]
107         a4 [d,8 e fis d] |
108         g4 [e8 fis g d] cis4 [b8 cis] a4 |
109         [a8-. b-. cis-. d-. e-. fis-. ] 
110         g4 fis e |
111         fis a,  r8 cis8
112         d2.-\fermata
113         \bar "|.";
114     }
115     \paper {
116        linewidth = 14.0 \cm; % standard settings are too wide for a book
117    }
118 })
119
120 Before we will discuss the contents of the above, it would be best if
121 you would try to enter and save this text with a text editor, compile
122 it with LilyPond and view the output.  Details of this procedure may
123 vary from system to system.  On a Unix system, you should enter the
124 input in a file ending in file(.ly), such as file(menuet.ly).  To
125 create the output, one would issue code(ly2dvi menuet).
126
127 file(ly2dvi) is a little program that does the job of calling the
128 LilyPond and TeX() and adjusting page margins.
129
130 If all goes well, this will create the output file file(menuet.dvi).
131 To view this output, issue the command code(xdvi menuet).  Now that we
132 are familiar with the procedure to view the output, we will analyse
133 the input itself, line by line.
134
135 verb(% lines preceded by a percent are comments.)COMMENT(
136
137 )The percent sign, `code(%)', introduces a line comment.  If you want
138 make larger comments, you can use block comments. These are delimited
139 by `code(%{)' and `code(%})'
140
141 verb(\input "paper16.ly")
142
143 By default, LilyPond will use definitions for a staff of 20
144 nop(point)footnote(A point is the standard measure of length for
145 printing.  One point is 1/72.27 inch.)
146
147 high.  If you want smaller output (e.g., 16 point), you have to import
148 the settings for that size.  You can do this by including a file.
149 code(\include "file") is replaced by the contents of code(file).
150 LilyPond will inspect the standard search to look for the requested 
151 file.COMMENT(
152
153 )verb(\score {
154 ) COMMENT( 
155
156 ) A mudela file combines music with directions for outputting that
157 music.  The music is combined with the output directions by putting
158 them into a code(\score) block.
159 verb(
160    \notes               
161 ) COMMENT( 
162
163 )This makes LilyPond ready for accepting notes.
164 verb(
165     \relative c''
166 )COMMENT(
167
168 ) As we will see, pitches are combinations of octave, note name and
169 chromatic alteration.  In this scheme, the octave is indicated by
170 using raised quotes (`code(')') and ``lowered'' quotes (commas:
171 `code(,)').  The central C is denoted by code(c').  The C one octave
172 higher is code(c'').  One and two octaves below central C is denoted
173 by code(c) and code(c,) respectively.
174
175 If you have to indicate the pitches in a long piece that is written in
176 either a high or very low octave, you would have to type very many
177 quotes.  To remedy this, LilyPond has a so-called ``relative'' octave
178 entry mode.  In this mode, notes without quotes are chosen in such an
179 octave that they are the closest to the preceding note.  If you add a
180 high-quote an extra octave is added.  The lowered quote will substract
181 an extra octave.  Because the first note obviously has no predecessor,
182 you have to give the (absolute) pitch of the note to start with.
183 COMMENT(
184
185 )verb(
186        {                          % sequential music follows
187 )COMMENT(
188
189 ) The brace indicates that what follows is sequential music, i.e.,
190 notes that are to be played and printed after each other.  This is in
191 contrast with simultaneous music: notes that are to be played like a
192 chord.  You should be careful not to confuse this brace with the one
193 coming after code(\score).
194
195
196 verb(
197         \time 3/4;              % set the time signature.
198 ) COMMENT(
199
200 ) This command changes the time signature of the current piece: this
201 prints a 3/4 sign.  The 3/4 value is also used to generate bar lines
202 in the right spots.
203 verb(
204         \key g;
205 ) COMMENT(
206
207 ) This command changes the current key to G-major.  Although
208 this command comes after the code(\time) command, in the
209 output, the key comes before the time signature: LilyPond knows about
210 music typesetting conventions.
211 verb(
212         d4
213 ) COMMENT(
214
215 ) This is a code(d) note.  The relative music was started with a
216 code(c''), the real pitch of this note is code(d'').  The 4 
217 designates the duration of the note (it is a quarter note). COMMENT(
218
219 )verb(
220         [g,8
221 )COMMENT(
222
223 )The open bracket starts a beam.  This bracket is connected to the
224 following note, which is an eighth with pitch code(g') (remember
225 relative mode for pitches!)
226 COMMENT(
227
228 )verb(
229         a b
230 )COMMENT(
231
232 )These are notes with pitch code(a') and code(b').  Because their
233 duration is the same as the code(g), there is no need to enter the
234 duration (It is not illegal to enter it anyway.  You would then enter
235 code(a8 b8)) COMMENT(
236
237 )verb(
238           c]
239 ) COMMENT(
240
241 ) This ends the beam started four notes earlier, at the code(g).  In
242 the output, you will notice a measure bar following this note.  You do
243 not have to enter anything to make LilyPond create a bar.  Instead
244 Lily will deduce where bars have to be by comparing durations of notes with
245 the current time signature.  COMMENT(
246
247 )verb(
248           d4 g, g |
249 ) COMMENT(
250
251 ) Three more notes:  The code(|) is a ``barcheck''.  When processing the
252 music, LilyPond will check  that barchecks are found at the start of
253 a bar.  This can help you track down notes you forgot to enter. 
254 verb(
255         e'4 [c8 d e fis]
256 ) COMMENT(
257
258 ) So far, no notes were chromatically altered.  Here is the first one
259 that is: code(fis). Mudela by default uses Dutch note names, and
260 ``Fis'' is the Dutch note name for ``F sharp''.  However, there is no
261 sharp sign in the output. The program keeps track of key signatures,
262 and will only print accidentals if they are needed.
263 verb(
264         c4 [d8( )c b a( ] )b4 [c8 b a g] |
265 ) COMMENT(
266
267 ) The next line shows something new: In mudela, one enters a slur by
268 marking the beginning and ending note of the slur with an opening and
269 closing parenthesis respectively.  In the line shown above this is
270 done for two slurs.  Note that parentheses (slur markers) are between
271 the notes, and the brackets (beam markers) around the notes. As you
272 can see, the brackets and parentheses do not have to nest.
273
274 verb(
275         a4 [b8 a g fis] g2.  |
276 ) COMMENT(
277
278 ) A duration that is to be augmented with a duration dot, is notated
279 with a duration number followed by periods, as many as you want
280 augmentation dots.COMMENT(
281
282 )verb(
283         \bar ":|";              % a repeat sign
284 ) COMMENT(
285
286 )
287 Lily can not detect where you want your music to be repeated, so you
288 have to instruct her: a special bar symbol can be entered by the
289 code(\bar) command followed by a string that signifies the bar symbol
290 that you want.  Then comes a semicolon to separate the string from the
291 rest of the notes, just like with code(\key) and code(\time).
292 verb(
293      cis'4 [b8 cis] a4 |
294 ) COMMENT(
295
296 ) This line shows that Lily will print an accidental if that is
297 needed: the first C sharp will be printed with an accidental, the
298 second one without.  COMMENT(
299
300 )verb( [a8-. b-. cis-. d-. e-. fis-. ] % try some super and subscripts.)COMMENT(
301
302 )There is more to music than just pitches and rhythms.  An important
303 aspect is articulation.  You can enter articulation signs either in an
304 abbreviated form, by a dash and the the character for the
305 articulation to use,  e.g. code(-.) for staccato as shown above.
306 COMMENT(
307
308 )verb(
309         fis a,  r8 cis8
310 ) COMMENT(
311
312 )
313 Rests are denoted by the special notename code(r).  You can also make
314 an invisible rest by using the special notename code(s).
315 verb(
316         d2.-\fermata
317 ) COMMENT(
318
319 ) All articulations have a verbose form, like code(\fermata).  The
320 ``command'' COMMENT(Hi Adrian :-) code(\fermata) is not part of the
321 core of the language (most of the other discussed elements are), but
322 it is an abbreviation of a more complicated description of a fermata.
323 code(\fermata) names that description and is therefore called an
324 em(identifier).
325
326 verb(
327         }
328 ) COMMENT(
329
330 )
331 This ends the sequential music.
332
333 verb(\paper {
334             linewidth = 10.0\cm;
335 })
336 This specifies a conversion from music to notation output.  Most of
337 the details of this conversions (font sizes, dimensions, etc.) have
338 been taken care of, but  to fit the output  in this document, it has
339 to be smaller.  We do this by setting the line width to 10 centimeter
340 (approximately 4 inches).
341
342 verb(
343         }
344 )COMMENT(
345
346 )The last brace ends the code(\score) block.
347
348
349
350 There are a couple of things to note here.  The input format tries to
351 capture the meaning of em(music), and not notation.  Therefore the
352 format contains musical concepts like pitches and durations, instead
353 of symbols and positions.  Second, the format tries to be
354 em(context-free): a note will sound the same regardless of the current
355 time signature, the key nop(etc.)
356
357 The purpose of LilyPond informally is explained by the term `music
358 typesetter'. As you may have figured out by now, this is not a really
359 adequate name: not only does the program print musical symbols, it
360 also tries to make esthetic decisions, and it also em(generates) both
361 the symbols and the decisions from a high-level musical description.
362 In other words, the function of LilyPond would be best described by
363 `music compiler' or `music to notation compiler'.
364
365 As you can see, the most interesting part of the input is music
366 itself, in this case the sequence of notes.  We will therefore focus
367 on entering music for now.  Consequently, when we mean
368 verb(\score {
369         \notes { XXXX } 
370         \paper {  }
371 })COMMENT(
372
373 ) we will leave out the the repetitive details for now and just print
374 code(XXXX).
375
376
377
378 sect(When you know the notes to nop(print)ellipsis())
379
380 The basic building block of music is the note.  We lightly touched
381 notes in the previous example.  Here comes the full explanation A note
382 is made of a pitch and a duration.  The pitch of the central C is
383 written as code(c').  This is in line with musicological notation;
384 there this pitch is transcribed as nop(c)sups(1) or c'.  A
385 quarter-note duration is written as code(4).  So, to print a quarter
386 note whose pitch is central C, you enter the following code(c'4).
387
388 subsect(Duration)
389
390 The duration of a note is specified as a number: a whole note is
391 denoted by 1, a half note by 2, a quarter by 4, and so on.  If you
392 want to augment a duration with a dot, simply affix a period to the
393 number.  You can also print notes longer than a whole.  You do this by
394 using identifiers (code(\breve) and code(\longa)):
395 Here are some random notes to show how it works.
396
397 verb(
398   c'\longa c'\breve  
399   c'1 c'2 c'4 c'8 c'16 c'32 c'64 c'64 c'2. c'8. c'16
400 )
401
402 mudela()(
403 \score {
404        \notes {
405          c'\longa c'\breve  
406          c'1 c'2 c'4 c'8 c'16 c'32 c'64 c'64 c'2. c'8. c'16
407          }
408          \paper {
409                 linewidth = -1.0;
410                 \translator { \type "Score_engraver";
411                             \name "Score";
412                             \consists "Note_heads_engraver";
413                             \consists "Stem_engraver";
414                             \consists "Rhythmic_column_engraver";
415          }}}
416          
417 )
418
419 subsect(Basic pitches)
420
421 The pitch code(c') consists of two parts: one part for the note name,
422 and one for the octave.  The letter specifies which note name to use:
423 note names simply are the letters code(a) to code(g).  The number of
424 apostrophes specifies the octave to use: the central C is denoted by
425 code(c').footnote(By convention, the A above central C at concert
426 pitch is the tone that is used to tune instruments.  Its frequency is
427 about 440 Hz.)  The C which is an eighth higher (the C in the
428 ``two-line octave'') is denoted by code(c''): every octave adds a
429 quote.  A note name without quotes designates the pitch below code(c')
430 (the C in the ``small octave''). If you want to go down even further,
431 commas (sunken apostrophes) should be added, e.g., the C in the
432 ``contra octave'' is expressed as code(c,,).
433
434 This example demonstrates pitches
435 mudela(fragment,verbatim,center)(
436    c,,4 c, c c' c'' c''' d'4 e'4 f'4 g'4
437 )
438
439
440 subsect(Alterations)
441
442 We have so far ignored chromatically altered pitches.  The names `a'
443 to `g' for entering pitches are convenient: they are short,
444 pronounceable and they resemble the words for pitches in normal
445 musical vocabulary.
446
447 Enter flats and sharps.  In English there is no standard terse word
448 for C sharp or C flat.  For this reason, mudela uses a different,
449 non-English convention for entering altered pitches: a note is made
450 sharp by adding the suffix `--is' to its name, and flat by adding the
451 suffix `--es'.  For a double sharp another `--is' suffix is added, for
452 flats another `--es' nop(suffix.)  footnote(Variations on this
453 convention are used in a number of germanic languages, notably Dutch,
454 German, Swedish, and Norwegian.) The names for the alterations of C
455 are given in bind(Table)ref(notename-tab).
456
457 latexcommand(\begin{table}[h])
458   center(
459     table(2)(ll)(
460       row(cell(English)cell(LilyPond))
461       rowline()
462       row(cell(c double flat)cell(ceses))
463       row(cell(c flat)cell(ces))
464       row(cell(c natural)cell(c))
465       row(cell(c sharp)cell(cis))
466       row(cell(c double sharp)cell(cisis))
467     )
468   )
469   latexcommand(\caption{Default note names})
470   label(notename-tab)
471 latexcommand(\end{table})
472
473 Throughout this document we will continue to use these names.footnote(
474   Mudela defaults to Dutch notenames.  To make (Dutch) pronunciation
475   easier, the a-flat and e-flat are contracted to code(as) and
476   code(es).  Similarly, the a double flat and e double flat are
477   contracted to code(ases) and code(eses).  For consistency, the dutch
478   names also include code(aes), code(aeses), code(ees) and
479   code(eeses)) 
480
481 If you are not comfortable with these names, you can make your own.
482 Note names for different languages are included with the example
483 initialisation files, amongst others English (C sharp is abbreviated
484 to code(cs)), Italian, Swedish and Norwegian.  If you want to use
485 these names, issue code(\include "LANGUAGE.ly") where you could
486 substitute code(italiano), code(deutsch) etc.  for LANGUAGE.  You
487 should include these files at toplevel, i.e., before opening a
488 code(\score) block.
489
490
491 sect(Chords)
492
493 The previous examples all notes   that were to be played sequentially,  one
494 note following the other.  You can also use LilyPond to typeset
495 chords.  You do this  by expressing in mudela simultaneous music,
496 i.e.,   notes that are to be played concurrently. 
497
498 In Mudela you can form simultaneous music by enclosing notes in
499 pointed parentheses, bind(i.e.)bind(langle())bind(and)rangle().  ASCII
500 doesn't really have these delimiters, so Mudela uses the larger-than
501 (code(>)) and smaller-than (code(<)) signs instead.  For example, a
502 D-major chord is expressed as
503 mudela(fragment,verbatim,center)(
504   <d'8  fis'8 a'8 d''8>
505 )
506
507 Chords can be entered in the music in the same places that notes
508 can.  As an example we give a snippet of ``Twinkle Twinkle Little
509 Star'' in chords.  The chords may seem slightly unconventional, but
510 they only serve to show how chords work.  We've aligned the chords in
511 the input on their starting beat just to help you reading it.  This
512 layout does not influence the typesetting result in any way.
513
514 mudela(verbatim, fragment)(
515   \relative c' {
516   \time 2/4;
517   c4       c            <c g'>    <c e g>
518   <c e a>  <b d a'>     <b2 d g>
519   <a4 d f> <bes d f>    <bes c e> <g c e>
520   <e a d>  <a, g' cis'> <d2 f d'>
521   }
522 )
523
524 There is one thing to note, in sequences of chords, the (relative)
525 pitch of a is taken with reference to the first note of the previous
526 chord.
527
528 You can nest simultaneous and sequential music in any way you want,
529 e.g., COMMENT(
530
531 )mudela(verbatim,fragment,center)(
532         < { g''4 g''4 }
533           { c'8 <c' e'> c' <c' e'> } >
534 )COMMENT(
535
536 )
537 As you can see, LilyPond has some difficulty typesetting this
538 elegantly.  To adequately solve this, you have to persuade LilyPond to
539 make separate stems for both sequential music lists.   This is a topic
540 that is covered in bind(Section)ref(sec:polyphonic).
541
542 [Chords and relative mode]
543
544
545 sect(Adding nuances: articulation and dynamics)
546
547 Music can have articulation, dynamics (louder and softer), etc.  These
548 aspecs have notation, so LilyPond can print those.  We'll start out by
549 explaining how to obtain the smallest grains of nuance: the
550 articulation of a single note.  Articulation is entered by writing a
551 dash and the name of the desired articulation mark.  You have to add a
552 backslash in front of the name to distinguish it from the name of a
553 note. COMMENT(
554
555 )mudela(fragment,verbatim)(
556   c''4-\staccato
557   c''4-\tenuto )COMMENT(
558
559 ) Typing a lot of staccato notes in this syntax will get tedious very
560 quickly.  Therefore, Mudela has some handy abbreviations for
561 articulation marks such as staccato and tenuto.  They are shown in the
562 following example: COMMENT(
563
564 )mudela()(
565 \score{ <
566         \property Score.textstyle = typewriter
567         \type Staff \notes {
568                 c''4-.
569                 c''4--
570                 c''4-+
571                 c''4-|
572                 c''4->
573                 c''4-^
574                 }
575         \type Lyrics\lyrics {
576               "."4 "-" "+" "|" ">" "\^{ }" }
577         >
578         \paper { linewidth = -1.\cm; }
579 })COMMENT(
580
581 )Text and digits for fingering can be entered in the same manner: add a
582 dash and the text or digit to be printed:
583 COMMENT(
584
585 )mudela(fragment,verbatim)(c''4-1 g''4-5 c''-"Sul tasto" )
586 COMMENT(Currently, the meaning of the
587 syntax `note-dash-digit/articulation/text' is just ``add a superscript to this
588 note.''  This is not in line with our goal to em (define) music with
589 Mudela.  We hope that this will be fixed in a future version of the
590 language.  In the meantime you can abuse this: the super- and
591 subscripts can be forced into up or down position respectively by entering an
592 a caret (code(^)) or an underscore, code (_) instead of the dash:
593 mudela (fragment,verbatim,center) (
594   c'4-^ c'4^^ c'''4-^ c'''4_^
595 ))
596
597 Dynamic markings are another way to add a nuance to a note.  They are
598 entered by adding the name for the dynamic sign after the note.  You
599 should not enter a dash between the name and the note.footnote(This
600   is inconsistent.  We hope that this will be fixed in a later
601   version of the language.)COMMENT(
602
603 )mudela(verbatim,fragment)(
604   c4 \ff c4 \fp c4 c4 \ppp c4 c4 \sfz
605 )COMMENT(
606 UGH
607
608 )
609
610 sect(Bridging the notes: beams, slurs and ties)
611
612 Music typesetting does not use fixed symbols only.  A lot of symbols
613 are variable: they run from one note to another. In LilyPond
614 terminology, such a symbol is called a em(spanner).  To print a
615 spanner, you have to attach a marker to the note that begins it and to
616 the one that ends it.  These are the spanners that are entered like
617 this:
618
619 description(
620 dit(Slur)
621  The slur has the opening parenthesis as 
622 start marker is.  The stopping marker is the closing parenthesis.
623 For example:
624 mudela(fragment,center,verbatim)( c'4( )c'4 )
625
626   The slur is quite flexible: you can nest nop(slurs,)footnote(This is
627 inconsistent when compared to the syntax for articulation hints.  This
628 will be fixed some time, we hope.) and you can connect a note with a
629 slur on both the left and the right side:
630
631 mudela(fragment,verbatim,center)(
632   c'4((   )c''4 )c'4( )g'4 
633 )
634
635 dit(Beam)
636
637 The starting marker for the beam is the opening bracket, the ending
638 marker is the closing bracket.  The brackets have to be em(around) the
639 beamed notes.  footnote(Strictly speaking, a beam is not a musical
640 concept: beaming doesn't change the meaning of music, it only
641 clarifies the rhythmic structure.  One might argue that beams should
642 not be present in a music representation language.  Unfortunately,
643 LilyPond is not smart enough to insert beams into music on its own.
644
645 LilyPond does have code that guesses what the pattern should look
646 like, so that you don't have to specify the beaming for complicated
647 patterns.  Alas, the algorithm used is not foolproof
648 yet: code([c8. c32 c32]) will produce incorrect results.)
649
650 mudela(fragment,verbatim)(
651 [c'8 c'] [c'16 c' c' c'] [c'16. c'32 c' c'16.]
652 )
653
654 dit(Tie) LilyPond's tie is entered as a tilde, `code(~)', in analogy
655  with TeX()'s tie (which ties together words with a space), The tie is
656  similar to the slur: it looks like a slur, but a slur connects whole
657  chords, whereas the tie connects note heads.  
658
659 The input convention for the tilde is somewhat peculiar when used in
660 conjunction with chords.  Internally, the extra information that is
661 represented by the tilde has to be attached to a note (or to a rest,
662 for that matter).  For this reason, you can't put the tilde between
663 two chords (as in code(<c' g'> ~nop( )<c' g'>)).  The tilde sign must be
664 directly after a note of the chords.  It does not matter which
665 one. The following example demonstrates the use of ties:
666 mudela(fragment,verbatim,center)(
667   c''1 ~ c''4
668   <c'2. ~  g' es''> <c'4 g' es''> 
669 )
670
671 dit(Hairpins)
672 Crescendi and decrescendi can be printed in hairpin style.  The
673 starting marker for the crescendo is code(\<), and for the decrescendo
674 code(\>).  Both have code(\!) as the ending marker.
675 mudela(fragment, verbatim)(
676    \relative c'' { c4 \< \! c4 \> \! c2
677       < c1
678         { s4 \< \! s4 \> \! s2 } > }
679 )
680 This example shows a trick: by attaching the markings to space rests
681 that run parallel to the whole note, you can have dynamic markings
682 within a note. 
683
684 )
685
686 It is your job to make sure that each spanner that you start, also
687 ends.  If it doesn't, then Bad Things are likely to happen. If you end
688 spanners that are not started, LilyPond will warn you about illegal
689 ending markers.
690
691 sect(Commands)
692 label(sec:commands)
693
694 Music notation constructs with no duration, like clefs and key
695 signatures, can be entered by inserting various
696 commands between the music.  The general form of these commands is
697 COMMENT(
698
699 )center(code(\keyword argument argument ... ;))COMMENT(
700
701 )These are the commands that are currently supported in alfabetic order:
702 description(
703 dit(code(\bar) var(bartype))
704   This command makes LilyPond print special bar
705   lines and repeat symbols.  You can also use it to allow line breaks
706   when entering cadenzas.  The argument var(bartype) is a string that
707   describes what kind of bar line to print.
708 COMMENT(
709
710 )mudela(fragment,verbatim)(
711     \bar "|:"; c'4 \bar ":|:";    c'4  \bar ":|";  c'4 \bar "||";
712     c'4 \bar "empty"; c'4 \bar "|.";
713 )COMMENT(
714
715 )The command `code(\bar "empty")' does not create any visible bar
716   line, but it tells LilyPond to allow a linebreak at that position.
717   The `code(\bar)' command prints the specified symbol where you enter
718   it.  If you give a `code(\bar)' command at the end of a measure then
719   the specified symbol replaces the automatic bar line.
720   
721   The code(\bar) command does not affect metric structure.
722
723 dit(code(\cadenza) var(togglevalue)) This command toggles the
724 automatic printing of barlines.  `code(\cadenza 1)' turns off the
725 automatically generated bar lines.  They are switched on again with
726 `code(\cadenza 0)'. Then a bar line is printed, and LilyPond will act
727 as if you are again at the start of a measure.
728
729
730 This is useful when typesetting music without a meter (such as an ad
731  libitum cadenza).
732
733 dit(code(\clef) var(clefname)) This command sets the current clef for notation,
734   i.e., a clef symbol is printed and the notes following this command
735   are shifted vertically.  The argument is a string, the name of the
736   new clef. The default clef is the treble clef.
737   mudela(fragment,verbatim)(
738     \clef "bass"; c'4
739     \clef "treble"; c'4
740     \clef "alto"; c'4    
741   )
742
743 dit(code(\key) var(pitch) var(type)) This command changes the current
744   key signature.  The key signature is printed at the start of every
745   line. The var(type) argument is an integer. Useful values are
746   available as the predefined identifiers code(\major) and
747   code(\minor).  Omitting the second argument gives major keys. The
748   key of C-minor can thus be specified as `code(\key es)' or
749   `code(\key c \minor)'.
750   
751 dit(code(\keysignature) var(pitchlist))
752
753 This command changes the current key signature.  Unlike the
754 `code(\key)' command, this command can produce arbitrary key
755 signatures, which can be useful for unconventional keys or modes.  The
756 key signature is given in the form of a list of notes.  The notes will
757 be printed in the key signature in the order that they appear on the
758 list.  For example, the key of C-minor can be specified as
759 `code(\keysignature bes es as)'.  The command `code(\keysignature fis
760 es bis)' provides a more exotic example.
761
762
763 dit(code(\time) var(numerator)code(/)var(denominator))
764   This command changes the current time signature.
765   The default value for this time signature is common time (4/4).
766
767 dit(code(\partial) var(duration))
768   This command allows you to make
769   upsteps at the start of a piece.
770   The var(duration) argument has the same form as the duration of a
771   note.
772
773 The `code(\partial)' command cannot be used to generate partial
774 measures in the middle of the music.  
775 Example:
776   mudela(fragment,verbatim)(
777     \time 4/4;
778     \partial 4;
779     [d'8 dis'] e' c''4 e'8 c''4 
780   )
781
782 dit(code(\grouping) var(durationslist)) sets the metric structure of the measure.
783     Its effect can best be shown by an example:
784     mudela(fragment,verbatim,center)(
785         \time 5/16;
786         \grouping 16*3 16*2;
787         [c'8 c'16 c'8]
788         \grouping 16*2 16*3;
789         [c'8 c'16 c'8]
790         \grouping 16*5 ;
791         [c'8 c'16 c'8]
792     )
793
794 In practice, you won't be needing this command very often: the
795 grouping is switched automatically when you issue a code(\time)
796 command.  It is set to a combination of groups
797 of 2 and 3 beats, with as many groups of
798 3 as possible (in other words: 4/4 is divided in two times two beats
799 (2+2), 8/8 in 3+3+2)
800 )
801
802 The commands described above aren't really music, but they can best be
803 thought as as notes with no duration.  Since they are grammatically
804 equivalent to notes, these commands can be used in the same places as
805 notes.
806
807 sect(Notation context)
808
809 COMMENT(This section is about translation contexts, a topic of LilyPond that
810 is somewhat advanced.  You don't have to understand this to use
811 LilyPond to print simple music.  If you don't want to typeset fancy
812 polyphonic music or tweak the LilyPond notation engine, you can skip
813 the next two sections.)
814
815 In bind(Section)ref(tutorial:more-staffs) it was explained that there
816 are more ways to notate a simple chord: as a single voice on a single
817 staff or in multiple staffs (and we'll soon see, that you can typeset
818 it as multiple voices on a staff).  Obviously the concept of staff is
819 not really something musical.  But what is it then?
820
821 The most simplistic explanation is: a staff is a graphic peculiarity
822 of the notation system.  In other words, a staff is a picture of five
823 lines on which one can print note heads.  We will call this view on
824 the concept of staff `staff symbol' from now on.
825
826 But 
827 there is more to a staff than just the symbol.  A staff
828 contains---besides a staff symbol--- some more components:
829 itemize(
830 it()A staff can have a key signature (printed at the left)
831 it()A staff can have a time signature (printed at the left)
832 it()A staff has bar lines
833 it()A staff has a clef (printed at the left)
834 ) COMMENT(
835
836 )
837 To explain what a staff really is, we'll try to print music without
838 these components.   Without those, it is still
839 possible to print music:
840 mudela()(\score{
841 \notes \relative c' {  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
842 \paper { 
843   linewidth = -1.;
844   \translator {
845   \StaffContext
846   \remove "Time_signature_engraver";
847   \remove "Bar_engraver";
848   \remove "Staff_symbol_engraver";
849   \remove "Clef_engraver";
850   \remove "Key_engraver";
851   }
852  }
853 })COMMENT(
854
855 ) As you can see, you can still make out the general form of the
856 melody and the rhythm that is to be played, but the notation is
857 difficult to read.  Moreover, the musical information is not complete.
858 The stress pattern in the notes can't be deduced from this output.
859 For this, we need a time signature:
860
861 mudela()(
862 \score {
863   \notes \relative c' {  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
864   \paper{
865   linewidth = -1.;
866 \translator{
867   \StaffContext
868   \remove "Bar_engraver";
869   \remove "Staff_symbol_engraver";
870   \remove "Clef_engraver";
871   \remove "Key_engraver";
872   }}
873  }) COMMENT(
874
875 )Technically speaking you can find out where the strong and weak beats are, but
876 it is difficult to find them quickly.   Bar lines  help you in finding
877 the location  of the notes within the measure:
878 mudela()(
879 \score {
880   \notes \relative c' {  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
881   \paper{
882   linewidth = -1.;
883 \translator{
884   \StaffContext
885   \remove "Staff_symbol_engraver";
886   \remove "Clef_engraver";
887   \remove "Key_engraver";}
888   }
889  }
890 )
891
892 We can remedy part of the difficulties with reading pitches by adding a staff
893 symbol:
894
895 mudela()(\score{
896   \notes\relative c' { \time 2/4; g'4 c,4
897 a'4 f4 e c d2 } \paper {
898   linewidth = -1.;
899   \translator {
900   \StaffContext
901
902   \remove "Clef_engraver";
903   \remove "Key_engraver";
904   }
905  }
906 })COMMENT(
907
908 )
909 This makes the output decidedly easier to read, but you still don't
910 know what the pitches of the notes above are.  So this is still not
911 enough.  But suppose you see the following notation:
912 mudela()(\score {
913   \notes \relative c' {\clef alto;  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
914   \paper {
915     linewidth = -1.;
916   }
917 })COMMENT(
918
919 ) Now you know the pitch of the notes: you look at the start of the line
920 and see a clef, and with this clef, you can determine the notated pitches.
921 You have found the em(context) in which the notation is to be
922 interpreted!
923
924 So the context determines the relationship between a piece of music
925 and its notation: you, the reader, use context to deduce music from
926 notation.  Because LilyPond writes notation, context works the
927 other way around for LilyPond: with context a piece of music can be
928 converted to notation.
929
930 We see that a staff forms context, and that context is needed to
931 convert between notation and music.  In LilyPond we turn around this
932 reasoning: LilyPond has a notion of notation context, and the staff is
933 just one example of a notation context.  In fact, the arguments of the
934 code(\type) command (Staff, GrandStaff) were all names of different
935 contexts.  A notation context is a conversion between music and
936 notation.
937
938 We make one final observation before listing the standard notation
939 contexts: a score can contain many staffs and a staff can contain many
940 voices.  This suggests that notation contexts are objects that can
941 contain other notation contexts.
942
943 The following
944  is a list in alfabetic order of the contexts that are supported by
945 LilyPond.  Each notation context is characterised by its name, the
946 notation elements it creates, and the contexts that it can contain.
947 description(
948
949
950 dit(GrandStaff) A code(GrandStaff) context contains code(Staff)
951   contexts, and it adds a brace to the output at the
952   nop(left.)footnote(The GrandStaff is quite limited, LilyPond can not
953   do cross staff beaming and slurring.)
954     
955     A code(GrandStaff) context can contain code(Staff)s. Typically, it
956   will contain two code(Staff)s, one treble staff, and one bass
957   staff. The bar lines of the contained staffs are connected
958   vertically.
959
960 dit(Lyrics) As its name suggests, The code(Lyrics) context deals with
961   typesetting lyrics.  This topic will be covered in
962   bind(Section)ref(tutorial:lyrics).
963   
964 dit(Score) The code(Score) context is the toplevel context: no context
965   can contain a code(Score) context.  The code(Score) context handles
966   the administration of time signatures.  It also makes sure that
967   items such as clefs, time signatures, and key-signatures are aligned
968   in columns across staffs.
969   
970   The code(Score) can contain code(Staff), code(StaffGroup),
971   code(Lyrics), code(GrandStaff) and code(RhythmicStaff) contexts.
972
973 dit(RhythmicStaff) The code(RhythmicStaff) context is like the staff,
974   but much simpler: the notes are printed on one line, and pitches are
975   ignored.  code(RhythmicStaff) can contain code(Voice)
976   contexts.
977
978
979 dit(Staff) The code(Staff) context handles clefs, bar lines, keys,
980   accidentals.  A code(Staff) context can contain code(Voice)
981   contexts.
982
983 dit(StaffGroup) A code(StaffGroup) context contains
984   code(Staff) or code(Lyrics) contexts, and prints a bracket at the
985   left.  The bar lines in the participating staffs are connected.
986
987   
988 dit(Voice) The code(Voice) context is a context that corresponds to a
989   voice on a staff.  This context handles the conversion of noteheads,
990   dynamic signs, stems, beams, super- and subscripts, slurs, ties and rests
991
992 COMMENT(do ChoireStaff)
993 )
994
995
996 If you are familiar with structured documents, you might see the
997 analogy of a context with a stylesheet: a stylesheet is neither
998 presentation nor information, but rather a recipe em(how) a specific
999 piece of information should be presented.  The big difference with
1000 stylesheets is that in music notation the elements provided by context
1001 are essential to understanding what is notated.
1002
1003 The notions of ``current clef'' and ``current position within the
1004 measure'' are all properties of notation contexts.  Commands like
1005 code(\clef) and code(\cadenza) change these properties, and this
1006 explains why they are fundamentally different from musical expressions
1007 like notes and rests.
1008
1009 A notation context is not a primitive element of LilyPond: in
1010 bind(Section)ref(tutorial:engravers) it will be explained how you can
1011 form your own notation contexts.
1012
1013 sect(Nested music: multiple staffs)
1014 label(tutorial:more-staffs)
1015
1016 Now we explain how to typeset music that runs in multiple staffs.
1017 Consider the following---somewhat unrealistic---example:
1018
1019 mudela(fragment)(
1020  \type GrandStaff <e'4 {\clef bass; g4^""} >
1021 )
1022
1023 In this example the music consists of two notes.  The above would
1024 sound the same if it were written as a single chord on a single staff,
1025 i.e., COMMENT(
1026
1027 )mudela(fragment)(
1028         <g4 e'4>
1029 )COMMENT(
1030
1031 ) The Mudela construct for multiple staffs reflects the similarity
1032 between the two examples: to get multiple staffs in Mudela you enter a
1033 chord, with an additional instruction to tell LilyPond that the chord
1034 does not represent notes stacked together, but staffs stacked
1035 together.
1036
1037 If a piece of music is to be interpreted as a staff, then this can be
1038 expressed with the code(\type) construct.  The following input says
1039 ``the quarter note with pitch e should be put on a staff.''
1040
1041 verb(
1042   \type Staff e'4 
1043 )COMMENT(
1044
1045 ) The same can be done for the other note, i.e.,
1046
1047 verb(
1048   \type Staff g4 
1049 ) COMMENT(
1050
1051 ) If you want to stack these staffs, you must create a chord of both:
1052
1053 verb(
1054   < \type Staff e'4
1055     \type Staff g4 
1056   >
1057 )
1058
1059 This looks reasonable, but the effect of this input is not what you
1060 might expect:
1061 mudela(fragment)(
1062   < \type Staff e'4
1063     \type Staff g4 
1064   >
1065 )COMMENT(
1066
1067 ) Since there are no names specified for the desired staffs, LilyPond
1068 thinks your wishes are fullfilled by putting the code(g) on the same
1069 staff as the code(e).  The correct solution is to label both staffs
1070 with different names, for example code(trebleStaff) and
1071 code(bassStaff).  This makes LilyPond distinguish between them, and
1072 create two staffs:
1073
1074 mudela(verbatim,fragment)(
1075   < \type Staff = trebleStaff e'4
1076     \type Staff = bassStaff  g4 
1077   >
1078 )COMMENT(
1079
1080 ) The names that you choose do not matter just as long as they are
1081 different.  This is almost right, except for the brace at the left and
1082 the clef of the second staff.  If you want a brace, then you have to
1083 tell LilyPond that the chord you just formed is to be interpreted as a
1084 so-called grand staff.  This is also done with the code(\type)
1085 command.  The bass clef is made with a clef command:  COMMENT(
1086
1087 ) mudela(verbatim,fragment)(
1088   \type GrandStaff <
1089         \type Staff = treblestaff e'4
1090         \type Staff = bassstaff { \clef "bass"; g4 }
1091   >)COMMENT(
1092
1093 )
1094
1095 sect(Polyphonic music (or: Notation context properties))
1096 label(sec:polyphonic)
1097
1098 In the section on notation contexts we explained that a notation
1099 context can have properties that influence the conversion from music
1100 to notation.  A simple example of such a property is the clef: the
1101 type of a clef helps determines the vertical position of note heads in
1102 a staff.  Some of these properties can be modified by commands such as
1103 code(\clef) and code(\time).  But notation contexts can have other
1104 properties, that are settable in
1105 a generic fashion.  We will demonstrate this feature by printing
1106 multiple voices on a staff.
1107
1108 Printing more than one voice on a staff is like printing multiple
1109 staffs stacked together.  This suggests that the template to follow is
1110 this:COMMENT(
1111
1112 )verb(
1113   \type Staff <
1114     \type Voice = one  ...
1115     \type Voice = two  ...
1116   >
1117 ) COMMENT(
1118
1119 ) On the ellipsis there should be music going from left to right, in
1120 other words, notes enclosed in braces.  Let us try the following counterpoint:COMMENT(
1121
1122 )mudela(fragment,verbatim)(
1123 \type "Staff" <
1124   \type "Voice" = "one" { r4 as'4 () as'4 g'4 }
1125   \type "Voice" = "two" { g'2 f'4 e'4 }
1126 >)
1127
1128 As you can see the result is not quite perfect.  The notes on the last
1129 two beats look like plain chords and not like separate voices.  What
1130 really happened was that the stems of the upper and lower voices were
1131 printed on top of each other.
1132
1133 To remedy this, engravers traditionally make the stems of the lower
1134 voice point down, and the stems of the upper up, as shown in
1135 bind(Figure)ref(tutorial:multi-voice-fig).
1136
1137 Surely the direction of a single stem is a property of the stem as a
1138 graphical object.  But the fact that all of the stems in a voice point
1139 in the same direction is not directly graphical.  Since this is a
1140 property shared by all the stems in the voice, this property is a
1141 property of the context code(Voice).  The context code(Voice) has an
1142 attribute whose value is the direction to use for stems.  You can
1143 change it to `up' by issuing the following phrase:
1144 verb(
1145   \property "Voice"."ydirection" = "1"
1146 )
1147
1148 This command should be read as ``change the property called
1149 code(ydirection) within the current code(Voice) context to the value
1150 code(-1).''  For the property code(ydirection) the value code(1) means
1151 `up', and code(-1) means `down'. So, the
1152 proper way to code the polyphonic example is given in
1153 bind(Figure)ref(tutorial:multi-voice-fig).
1154
1155 latexcommand(\begin{figure}[h])
1156 mudela(fragment,verbatim,center)(
1157   \type "Staff" <
1158     \type "Voice" =  "one"  {
1159       \property Voice.ydirection = "1"
1160       r4 as'4 () as'4 g'4 }
1161     \type "Voice" =  "two"  {
1162       \property Voice.ydirection = "-1"
1163       g'2 f'4 e'4 }
1164   >
1165 )
1166     latexcommand(\caption{Multiple voices})
1167     label(tutorial:multi-voice-fig)
1168 latexcommand(\end{figure})
1169
1170 As you can see, this property also controls the directions of slurs.
1171
1172 Other properties can also be set, and they can be within different
1173 contexts.  In general, you can set a property by specifying
1174 code(\property) var(contexttype)code(.)var(propertyname) code(=)
1175 var(value).  Both var(contexttype), var(propertyname) and var(value)
1176 should be strings.
1177
1178 The effect of a property is pretty much hardwired into the
1179 implementation (and thus subject to change), so we will not deal with
1180 all the possible properties in detail. Among other characteristics
1181 that can be set are the layout of slurs and beams.  The initialisation
1182 file file(property.ly) and the reference manual contain explanations
1183 of all properties.
1184
1185 sect(Lyrics)
1186 label(tutorial:lyrics)
1187
1188 Since a lyrics can have durations just like notes, we consider them to
1189 be music too.  Entering lyrics in mudela has two aspects. First, you
1190 have to enter the text, i.e., the syllables along with their
1191 durations.  After this, you have to specify how to convert these to
1192 graphics.
1193
1194 Lyrics consist of syllables, which are strings together with
1195 durations.  For entering lyrics we have to instruct LilyPond that what
1196 we enter are not note names but strings.  This instruction is the
1197 keyword code(\lyrics).  After entering this keyword you can enter a
1198 musical construct---sequential music, simultaneous music, code(\type)
1199 entries, etc.--- but with syllables instead of pitches.  For example:
1200 COMMENT(
1201
1202 )verb(\lyrics { 'got8 me on my knees4, Le-8 lie! })COMMENT(
1203
1204 ) Next comes the conversion to notation.  LilyPond can't (yet) figure
1205 out that lyrics need different treatment than notes.  As a result, the
1206 default conversion will try to put the text you entered as note heads
1207 onto a staff, and this will fail.  This default must be overridden
1208 with a code(\type) keyword.  Printing syllables of text in a line is
1209 done by a context called code(Lyrics).  You can select this context
1210 with the code(\type) keyword.  Here is a simple example with output:
1211 COMMENT(
1212
1213 )mudela(fragment,verbatim)(
1214 \type Lyrics \lyrics { 'got8 me on my knees,4 Le-8 lie! })COMMENT(
1215
1216 )The result is technically correct, but it needs a melody to make it
1217 performable: COMMENT(
1218
1219 )mudela(fragment,verbatim)(
1220   <
1221     \type Staff  { c''8. c''16 bes'8. a'16 g'4 f'8 g'4. }
1222     \type Lyrics \lyrics { 'got8. me16 on8. my16 knees,4 Le-8 lie!4. }
1223   >
1224 ) COMMENT(
1225
1226 ) The strings that makes up each syllable in the lyrics block are
1227 passed along to TeX() verbatim, so if you are proficient with TeX()
1228 you can do various nifty things.  Just keep in mind that a syllable
1229 either starts with a letter (a character in the range `code(a)' to
1230 `code(z)' or `code(A)' to `code(Z)'), or it is a string enclosed
1231 double quotes. It ends with either a number for the duration, or a space.
1232 These tricks are demonstrated in the following example:
1233 COMMENT(
1234
1235 urg
1236 \type Lyrics \lyrics { 'got8 m\textbf{e}8 on8. m$\cal_Y$16 knees,4 Le-8 lie!4.}
1237 \type Lyrics \lyrics { 'got8 m{\bf e}4 on8. m$\cal_Y$16 knees,4 Le-8 lie!4.}
1238 )COMMENT(
1239
1240 )mudela(fragment,verbatim)(<
1241   \type Staff  { c''8. c''16 bes'8. a'16 g'4 f'8 g'4. }
1242   \type Lyrics \lyrics { 'got8 me8 on8. m$\cal_Y$16 "3s,"4 Le-8 lie!4.}
1243 >
1244 )COMMENT(
1245
1246
1247 ) COMMENT(Groen is de kleur van geluk.  Dat geldt zeker voor Bj"ork)
1248
1249 sect(Toplevel Mudela)
1250
1251 Back in bind(Section)ref(sec:firsttune) we said we would ignore
1252 toplevel constructions (e.g., code(\score)) until some later moment.
1253 Now we will look at these constructions.
1254
1255
1256 Mudela allows you to name constructs of the language.  This is done by
1257 using an em(identifier). Generally you can define an
1258 identifier by entering code(identifierName = ... ) where there can be
1259 a variety of things on the ellipsis.
1260 COMMENT(
1261 Here is a (partial) list of what you can abbreviate with identifiers
1262 at top-level.
1263 itemize(
1264 it()The code(\score) block
1265 it()The code(\paper) block
1266 it()The code(\midi) block (to be explained in
1267   bind(Section)ref(tutorial:sound))
1268 it()Music (sequential music, simultaneous music etc.)
1269 it()Durations
1270 it()Strings
1271 it()Translators (to be explained in bind(Section)ref(tutorial:engravers))
1272 it()Integers
1273 it()Reals  
1274 )
1275 )
1276
1277 When you refer
1278 to the abbreviated entity, you must precede code(identifierName)
1279 with a backslash, i.e., code(\identifierName).  For example:
1280 mudela(verbatim)(
1281   czerny = \notes { [c16 g e g] }
1282   \score {
1283     \notes \type GrandStaff <
1284       { c''2 g''2 }
1285       { \clef bass; \czerny \czerny \czerny \czerny}
1286     >
1287     \paper {
1288       linewidth = -1.0;
1289     }
1290   }
1291 )
1292
1293 You can also see identifiers in action within the code(\paper) block:
1294 the value code(-1.0) is assigned to code(linewidth).  Within the
1295 code(\paper) block, identifiers are not used as abbreviation only:
1296 assigning to some identifiers can influence the output: in this case,
1297 the music line is printed at natural width.  
1298
1299 Recall the properties of a context, that could be set with
1300 code(\property).  It is a very general mechanism to tune the output of
1301 the music, that is neatly separated from the real music.
1302 Unfortunately, it is not convenient to type or read, and the precise
1303 effect of a setting property isn't always apparent from its
1304 definition.  To remedy this, we can use an identifier to capture the
1305 meaning of a code(\property).
1306
1307 mudela(verbatim)(
1308 stemup = \property Voice.ydirection = "1"
1309 stemdown = \property Voice.ydirection = "-1"
1310 shift = \property Voice.hshift = "1"
1311 \score {
1312   \type "Staff" \notes <
1313     \type "Voice" =  "one"  { \stemup r4 as'4 () as'4 g'4 }
1314     \type "Voice" =  "two"  { \stemup \shift g'2 f'4 e'4 }
1315     \type "Voice" = "three" { \stemdown [d'8 dis'] [d' cis'] [c' b] c'4 } >
1316   \paper{  linewidth = -1.0\pt; }
1317 })
1318
1319 Several abbreviations like code(\stemup) are defined in the standard
1320 initialisation file file(property.ly).  Setting or changing context
1321 properties can have a similar effect as the commands that were
1322 discussed in bind(Section)ref(sec:commands).  Don't be fooled by the
1323 similarity in appearance between a declared property-setting entry and
1324 a real command like code(\clef) or code(\bar).  Real commands are
1325 hardcoded into the language and they have to be terminated by
1326 semicolons.
1327
1328
1329 sect(Sound output)
1330 label(tutorial:sound)
1331
1332 You get output by combining music with definition a conversion to
1333 output.  LilyPond currently supports one other conversion besides
1334 notation: the conversion from abstract music to sound. You can have
1335 LilyPond play the music that you entered.  The format that is used for
1336 this output is MIDI.  The result is not very pretty, but it is useful
1337 for prooflistening your files: typing errors stand out when you
1338 listen, especially if they involve accidentals.
1339
1340 The only information that you need to enter is the
1341 tempo (Unfortunately, at this time, this the only thing that can be
1342 tuned.).  The syntax for the tempo is code(\tempo )var(duration) =
1343 var(beatsperminute);), for example: COMMENT(
1344
1345
1346 )verb(
1347 \score {
1348    ...music...
1349    \midi { \tempo 4 = 76; }
1350 }) COMMENT(
1351
1352 )
1353
1354 sect(Contexts revisited: engravers)
1355 label(tutorial:engravers)
1356
1357 As was promised, we will now take a dive into the more wizardrous parts
1358 of LilyPond: redefining (notation) contexts.  We previously explained
1359 that a context 
1360 itemize(
1361 it()has a name
1362 it()is a conversion from music to notation,
1363 it()can contain other contexts
1364 it()handles specific notation constructs
1365 )
1366
1367 This characterization almost automatically explains what the definition of a
1368 context should look like:
1369 itemize(
1370 it() It should define a name
1371 it()It should be part of the ``notation output definition,'' i.e., the
1372   code(\paper) block
1373 it()
1374   It should contain a specification of what other contexts may be contained
1375   in the context we're defining.
1376 it()
1377   It should contain a list of the notation constructs  to be
1378   handled.
1379 )
1380
1381 LilyPond can create notation for a large number of symbols.  This code
1382 is split up into basic building blocks. Each building block is called
1383 an em(engraver), and an engraver generally handles only one notation
1384 construct: the code(Clef_engraver) takes care of the clefs, the
1385 code(Time_signature_engraver) takes care of printing time signatures,
1386 etc.
1387
1388 A notation context is formed by a group of engravers.  A special class
1389 in LilyPond---the code(Engraver_group_engraver)---allows engravers to
1390 cooperate in a group, thus forming a notation context.  A variant of
1391 this grouping object is the code(Line_group_engraver_group), which
1392 puts the output of all the engravers (clefs, bars, etc) into a
1393 compound object, so that these symbols can be treated as a whole.
1394
1395 The following definition shows a simplified Staff context: COMMENT(
1396
1397 )verb(
1398 \translator
1399 {
1400         \type "Line_group_engraver_group";
1401         \name Staff ;
1402
1403         \consists "Bar_engraver";
1404         \consists "Clef_engraver";
1405         \consists "Key_engraver";
1406         \consists "Local_key_engraver";
1407         \consists "Time_signature_engraver";
1408         \consists "Staff_symbol_engraver";
1409
1410         defaultClef = treble;
1411           
1412         \accepts "Voice";
1413 }) COMMENT(
1414
1415 ) This context, named Staff, puts its graphic objects into a compound
1416 object (a so-called ``Line group'').  At Staff level, bars, clefs,
1417 keys, accidentals, time signatures and the staff symbol are handled.
1418 A staff can contain a Voice context.  You can also preset properties
1419 of contexts: for instance, the clef that is printed upon starting a
1420 Staff, is the treble clef.
1421
1422
1423
1424 As a practical example, we will show how to typeset polymetric music,
1425 i.e., music where the meter can differ for each staff.  The solution
1426 is not very complicated: normally all timing information (time
1427 signature, rhythmic grouping) is synchronised across each staff.  This
1428 is done by having only one administration for timing information: in
1429 the default configuration there is only one code(Timing_engraver), in
1430 the code(Score) context.
1431
1432 All staffs use the information in the code(Timing_engraver) for
1433 generating bar lines and time signatures.  So, we can have different
1434 timing for every staff, by moving the code(Timing_engraver) into the
1435  Staff context.
1436
1437 You can also declare contexts, and reference them.  This is useful in
1438 combination with code(\remove "..."): code(\remove) does the opposite
1439 of code(\consists).
1440
1441
1442 mudela(verbatim)(
1443   \score {
1444    \notes <
1445      \type Staff = one { \time 2/4; c'4 c'4 c'4 c'4 c'4 c'4 }
1446      \type Staff = two { \time 3/4; c'4 c'4 c'4 c'4 c'4 c'4 }
1447    >
1448    \paper { 
1449      linewidth = -1.;
1450      \translator {
1451        \ScoreContext
1452        \remove "Timing_engraver";
1453      }
1454      \translator {
1455        \StaffContext
1456        \consists "Timing_engraver";
1457      }
1458   }
1459 }
1460 )
1461
1462 The context definitions provided as a default are in the standard
1463 initialisation file file(engraver.ly).
1464
1465
1466 sect(Urtexts and context selection)
1467 label(tutorial:urtext)
1468
1469 In bind(Section)ref(tutorial:more-staffs), we have shown you how to
1470 make multiple staffs, and explained that you have to label every staff
1471 (or more precisely: different contexts), to make sure that new ones
1472 are created when you need them.  In this section, the real power of
1473 this mechanism will be unveiled.
1474
1475 By naming other contexts that you create, you can reference other
1476 contexts than the current context from within the music.  For example,
1477 from within the music that you enter for staff code(One), one could
1478 enter a small piece of music, and send it to staff code(Two), e.g.,
1479 COMMENT(
1480
1481 ) mudela(fragment,verbatim)(
1482   <
1483     \type Staff = one { c''4 \type Staff = two { c4 c4 } c''4 }
1484     \type Staff = two { \clef bass; g,4 g,4 g,4 g,4  }    
1485   >
1486 )
1487
1488
1489
1490 The mechanism of context selection can be used to fabricate an
1491 nop(Urtext)footnote(em(Urtext) is the German word for `original text'.
1492 The Urtext edition of a piece of music, is an edition that reflects
1493 the original writing of the composer.  Such editions are useful for
1494 musicologists, and performers that want to perform authentic
1495 interpretations.  However, for mere mortals, the Urtext can be quite
1496 hard to read.  It might not contain fingering and beaming, and
1497 typically it is full of footnotes.  Moreover, common interpretations
1498 may have emerged---after the composer died.  For this reason, the
1499 music that can be had as Urtext usually is also available in enhanced
1500 and edited editions.  ) and an edited edition from em(one source).  We
1501 will use the first few bars of bind(J.)bind(S.)Bach's first Cello
1502 suite to demonstrate this.  The example makes heavy use of space
1503 rests; here they are used as a placeholder to attach articulation
1504 marks to.  
1505
1506 mudela(verbatim)(
1507   bach =  \notes \relative c { [g16 d' b' a] [b d, b' d,] }
1508   
1509   staffStuff = \notes { \clef bass; \time 4/4; s1 \bar "|."; }
1510   
1511   slursOne = \notes { s16( s s s s16 s s )s }
1512   slursTwo = \notes { s16-. s s() s s16() s  s ()s }
1513
1514   \score{
1515     { < \type Voice = celloVoice { \bach \bach }
1516         \type Voice = celloVoice { \slursOne \slursOne }
1517         \staffStuff
1518       >
1519       <
1520         \type Voice = celloVoice { \bach \bach }
1521         \type Voice = celloVoice { \slursTwo \slursTwo }
1522         \staffStuff
1523       >
1524     }
1525     \paper { linewidth = -1.;}
1526   }
1527 )
1528
1529 The slurs that you define should be put on the music that is defined
1530 by the code(\bach) identifier.  By labeling a code(Voice) context, and
1531 directing both the articulation and the notes to that same code(Voice)
1532 context, the articulation is put over the right notes.
1533
1534
1535 sect(Transposing)
1536 label(tutorial:more-grammar)
1537
1538
1539 One of the things that you can do with music is em(transposing) it.
1540 If you want to transpose a piece of music, then you should prefix it
1541 with the keyword code(\transpose) along with the pitch (relative to
1542 the central C, i.e., code(c')) for the transposition.footnote(the
1543 code(\type Staff) is to make sure that no separate staffs are created
1544 for the code(\scale) and code(\transpose cis' \scale) part.)
1545
1546
1547 mudela(verbatim)(
1548 scale = \notes \relative c' { [c8 d e f] }
1549 \score {
1550   \notes {
1551     \type Staff { \scale \transpose cis'  \scale }
1552     }
1553   \paper { linewidth = -1.0; }
1554 })
1555
1556
1557 sect(Staff switching)
1558
1559 We have seen that contexts can be nested.  This means that they form a
1560 tree.  It is possible to edit this tree: for example, a code(Voice)
1561 context can be taken out of a code(Staff) context, and put into
1562 another.  This has the effect of the voice switching staffs (something
1563 that often happens in keyboard music).  The syntax for this operation
1564 with these particular contexts is code(\translator Staff = newStaffName).
1565
1566 The effect is analogous to the first example in section
1567 ref(tutorial:urtext), but with the code(\translator) construction it
1568 is possible to split the real music and the commands that determine in
1569 which staff the music is printed.  For example:
1570
1571 mudela(verbatim)(
1572
1573 % real music
1574 aVoice = \type Voice = voiceA \notes { c''4 c4 c4 c''4 }
1575 bVoice = \type Voice = voiceB \notes { g,4 g,4 g,4 g,4  }    
1576
1577 % staff switching stuff
1578 switch = \type Voice = voiceA \notes { s4 \translator Staff = staffB s4
1579                    s4 \translator Staff = staffA s4 }
1580
1581 \score {
1582   <
1583     \type Staff = staffA < \aVoice \switch >
1584     \type Staff = staffB < \bVoice \clef bass; >
1585   >
1586   \paper { linewidth = -1.; }
1587 }
1588 )
1589
1590 Don't try to switch staffs when you are in the middle of a slur or
1591 beam, though: the spanners won't switch along.
1592
1593 sect(Tuplets)
1594
1595 The notes in a triplet take 2/3 of their notated duration.  The syntax
1596 for triplet in LilyPond reflects this.  To make a triplet, you enter
1597 COMMENT(
1598
1599 )mudela(fragment,center)(
1600         \type Voice \times 2/3 { c''4 c''4 c''4 }
1601 )COMMENT(
1602
1603 )Since tuplet brackets are printed by the Voice context, a Voice
1604 context must exist before code(\times) is interpreted.  To ensure this,
1605 a context is instantiated explicitly with code(\type Voice).  You
1606 don't need this, if a Voice context is already instantiated, like in
1607 the following example:
1608 COMMENT(
1609
1610 )mudela(fragment,center)(
1611         c''4 \times 2/3 { c''4 c''4 c''4 } c''4
1612 )COMMENT(
1613
1614 )
1615
1616 Of course, you can also use different ratios, and use beamed notes or
1617 rests:
1618 mudela(fragment)(
1619         \type Voice \times 4/5 { [c''8 c''16 c''16] r8 [g'8 g'8] }
1620 )
1621 If you make a tuplet of  beamed notes, where the beam is as wide as
1622 the bracket, the bracket is omitted.
1623
1624 sect(Repeats)
1625