]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/tex/tutorial.yo
cdcfb23aff5353f00c5bb10ef013460b44a130bb
[lilypond.git] / Documentation / tex / tutorial.yo
1 mailto(gnu-music-discuss@gnu.org)
2 COMMENT(-*-text-*-)
3
4 redef(var)(1)(whenlatex(latexcommand({\normalfont\scshape )ARG1+latexcommand(}))\
5     whenhtml(sc(ARG1)))
6
7 COMMENT(urg)
8 DEFINEMACRO(Large)(1)(\
9     whenlatex(latexcommand(\Large{)ARG1+latexcommand(}))\
10     whentexinfo(texinfocommand(@strong{)ARG1+texinfocommand(}))\
11     whenhtml(htmlcommand(<font size=+2>)ARG1+htmlcommand(</font>))\
12     whentxt(ARG1))
13
14 COMMENT( This document contains Mudela fragments.  You need at least
15 Yodl-1.30.18 to convert this to tex or html.
16
17 TODO
18
19     * pipethrough(date) sucks.
20     * paragraphs have too much space.
21     * fix the amount of spaces (urg:tabs) at the start of verb() blocks
22       or even better: do verb and description side-by side
23       (TeX: use minipage construct):
24
25       \foo                  This does the
26                             foo construct
27
28       The explaining texts are right in between examples.
29       Constructs like 'This shows' and 'The next line' are esp.
30       confusing, here.
31 )
32
33 COMMENT(
34         Mainly written by Han-Wen Nienhuys, 
35
36         with help of (among others)
37
38         * Jan Nieuwenhuizen
39 )
40
41 htmlbodyopt(bgcolor)(white)
42 htmlcommand(<font color=black>)
43
44 latexlayoutcmds(
45     \topmargin -0.25in  
46     \textheight 53\baselineskip
47     \advance\textheight by \topskip
48     \marginparwidth 1 in        %   Width of marginal notes.
49     \oddsidemargin 0.25 in      %   Note that \oddsidemargin = \evensidemargin
50     \evensidemargin 0.25 in
51     \marginparwidth 0.75 in
52     \textwidth 5.875 in % Width of text line.
53     \input mudela-book
54 )
55
56 whenlatex(notableofcontents())
57 whentexinfo(notableofcontents())
58
59 article(Typesetting music with LilyPond)
60       (Han-Wen Nienhuys and Jan Nieuwenhuizen)
61       (nop()PIPETHROUGH(date "+%B %d, %Y")()()nop())
62
63
64 latexcommand(\def\interexample{})
65 latexcommand(\def\preexample{\par})
66 latexcommand(\def\postexample{\par\medskip})
67 latexcommand(\def\file#1{{code(#1)}})
68
69 whenhtml(
70 includefile(html-disclaimer.yo-urg)
71 )
72
73
74 sect(Introduction)
75 label(tutorial:introduction)
76 latexcommand(\parindent2pc)
77   
78 LilyPond prints music from a specification that you, the user, supply.
79 You have to give that specification using a em(language).  This
80 document is a gentle introduction to that language, which is called
81 Mudela, an acronym of Music Definition Language.
82
83 This tutorial will demonstrate how to use Mudela by presenting
84 examples of input along with resulting output.  We will use English
85 terms for notation.  In case you are not familiar with those, you may
86 consult the glossary that is distributed with LilyPond.
87
88 The examples discussed are included in the distribution, in the
89 subdirectory file(input/tutorial/).  It is recommended that you
90 experiment with writing Mudela input yourself, to get a feel for
91 how LilyPond behaves.
92
93 sect(The first tune)
94 label(sec:firsttune)
95
96 To demonstrate what LilyPond input looks like, we start off with a
97 full fledged, yet simple example. It is a convoluted version
98 of the famous menuet in bind(J.)bind(S.)Bach's em(Klavierbuechlein).
99
100 COMMENT(urg: the fermata sign is placed below the note by default)
101 mudela(verbatim)(% lines preceded by a percent are comments.
102 \include "paper16.ly"
103 \score {
104     \notes                      
105     \relative c'' {             
106             \key g;
107             \time 3/4;          
108
109         \repeat "volta" 2 {
110             d4 g,8 a b c d4 g, g |
111             e'4 c8 d e fis g4 g, g |
112             c4 d8()c b a( )b4 c8 b a g |
113             a4 [b8 a] [g fis] g2.  |
114         }
115
116         b'4 g8 a b g
117         a4 d,8 e fis d |
118         g4 e8 fis g d cis4 b8 cis a4 |
119         a8-. b-. cis-. d-. e-. fis-.
120         g4 fis e |
121         fis a,  r8 cis8
122         d2.-\fermata
123         \bar "|.";
124     }
125     \paper {
126        % standard settings are too wide for a book
127        linewidth = 14.0 \cm;
128    }
129 })
130
131 Enter it (or copy it, the filename is file(menuet.ly)), compile it
132 with LilyPond and view the output.  Details of this procedure may vary
133 from system to system.  To create the output, one would issue the
134 command `code(ly2dvi menuet)'.  file(ly2dvi) is a program that does
135 the job of running LilyPond and TeX(), handling of titles and
136 adjusting of page margins.
137
138 If all goes well, the file file(menuet.dvi) will be created.
139 To view this output, issue the command `code(xdvi menuet)'.
140
141 Now that we are familiar with the procedure of producing output, we
142 will analyse the input, line by line.COMMENT(
143
144 )verb(
145         % lines preceded by a percent are comments.
146 )COMMENT(
147
148 )The percent sign, `code(%)', introduces a line comment.  If you want to
149 make larger comments, you can use block comments. These are delimited
150 by `code(%{)' and `code(%})'COMMENT(
151
152 )verb(
153         \input "paper16.ly"
154 )COMMENT(
155
156 )By default, LilyPond will use definitions for a 20
157 nop(point)footnote(A point is the standard measure of length for
158 printing.  One point is 1/72.27 inch.) high staff.  We want smaller
159 output (16 point staff height), so we must import the settings for
160 that size, which is done.COMMENT(
161
162 )verb(
163         \score {
164 ) COMMENT(
165
166 ) A mudela file combines music with directions for outputting that
167 music.  The music is combined with the output directions by putting
168 them into a code(\score) block.
169 verb(
170         \notes          
171 ) COMMENT( 
172
173 )This makes LilyPond ready for accepting notes.
174 verb(
175         \relative c''
176 )COMMENT(
177
178 ) As we will see, pitches are combinations of octave, note name and
179 chromatic alteration.  In this scheme, the octave is indicated by
180 using raised quotes (`code(')') and ``lowered'' quotes (commas:
181 `code(,)').  The central C is denoted by code(c').  The C one octave
182 higher is code(c'').  One and two octaves below the central C is
183 denoted by code(c) and code(c,) respectively.
184
185 For pitches in a long piece you might have to type many quotes.  To
186 remedy this, LilyPond has a ``relative'' octave entry mode.  In this
187 mode, octaves of notes without quotes are chosen such that a note is
188 as close as possible (graphically, on the staff) to the the preceding
189 note.  If you add a high-quote an extra octave is added.  The lowered
190 quote (a comma) will subtract an extra octave.  Because the first note
191 has no predecessor, you have to give the (absolute) pitch of the note
192 to start with.COMMENT(
193
194 )verb(
195         \sequential {
196 )COMMENT(
197
198 )  What follows is sequential music, i.e.,
199 notes that are to be played and printed after each other.COMMENT(
200
201 )verb(
202         \time 3/4;
203 ) COMMENT(
204
205 ) This command changes the time signature of the current piece: a 3/4
206 sign is printed.  This command is also used to generate bar lines in
207 the right spots.COMMENT(
208
209 )verb(
210         \key g;
211 )COMMENT(
212
213 ) This command changes the current key to G-major.  Although this
214 command comes after the code(\time) command, in the output, the key
215 signature comes before the time signature: LilyPond knows about music
216 typesetting conventions. COMMENT(
217
218 )verb(
219         \repeat "volta" 2
220 ) COMMENT(
221
222 ) This command tells LilyPond that the following piece of music must
223 be played twice; code("volta") volta brackets should be used for
224 alternatives---if there were any.
225 COMMENT(
226
227 )verb(
228         {
229 )COMMENT(
230
231 )The subject of the repeat is again sequential music.  Since
232 code(\sequential) is such a common construct, a shorthand is provided:
233 just leave off code(\sequential), and the result is the same. COMMENT(
234
235 )verb(
236         d4
237 )COMMENT(
238
239 ) This is a note with pitch code(d) (determined up to octaves).  The
240 relative music was started with a code(c''), so the real pitch of this
241 note is code(d'').  The code(4) designates the duration of the note
242 (it is a quarter note). COMMENT(
243
244 )verb(
245         a b
246 )COMMENT(
247
248 )These are notes with pitch code(a') and code(b').  Because their
249 duration is the same as the code(g), there is no need to enter the
250 duration (You may enter it anyway, eg. code(a4 b4)) COMMENT(
251
252 )verb(
253         d4 g, g |
254 )COMMENT(
255
256 ) Three more notes.  The `code(|)' character is a `barcheck'.  When
257 processing the music, LilyPond will verify that barchecks are found at
258 the start of a measure.  This can help you track down errors.
259 COMMENT(
260
261 )verb(
262         e'4 
263 ) COMMENT(
264
265 ) So far, no notes were chromatically altered.  Here is the first one
266 that is: code(fis). Mudela by default uses Dutch note names, and
267 ``Fis'' is the Dutch note name for ``F sharp''.  However, there is no
268 sharp sign in the output. The program keeps track of key signatures,
269 and will only print accidentals if they are needed.
270 COMMENT(
271
272 )verb(
273         c8 d e fis
274 )COMMENT(
275
276 )LilyPond guesses were beams can be added to eighth and shorter notes.
277 In this case, a beam over 4 eighths is added.
278 COMMENT(
279
280 )verb(
281         c4 d8( )c b a( )b4 c8 b a g |
282 ) COMMENT(
283
284 ) The next line shows how to make a slur:
285 the beginning and ending note of the slur is marked with an opening and
286 closing parenthesis respectively.  In the line shown above this is
287 done for two slurs.  Slur markers (parentheses) are between
288 the notes.COMMENT( 
289 )verb(
290         a4 [b8 a] [g fis] 
291 )COMMENT(
292
293 )Automatic beaming can be overridden by inserting beam marks
294 (brackets).  Brackets are put around notes you want beamed.COMMENT(
295
296 )verb(
297         g2.  |
298 )COMMENT(
299
300 )A duration with augmentation dot  is notated
301 with the duration number followed by a period.COMMENT(
302 )verb(
303         }
304 ) COMMENT(
305
306 ) This ends the sequential music to be repeated.  LilyPond will typeset
307 a repeat bar.  COMMENT(
308
309 )verb(
310         cis'4 b8 cis a4 |
311 ) COMMENT(
312
313 )This line shows that Lily will print an accidental if that is
314 needed: the first C sharp will be printed with an accidental, the
315 second one without.  COMMENT(
316
317 )verb(
318         a8-. b-. cis-. d-. e-. fis-.
319 )COMMENT(
320
321 )You can enter articulation signs either in a verbose form using a
322 shorthand.  Here we demonstrate the shorthand: it is formed by a dash
323 and the the character for the articulation to use, e.g. `code(-.)' for
324 staccato as shown above.  COMMENT(
325
326 )verb(
327         fis a, r8 cis8
328 ) COMMENT(
329
330 )
331 Rests are denoted by the special notename `code(r)'.  You can also enter
332 an invisible rest by using the special notename `code(s)'.
333 verb(
334         d2.-\fermata
335 ) COMMENT(
336
337 )All articulations have a verbose form, like code(\fermata).  The
338 command `code(\fermata)' is not part of the core of the language (most
339 of the other discussed elements are), but it is a shorthand for a more
340 complicated description of a fermata.  code(\fermata) names that
341 description and is therefore called an em(identifier). COMMENT(
342
343 )verb(
344         }
345 ) COMMENT(
346
347 )
348 Here the music ends.
349 COMMENT(
350
351 )verb(
352         \paper {
353                 linewidth = 14.0\cm;
354         }
355 )COMMENT(
356
357 )This specifies a conversion from music to notation output.  Most of
358 the details of this conversions (font sizes, dimensions, etc.) have
359 been taken care of, but  to fit the output  in this document, it has
360 to be smaller.  We do this by setting the line width to 14 centimeters
361 (approximately 6 inches).
362 COMMENT(
363
364 )verb(
365         }
366 )COMMENT(
367
368 )The last brace ends the code(\score) block.
369
370 There are two things to note here. The format contains musical
371 concepts like pitches and durations, instead of symbols and positions:
372 the input format tries to capture the meaning of em(music), and not
373 notation.  Therefore Second, the format tries to be em(context-free):
374 a note will sound the same regardless of the current time signature,
375 the key, etc.
376
377 The purpose of LilyPond is explained informally by the term `music
378 typesetter'.  This is not a fully correct name: not only does the
379 program print musical symbols, it also makes esthetic decisions.  All
380 symbols and their placement is em(generated) from a high-level musical
381 description.  In other words,  LilyPond would be best
382 described by `music compiler' or `music to notation compiler'.
383
384
385 sect(Lyrics and chords)
386
387 In this section we show how to typeset a song of (unknown
388 origin)footnote(The author would welcome information about the origin
389 of this song.).
390
391 verb(\header {
392         title = "The river is flowing";
393         composer = "Traditonal (?)";
394 }
395 \include "paper16.ly"
396 melody = \notes \relative c' {
397         \partial 8;
398         g8 |
399         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
400         c4 c8 d [es () d] c4 | d4 es8 d c4.
401         \bar "|.";
402 }
403
404 text = \lyrics {
405         The ri -- ver is flo- __ wing, flo -- wing and gro -- wing, the
406         ri -- ver is flo -- wing down to the sea.
407 }
408
409 accompaniment =\chords {
410         r8
411         c2-3- f-3-.7 d-min es4 c8-min r8
412         c2-min f-min7 g-7^3.5 c-min }
413
414 \score {
415         \simultaneous {
416 %         \accompaniment
417           \context ChordNames \accompaniment
418
419           \addlyrics
420             \context Staff = mel {      
421               \property Staff.noAutoBeaming = "1"
422               \property Staff.automaticMelismas = "1"
423               \melody 
424             }
425             \context Lyrics \text
426         }
427         \midi  { }
428         \paper { linewidth = 10.0\cm; }
429 })
430
431
432 The result would look bind(this)footnote(The titling and font size shown
433 may differ, since the titling in this document is not generated by
434 file(ly2dvi).).
435
436 center(bf(Large(The river is flowing))
437
438 var(Traditonal (?))
439 )
440
441 mudela(center)(\header {
442         title = "The river is flowing";
443         composer = "Traditonal (?)";
444 }
445 \include "paper16.ly"
446 melody = \notes \relative c' {
447         \partial 8;
448         g8 |
449         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
450         c4 c8 d [es () d] c4 | d4 es8 d c4.
451         \bar "|.";
452 }
453
454 text = \lyrics {
455         The ri -- ver is flo- __ wing, flo -- wing and gro -- wing, the
456         ri -- ver is flo -- wing down to the sea.
457 }
458
459 accompaniment =\chords {
460         r8
461         c2-3- f-3-.7 d-min es4 c8-min r8
462         c2-min f-min7 g-7^3.5 c-min }
463
464 \score {
465         \simultaneous {
466 %         \accompaniment
467           \context ChordNames \accompaniment
468
469           \addlyrics
470             \context Staff = mel {
471               \property Staff.noAutoBeaming = "1"
472               \property Staff.automaticMelismas = "1"
473               \melody 
474             }
475             \context Lyrics \text
476         }
477         \midi  { }
478         \paper { linewidth = 10.0\cm; }
479 })
480
481 Again, we will dissect the file line by line.COMMENT(
482
483 )verb(
484         \header {
485 )COMMENT(
486
487 )Information about the music you are about to typeset goes into a
488 code(\header) block.  The information in this block is not used by
489 LilyPond, but it is included in the output.  file(ly2dvi) uses this
490 information to print titles above the music.
491 verb(
492         title = "The river is flowing";
493         composer = "Traditonal (?)";)COMMENT(
494
495 )the code(\header) contains block assignments.  An assignment starts
496 with a string.  (which is unquoted, in this case). Then comes the
497 equal sign `code(=)'.  After the equal sign comes the expression you
498 want to store.  In this case, you want to put in strings.  The
499 information have to be quoted, because they contain spaces. The
500 assignment is finished with a semicolon.COMMENT(
501
502 )verb(
503         \include "paper16.ly"
504 )COMMENT(
505
506 )Smaller size for inclusion in a book.COMMENT(
507
508 )verb(
509         melody = \notes \relative c' {
510 )COMMENT(
511
512 )The structure of the file will be the same as the previous one, a
513 code(\score) block with music in it.  To keep things readable, we will
514 give the different parts of music names, and use names to construct
515 music within the score block.
516
517 verb(
518         \partial 8;
519 )
520
521 The piece starts an anacrusis of one eighth.  COMMENT(
522
523 )verb(
524         c4 c8 d [es () d] c4 | f4 f8 g [es() d] c g |
525         c4 c8 d [es () d] c4 | d4 es8 d c4.
526         \bar "|.";
527 )COMMENT(
528
529 )We use explicit beaming.  Since this is a song,  we turn automatic
530 beams off, and use explicit beaming where needed.COMMENT(
531
532 )verb(
533         }
534 )COMMENT(
535
536 )This ends the definition of code(melody).  Note that there are no
537 semicolons after declarations at top level.COMMENT( 
538
539 )verb(
540         text = \lyrics {
541 )COMMENT(
542
543 )Another identifier assignment.  This one is for the lyrics. 
544 Lyrics are formed by syllables that have duration, and not by
545 notes. To make LilyPond parse words as syllables,  switch it  into
546 lyrics mode with code(\lyrics).  Again, the brace after code(\lyrics)
547 is a shorthand for code(\sequential {). COMMENT(
548
549 )verb(
550         The4 ri -- ver is flo- __ wing,  flo -- wing and gro -- wing, the
551         ri- ver is flo- __ wing down to the sea.
552         }
553 )COMMENT(
554
555 )The syllables themselves, separated by spaces.  You can get syllable
556 extenders by entering `code(__)', and centered hyphens with
557 `code(--)'.  We enter the syllables as if they are all quarter notes
558 in length (hence the code(4)), and use a feature to align the
559 syllables to the music, which obviously isn't all quarter notes.
560 COMMENT(
561
562 )verb(
563         accompaniment =\chords {
564 )COMMENT(
565
566 )We'll put chords over the music.  There is a special mode (analogous
567 to code(\lyrics) and code(\notes) mode) where you can give the names
568 of the chords you want, instead of the notes comprising the chord.
569 COMMENT(
570
571 )verb(
572         r8
573 )COMMENT(
574
575 )There is no accompaniment during the anacrusis.COMMENT(
576
577 )verb(
578         c2-3- f-3-.7
579 )A chords is started by a note that is the tonic of the chord. The
580 first one lasts a half note.  An unadorned note creates a major
581 triad, while a minor triad is wanted.  code(3-) modifies the third to
582 be small. code(7) modifies (adds) a seventh, which is small by default
583 to create the code(f a c es) chord.  Multiple modifiers must be
584 separated by a dot.COMMENT(
585
586 )verb(
587         d-min es4 c8-min r8
588 )COMMENT(
589
590 )Some modifiers have predefined names, eg. code(min) is  the same as
591 code(3-), so code(d-min) is a minor code(d) chord.COMMENT(
592
593 )verb(
594         c2-min f-min7 g-7^3.5 c-min }
595 )COMMENT(
596
597 )A named modifier code(min) and a normal modifier code(7) do not have
598 to be separated by a dot.  Tones from a chord are removed with chord
599 subtractions.  Subtractions are started with a caret, and they are
600 also separated by dots.  In this example, code(g-7^3.5) produces a
601 minor seventh.  The brace ends the sequential music. COMMENT(
602
603 )verb(
604         \score {
605                 \simultaneous {
606 )COMMENT(
607
608 )We assemble the music in the code(\score) block.  Melody, lyrics and
609 accompaniment have to sound at the same time, so they should be
610 code(\simultaneous).COMMENT(
611
612 )verb(
613         %\accompaniment
614 )COMMENT(
615
616 )Chord mode generates notes grouped in code(\simultaneous) music.  If
617 you remove the comment sign, you can see the chords in normal
618 notation.  The chords will then be printed as chords of note heads on
619 a separate staff. COMMENT(
620
621 )verb(
622         \context ChordNames \accompaniment
623 )COMMENT(
624
625 )Normally, the notes that you enter are transformed into note heads.
626 The note heads alone make no sense, they need surrounding information:
627 a key signature, a clef, staff lines, etc.  They need em(context).
628 This context also is a thing that has to be created. This is done by
629 code(\context).  It takes two arguments.  The first is the name of a
630 em(notation) or em(interpration context).  The name is a string, it
631 can be quoted with code(") quotes).  The second argument is the music
632 that should be interpreted in this context.
633
634 By default, LilyPond will create a Staff context for you.  If you
635 would remove the code(%) sign in the previous line, you see that
636 mechanism in action.  For the previous line, we could have written
637 code(\context Staff \accompaniment), and get the same effect.COMMENT(
638
639 )verb(
640         \addlyrics
641 )COMMENT(
642         
643 )The lyrics need to be aligned with the melody.  This is done by
644 combining both with code(\addlyrics).  code(\addlyrics) takes two
645 pieces of music (usually a melody and lyrics, in that order) and
646 aligns the lyrics syllables of the second piece under the notes of the
647 first piece.  If you would reverse the order, the notes would be
648 aligned on the lyrics, which is not very useful. (Besides, it looks
649 silly.)COMMENT(
650
651 )verb(
652         \context Staff = mel {
653 )COMMENT(
654
655 )This is first piece of music.  We instantiate a code(Staff) context
656 explicitly: should you chose to remove comment before the ``note
657 heads'' version of the accompaniment, the accompaniment will be on a
658 nameless staff.  In that case, the melody has to be on a different
659 staff as the accompaniment.  This is accomplished by giving the melody
660 staff a different name.COMMENT(
661
662 )verb(
663         \property Staff.noAutoBeaming = "1"
664 )COMMENT(
665
666 )An interpretation context has variables that tune its behaviour.  One
667 of the variables is code(noAutoBeaming).  If set and non-zero (i.e.,
668 true) LilyPond will not try to automatic beaming on the current
669 staff.COMMENT(
670
671 )verb(
672         \property Staff.automaticMelismas = "1"
673 )COMMENT(
674
675 )Similarly, we want do not want to put a lyric syllable when there is
676 a slur. This sets up the Staff context to signal slurs while
677 code(\addlyrics) is processed. COMMENT(
678
679 )verb(
680           \melody
681         }
682 )COMMENT(
683
684 )Finally, we put the melody on the current.  Note that the
685 code(\property) directives and code(\melody) are grouped in sequential
686 music.  So the property settings are done before the melody is
687 processed.  COMMENT(
688
689 )verb(
690         \context Lyrics \text
691 )COMMENT(
692
693 )The second argument of code(\addlyrics) is the text.  Text also
694 should land on a Staff, but on a context for syllables,
695 extenders, hyphens etc.  This context is called Lyrics.COMMENT(
696
697 )verb(
698         }
699 )COMMENT(
700
701 )This ends code(\simultaneous).COMMENT(
702
703 )verb(
704         \midi  { }
705 )COMMENT(
706
707 )This makes the music go to a MIDI file as well.  MIDI is great for
708 checking music you enter.  You listen to the MIDI file: if you hear
709 something unexpected, it's probably a typing error.  code(\midi) is a
710 `output definition', a declaration that specifies how to output music
711 analogous to code(\paper { }).COMMENT(
712
713 )verb(
714         \paper { linewidth = 10.0\cm; }
715 )COMMENT(
716
717 )We also want notation output.COMMENT(
718
719 )verb(
720         }
721 )COMMENT(
722
723 )End the score block.