]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
Misc LM updates.
[lilypond.git] / Documentation / user / fundamental.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond-learning.tely
3
4 @node Fundamental concepts
5 @chapter Fundamental concepts
6
7 @menu
8 * How LilyPond files work::     
9 * Voices contain music::        
10 * TODO new sec fundamental::    
11 * Extending the templates::     
12 * Scores and parts::            
13 @end menu
14
15
16 @node How LilyPond files work
17 @section How LilyPond files work
18
19 The LilyPond input format is quite free-form, giving experienced
20 users a lot of flexibility to structure their files however they
21 wish.  However, this flexibility can make things confusing for new
22 users.  This section will explain some of this structure, but may
23 gloss over some details in favor of simplicity.  For a complete
24 description of the input format, see @ruser{File structure}.
25
26 @menu
27 * Introduction to the LilyPond file structure::  
28 * Score is a (single) compound musical expression::  
29 @end menu
30
31 @node Introduction to the LilyPond file structure
32 @subsection Introduction to the LilyPond file structure
33
34 A basic example of a lilypond input file is
35
36 @example
37 \version "2.11.23"
38 \score @{
39   @var{...compound music expression...}  % all the music goes here!
40   \header @{ @}
41   \layout @{ @}
42   \midi @{ @}
43 @}
44 @end example
45
46 @noindent
47 There are many variations of this basic pattern, but this
48 example serves as a useful starting place.
49
50 At this point, you may be confused, since you have never seen a
51 @code{\score@{@}} before.  This is because LilyPond automatically
52 adds the extra commands when you give it simple input.  LilyPond
53 treats input like this:
54
55 @example
56 \relative c'' @{
57   c4 a d c
58 @}
59 @end example
60
61 @noindent
62 as shorthand for this:
63
64 @example
65 \score @{
66   \relative c'' @{
67     c4 a b c
68   @}
69   \layout @{ @}
70 @}
71 @end example
72
73 In other words, if the input contains a single music expression,
74 LilyPond will interpret the file as though the music expression
75 was wrapped up inside a @code{\score@{@}}.
76
77 @smallspace
78
79 A @code{\score} must begin with a compound music expression.
80 Remember that a music expression could be anything from a single
81 note to a huge
82
83 @example
84 @{
85   \new GrandStaff <<
86     @var{...insert the whole score of a Wagner opera in here...}
87   >>
88 @}
89 @end example
90
91 @noindent
92 Since everything is inside @code{@{ ... @}}, it counts
93 as one music expression.
94
95 As we saw previously, the @code{\score} can contain other things,
96 such as
97
98 @example
99 \score @{
100   @{ c'4 a b c' @}
101   \header @{ @}
102   \layout @{ @}
103   \midi @{ @}
104 @}
105 @end example
106
107 @noindent
108 Some people put some of those commands outside the @code{\score}
109 block -- for example, @code{\header} is often placed above the
110 @code{\score}.  That's just another shorthand that LilyPond
111 accepts.
112
113 @smallspace
114
115 @cindex variables
116
117 Another great shorthand is the ability to define variables.  All
118 the templates use this
119
120 @example
121 melody = \relative c' @{
122   c4 a b c
123 @}
124
125 \score @{
126   @{ \melody @}
127 @}
128 @end example
129
130 When LilyPond looks at this file, it takes the value of
131 @code{melody} (everything after the equals sign) and inserts it
132 whenever it sees @code{\melody}.  There's nothing special about
133 the names -- it could be @code{melody}, @code{global},
134 @code{pianorighthand}, or @code{foofoobarbaz}.  You can use
135 whatever variable names you want.  For more details, see
136 @ruser{Saving typing with variables and functions}.
137
138
139 @seealso
140
141 For a complete definition of the input format, see @ruser{File
142 structure}.
143
144
145 @node Score is a (single) compound musical expression
146 @subsection Score is a (single) compound musical expression
147
148 @cindex Compound music expression
149 @cindex Music expression, compound
150
151 We saw the general organization of LilyPond input files in the
152 previous section, @ref{How LilyPond files work}.  But we seemed to
153 skip over the most important part: how do we figure out what to
154 write after @code{\score}?
155
156 We didn't skip over it at all.  The big mystery is simply that
157 there @emph{is} no mystery.  This line explains it all:
158
159 @quotation
160 @emph{A @code{\score} must begin with a compound music expression.}
161 @end quotation
162
163 @noindent
164 You may find it useful to review @ruser{Music expressions
165 explained}.  In that section, we saw how to build big music
166 expressions from small pieces -- we started from notes, then
167 chords, etc.  Now we're going to start from a big music expression
168 and work our way down.
169
170 @example
171 \score @{
172   @{ % this brace begins the overall compound music expression
173     \new GrandStaff <<
174       @var{...insert the whole score of a Wagner opera in here...}
175     >>
176   @} % this brace ends the overall compound music expression
177   \layout @{ @}
178 @}
179 @end example
180
181 A whole Wagner opera would easily double the length of this
182 manual, so let's just add a singer and piano.  We don't need a
183 @code{GrandStaff} for this ensemble, so we shall remove it.  We
184 @emph{do} need a singer and a piano, though.
185
186 @example
187 \score @{
188   @{
189     <<
190       \new Staff = "singer" <<
191       >>
192       \new PianoStaff = piano <<
193       >>
194     >>
195   @}
196   \layout @{ @}
197 @}
198 @end example
199
200 Remember that we use @code{<<} and @code{>>} to show simultaneous
201 music.  And we definitely want to show the vocal part and piano
202 part at the same time, not one after the other!
203
204 @example
205 \score @{
206   @{
207     <<
208       \new Staff = "singer" <<
209         \new Voice = "vocal" @{ @}
210       >>
211       \new Lyrics \lyricsto vocal \new Lyrics @{ @}
212       \new PianoStaff = "piano" <<
213         \new Staff = "upper" @{ @}
214         \new Staff = "lower" @{ @}
215       >>
216     >>
217   @}
218   \layout @{ @}
219 @}
220 @end example
221
222 Now we have a lot more details.  We have the singer's staff: it
223 contains a @code{Voice} (in LilyPond, this term refers to a set of
224 notes, not necessarily vocal notes -- for example, a violin
225 generally plays one voice) and some lyrics.  We also have a piano
226 staff: it contains an upper staff (right hand) and a lower staff
227 (left hand).
228
229 At this stage, we could start filling in notes.  Inside the curly
230 braces next to @code{\new Voice = vocal}, we could start writing
231
232 @example
233 \relative c'' @{
234   a4 b c d
235 @}
236 @end example
237
238 But if we did that, the @code{\score} section would get pretty
239 long, and it would be harder to understand what was happening.  So
240 let's use variables instead.
241
242 @example
243 melody = @{ @}
244 text = @{ @}
245 upper = @{ @}
246 lower = @{ @}
247 \score @{
248   @{
249     <<
250       \new Staff = "singer" <<
251         \new Voice = "vocal" @{ \melody @}
252       >>
253       \new Lyrics \lyricsto vocal \new Lyrics @{ \text @}
254       \new PianoStaff = "piano" <<
255         \new Staff = "upper" @{ \upper @}
256         \new Staff = "lower" @{ \lower @}
257       >>
258     >>
259   @}
260   \layout @{ @}
261 @}
262 @end example
263
264 @noindent
265 Remember that you can use almost any name you like.  The
266 limitations on variable names are detailed in @ruser{File
267 structure}.
268
269 When writing (or reading) a @code{\score} section, just take it
270 slowly and carefully.  Start with the outer layer, then work on
271 each smaller layer.  It also really helps to be strict with
272 indentation -- make sure that each item on the same layer starts
273 on the same horizontal position in your text editor.
274
275
276 @node Voices contain music
277 @section Voices contain music
278
279 TODO: something cheesy and vaguely witty about voices being the
280 fundamental thing that includes music in lilypond.
281
282 @menu
283 * I'm seeing Voices::           
284 * Explicitly instantiating voices::  
285 * Voices and vocals::           
286 @end menu
287
288 @c too cheesy?  I'm not certain.  -gp
289 @node I'm seeing Voices
290 @subsection I'm seeing Voices
291
292 @cindex polyphony
293
294 TODO: add intro about voices vs. layers vs. whatever.
295
296 TODO: sex this up with colors and stuff.  -gp 
297
298 TODO: actually, a general rewrite is needed.  -gp
299
300
301 The easiest way to enter fragments with more than one voice on a
302 staff is to enter each voice as a sequence (with @code{@{...@}}),
303 and combine them simultaneously, separating the voices with
304 @code{\\}
305
306 @funindex \\
307
308 @lilypond[quote,verbatim,fragment]
309 \new Staff \relative c' {
310   c16 d e f
311   <<
312     { g4 f e | d2 e2 } \\
313     { r8 e4 d c8 ~ | c b16 a b8 g ~ g2 } \\
314     { s2. | s4 b4 c2 }
315   >>
316 }
317 @end lilypond
318
319 @cindex layers
320
321 The separator causes @internalsref{Voice}
322 contexts@footnote{Polyphonic voices are sometimes
323 called @q{layers} in other notation packages} to be instantiated.
324 They bear the names @code{"1"}, @code{"2"}, etc.  In each of these
325 contexts, vertical direction of slurs, stems, etc., is set
326 appropriately.
327
328 These voices are all separate from the voice that contains the
329 notes just outside the @code{<< \\ >>} construct.  This should be
330 noted when making changes at the voice level.  This also means
331 that slurs and ties cannot go into or out of a @code{<< \\ >>}
332 construct.  Conversely, parallel voices from separate @code{<< \\
333 >>} constructs on the same staff are the same voice.  Here is the
334 same example, with different noteheads and colors for each voice.
335 Note that the change to the note-head style in the main voice does
336 not affect the inside of the @code{<< \\ >>} constructs.  Also,
337 the change to the second voice in the first @code{<< \\ >>}
338 construct is effective in the second @code{<< \\ >>}, and the
339 voice is tied across the two constructs.
340
341 @cindex note heads, styles
342
343 @lilypond[quote,verbatim,fragment]
344 \new Staff \relative c' {
345   \override NoteHead #'style = #'cross
346   \override NoteHead #'color = #red
347   c16 d e f
348   <<
349     { g4 f e } \\
350     { \override NoteHead #'style = #'triangle
351       \override NoteHead #'color = #blue
352     r8 e4 d c8 ~ }
353   >> |
354   <<
355     { d2 e2 } \\
356     { c8 b16 a b8 g ~ g2 } \\
357     { \override NoteHead #'style = #'slash 
358       \override NoteHead #'color = #green
359       s4 b4 c2 }
360   >>
361 }
362 @end lilypond
363
364 Polyphony does not change the relationship of notes within a
365 @code{\relative @{ @}} block.  Each note is calculated relative to
366 the note immediately preceding it.
367
368 @example
369 \relative @{ noteA << noteB \\ noteC >> noteD @}
370 @end example
371
372 @code{noteC} is relative to @code{noteB}, not @code{noteA};
373 @code{noteD} is relative to @code{noteC}, not @code{noteB} or
374 @code{noteA}.
375
376
377 @node Explicitly instantiating voices
378 @subsection Explicitly instantiating voices
379
380 TODO: more colors and stuff.
381
382 @internalsref{Voice} contexts can also be instantiated manually
383 inside a @code{<< >>} block to create polyphonic music, using
384 @code{\voiceOne}, up to @code{\voiceFour} to assign stem
385 directions and a horizontal shift for each part.
386
387 Specifically,
388 @example
389 << \upper \\ \lower >>
390 @end example
391
392 @noindent
393 is equivalent to
394
395 @example
396 <<
397   \new Voice = "1" @{ \voiceOne \upper @}
398   \new Voice = "2" @{ \voiceTwo \lower @}
399 >>
400 @end example
401
402 The @code{\voiceXXX} commands set the direction of stems, slurs,
403 ties, articulations, text annotations, augmentation dots of dotted
404 notes, and fingerings.  @code{\voiceOne} and @code{\voiceThree}
405 make these objects point upwards, while @code{\voiceTwo} and
406 @code{\voiceFour} make them point downwards.  The command
407 @code{\oneVoice} will revert back to the normal setting.
408
409 An expression that appears directly inside a @code{<< >>} belongs
410 to the main voice.  This is useful when extra voices appear while
411 the main voice is playing.  Here is a more correct rendition of
412 the example from the previous section.  The crossed colored
413 noteheads demonstrate that the main melody is now in a single
414 voice context.
415
416 @lilypond[quote,ragged-right,verbatim]
417 \new Staff \relative c' {
418   \override NoteHead #'style = #'cross
419   \override NoteHead #'color = #red
420   c16 d e f
421   \voiceOne
422   <<
423     { g4 f e | d2 e2 }
424     \new Voice="1" { \voiceTwo
425       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
426       \oneVoice
427     }
428     \new Voice { \voiceThree
429       s2. | s4 b4 c2
430       \oneVoice
431     }
432   >>
433   \oneVoice
434 }
435 @end lilypond
436
437 The correct definition of the voices allows the melody to be
438 slurred.
439
440 @lilypond[quote,ragged-right,verbatim]
441 \new Staff \relative c' {
442   c16^( d e f
443   \voiceOne
444   <<
445     { g4 f e | d2 e2) }
446     \context Voice="1" { \voiceTwo
447       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
448       \oneVoice
449     }
450     \new Voice { \voiceThree
451       s2. s4 b4 c2
452       \oneVoice
453     }
454   >>
455   \oneVoice
456 }
457 @end lilypond
458
459 Avoiding the @code{\\} separator also allows nesting polyphony
460 constructs, which in some case might be a more natural way to
461 typeset the music.
462
463 @lilypond[quote,ragged-right,verbatim]
464 \new Staff \relative c' {
465   c16^( d e f
466   \voiceOne
467   <<
468     { g4 f e | d2 e2) }
469     \context Voice="1" { \voiceTwo
470       r8 e4 d c8 ~ |
471       <<
472         {c8 b16 a b8 g ~ g2}
473         \new Voice { \voiceThree
474           s4 b4 c2
475           \oneVoice
476         }
477       >>
478     \oneVoice
479     }
480   >>
481   \oneVoice
482 }
483 @end lilypond
484
485
486 @node Voices and vocals
487 @subsection Voices and vocals
488
489 Vocal music presents a special difficulty: we need to combine two
490 expressions -- notes and lyrics.
491
492 You have already seen the @code{\lyricsAdd@{@}} command, which
493 handles simple cases for you.  However, @code{\lyricsAdd@{@}} is
494 very limited.  For most music, you must explicitly link the lyrics
495 to the notes with @code{\lyricsTo@{@}}
496
497 @lilypond[quote,verbatim,fragment]
498 <<
499   \new Voice = "one" \relative c'' {
500     \autoBeamOff
501     \time 2/4
502     c4 b8. a16 g4. f8 e4 d c2
503   }
504   \new Lyrics \lyricsto "one" {
505     No more let sins and sor -- rows grow.
506   }
507 >>
508 @end lilypond
509
510 TODO: get some vocal person to write more.
511
512
513 @node TODO new sec fundamental
514 @section TODO new sec fundamental
515
516 blh blah
517
518
519 @menu
520 * On the un-nestedness of brackets and ties::  
521 @end menu
522
523 @c my name start sucking the more docs I write. -gp
524 @node On the un-nestedness of brackets and ties
525 @subsection On the un-nestedness of brackets and ties
526
527 Different kinds of brackets and ties may be mixed freely,
528
529 TODO: improve this example
530
531 @lilypond[quote,verbatim,fragment,ragged-right]
532 {
533   r16[ g16 \times 2/3 {r16 e'8] }
534   g16( a \times 2/3 {b d e') }
535   g8[( a \times 2/3 {b d') e'~]}
536   \times 4/5 {e'32\( a b d' e'} a'4.\)
537 }
538 @end lilypond
539
540 TODO... add more info?  Fluff up the first sentence?
541
542
543 @node Extending the templates
544 @section Extending the templates
545
546 You've read the tutorial, you know how to write music.  But how can you
547 get the staves that you want?  The templates are ok, but what if you
548 want something that isn't covered?
549
550 @menu
551 * Soprano and cello::           
552 * TODO another example of extending templates::  
553 @end menu
554
555 @node Soprano and cello
556 @unnumberedsubsec Soprano and cello
557
558 Start off with the template that seems closest to what you want to end
559 up with.  Let's say that you want to write something for soprano and
560 cello.  In this case, we would start with @q{Notes and lyrics} (for the
561 soprano part).
562
563 @example
564 \version "2.11.23"
565 melody = \relative c' @{
566   \clef treble
567   \key c \major
568   \time 4/4
569
570   a4 b c d
571 @}
572
573 text = \lyricmode @{
574   Aaa Bee Cee Dee
575 @}
576
577 \score@{
578   <<
579     \new Voice = "one" @{
580       \autoBeamOff
581       \melody
582     @}
583     \new Lyrics \lyricsto "one" \text
584   >>
585   \layout @{ @}
586   \midi @{ @}
587 @}
588 @end example
589
590 Now we want to add a cello part.  Let's look at the @q{Notes only} example:
591
592 @example
593 \version "2.11.23"
594 melody = \relative c' @{
595   \clef treble
596   \key c \major
597   \time 4/4
598
599   a4 b c d
600 @}
601
602 \score @{
603 \new Staff \melody
604 \layout @{ @}
605 \midi @{ @}
606 @}
607 @end example
608
609 We don't need two @code{\version} commands.  We'll need the @code{melody}
610 section.  We don't want two @code{\score} sections -- if we had two
611 @code{\score}s, we'd get the two parts separately.  We want them together,
612 as a duet.  Within the @code{\score} section, we don't need two
613 @code{\layout} or @code{\midi}.
614
615 If we simply cut and paste the @code{melody} section, we would end up with
616 two @code{melody} sections.  So let's rename them.  We'll call the section
617 for the soprano @code{sopranoMusic} and the section for the cello
618 @code{celloMusic}.  While we're doing this, let's rename @code{text}
619 to be @code{sopranoLyrics}.  Remember to rename both instances of all
620 these names -- both the initial definition (the
621 @code{melody = relative c' @{ } part) and the name's use (in the
622 @code{\score} section).
623
624 While we're doing this, let's change the cello part's staff -- celli
625 normally use bass clef.  We'll also give the cello some different
626 notes.
627
628 @example
629 \version "2.11.23"
630 sopranoMusic = \relative c' @{
631   \clef treble
632   \key c \major
633   \time 4/4
634
635   a4 b c d
636 @}
637
638 sopranoLyrics = \lyricmode @{
639   Aaa Bee Cee Dee
640 @}
641
642 celloMusic = \relative c @{
643   \clef bass
644   \key c \major
645   \time 4/4
646
647   d4 g fis8 e d4
648 @}
649
650 \score@{
651   <<
652     \new Voice = "one" @{
653       \autoBeamOff
654       \sopranoMusic
655     @}
656     \new Lyrics \lyricsto "one" \sopranoLyrics
657   >>
658   \layout @{ @}
659   \midi @{ @}
660 @}
661 @end example
662
663 This is looking promising, but the cello part won't appear in the
664 score -- we haven't used it in the @code{\score} section.  If we
665 want the cello part to appear under the soprano part, we need to add
666
667 @example
668 \new Staff \celloMusic
669 @end example
670
671 @noindent
672 underneath the soprano stuff.  We also need to add @code{<<} and
673 @code{>>} around the music -- that tells LilyPond that there's
674 more than one thing (in this case, @code{Staff}) happening at once.  The
675 @code{\score} looks like this now
676
677 @example
678 \score@{
679   <<
680     <<
681       \new Voice = "one" @{
682         \autoBeamOff
683         \sopranoMusic
684       @}
685       \new Lyrics \lyricsto "one" \sopranoLyrics
686     >>
687     \new Staff \celloMusic
688   >>
689   \layout @{ @}
690   \midi @{ @}
691 @}
692 @end example
693
694 @noindent
695 This looks a bit messy; the indentation is messed up now.  That is
696 easily fixed.  Here's the complete soprano and cello template.
697
698 @lilypond[quote,verbatim,ragged-right]
699 \version "2.11.23"
700 sopranoMusic = \relative c' {
701   \clef treble
702   \key c \major
703   \time 4/4
704
705   a4 b c d
706 }
707
708 sopranoLyrics = \lyricmode {
709   Aaa Bee Cee Dee
710 }
711
712 celloMusic = \relative c {
713   \clef bass
714   \key c \major
715   \time 4/4
716
717   d4 g fis8 e d4
718 }
719
720 \score{
721   <<
722     <<
723       \new Voice = "one" {
724         \autoBeamOff
725         \sopranoMusic
726       }
727       \new Lyrics \lyricsto "one" \sopranoLyrics
728     >>
729     \new Staff \celloMusic
730   >>
731   \layout { }
732   \midi { }
733 }
734 @end lilypond
735
736
737 @node TODO another example of extending templates
738 @unnumberedsubsec TODO another example of extending templates
739
740 TODO: somebody else fill this in.  You guys like vocal stuff,
741 right?  Get to it.  :)  -gp
742
743
744
745 @node Scores and parts
746 @section Scores and parts
747
748 TODO: this is really old stuff from the really old tutorial.
749 Rewrite, fix, etc.  -gp
750
751 In orchestral music, all notes are printed twice.  Once in a part for
752 the musicians, and once in a full score for the conductor.  Variables can
753 be used to avoid double work.  The music is entered once, and stored in
754 a variable.  The contents of that variable is then used to generate
755 both the part and the full score.
756
757 It is convenient to define the notes in a special file.  For example,
758 suppose that the file @file{horn-music.ly} contains the following part
759 of a horn/@/bassoon duo
760
761 @example
762 hornNotes = \relative c @{
763   \time 2/4
764   r4 f8 a cis4 f e d
765 @}
766 @end example
767
768 @noindent
769 Then, an individual part is made by putting the following in a file
770
771 @example
772 \include "horn-music.ly"
773 \header @{
774   instrument = "Horn in F"
775 @}
776
777 @{
778  \transpose f c' \hornNotes
779 @}
780 @end example
781
782 The line
783
784 @example
785 \include "horn-music.ly"
786 @end example
787
788 @noindent
789 substitutes the contents of @file{horn-music.ly} at this position in
790 the file, so @code{hornNotes} is defined afterwards.  The command
791 @code{\transpose f@tie{}c'} indicates that the argument, being
792 @code{\hornNotes}, should be transposed by a fifth upwards.  Sounding
793 @samp{f} is denoted by notated @code{c'}, which corresponds with the
794 tuning of a normal French Horn in@tie{}F.  The transposition can be seen
795 in the following output
796
797 @lilypond[quote,ragged-right]
798 \transpose f c' \relative c {
799   \time 2/4
800   r4 f8 a cis4 f e d
801 }
802 @end lilypond
803
804 In ensemble pieces, one of the voices often does not play for many
805 measures.  This is denoted by a special rest, the multi-measure
806 rest.  It is entered with a capital @samp{R} followed by a duration
807 (@code{1}@tie{}for a whole note, @code{2}@tie{}for a half note,
808 etc.).  By multiplying the
809 duration, longer rests can be constructed.  For example, this rest
810 takes 3@tie{}measures in 2/4 time
811
812 @example
813 R2*3
814 @end example
815
816 When printing the part, multi-rests
817 must be condensed.  This is done by setting a run-time variable
818
819 @example
820 \set Score.skipBars = ##t
821 @end example
822
823 @noindent
824 This command sets the property @code{skipBars} in the
825 @code{Score} context to true (@code{##t}).  Prepending the rest and
826 this option to the music above, leads to the following result
827
828 @lilypond[quote,ragged-right]
829 \transpose f c' \relative c {
830   \time 2/4
831   \set Score.skipBars = ##t
832   R2*3
833   r4 f8 a cis4 f e d
834 }
835 @end lilypond
836
837
838 The score is made by combining all of the music together.  Assuming
839 that the other voice is in @code{bassoonNotes} in the file
840 @file{bassoon-music.ly}, a score is made with
841
842 @example
843 \include "bassoon-music.ly"
844 \include "horn-music.ly"
845
846 <<
847   \new Staff \hornNotes
848   \new Staff \bassoonNotes
849 >>
850 @end example
851
852 @noindent
853 leading to
854
855 @lilypond[quote,ragged-right]
856 \relative c <<
857   \new Staff {
858     \time 2/4 R2*3
859     r4 f8 a cis4 f e d
860   }
861   \new Staff {
862     \clef bass
863     r4 d,8 f | gis4 c | b bes |
864     a8 e f4 | g d | gis f
865   }
866 >>
867 @end lilypond
868
869
870