]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/tutorial.itely
release: 1.3.128
[lilypond.git] / Documentation / user / tutorial.itely
1 @c -*-texinfo-*-
2
3 @node Tutorial
4 @chapter Tutorial
5
6 @menu
7 * Introduction::                   Introduction
8 * Running LilyPond::               Getting started
9 * The first tune::                 The first tune
10 * Lyrics and chords::              Lyrics and chords
11 * More movements::                 More than one movement in a file
12 * A piano excerpt::                    Piano music
13 * end of tutorial::                The end
14 @end menu
15
16 @node Introduction
17 @section Introduction
18
19   
20 LilyPond prints music from a specification that you, the user, supply.
21 You have to give that specification using a @emph{language}.  This
22 chapter is a gentle introduction to that language.
23
24 This tutorial will demonstrate how to use Lilypond by presenting
25 examples of input along with resulting output.  We will use English
26 terms for notation.  In case you are not familiar with those, you may
27 consult the glossary that is distributed with LilyPond.
28
29 @cindex examples, tutorial
30
31 The examples discussed are included in the distribution, in the
32 subdirectory @file{input/tutorial/}.@footnote{When we refer
33 to filenames, they are relative to the top directory of the source
34 package.
35 @cindex file names
36 } We recommend that you experiment with writing Lilypond input yourself,
37 to get a feel for how the program behaves.
38
39
40 @node Running LilyPond
41 @section Running LilyPond
42
43 Before we dive into describing the input language of LilyPond, we first
44 show you through the procedure for getting notes on your screen and out
45 of your printer.
46
47 The first step is creating an input file. Using your favorite
48 text-editor, create @file{test.ly} containing
49
50 @example
51 \header @{
52  title = "Test";
53 @}
54
55 \score @{
56   \notes @{ c'4 e'4 g'4 @}
57   \paper @{ @}
58 @} 
59 @end example
60
61 @unnumberedsubsec Unix
62
63 @cindex Unix, Running lilypond on
64
65 If you run Unix, proceed as follows: run lilypond on the file, i.e.,
66 @example
67   lilypond test
68 @end example
69 You will the following on your screen:
70 @example
71 GNU LilyPond 1.3.125.
72 Now processing: `input/tutorial/test.ly'
73 Parsing...
74 Interpreting music...[1]
75 Preprocessing elements... 
76 Calculating column positions... [2]
77 paper output to test.tex...
78 @end example
79
80 Now, run @TeX{}@footnote{@TeX{} is a text-typesetting system that is
81 especially suited for typesetting mathematics}. The result should
82 resemble this: 
83 @example
84 This is TeX, Version 3.14159 (Web2C 7.3.1)
85 (test.tex (/home/hanwen/usr/share/lilypond/tex/lilyponddefs.tex
86 (/home/hanwen/usr/share/lilypond/tex/lilypond-plaintex.tex
87 LilyPond Plain TeX settings) (/home/hanwen/usr/src/  ...
88 (/home/hanwen/usr/share/lilypond/tex/lily-ps-defs.tex) [footer empty]
89 (/home/hanwen/usr/share/lilypond/tex/fetdefs.tex)) [1] )
90 Output written on test.dvi (1 page, 3716 bytes).
91 Transcript written on test.log.
92 @end example
93 The result of the @TeX{} run is a @TeX{} ``DeVice Independent'' file
94 (@file{test.dvi}).
95 @cindex DVI file
96 @cindex @TeX{}
97
98 @cindex Viewing music
99 @cindex @code{xdvi}
100 To view the output, run Xdvi, i.e.
101 @example
102         xdvi test
103 @end example
104 You should will see this
105 @lilypond
106 \header {
107  title = "Test";
108 }
109
110 \score {
111   \notes { c'4 e'4 g'4 }
112   \paper { }
113
114 @end lilypond
115 along with some buttons in a window.
116
117 @cindex postscript, converting to
118 When you're satisfied with the result, you can print it. For printing,
119 you have to generate a postscript file:
120 @example
121         dvips -o test.ps test.dvi
122 @end example
123 which looks like this:
124 @example
125 This is dvips(k) 5.86 Copyright 1999 Radical Eye Soft ...
126 ' TeX output 2001.01.27:1806' -> test.ps
127 <texc.pro><special.pro>. [1]
128 @end example
129
130 @cindex PostScript
131 @cindex Printing output
132 @cindex GhostScript
133 @cindex @code{lpr}
134 PostScript is a page description language, similar to PDF. Some printers
135 can understand a postscript file directly, but the cheapers need the
136 intervention of GhostScript, a PostScript   emulator that runs on your
137 computer instead of your printer. Most Linux distributions nowadays have
138 GhostScript running ``in the  background'', so any configured printer
139 will act as a PostScript printer.  Assuming this,  the
140 following command will print the  file
141 @example
142         lpr test.ps
143 @end example
144 If this does not make your printer produce a page of music, then you
145 should look into installing and configuring ghostscript.  Refer to
146 GhostScript's website at @uref{http://www.ghostscript.com}.  
147
148 There are two different routes: firstly, you can add titling to the
149 output. This is done by a separate program called ly2dvi: this program
150 first calls LilyPond to process the @file{.ly} file, and then runs
151 @TeX{} on it to produce a @file{.dvi} file with proper margin settings
152 and titling.
153
154 @cindex titles, adding
155 @cindex ly2dvi
156
157 @example
158         ly2dvi test.ly
159 @end example
160 After some disk-activity, you should end up with a @file{.dvi} file.
161 6
162 Secondly, you can generate PostScript directly. This is not very useful
163 currently, but here goes:
164 @cindex PostScript output
165 @example
166       lilypond -f ps test.ly  
167 @end example
168
169 [treat FAQs here, eg. about env vars.]
170
171
172 @unnumberedsubsec Windows
173
174 [todo]
175
176
177 @node The first tune
178 @section The first tune
179
180
181 To demonstrate what LilyPond input looks like, we start off with a
182 full-fledged, yet simple example. It is a convoluted version
183 of the famous menuet in J. S. Bach's @emph{Klavierb@"uchlein}. The file
184 is included in the distribution as  @file{menuet.ly}.
185 @cindex Bach, Johann Sebastian 
186
187 @lilypond[verbatim]
188 % lines preceded by a percent are comments which
189 % are ignored by Lilypond.
190 \include "paper16.ly"
191 \score {
192     \notes                        
193     \relative c'' \sequential{                
194             \time 3/4;                
195             \key g \major;
196
197         \repeat "volta" 2 {
198             d4 g,8 a b c d4 g, g |
199             e'4 c8 d e fis g4 g, g |
200             c4 d8()c b a( )b4 c8 b a g |
201             a4 [b8 a] [g fis] g2.  |
202         }
203
204         b'4 g8 a b g
205         a4 d,8 e fis d |
206         g4 e8 fis g d cis4 b8 cis a4 |
207         a8-. b-. cis-. d-. e-. fis-.
208         g4 fis e |
209         fis a,  r8 cis8
210         d2.-\fermata
211         \bar "|.";
212     }
213     \paper {
214        % standard settings are too wide for a book
215        linewidth = 14.0 \cm;
216    }
217 }
218 @end lilypond
219
220 We will analyse the input, line by line. 
221 @example
222         % lines preceded by a percent are comments which
223         % are ignored by Lilypond.
224 @end example 
225 The percent sign, @code{%}, introduces a line comment.  If you want to
226 make larger comments, you can use block comments. These are delimited
227 by @code{%@{} and @code{%@}}
228 @cindex comment
229 @cindex block comment
230 @cindex line comment
231
232 @example 
233
234         \include "paper16.ly"
235  
236 @end example
237 @cindex @code{\include}
238 @cindex point, printer's
239 @cindex staff size setting 
240 By default, LilyPond will use definitions for a staff that is 20
241 point@footnote {A point is the standard measure of length for printing;
242 one point is 1/72.27 inch. [TODO: mm vs. pt]} high.  We want smaller
243 output (16 point staff height), so we must import the settings for that
244 size, which is done here.
245 @example 
246
247         \score @{
248  
249 @end example 
250 A lilypond file combines music with directions for outputting that
251 music.  The music is combined with the output directions by putting
252 them into a @code{\score} block.
253 @example 
254
255         \notes                
256  
257 @end example 
258  This makes LilyPond ready for accepting notes.
259 @example 
260
261         \relative c''
262  
263 @end example
264
265 @cindex octaves, choosing
266 @cindex pitch
267 As we will see, pitches are combinations of octave, note name and
268 chromatic alteration.  In this scheme, the octave is indicated by
269 using raised quotes (@code{'}) and ``lowered'' quotes (commas:
270 @code{,}).  The central C is denoted by @code{c'}.  The C one octave
271 higher is @code{c''}.  One and two octaves below the central C is
272 denoted by @code{c} and @code{c,} respectively.
273
274 @cindex relative
275 For pitches in a long piece you might have to type many quotes.  To
276 remedy this, LilyPond has a ``relative'' octave entry mode.  In this
277 mode, octaves of notes without quotes are chosen such that a note is
278 as close as possible (graphically, on the staff) to the the preceding
279 note.  If you add a high-quote an extra octave is added.  The lowered
280 quote (a comma) will subtract an extra octave.  Because the first note
281 has no predecessor, you have to give the (absolute) pitch of the note
282 to start with.
283 @example 
284
285         \sequential @{
286  
287 @end example 
288 What follows is sequential music, i.e.,
289 @cindex sequential music
290 notes that are to be played and printed after each other.
291 @example 
292
293         \time 3/4;
294  
295 @end example
296 @cindex time signature, setting
297 @cindex @code{\time}
298   This command changes the time signature of the current piece: a 3/4
299 sign is printed.  This command is also used to generate bar lines in
300 the right spots.
301 @example 
302
303         \key g \major;
304  
305 @end example
306 @cindex key signature, setting
307 @cindex @code{\key}
308   This command changes the current key signature to G-major.  Although this
309 command comes after the @code{\time} command, in the output, the key
310 signature comes before the time signature: LilyPond knows about music
311 typesetting conventions.
312 @example 
313
314         \repeat "volta" 2
315  
316 @end example 
317   This command tells LilyPond that the following piece of music must be
318 played twice. The first argument indicates the type of repeat. In this
319 case, @code{"volta"} means that volta brackets are be used for
320 alternatives---if there were any.
321 @example 
322
323         @{
324  
325 @end example 
326 The subject of the repeat is again sequential music.  Since
327 @code{\sequential} is such a common construct, a shorthand is provided:
328 just leave off @code{\sequential}, and the result is the same.
329 @example 
330
331         d4
332  
333 @end example 
334  This is a note with pitch @code{d} (determined up to octaves).  The
335 relative music was started with a @code{c''}, so the real pitch of this
336 note is @code{d''}.  The @code{4} designates the duration of the note
337 (it is a quarter note).
338 @example 
339
340         a b
341  
342 @end example 
343 These are notes with pitch @code{a} and @code{b}.  Because their
344 duration is the same as the @code{g}, there is no need to enter the
345 duration (You may enter it anyway, e.g. @code{a4 b4})
346 @example 
347
348         d4 g, g |
349  
350 @end example
351 @cindex bar check
352 @cindex @code{|}
353 @cindex errors, finding 
354  Three more notes.  The @code{|} character is a `bar check'.  When
355 processing the music, LilyPond will verify that bar checks are found at
356 the start of a measure.  This can help you track down errors.
357
358 @cindex alteration, chromatic
359 @cindex chromatic alteration
360 So far, no notes were chromatically altered.  Here is the first one
361 that is: @code{fis}. Lilypond by default uses Dutch note names, and
362 ``Fis'' is the Dutch note name for ``F sharp''.  However, there is no
363 sharp sign in the output. The program keeps track of key signatures,
364 and will only print accidentals if they are needed.
365 @example 
366
367         c8 d e fis
368  
369 @end example 
370 LilyPond guesses were beams can be added to eighth and shorter notes.
371 In this case, a beam over 4 eighths is added.
372 @example 
373
374         c4 d8( )c b a( )b4 c8 b a g |
375  
376 @end example 
377   The next line shows how to make a slur: the beginning and ending note
378 of the slur is marked with an opening and closing parenthesis
379 respectively.  In the line shown above, this is done for two slurs.
380 Slur markers (parentheses) are put between the slurred notes.
381 @example 
382
383         a4 [b8 a] [g fis] 
384  
385 @end example 
386 Automatic beaming can be overridden by inserting beam marks
387 (brackets).  Brackets are put around the notes you want beamed.
388 @example 
389
390         g2.  |
391  
392 @end example
393 @cindex augmentation dot
394 @cindex dot
395 A duration with augmentation dot  is notated
396 with the duration number followed by a period.
397 @example 
398
399         @}
400  
401 @end example 
402   This ends the sequential music to be repeated.  LilyPond will typeset
403 a repeat bar.
404 @example 
405
406         cis'4 b8 cis a4 |
407  
408 @end example 
409  This line shows that Lily will print an accidental if that is
410 needed: the first C sharp of the bar will be printed with an accidental,
411 the second one without.
412 @example 
413
414         a8-. b-. cis-. d-. e-. fis-.
415  
416 @end example
417 @cindex articulation
418 You can enter articulation signs either in a verbose form using a
419 shorthand.  Here we demonstrate the shorthand: it is formed by a dash
420 and the character for the articulation to use, e.g. @code{-.} for
421 staccato as shown above.
422 @example 
423
424         fis a, r8 cis8
425  
426 @end example 
427  
428 Rests are denoted by the special notename @code{r}.  
429 @example 
430
431         d2.-\fermata
432  
433 @end example 
434  All articulations have a verbose form, like @code{\fermata}.  The
435 command @code{\fermata} is not part of the core of the language (most
436 of the other discussed elements are), but it is a shorthand for a more
437 complicated description of a fermata.  @code{\fermata} names that
438 description and is therefore called an identifier.
439 @cindex identifier
440 @cindex @code{\fermata}
441 @example 
442
443         @}
444  
445 @end example 
446  
447 Here the music ends.
448 @example 
449
450         \paper @{
451                 linewidth = 14.0\cm;
452         @}
453  
454 @end example 
455 This specifies a conversion from music to notation output.  Most of
456 the details of this conversions (font sizes, dimensions, etc.) have
457 been taken care of, but  to fit the output  in this document, it has
458 to be smaller.  We do this by setting the line width to 14 centimeters
459 (approximately 5.5 inches).
460 @example 
461
462         @}
463  
464 @end example 
465 The last brace ends the @code{\score} block.
466
467
468
469
470 @node Lyrics and chords
471 @section Lyrics and chords
472
473 In this section we show how to typeset a song.@footnote{The author would
474 welcome information about the origin of this song.}. This file is
475 included as @file{flowing.ly}.
476
477 @example 
478 \header @{
479         title = "The river is flowing";
480         composer = "Traditional (?)";
481 @}
482 \include "paper16.ly"
483 melody = \notes \relative c' @{
484         \partial 8;
485         \key c \minor;
486         g8 |
487         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
488         c4 c8 d [es () d] c4 | d4 es8 d c4.
489         \bar "|.";
490 @}
491
492 text = \lyrics @{
493         The ri -- ver is flo- __ wing, flo -- wing and gro -- wing, the
494         ri -- ver is flo -- wing down to the sea.
495 @}
496
497 accompaniment =\chords @{
498         r8
499         c2:3- f:3-.7 d:min es4 c8:min r8
500         c2:min f:min7 g:7^3.5 c:min @}
501
502 \score @{
503         \simultaneous @{
504 %         \accompaniment
505           \context ChordNames \accompaniment
506
507           \addlyrics
508             \context Staff = mel @{        
509               \property Staff.noAutoBeaming = ##t
510               \property Staff.automaticMelismata = ##t
511               \melody 
512             @}
513             \context Lyrics \text
514         @}
515         \midi  @{ \tempo 4=72;@}
516         \paper @{ linewidth = 10.0\cm; @}
517 @} 
518 @end example 
519
520
521 The result would look this@footnote{The titling and font size shown
522 may differ, since the titling in this document is not generated by
523 @file{ly2dvi}.}.
524
525 @center @strong{The river is flowing}
526 @center Traditional 
527
528 @lilypond[center]
529 \header {
530         title = "The river is flowing";
531         composer = "Traditional (?)";
532 }
533 \include "paper16.ly"
534 melody = \notes \relative c' {
535         \partial 8;
536         \key c \minor;
537         g8 |
538         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
539         c4 c8 d [es () d] c4 | d4 es8 d c4.
540         \bar "|.";
541 }
542
543 text = \lyrics {
544         The ri -- ver is flo- __ wing, flo -- wing and gro -- wing, the
545         ri -- ver is flo -- wing down to the sea.
546 }
547
548 accompaniment =\chords {
549         r8
550         c2:3- f:3-.7 d:min es4 c8:min r8
551         c2:min f:min7 g:7^3.5 c:min }
552
553 \score {
554         \simultaneous {
555 %         \accompaniment
556           \context ChordNames \accompaniment
557
558           \addlyrics
559             \context Staff = mel {
560               \property Staff.noAutoBeaming = ##t
561               \property Staff.automaticMelismata = ##t
562               \melody 
563             }
564             \context Lyrics \text
565         }
566         \midi  { \tempo 4=72;}
567         \paper { linewidth = 10.0\cm; }
568 }
569 @end lilypond
570
571 Again, we will dissect the file line by line.
572 @example 
573
574         \header @{
575  
576 @end example
577 @cindex @code{\header}
578 Information about the music you are about to typeset goes into a
579 @code{\header} block.  The information in this block is not used by
580 LilyPond, but it is passed into the output.  @file{ly2dvi} uses this
581 information to print titles above the music.
582 @example 
583
584         title = "The river is flowing";
585         composer = "Traditional (?)"; 
586 @end example
587 @cindex assignments
588 @cindex identifier assignment
589 the @code{\header} block contains assignments.  An assignment starts
590 with a string.  (which is unquoted, in this case). Then comes the
591 equal sign.  After the equal sign comes the expression you
592 want to store.  In this case, you want to put in strings.  The
593 information has to be quoted here, because it contains spaces. Each
594 assignment is finished with a semicolon.
595 @example 
596
597         \include "paper16.ly"
598  
599 @end example
600 Smaller size for inclusion in a book.
601 @example 
602
603         melody = \notes \relative c' @{
604  
605 @end example 
606 The structure of the file will be the same as the previous one, a
607 @code{\score} block with music in it.  To keep things readable, we will
608 give names to the different parts of music, and use the names to
609 construct the music within the score block.
610
611 @example 
612         \partial 8;
613 @end example 
614
615 @cindex @code{\partial}
616 @cindex anacrusis
617 The piece starts with an anacrusis of one eighth.
618 @example
619         \key c \minor;
620 @end example
621 The key is C minor: we have three flats.
622
623 @example 
624
625         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
626         c4 c8 d [es () d] c4 | d4 es8 d c4.
627         \bar "|.";
628  
629 @end example 
630
631 @cindex manual beaming
632 @cindex automatic beaming, turning off
633 We use explicit beaming.  Since this is a song,  we will turn automatic
634 beams off, and use explicit beaming where needed.
635 @example 
636
637         @}
638  
639 @end example 
640 This ends the definition of @code{melody}.  Note that there are no
641 semicolons after assignments at top level.
642 @example 
643
644         text = \lyrics @{
645  
646 @end example
647 @cindex lyrics
648 @cindex identifier assignment
649 @cindex syllables, entering
650 Another identifier assignment.  This one is for the lyrics. 
651 Lyrics are formed by syllables that have duration, and not by
652 notes. To make LilyPond parse words as syllables,  switch it  into
653 lyrics mode with @code{\lyrics}.  Again, the brace after @code{\lyrics}
654 is a shorthand for @code{\sequential @{}.
655 @example 
656
657   The4 ri -- ver is flo- __ wing,  flo -- wing and gro -- wing, the
658   ri- ver is flo- __ wing down to the sea.
659 @}
660  
661 @end example
662 @cindex extenders, lyric
663 @cindex hyphens, lyric 
664 The syllables  themselves are  separated by spaces.  You can get syllable
665 extenders by entering @code{__}, and centered hyphens with
666 `@code{-}@code{-}'.  We enter the syllables as if they are all quarter notes
667 in length (hence the @code{4}), and use a feature to align the
668 syllables to the music (which obviously isn't all quarter notes.)
669 @example 
670
671         accompaniment =\chords @{
672  
673 @end example
674 @cindex chords
675 @cindex mode, chords
676 We'll put chords over the music, to enter them, there is a special mode,
677 called @code{\chords}.  There is a special mode (analogous
678 to @code{\lyrics} and @code{\notes} mode) where you can give the names
679 of the chords you want, instead of the notes comprising the chord.
680 @example 
681
682         r8
683  
684 @end example 
685 There is no accompaniment during the anacrusis.
686 @example 
687
688         c2:3- f:3-.7
689  
690 @end example
691
692 @cindex tonic
693 @cindex chord modifier
694 @cindex modifier, chord 
695 A chord is started by  the tonic of the chord. The
696 first one lasts a half note.  An unadorned note creates a major
697 triad, while a minor triad is wanted.  @code{3-} modifies the third to
698 be small. @code{7} modifies (adds) a seventh, which is small by default
699 to create the @code{f a c es} chord.  Multiple modifiers must be
700 separated by a dot.
701 @example 
702
703         d:min es4 c8:min r8
704  
705 @end example
706
707 Some modifiers have predefined names, eg. @code{min} is  the same as
708 @code{3-}, so @code{d-min} is a minor @code{d} chord.
709 @example 
710
711         c2:min f:min7 g:7^3.5 c:min @}
712  
713 @end example
714 @cindex named modifier
715
716 A named modifier @code{min} and a normal modifier @code{7} do not have
717 to be separated by a dot.  Tones from a chord are removed with chord
718 subtractions.  Subtractions are started with a caret, and they are
719 also separated by dots.  In this example, @code{g:7^3.5} produces a
720 minor seventh.  The brace ends the sequential music.
721 @example 
722
723         \score @{
724                 \simultaneous @{
725  
726 @end example 
727 We assemble the music in the @code{\score} block.  Melody, lyrics and
728 accompaniment have to sound at the same time, so they should be
729 @code{\simultaneous}.
730 @cindex @code{\simultaneous}
731 @example 
732
733         %\accompaniment
734  
735 @end example 
736 Chord mode generates notes grouped in @code{\simultaneous} music.  If
737 you remove the comment sign, you can see the chords in normal
738 notation: they will be printed as note heads on a separate
739 staff.
740 @example 
741
742         \context ChordNames \accompaniment
743  
744 @end example
745 @cindex context
746 @cindex interpretation context
747 @cindex notation context
748 Normally, the notes that you enter are transformed into note heads.
749 The note heads alone make no sense, they need surrounding information:
750 a key signature, a clef, staff lines, etc.  They need @emph{context}.  In
751 LilyPond, these symbols are created by objects called `interpretation
752 contexts'.  Interpretation contexts only exist during a run of
753 LilyPond.  Interpretation contexts that are for printing music (as
754 opposed to playing music) are called `notation contexts'.
755
756 By default, LilyPond will create a Staff context for you.  If you
757 removed the @code{%} sign in the previous line, you would see that
758 mechanism in action.
759
760 We don't want that default here, because we want chord names, not note heads.
761 An interpretation context can also created upon explicit request. The
762 keyword for such a request is @code{\context}.  It takes two arguments.
763 The first is the name of an interpretation context.  The name is a
764 string, it can be quoted with double quotes).  The second argument is
765 the music that should be interpreted in this context.  For the previous
766 line, we could have written @code{\context Staff \accompaniment}, and
767 get the same effect.
768 @example 
769
770         \addlyrics
771  
772 @end example
773 @cindex @code{\addlyrics}
774 @cindex lyrics and melody, combining
775 @cindex combining lyrics and melody
776
777 The lyrics need to be aligned with the melody.  This is done by
778 combining both with @code{\addlyrics}.  @code{\addlyrics} takes two
779 pieces of music (usually a melody and lyrics, in that order) and
780 aligns the syllables of the second piece under the notes of the
781 first piece.  If you would reverse the order, the notes would be
782 aligned on the lyrics, which is not very useful. (Besides, it looks
783 silly.)
784 @example 
785
786         \context Staff = mel @{
787  
788 @end example
789
790 This is the argument of @code{\addlyrics}.  We instantiate a
791 @code{Staff} context explicitly: should you chose to remove the comment
792 before the ``note heads'' version of the accompaniment, the
793 accompaniment will be on a nameless staff.  The melody has to be on a
794 different staff as the accompaniment.  This is accomplished by giving
795 the melody staff a different name.
796 @example 
797
798         \property Staff.noAutoBeaming = ##t
799  
800 @end example
801 @cindex \property
802 @cindex context variables
803 @cindex setting context variables
804 An interpretation context has variables that tune its behaviour.  One of
805 the variables is @code{noAutoBeaming}.  If set to @code{##t}, which is
806 the boolean value @var{true}, LilyPond will not try to put automatic beaming
807 on the current staff.
808
809 @cindex GUILE
810 @cindex Scheme
811 @cindex accessinng Scheme
812 @cindex evaluating Scheme
813 @cindex LISP
814
815 LilyPond internally uses GUILE, a Scheme-interpreter@footnote{Scheme is
816 a language from the LISP family. You can learn more about Scheme at
817 @uref{http://www.scheme.org}.} to represent data throughout the whole
818 program. The hash-sign (@code{#}) accesses GUILE directly: the code
819 following the hash-sign is evaluated as Scheme.  The boolean value
820 @var{true} is @code{#t} in Scheme, so for LilyPond @var{true} looks like
821 @code{##t}
822
823 @example 
824
825         \property Staff.automaticMelismata = ##t
826  
827 @end example
828 @cindex automaticMelismata
829 @cindex melismata
830 @cindex @code{\addlyrics} and slurs
831 Similarly, we  don't want to print a  syllable when there is
832 a slur. This sets up @code{\addlyrics} to not put lyrics under notes
833 while there is a  slur.
834 @example 
835
836           \melody
837         @}
838  
839 @end example 
840 Finally, we put the melody on the current staff.  Note that the
841 @code{\property} directives and @code{\melody} are grouped in sequential
842 music,  so the property settings are done before the melody is
843 processed.
844 @example 
845
846         \context Lyrics \text
847  
848 @end example 
849 The second argument of @code{\addlyrics} is the text. The text also
850 should not land on a Staff, but on a interpretation context for
851 syllables, extenders, hyphens etc.  This context is called
852 Lyrics.
853 @example 
854
855         @}
856  
857 @end example 
858 This ends @code{\simultaneous}.
859 @example 
860
861         \midi  @{ \tempo 4=72;@}
862  
863 @end example 
864 This makes the music go to a MIDI file.  MIDI is great for checking
865 music you enter.  You listen to the MIDI file: if you hear something
866 unexpected, it's probably a typing error.  @code{\midi} starts an output
867 definition, a declaration that specifies how to output music analogous
868 to @code{\paper @{ @}}. You can specify the tempo using the
869 @code{\tempo} command, in this case the tempo of quarter notes is set to
870 72 beats per minute.
871 @example 
872
873         \paper @{ linewidth = 10.0\cm; @}
874  
875 @end example 
876 We also want notation output.  The linewidth is short so the piece
877 will be set in two lines.
878 @example 
879
880         @}
881  
882 @end example 
883 End the score block.
884
885 @node More movements 
886 @section More movements
887
888 You probably ran @file{ly2dvi} on the last example, and ended up with a
889 viewable @file{.dvi} file.  However, between there are a few steps of
890 which LilyPond is only one. To enhance your understanding of what's
891 happening under the hood when you run @code{ly2dvi}, we explain what
892 programs are run.
893
894 @code{ly2dvi} is a program that calls a number of programs  in sequence.
895 The first thing it does, is running LilyPond on the input file. After
896 some calculations, a @file{.tex} is produced. The contents
897 of this file are very  low-level instructions.
898
899 For example,  the following file (@file{layout.ly}) 
900
901 @example
902   \version "1.3.124";
903   \header @{ title = "Two miniatures";  @}
904   
905   #(set! point-and-click #t)
906   
907   \paper @{
908         linewidth = -1.0; @}
909
910   \score @{
911     \notes @{ c'4 d'4 @}
912     \header @{
913         opus = "Opus 1.";
914         piece = "Up"; @}
915   @}
916   \score @{
917     \notes @{ d'4 c'4  @}
918     \header @{
919         opus = "Opus 2.";
920         piece = "Down"; @}
921   @}
922 @end example
923  results in something like this@footnote{The titling in this manual was
924 not generated by ly2dvi, so details will differ.}
925
926 @center @strong{Two miniatures}
927 @flushright
928   Opus 1.
929 @end flushright
930 @flushleft
931 @var{Up}
932 @end flushleft
933 @lilypond
934   \score {
935     \notes { c'4 d'4 }
936     \paper {
937         linewidth = -1.0; }
938   }
939 @end lilypond
940 @flushright
941   Opus 2.
942 @end flushright
943 @flushleft
944 @var{Down}
945 @end flushleft
946 @lilypond
947   \score {
948     \notes { d'4 c'4 }
949     \paper {
950         linewidth = -1.0; }
951   }
952 @end lilypond
953
954 This file is produced by ly2dvi in a few stages, with the help of text
955 formatting tools. LilyPond produces two output files, @file{layout.tex}
956 and @file{layout-1.tex}.  They both look like this:
957
958 @example
959         ...
960   \placebox@{-5  \outputscale @}%
961   @{  8.7229  \outputscale @}%
962   @{\magfontWXGEomMMBo\char90 @}%
963   
964   \placebox@{-4  \outputscale @}%
965   @{ 81.0647  \outputscale @}%
966         ...
967 @end example
968
969 @file{ly2dvi} analyses the progress indication that LilyPond spews out,
970 and generates a file called @file{layout_ly1.tex}. This file contains
971 formatting instructions for the title and page layout.  A fragment might
972 look like
973 @example
974
975         \geometry@{width=540.602362pt,headheight=2mm, ...
976         \renewcommand@{\@@oddfoot@}@{\parbox@{\textwidth@}@{\mbox@{@}   ...
977         \begin@{document@}
978         \lilypondtitle@{foo@}%
979         \makelilytitle
980         \input@{ly2dvi.tex@}
981
982 @end example
983
984 @file{ly2dvi} runs it through LaTeX. LaTeX is a text-formatting system
985 built on top of @TeX{}. It's very popular in the academic world. If LaTeX
986 is successful, this will produce a @file{.dvi} file, containing both the
987 titling and notes.  @code{ly2dvi} completes its task by deleting the two
988 temporary files, leaving only @file{layout.dvi}.
989
990 Next, now we'll look at the examples line by line to explain new things.
991
992 @example 
993 \version "1.3.124";
994 @end example 
995 Lilypond and its language are still under development, and occasionally,
996 details of the syntax are changed. This fragment indicates for which
997 version the input file was written. When you compile this file, the
998 version number will be checked, and you will get a warning when the file
999 is too old.
1000
1001 This version number is also used by the @code{convert-ly} program (See
1002 @ref{convert-ly}), which uses it update the file to the latest lily
1003 version.
1004
1005 @example
1006   \header @{ title = "Two miniatures";  @}
1007 @end example
1008 This sets the titling information for the entire file.
1009
1010 @example
1011         #(set! point-and-click #t)
1012 @end example
1013
1014 This is Scheme code. It sets the variable @code{point-and-click} to the
1015 value @var{true}. 
1016
1017 Editing input files can be quite complicated if you're working with
1018 large files: if you're digitizing existing music, you have to
1019 synchronize the .ly file, the sheet music on your lap and the sheet
1020 music on the screen.  The point-and-click mechanism makes it easy to
1021 find the origin of an error in the .ly file: @footnote{This feature is
1022 presently only available on X-windows using patched versions of Xdvi and
1023 emacs} when you view the file with Xdvi and click on a note using
1024 control-mousebutton 1@footnote{If you're using a patched xdvik, the
1025 command is control-mousebutton-2}, your editor will jump to the spot
1026 where that note was entered.
1027
1028 More information is in in @ref{Point and click} 
1029
1030 @example
1031   \paper @{ 
1032 @end example
1033
1034 The @code{\score} blocks that follow in the file don't have
1035 @code{\paper} sections, so the settings of this block are substituted: A
1036 paper block, at top-level, i.e. not in a @code{\score} block sets the
1037 default page layout.
1038
1039 @example
1040   linewidth = -1.0; @}
1041 @end example
1042
1043
1044
1045 The variable @code{linewidth} normally sets the length of the systems on
1046 the page. However, a negative value has a special meaning. If
1047 @code{linewidth} is less than 0, no line breaks are inserted into the
1048 score, and the spacing is set to natural length: a short phrase takes up
1049 little space, a longer phrase more space.
1050
1051 @example
1052   \score @{
1053     \notes @{ c'4 d'4 @}
1054 @end example
1055
1056 In previous examples, notes were specified in relative octaves,
1057 i.e. each note was put in the octave that would put it closest to its
1058 predecessor. Besides relative, there is also absolute octave
1059 specification, and it is turned on by default. In this input mode, the
1060 central C is denoted by @code{c'}. Going down, you get @code{c}
1061 @code{c,} @code{c,,} etc.  Going up, you get @code{c''} @code{c'''} etc.
1062
1063 When you're copying music from existing sheet music, relative octaves
1064 are probably the easiest to use: it's less typing work and errors are
1065 easily spotted. However, if you write LilyPond input, either by hand
1066 (ie. composing) or by computer, absolute octaves are probably less work.
1067
1068
1069 @example
1070     \header @{
1071 @end example
1072
1073 The @code{\header} is normally at the top of the file, where it sets
1074 values for the rest of the file. If you want to typeset different pieces
1075 from one file (for example, if there are multiple movements, or if
1076 you're making a etude-book), you can put different @code{\score} blocks
1077 into the input file. ly2dvi will assemble all LilyPond output files into
1078 a big document. The contents of \header blocks specified within each
1079 score, are used for the titling of each movement.
1080 @example
1081         opus = "Opus 1.";
1082         piece = "Up"; @}
1083 @end example
1084 For example, the Opus number is put at the right, and the piece string
1085 will be at the left.
1086
1087
1088
1089 @node A piano excerpt
1090 @section A piano excerpt
1091
1092 Our third subject is a piece of piano music.  The fragment in the input
1093 file is a piano reduction of the G major Sinfonia by Giovanni Battista
1094 Sammartini.  It was composed around 1740.  It's in the source package
1095 under  the name @file{sammartini.ly}.
1096
1097 @lilypond[verbatim]
1098 \include "paper16.ly";
1099
1100 stemdown = \property Voice.Stem \override #'direction = #-1
1101 stemup = \property Voice.Stem \override #'direction = #1
1102 stemboth = \property Voice.Stem \revert #'direction  
1103
1104 viola = \notes \relative c' \context Voice = viola {
1105         <c4-\f-\arpeggio g' c>
1106         \stemdown g'8. b,16
1107         s1 s2. r4
1108         g
1109 }
1110
1111 oboes = \notes \relative c'' \context Voice = oboe {
1112         \stemup s4  g8. b,16 c8 r <e'8.^\p g> <f16 a>
1113         \grace <e8( g> <d4 f> <c2 e> \times 2/3 { <d8 \< f> <e g> <f a> }
1114         <
1115           { \times 2/3 { a8 g c } \! c2 }
1116           \context Voice = oboeTwo {
1117                 \stemdown
1118                 \grace {
1119                   \property Grace.Stem \override #'direction = #-1
1120                   [f,16 g] }
1121                 f8 e e2
1122         } >
1123         \stemboth
1124         \grace <c,8( e> <)b8. d8.-\trill> <c16 e> | 
1125         [<d ( f> < )f8. a>] <)b,8 d> r [<d16( f> <f8. )a>] <b,8 d> r  |
1126         [<c16( e>  < )e8. g>] <c8 e,>
1127 }
1128
1129 hoomPah  =  \repeat unfold 8
1130   \notes  \transpose c' { c8 \stemdown c'8 \stemup }
1131
1132 bassvoices = \notes \relative c' {
1133         c4 g8. b,16
1134         \autochange Staff \hoomPah
1135         \translator Staff = down
1136         \stemdown [c8 c'8] r4
1137         <g d'> r4
1138         < {\stemup r2 <e4 c'> <c8 g'> }
1139           \context Voice = reallyLow  {\stemdown g2 ~ | g4 c8 } >
1140 }
1141
1142 \score {
1143         \context PianoStaff \notes <
1144                 \context Staff = up < \time 2/2;
1145                         \viola
1146                         \oboes
1147                 >
1148                 \context Staff = down < \time 2/2; \clef bass;
1149                         \bassvoices
1150                 >
1151         >
1152         \midi { }
1153         \paper {
1154           indent = 0.0;
1155           linewidth = 15.0 \cm; }
1156 }
1157 @end lilypond
1158
1159 If it looks like incomprehensible gibberish to you, then you are right.
1160 This example has been doctored to have as many quirks as possible.
1161
1162 @example
1163  stemdown =  \property Voice.Stem \override #'direction = #-1
1164 @end example
1165
1166 As you can see, this example features more voices on one staff. To make
1167 room for those voices, their notes have to be stemmed in opposite
1168 directions. These are the commands to make that happen.
1169
1170 The symbols that are printed, are internally represented by so-called
1171 Graphical Objects (or more colloquially: Grobs).  These statements
1172 concern the grob called `Stem'. Each grob is described by a bunch of
1173 settings. These setting determine the fonts, offsets, sub-routines to be
1174 called on the grob, etc.  The initial values of these settings are set
1175 in the Scheme file @file{scm/grob-description.scm}.
1176
1177 This statement adds a the setting for all Stem grobs in the current
1178 Voice: @code{direction} is set to @code{-1}, which encodes down. The
1179 setting remains in effect until it is reverted.  
1180
1181 @example
1182  \property Voice.Stem \revert #'direction  
1183 @end example
1184
1185 This statement reverts the old setting. If you do this, the effect of a
1186 @code{\stemdown} or @code{\stemup} is neutralised.
1187
1188 @code{\override} and @code{\revert} function like a stack: you can push
1189 values onto the grob-setting-stack with @code{\override} and you pop
1190 them with @code{\revert}.
1191
1192 LilyPond includes the identifiers @code{\stemUp}, @code{\stemDown} along
1193 with some more often used formatting instructions, but to explain how it
1194 works, we wrote our own here.  Of course, you should use predefined
1195 identifiers like these if possible: then you will be affected less by
1196 the implementation changes we occasionally make.
1197
1198 @example 
1199 viola = \notes \relative c'  \context Voice = viola @{ 
1200 @end example 
1201 In this example, you can see multiple parts on a staff.  Each part is
1202 associated with one notation context.  This notation context handles
1203 stems and dynamics (among others).  The name of this context is
1204 @code{Voice}.  For each part we have to make sure that there is
1205 precisely one @code{Voice} context, so we give it an unique name
1206 (`@code{viola}').
1207
1208 @example 
1209 <c4-\f-\arpeggio g' c>
1210 @end example 
1211 The delimiters @code{<} and @code{>} are shorthands for
1212 @code{\simultaneous @{} and @code{@}}. The expression enclosed in
1213 @code{<} and @code{>} is a chord.
1214
1215 @cindex dynamics
1216 @cindex loudness
1217 @cindex forte
1218 @cindex arpeggio
1219
1220 @code{\f} places a forte symbol under the chord. The forte applies to
1221 the whole chord, but the syntax requires that commands like forte and
1222 arpeggio are attached to a note, so here we attach them to the first
1223 note.
1224
1225 @code{\arpeggio} draws an vertical wavy line before the chord,
1226 signifying an arpeggio.
1227
1228 @example 
1229    \stemdown
1230 @end example 
1231
1232
1233 @example 
1234         g'8. b,16 
1235 @end example 
1236 Relative octaves work a little differently with chords.  The starting
1237 point for the note following a chord is the first note of the chord.  So
1238 the @code{g} gets an octave up quote: it is a fifth above the starting
1239 note of the previous chord (the central C).
1240
1241 @example 
1242 s1 s2. r4 
1243 @end example 
1244 @code{s} is a spacer rest.  It does not print anything, but it does have
1245 the duration of a rest. It is useful for filling up voices that
1246 temporarily don't play. In this case, the viola doesn't come until one
1247 and a half measure later.
1248
1249 @example 
1250 oboes = \notes \relative c'' \context Voice = oboe @{ 
1251 @end example 
1252 Now comes a part for two oboes.  They play homophonically, so we
1253 print the notes as one voice that makes chords. Again, we insure that
1254 these notes are indeed processed by precisely one context with
1255 @code{\context}.
1256 @example 
1257 \stemup s4  g8. b,16 c8 r <e'8.-\p g> <f16 a> 
1258 @end example 
1259 @code{\stemup} is a reference to the @code{\property \override} command
1260 defined above.   .
1261 @example 
1262 \grace <e8 g> < d4 f> <c2 e> 
1263 @end example
1264 @cindex @code{\grace}
1265 @cindex ornaments
1266 @cindex grace notes
1267
1268 @code{\grace} introduces grace notes.  It takes one argument, in this
1269 case a chord.
1270
1271 @ignore
1272 The slur started on the @code{e} of the chord
1273 will be attached to the next note.@footnote{LilyPond will squirm
1274 about unended Slurs.  In this case, you can ignore the warning}.
1275 @end ignore
1276 @example 
1277 \times 2/3 
1278 @end example
1279 @cindex tuplet
1280 @cindex triplets
1281 Tuplets are made with the @code{\times} keyword.  It takes two
1282 arguments: a fraction and a piece of music.  The duration of the piece
1283 of music is multiplied by the fraction.  Triplets make notes occupy 2/3
1284 of their notated duration, so in this case the fraction is 2/3.
1285 @example 
1286 @{ <d8 \< f> <e g> <f a> @} 
1287 @end example 
1288 The piece of music to be `tripletted' is sequential music containing
1289 three notes.  On the first chord, a crescendo is started with
1290 @code{\<}. To be precise, the crescendo start is syntactically attached
1291 to the preceding note, the @code{d}.
1292
1293 @cindex dynamics
1294 @cindex crescendo
1295 @cindex @code{\<}
1296
1297 @example 
1298
1299 @end example 
1300 At this point, the homophonic music splits into two rhythmically
1301 different parts.  We can't use a sequence of chords to enter this, so
1302 we make a `chord' of sequences to do it.  We start with the upper
1303 voice, which continues with upward stems:
1304 @example 
1305  @{ \times 2/3 @{ a8 g c @} \! c2 @} 
1306 @end example
1307
1308 @cindex @code{\!}
1309
1310 The crescendo is ended at the half note by the escaped exclamation
1311 mark @code{\!}.
1312 @example 
1313 \context Voice = oboeTwo @{
1314 \stemDown 
1315 @end example 
1316 We can't share stems with the other voice, so we have to create a new
1317 @code{Voice} context.  We give it the name @code{oboeTwo} to distinguish
1318 it from the other context.  Stems go down in this voice.
1319 @example 
1320 \grace @{  
1321 @end example
1322 @cindex Grace context
1323 When a grace section is processed, a @code{Grace} context is
1324 created. This context acts like a miniature score of its own.  It has
1325 its own time bookkeeping, and you can make notes, beams, slurs
1326 etc. Here we fiddle with a property and make a beam.  The argument of
1327 @code{\grace} is sequential music.
1328
1329 @example 
1330 \property Grace.Stem \override #'direction = #-1
1331 [f,16 g] @}
1332 @end example 
1333
1334 Normally, grace notes are always stem up, but in this case, the upper
1335 voice interferes. We set the stems down here.
1336
1337 As far as relative mode is concerned, the previous note is the
1338 @code{c'''2} of the upper voice, so we have to go an octave down for
1339 the @code{f}.
1340 @example 
1341
1342   f8 e e2
1343 @} > 
1344 @end example 
1345 This ends the two-part section.
1346 @example 
1347 \stemboth
1348 \grace <c,8( e> <)b8. d8.-\trill> <c16 e> |  
1349 @end example
1350 @cindex trill
1351 @cindex stemboth
1352
1353 @code{\stemBoth} ends the forced stem directions. From here, stems are
1354 positioned as if it were single part music.
1355
1356 The bass has a little hoom-pah melody to demonstrate parts switching
1357 between staffs.  Since it is repetitive, we use repeats:
1358 @example 
1359 hoomPah  =  \repeat unfold 8
1360 @end example
1361 @cindex unfolded @code{\repeat}
1362 This repeat print the following sequence notes eight times.
1363 @example
1364 \notes \transpose c' @{
1365 @end example
1366 @cindex transposing
1367 @cindex relative mode and transposing
1368
1369 Transposing can be done with @code{\transpose}.  It takes two arguments;
1370 the first specifies what central C should be transposed to.  The second
1371 is the to-be-transposed music.  As you can see, in this case, the
1372 transposition is a no-op, as central C stay at central C.
1373
1374 The purpose of this no-op is circumventing relative mode.  Relative mode
1375 can not be used together with transposition, so @code{\relative} will
1376 leave the contents of @code{\hoomPah} alone.  We can use it without
1377 having to worry about getting the motive in a wrong octave.
1378 @example 
1379 bassvoices = \notes \relative c' @{
1380 c4 g8. b,16
1381 \autochange Staff \hoomPah 
1382 @end example
1383 @cindex staff switch, automatic
1384 @cindex cross staff voice, automatic
1385 @cindex @code{\autochange}
1386
1387 Voices can switch between staffs. The easiest way to get this, is to use
1388 @code{\autochange}. This command looks at the pitch of each note, and if
1389 necessary, will cross to the other staff. For this to work, the two
1390 staffs must be called @code{"up"} and @code{"down"}. 
1391 @example
1392         \translator Staff = down
1393 @end example
1394 @cindex staff switch
1395 @cindex cross staff voice
1396 The rest of this melody must be in the lower staff, so we do a manual
1397 staff switch here.
1398
1399
1400 @example 
1401 \context Voice = reallyLow  @{\stemDown g2 ~ | g4 c8 @} > 
1402 @end example
1403 @cindex tie
1404 @cindex @code{~}
1405 After skipping some lines, we see @code{~}.  This mark makes ties.
1406 @example 
1407 \context PianoStaff 
1408 @end example 
1409  A special context is needed to get cross staff beaming right.  This
1410 context is called @code{PianoStaff}.
1411 @example 
1412 \context Staff = bottom < \time 2/2; \clef bass; 
1413 @end example 
1414 The bottom staff must have a different clef.
1415 @example 
1416 indent = 0.0; 
1417 @end example 
1418 To make some more room on the line, the first (in this case the only)
1419 line is not indented.  The line still looks very cramped, but that is due
1420 to the page layout of this document.
1421
1422
1423 [TODO:
1424
1425 * arpeggio, glissando, 
1426
1427 * \apply, \outputproperty, \translator @{@}, \molecule hacking.
1428
1429 * font-size, cadenza. rhythmic staff, multi-stanza.
1430
1431
1432 * Orchestral: demonstrate Hara-Kiri, part combining, part extraction,
1433 scores, transposition, instrument names,
1434
1435 ]
1436
1437 @node  end of tutorial
1438 @section The end        
1439          
1440 That's all folks.  From here, you can either try fiddling with input
1441 files, or you can read the reference manual.