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