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