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