]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/learning/tutorial.itely
Doc: NR Update information for modern-cautionary
[lilypond.git] / Documentation / learning / tutorial.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
3 @ignore
4     Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5
6     When revising a translation, copy the HEAD committish of the
7     version that you are working on.  For details, see the Contributors'
8     Guide, node Updating translation committishes..
9 @end ignore
10
11 @include included/generating-output.itexi
12
13 @c \version "2.19.21"
14
15 @node Tutorial
16 @chapter Tutorial
17
18 This chapter gives a basic introduction to working with LilyPond.
19
20 @menu
21 * Compiling a file::
22 * How to write input files::
23 * Dealing with errors::
24 * How to read the manuals::
25 @end menu
26
27 @node Compiling a file
28 @section Compiling a file
29
30 This section introduces @qq{compiling}---the processing of
31 LilyPond input files (written by you) to produce output files.
32
33 @menu
34 * Entering input::
35 * MacOS X::
36 * Windows::
37 * Command-line::
38 @end menu
39
40 @node Entering input
41 @subsection Entering input
42
43 @cindex compiling
44 @cindex first example
45 @cindex example, first
46 @cindex case sensitive
47
48 @menu
49 * Producing output::
50 @end menu
51
52 @qq{Compiling} is the term used for processing an input file in
53 LilyPond format to produce output file(s).  Output files are
54 generally PDF (for printing or viewing), MIDI (for playing), and
55 PNG (for online use).  LilyPond input files are simple text files.
56
57 This example shows a simple input file:
58
59 @example
60 \version "@w{@version{}}"
61 @{
62   c' e' g' e'
63 @}
64 @end example
65
66 The graphical output is:
67
68 @c  in this case we don't want verbatim
69 @lilypond[quote]
70 {
71   c' e' g' e'
72 }
73 @end lilypond
74
75 @warning{Notes and lyrics in LilyPond input must always be
76 surrounded by @w{@strong{@{ curly braces @}}}.  The braces
77 should also be surrounded by a space unless they are at the
78 beginning or end of a line to avoid ambiguities.  They may
79 be omitted in some examples in this manual, but don't forget them
80 in your own music!  For more information about the display of
81 examples in the manual, see @ref{How to read the manuals}.}
82
83 In addition, LilyPond input is @strong{case sensitive}.
84 @w{@samp{@{ c d e @}}} is valid input; @w{@samp{@{ C D E @}}} will
85 produce an error message.
86
87
88 @smallspace
89
90 @node Producing output
91 @unnumberedsubsubsec Producing output
92
93 @cindex PDF file
94 @cindex viewing music
95 @cindex text editors
96
97 The method of producing output depends on your operating system
98 and the program(s) you use.
99
100 @itemize
101
102 @item
103 @ref{MacOS X, @sourceimage{logo-macosx,,,}}
104 @ref{MacOS X, MacOS X} (graphical)
105
106 @item
107 @ref{Windows, @sourceimage{logo-windows,,,}}
108 @ref{Windows, Microsoft Windows} (graphical)
109
110 @item
111 @ref{Command-line, @sourceimage{logo-linux,,,}
112 @sourceimage{logo-freebsd,,,}
113 @sourceimage{logo-macosx,,,}
114 @sourceimage{logo-windows,,,}
115 }
116 @ref{Command-line, All operating systems} (command-line)
117
118 @end itemize
119
120 There are several other text editors available with specific
121 support for LilyPond.  For more information, see
122 @rweb{Easier editing}.
123
124 @warning{The first time you ever run LilyPond, it may take a
125 minute or two because all of the system fonts have to be analyzed
126 first.  After this, LilyPond will be much faster!}
127
128
129 @node MacOS X
130 @subsection MacOS X
131
132 @cindex running LilyPond under MacOS X
133 @cindex MacOS X, running LilyPond
134
135 @lilypadOSX
136
137
138 @node Windows
139 @subsection Windows
140
141 @cindex running LilyPond under Windows
142 @cindex Windows, running LilyPond
143
144 @lilypadWindows
145
146
147 @node Command-line
148 @subsection Command-line
149
150 @cindex running LilyPond under Unix
151 @cindex Unix, running LilyPond
152
153 @lilypadCommandLine
154
155 @node How to write input files
156 @section How to write input files
157
158 This section introduces some basic LilyPond syntax to help get you
159 started writing input files.
160
161 @menu
162 * Simple notation::
163 * Working on input files::
164 @end menu
165
166
167 @node Simple notation
168 @subsection Simple notation
169
170 @cindex simple notation
171 @cindex notation, simple
172
173 @menu
174 * Pitches::
175 * Durations (rhythms)::
176 * Rests::
177 * Time signature::
178 * Tempo marks::
179 * Clef::
180 * All together::
181 @end menu
182
183 LilyPond will add some notation elements automatically.  In the
184 next example, we have only specified four pitches, but LilyPond
185 has added a clef, time signature, and rhythms.
186
187 @lilypond[verbatim,quote]
188 {
189   c' e' g' e'
190 }
191 @end lilypond
192
193 @noindent
194 This behavior may be altered, but in most cases these automatic
195 values are useful.
196
197
198 @node Pitches
199 @unnumberedsubsubsec Pitches
200
201 @cindex pitches
202 @cindex relative mode
203 @cindex quote, single
204 @cindex comma
205 @cindex accidentals and relative mode
206 @cindex relative mode, and accidentals
207
208 @funindex \relative
209 @funindex relative
210 @funindex '
211 @funindex ,
212
213 Music Glossary: @rglos{pitch}, @rglos{interval},
214 @rglos{scale}, @rglos{middle C}, @rglos{octave},
215 @rglos{accidental}.
216
217 The easiest way to enter notes is by using @code{\relative} mode.
218 In this mode, the octave is chosen automatically by assuming the
219 following note is always to be placed closest to the previous
220 note, i.e., it is to be placed in the octave which is within three
221 staff spaces of the previous note.  We begin by entering the most
222 elementary piece of music, a @notation{scale}, in which every note
223 is within just one staff space of the previous note.
224
225 @lilypond[verbatim,quote]
226 \relative {
227   c' d e f
228   g a b c
229 }
230 @end lilypond
231
232 The initial note is @notation{middle C}, denoted by @code{c'}.
233 Each successive note is
234 placed closest to the previous note -- in other words, the first
235 @code{c} is the closest C to middle C.  This is followed by the
236 closest D to the previous note.  We can create melodies which have
237 larger intervals, still using only @code{\relative} mode:
238
239 @lilypond[verbatim,quote]
240 \relative {
241   d' f a g
242   c b f d
243 }
244 @end lilypond
245
246 @noindent
247 In the previous example, the first note -- the @code{d'} with one
248 @code{'}-mark -- is the D in the octave starting from middle C
249 and going up to B.
250
251 By adding (or removing) quotes @code{'} or commas @code{,} from
252 the first note, we can change the starting octave:
253
254 @lilypond[verbatim,quote]
255 \relative {
256   e'' c a c
257 }
258 @end lilypond
259
260 Relative mode can be confusing initially, but is the easiest way
261 to enter most melodies.  Let us see how this relative calculation
262 works in practice.  Starting from a B, which is on the middle line
263 in a treble clef, you can reach a C, D and E within 3 staff spaces
264 going up, and an A, G and F within 3 staff spaces going down.  So
265 if the note following a B is a C, D or E it will be assumed to be
266 above the B, and an A, G or F will be assumed to be below.
267
268 @lilypond[verbatim,quote]
269 \relative {
270   b' c % c is 1 staff space up, so is the c above
271   b d  % d is 2 up or 5 down, so is the d above
272   b e  % e is 3 up or 4 down, so is the e above
273   b a  % a is 6 up or 1 down, so is the a below
274   b g  % g is 5 up or 2 down, so is the g below
275   b f  % f is 4 up or 3 down, so is the f below
276 }
277 @end lilypond
278
279 Exactly the same happens even when any of these notes are
280 sharpened or flattened.  @notation{Accidentals} are
281 @strong{totally ignored} in the calculation of relative position.
282 Precisely the same staff space counting is done from a note at any
283 other position on the staff.
284
285 To add intervals that are larger than three staff spaces, we can
286 raise the @notation{octave} by adding a single quote @code{'} (or
287 apostrophe) to the note name.  We can lower the octave by adding a
288 comma @code{,} to the note name.
289
290 @lilypond[verbatim,quote]
291 \relative {
292   a' a, c' f,
293   g g'' a,, f'
294 }
295 @end lilypond
296
297 @noindent
298 To change a note by two (or more!) octaves, we use multiple
299 @code{''} or @code{,,} -- but be careful that you use two single
300 quotes @code{''} and not one double quote @code{"}@tie{}!
301 @c " - keeps quotes in order for context-sensitive editor -td
302
303 @node Durations (rhythms)
304 @unnumberedsubsubsec Durations (rhythms)
305
306 @cindex note durations
307 @cindex durations
308 @cindex rhythms
309 @cindex whole note
310 @cindex half note
311 @cindex quarter note
312 @cindex dotted note
313 @cindex notating durations
314
315 Music Glossary: @rglos{beam}, @rglos{duration},
316 @rglos{whole note}, @rglos{half note}, @rglos{quarter note},
317 @rglos{dotted note}.
318
319 The @notation{duration} of a note is specified by a number after
320 the note name:  @code{1} for a @notation{whole note}, @code{2} for
321 a @notation{half note}, @code{4} for a @notation{quarter note} and
322 so on.  @notation{Beams} are added automatically.
323
324 If you do not specify a duration, the previous duration is used
325 for the next note.  The duration of the first note defaults to a
326 quarter note.
327
328 @lilypond[verbatim,quote]
329 \relative {
330   a'1
331   a2 a4 a8 a
332   a16 a a a a32 a a a a64 a a a a a a a a2
333 }
334 @end lilypond
335
336 To create @notation{dotted notes}, add a dot @code{.} to the
337 duration number.  The duration of a dotted note must be stated
338 explicitly (i.e., with a number).
339
340 @lilypond[verbatim,quote]
341 \relative {
342   a'4 a a4. a8
343   a8. a16 a a8. a8 a4.
344 }
345 @end lilypond
346
347 @node Rests
348 @unnumberedsubsubsec Rests
349
350 @cindex rest
351 @cindex notating rests
352
353 Music Glossary: @rglos{rest}.
354
355 A @notation{rest} is entered just like a note with the name
356 @code{r}@tie{}:
357
358 @lilypond[verbatim,quote]
359 \relative {
360   a'4 r r2
361   r8 a r4 r4. r8
362 }
363 @end lilypond
364
365
366 @node Time signature
367 @unnumberedsubsubsec Time signature
368
369 @cindex time signature
370
371 @funindex \time
372 @funindex time
373
374 Music Glossary: @rglos{time signature}.
375
376 The @notation{time signature} can be set with the @code{\time}
377 command:
378
379 @lilypond[verbatim,quote]
380 \relative {
381   \time 3/4
382   a'4 a a
383   \time 6/8
384   a4. a
385   \time 4/4
386   a4 a a a
387 }
388 @end lilypond
389
390 @node Tempo marks
391 @unnumberedsubsubsec Tempo marks
392
393 @cindex tempo marks
394 @cindex metronome marks
395
396 @funindex \tempo
397 @funindex tempo
398
399 Music Glossary: @rglos{tempo indication}, @rglos{metronome}.
400
401 The @notation{tempo indication} and @notation{metronome mark} can be
402 set with the @code{\tempo} command:
403
404 @lilypond[verbatim,quote]
405 \relative {
406   \time 3/4
407   \tempo "Andante"
408   a'4 a a
409   \time 6/8
410   \tempo 4. = 96
411   a4. a
412   \time 4/4
413   \tempo  "Presto" 4 = 120
414   a4 a a a
415 }
416 @end lilypond
417
418
419 @node Clef
420 @unnumberedsubsubsec Clef
421
422 @cindex clef
423 @cindex treble
424 @cindex alto
425 @cindex tenor
426 @cindex bass
427
428 @funindex \clef
429 @funindex clef
430
431 Music Glossary: @rglos{clef}.
432
433 The @notation{clef} can be set using the @code{\clef} command:
434
435 @lilypond[verbatim,quote]
436 \relative {
437   \clef "treble"
438   c'1
439   \clef "alto"
440   c1
441   \clef "tenor"
442   c1
443   \clef "bass"
444   c1
445 }
446 @end lilypond
447
448
449 @node All together
450 @unnumberedsubsubsec All together
451
452 Here is a small example showing all these elements together:
453
454 @lilypond[verbatim,quote]
455 \relative {
456   \clef "bass"
457   \time 3/4
458   \tempo "Andante" 4 = 120
459   c,2 e8 c'
460   g'2.
461   f4 e d
462   c4 c, r
463 }
464 @end lilypond
465
466
467 @seealso
468 Notation Reference: @ruser{Writing pitches},
469 @ruser{Writing rhythms}, @ruser{Writing rests},
470 @ruser{Time signature}, @ruser{Clef}.
471
472
473 @node Working on input files
474 @subsection Working on input files
475
476 @cindex curly braces
477 @cindex braces, curly
478 @cindex comments
479 @cindex line comment
480 @cindex comment, line
481 @cindex block comment
482 @cindex comment, line
483 @cindex case sensitive
484 @cindex whitespace insensitive
485 @cindex expressions
486 @cindex versioning
487 @cindex version
488 @cindex version number
489
490 @funindex \version
491 @funindex { ... }
492 @funindex %
493 @funindex %@{ ... %@}
494
495 LilyPond input files are similar to source files in many common
496 programming languages.  They contain a version statement, are case
497 sensitive, and white-space is generally ignored.  Expressions are
498 formed with curly braces @w{@code{@{ @}}}, and comments are
499 denoted with @code{%} or @w{@code{%@{ @dots{} %@}}}@tie{}.
500
501 If the previous sentences sound like nonsense, don't worry!  We'll
502 explain what all these terms mean:
503
504 @itemize
505
506 @item
507 @strong{Version statement}:
508 Every LilyPond file should contain a version statement.  A version
509 statement is a line that describes the version of LilyPond for which
510 the file was written, as in the following example:
511
512 @example
513 \version "@w{@version{}}"
514 @end example
515
516 By convention, the version statement is placed at the top of the
517 LilyPond file.
518
519 The version statement is important for at least two reasons.  First,
520 it allows automatic updating of the input file as LilyPond syntax
521 changes.  Second, it describes the version of LilyPond needed to
522 compile the file.
523
524 If the version statement is omitted from an input file, LilyPond will print
525 a warning during the compilation of the file.
526
527 @item
528 @strong{Case sensitive}:
529 it matters whether you enter a letter in lower case (e.g.
530 @w{@code{a, b, s, t}}) or upper case (e.g. @w{@code{A, B, S, T}}).
531 Notes are lower case: @w{@samp{@{ c d e @}}} is valid input;
532 @w{@samp{@{ C D E @}}} will produce an error message.
533
534 @item
535 @strong{Whitespace insensitive}:
536 it does not matter how many spaces (or tabs or new lines) you add.
537 @w{@samp{@{ c4 d e @}}} means the same thing as
538 @w{@samp{@{ c4 @tie{} @tie{} @tie{} d e @}}} and:
539
540 @example
541 @{ c4                       d
542                    e   @}
543 @end example
544
545 @noindent
546 Of course, the previous example is hard to read.  A good rule of
547 thumb is to indent code blocks with two spaces:
548
549 @example
550 @{
551   c4 d e
552 @}
553 @end example
554
555 However, whitespace @emph{is} required to separate many
556 syntactical elements from others.  In other words, whitespace can
557 always be @emph{added}, but not always @emph{eliminated}.  Since
558 missing whitespace can give rise to strange errors, it is
559 advisable to always insert whitespace before and after every
560 syntactic element, for example, before and after every curly
561 brace.
562
563 @item
564 @strong{Expressions}:
565 every piece of LilyPond input needs to have
566 @strong{@{@tie{}curly@tie{}braces@tie{}@}} placed around the
567 input.  These braces tell LilyPond that the input is a single
568 music expression, just like parentheses @code{()} in mathematics.
569 The braces should be surrounded by a space unless they are at the
570 beginning or end of a line to avoid ambiguities.
571
572 A LilyPond command followed by a simple expression in braces (such
573 as @q{@w{@code{@bs{}relative @{ @dots{} @}}}}) also counts as a
574 single music expression.
575
576 @cindex comments
577 @cindex line comment
578 @cindex block comment
579 @item
580 @strong{Comments}:
581 a comment is a remark for the human reader of the music input; it
582 is ignored while parsing, so it has no effect on the printed
583 output.  There are two types of comments.  The percent symbol
584 @code{%} introduces a line comment; anything after @code{%} on
585 that line is ignored.  By convention, a line comment is placed
586 @emph{above} the code it refers to.
587
588 @example
589 a4 a a a
590 % this comment refers to the Bs
591 b2 b
592 @end example
593
594 A block comment marks a whole section of music input as a comment.
595 Anything that is enclosed in @code{%@{} and @code{%@}} is ignored.
596 However, block comments do not @q{nest}.  This means that you
597 cannot place a block comment inside another block comment.  If you
598 try, the first @code{%@}} will terminate @emph{both} block
599 comments.  The following fragment shows possible uses for
600 comments:
601
602 @example
603 % notes for twinkle twinkle follow
604   c4 c g' g a a g2
605
606 %@{
607   This line, and the notes below are ignored,
608   since they are in a block comment.
609
610   f4 f e e d d c2
611 %@}
612 @end example
613
614 @end itemize
615
616
617 @node Dealing with errors
618 @section Dealing with errors
619
620 @cindex troubleshooting
621
622 Sometimes LilyPond doesn't produce the output you expect.  This
623 section provides some links to help you solve the problems you
624 might encounter.
625
626
627 @menu
628 * General troubleshooting tips::
629 * Some common errors::
630 @end menu
631
632 @node General troubleshooting tips
633 @subsection General troubleshooting tips
634
635 Troubleshooting LilyPond problems can be challenging for
636 people who are used to a graphical interface, because invalid
637 input files can be created.  When this happens, a logical approach
638 is the best way to identify and solve the problem.  Some guidelines
639 to help you learn to do this are provided in @rprogram{Troubleshooting}.
640
641
642 @node Some common errors
643 @subsection Some common errors
644
645 @cindex common errors
646 @cindex errors, common
647
648 There are a few common errors that are difficult to troubleshoot
649 based simply on the error messages that are displayed.  These are
650 described in @rprogram{Common errors}.
651
652
653
654 @node How to read the manuals
655 @section How to read the manuals
656
657 This section shows how to read the documentation efficiently, and
658 also introduces some useful interactive features available in the
659 online version.
660
661 @menu
662 * Omitted material::
663 * Clickable examples::
664 * Overview of manuals::
665 @end menu
666
667
668 @node Omitted material
669 @subsection Omitted material
670
671
672 @cindex how to read the manual
673 @cindex manual, reading
674 @cindex reading the manual
675 @cindex examples, clickable
676 @cindex clickable examples
677 @cindex tips for constructing files
678 @cindex templates
679 @cindex constructing files, tips
680 @cindex files, tips for constructing
681
682 LilyPond input must be surrounded by @code{@{ @}} marks or a
683 @q{@w{@code{@bs{}relative @{ @dots{} @}}}}, as we saw in
684 @ref{Working on input files}.  For the rest of this manual, most
685 examples will omit this.  To replicate the examples, you may copy
686 and paste the displayed input, but to put the pitches in the same
687 octaves as seen in the manual you @strong{must} add a particular
688 form of @code{@bs{}relative} with an explicit starting pitch
689 @code{c''} like this:
690
691 @example
692 \relative c'' @{
693   @dots{}example goes here@dots{}
694 @}
695 @end example
696
697 Why omit the braces?  Most examples in this manual can be inserted
698 into the middle of a longer piece of music.  For these examples,
699 it does not make sense to add
700 @q{@w{@code{@bs{}relative c'' @{ @dots{} @}}}} -- you should not
701 place a @code{\relative} inside another @code{\relative}!  If we
702 included @q{@w{@code{@bs{}relative c'' @{ @dots{} @}}}} around
703 every example, you would not be able to copy a small documentation
704 example and paste it inside a longer piece of your own.  Most
705 people want to add material to an existing piece, so we format the
706 manual this way.
707
708 Also, remember that every LilyPond file should have a
709 @code{\version} statement.  Because the examples in the manuals
710 are snippets, not files, the @code{\version} statement is omitted.
711 But you should make a practice of including them in your files.
712
713 @node Clickable examples
714 @subsection Clickable examples
715
716 @warning{This feature is only available in the HTML manuals.}
717
718 Many people learn programs by trying and fiddling around with the
719 program.  This is also possible with LilyPond.  If you click on a
720 picture in the HTML version of this manual, you will see the exact
721 LilyPond input that was used to generate that image.  Try it on
722 this image:
723
724 @c no verbatim here
725 @lilypond[quote]
726 \relative {
727   c''4-\markup { \bold \huge { Click here. } }
728 }
729 @end lilypond
730
731 By cutting and pasting everything in the @qq{ly snippet} section,
732 you have a starting template for experiments.  To see exactly the
733 same output (line-width and all), copy everything from @qq{Start
734 cut-&-pastable section} to the bottom of the file.
735
736
737 @ignore
738 This is item 825
739
740 @n ode Keyboard navigation
741 @s ubsection Keyboard navigation
742
743 @warning{This features is only available in the HTML manuals.}
744
745 @c TODO: once this is figured out, insert it here.
746
747 We are currently working on this feature.
748 @end ignore
749
750 @node Overview of manuals
751 @subsection Overview of manuals
752
753 There is a lot of documentation for LilyPond.  New users are
754 sometimes confused about what part(s) they should read, and
755 occasionally skip over reading vital portions.
756
757 @warning{Please do not skip over important parts of the
758 documentation.  You will find it much harder to understand later
759 sections.}
760
761 @itemize
762
763 @item
764 @strong{Before trying to do @emph{anything}}: read the Learning
765 manual's @ref{Tutorial}, and @ref{Common notation}.  If you
766 encounter musical terms which you do not recognize, please look
767 them up in the @rglosnamed{Top, Glossary}.
768
769 @item
770 @strong{Before trying to write a complete piece of music}: read
771 the Learning manual's @ref{Fundamental concepts}.  After that, you
772 may want to look in relevant sections of the
773 @rusernamed{Top, Notation reference}.
774
775 @item
776 @strong{Before trying to change the default output}: read the
777 Learning manual's @ref{Tweaking output}.
778
779 @item
780 @strong{Before undertaking a large project}: read the Usage
781 document's @rprogram{Suggestions for writing files}.
782
783 @end itemize
784
785
786