]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
identifier => variable.
[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 @end menu
12
13
14 @node How LilyPond files work
15 @section How LilyPond files work
16
17 The LilyPond input format is quite free-form, giving experienced
18 users a lot of flexibility to structure their files however they
19 wish.  However, this flexibility can make things confusing for new
20 users.  This section will explain some of this structure, but may
21 gloss over some details in favor of simplicity.  For a complete
22 description of the input format, see @ruser{File structure}.
23
24 @menu
25 * Introduction to the LilyPond file structure::  
26 * Score is a (single) compound musical expression::  
27 @end menu
28
29 @node Introduction to the LilyPond file structure
30 @subsection Introduction to the LilyPond file structure
31
32 A basic example of a lilypond input file is
33
34 @example
35 \version "2.11.23"
36 \score @{
37   @var{...compound music expression...}  % all the music goes here!
38   \header @{ @}
39   \layout @{ @}
40   \midi @{ @}
41 @}
42 @end example
43
44 @noindent
45 There are many variations of this basic pattern, but this
46 example serves as a useful starting place.
47
48 At this point, you may be confused, since you have never seen a
49 @code{\score@{@}} before.  This is because LilyPond automatically
50 adds the extra commands when you give it simple input.  LilyPond
51 treats input like this:
52
53 @example
54 \relative c'' @{
55   c4 a d c
56 @}
57 @end example
58
59 @noindent
60 as shorthand for this:
61
62 @example
63 \score @{
64   \relative c'' @{
65     c4 a b c
66   @}
67   \layout @{ @}
68 @}
69 @end example
70
71 In other words, if the input contains a single music expression,
72 LilyPond will interpret the file as though the music expression
73 was wrapped up inside a @code{\score@{@}}.
74
75 @smallspace
76
77 A @code{\score} must begin with a compound music expression.
78 Remember that a music expression could be anything from a single
79 note to a huge
80
81 @example
82 @{
83   \new GrandStaff <<
84     @var{...insert the whole score of a Wagner opera in here...}
85   >>
86 @}
87 @end example
88
89 @noindent
90 Since everything is inside @code{@{ ... @}}, it counts
91 as one music expression.
92
93 As we saw previously, the @code{\score} can contain other things,
94 such as
95
96 @example
97 \score @{
98   @{ c'4 a b c' @}
99   \header @{ @}
100   \layout @{ @}
101   \midi @{ @}
102 @}
103 @end example
104
105 @noindent
106 Some people put some of those commands outside the @code{\score}
107 block -- for example, @code{\header} is often placed above the
108 @code{\score}.  That's just another shorthand that LilyPond
109 accepts.
110
111 @smallspace
112
113 @cindex variables
114
115 Another great shorthand is the ability to define variables.  All
116 the templates use this
117
118 @example
119 melody = \relative c' @{
120   c4 a b c
121 @}
122
123 \score @{
124   @{ \melody @}
125 @}
126 @end example
127
128 When LilyPond looks at this file, it takes the value of
129 @code{melody} (everything after the equals sign) and inserts it
130 whenever it sees @code{\melody}.  There's nothing special about
131 the names -- it could be @code{melody}, @code{global},
132 @code{pianorighthand}, or @code{foofoobarbaz}.  You can use
133 whatever variable names you want.  For more details, see
134 @ruser{Saving typing with variables and functions}.
135
136
137 @seealso
138
139 For a complete definition of the input format, see @ruser{File
140 structure}.
141
142
143 @node Score is a (single) compound musical expression
144 @subsection Score is a (single) compound musical expression
145
146 @cindex Compound music expression
147 @cindex Music expression, compound
148
149 We saw the general organization of LilyPond input files in the
150 previous section, @ref{How LilyPond files work}.  But we seemed to
151 skip over the most important part: how do we figure out what to
152 write after @code{\score}?
153
154 We didn't skip over it at all.  The big mystery is simply that
155 there @emph{is} no mystery.  This line explains it all:
156
157 @quotation
158 @emph{A @code{\score} must begin with a compound music expression.}
159 @end quotation
160
161 @noindent
162 You may find it useful to review @ruser{Music expressions
163 explained}.  In that section, we saw how to build big music
164 expressions from small pieces -- we started from notes, then
165 chords, etc.  Now we're going to start from a big music expression
166 and work our way down.
167
168 @example
169 \score @{
170   @{ % this brace begins the overall compound music expression
171     \new GrandStaff <<
172       @var{...insert the whole score of a Wagner opera in here...}
173     >>
174   @} % this brace ends the overall compound music expression
175   \layout @{ @}
176 @}
177 @end example
178
179 A whole Wagner opera would easily double the length of this
180 manual, so let's just add a singer and piano.  We don't need a
181 @code{GrandStaff} for this ensemble, so we shall remove it.  We
182 @emph{do} need a singer and a piano, though.
183
184 @example
185 \score @{
186   @{
187     <<
188       \new Staff = "singer" <<
189       >>
190       \new PianoStaff = piano <<
191       >>
192     >>
193   @}
194   \layout @{ @}
195 @}
196 @end example
197
198 Remember that we use @code{<<} and @code{>>} to show simultaneous
199 music.  And we definitely want to show the vocal part and piano
200 part at the same time, not one after the other!
201
202 @example
203 \score @{
204   @{
205     <<
206       \new Staff = "singer" <<
207         \new Voice = "vocal" @{ @}
208       >>
209       \new Lyrics \lyricsto vocal \new Lyrics @{ @}
210       \new PianoStaff = "piano" <<
211         \new Staff = "upper" @{ @}
212         \new Staff = "lower" @{ @}
213       >>
214     >>
215   @}
216   \layout @{ @}
217 @}
218 @end example
219
220 Now we have a lot more details.  We have the singer's staff: it
221 contains a @code{Voice} (in LilyPond, this term refers to a set of
222 notes, not necessarily vocal notes -- for example, a violin
223 generally plays one voice) and some lyrics.  We also have a piano
224 staff: it contains an upper staff (right hand) and a lower staff
225 (left hand).
226
227 At this stage, we could start filling in notes.  Inside the curly
228 braces next to @code{\new Voice = vocal}, we could start writing
229
230 @example
231 \relative c'' @{
232   a4 b c d
233 @}
234 @end example
235
236 But if we did that, the @code{\score} section would get pretty
237 long, and it would be harder to understand what was happening.  So
238 let's use variables instead.
239
240 @example
241 melody = @{ @}
242 text = @{ @}
243 upper = @{ @}
244 lower = @{ @}
245 \score @{
246   @{
247     <<
248       \new Staff = "singer" <<
249         \new Voice = "vocal" @{ \melody @}
250       >>
251       \new Lyrics \lyricsto vocal \new Lyrics @{ \text @}
252       \new PianoStaff = "piano" <<
253         \new Staff = "upper" @{ \upper @}
254         \new Staff = "lower" @{ \lower @}
255       >>
256     >>
257   @}
258   \layout @{ @}
259 @}
260 @end example
261
262 @noindent
263 Remember that you can use almost any name you like.  The
264 limitations on variable names are detailed in @ruser{File
265 structure}.
266
267 When writing (or reading) a @code{\score} section, just take it
268 slowly and carefully.  Start with the outer layer, then work on
269 each smaller layer.  It also really helps to be strict with
270 indentation -- make sure that each item on the same layer starts
271 on the same horizontal position in your text editor.
272
273
274 @node Voices contain music
275 @section Voices contain music
276
277 TODO: something cheesy and vaguely witty about voices being the
278 fundamental thing that includes music in lilypond.
279
280 @menu
281 * I'm seeing Voices::           
282 * Explicitly instantiating voices::  
283 * Voices and vocals::           
284 @end menu
285
286 @c too cheesy?  I'm not certain.  -gp
287 @node I'm seeing Voices
288 @subsection I'm seeing Voices
289
290 @cindex polyphony
291
292 TODO: add intro about voices vs. layers vs. whatever.
293
294 TODO: sex this up with colors and stuff.  -gp 
295
296 TODO: actually, a general rewrite is needed.  -gp
297
298
299 The easiest way to enter fragments with more than one voice on a
300 staff is to enter each voice as a sequence (with @code{@{...@}}),
301 and combine them simultaneously, separating the voices with
302 @code{\\}
303
304 @funindex \\
305
306 @lilypond[quote,verbatim,fragment]
307 \new Staff \relative c' {
308   c16 d e f
309   <<
310     { g4 f e | d2 e2 } \\
311     { r8 e4 d c8 ~ | c b16 a b8 g ~ g2 } \\
312     { s2. | s4 b4 c2 }
313   >>
314 }
315 @end lilypond
316
317 @cindex layers
318
319 The separator causes @internalsref{Voice}
320 contexts@footnote{Polyphonic voices are sometimes
321 called @q{layers} in other notation packages} to be instantiated.
322 They bear the names @code{"1"}, @code{"2"}, etc.  In each of these
323 contexts, vertical direction of slurs, stems, etc., is set
324 appropriately.
325
326 These voices are all separate from the voice that contains the
327 notes just outside the @code{<< \\ >>} construct.  This should be
328 noted when making changes at the voice level.  This also means
329 that slurs and ties cannot go into or out of a @code{<< \\ >>}
330 construct.  Conversely, parallel voices from separate @code{<< \\
331 >>} constructs on the same staff are the same voice.  Here is the
332 same example, with different noteheads and colors for each voice.
333 Note that the change to the note-head style in the main voice does
334 not affect the inside of the @code{<< \\ >>} constructs.  Also,
335 the change to the second voice in the first @code{<< \\ >>}
336 construct is effective in the second @code{<< \\ >>}, and the
337 voice is tied across the two constructs.
338
339 @cindex note heads, styles
340
341 @lilypond[quote,verbatim,fragment]
342 \new Staff \relative c' {
343   \override NoteHead #'style = #'cross
344   \override NoteHead #'color = #red
345   c16 d e f
346   <<
347     { g4 f e } \\
348     { \override NoteHead #'style = #'triangle
349       \override NoteHead #'color = #blue
350     r8 e4 d c8 ~ }
351   >> |
352   <<
353     { d2 e2 } \\
354     { c8 b16 a b8 g ~ g2 } \\
355     { \override NoteHead #'style = #'slash 
356       \override NoteHead #'color = #green
357       s4 b4 c2 }
358   >>
359 }
360 @end lilypond
361
362 Polyphony does not change the relationship of notes within a
363 @code{\relative @{ @}} block.  Each note is calculated relative to
364 the note immediately preceding it.
365
366 @example
367 \relative @{ noteA << noteB \\ noteC >> noteD @}
368 @end example
369
370 @code{noteC} is relative to @code{noteB}, not @code{noteA};
371 @code{noteD} is relative to @code{noteC}, not @code{noteB} or
372 @code{noteA}.
373
374
375 @node Explicitly instantiating voices
376 @subsection Explicitly instantiating voices
377
378 TODO: more colors and stuff.
379
380 @internalsref{Voice} contexts can also be instantiated manually
381 inside a @code{<< >>} block to create polyphonic music, using
382 @code{\voiceOne}, up to @code{\voiceFour} to assign stem
383 directions and a horizontal shift for each part.
384
385 Specifically,
386 @example
387 << \upper \\ \lower >>
388 @end example
389
390 @noindent
391 is equivalent to
392
393 @example
394 <<
395   \new Voice = "1" @{ \voiceOne \upper @}
396   \new Voice = "2" @{ \voiceTwo \lower @}
397 >>
398 @end example
399
400 The @code{\voiceXXX} commands set the direction of stems, slurs,
401 ties, articulations, text annotations, augmentation dots of dotted
402 notes, and fingerings.  @code{\voiceOne} and @code{\voiceThree}
403 make these objects point upwards, while @code{\voiceTwo} and
404 @code{\voiceFour} make them point downwards.  The command
405 @code{\oneVoice} will revert back to the normal setting.
406
407 An expression that appears directly inside a @code{<< >>} belongs
408 to the main voice.  This is useful when extra voices appear while
409 the main voice is playing.  Here is a more correct rendition of
410 the example from the previous section.  The crossed colored
411 noteheads demonstrate that the main melody is now in a single
412 voice context.
413
414 @lilypond[quote,ragged-right,verbatim]
415 \new Staff \relative c' {
416   \override NoteHead #'style = #'cross
417   \override NoteHead #'color = #red
418   c16 d e f
419   \voiceOne
420   <<
421     { g4 f e | d2 e2 }
422     \new Voice="1" { \voiceTwo
423       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
424       \oneVoice
425     }
426     \new Voice { \voiceThree
427       s2. | s4 b4 c2
428       \oneVoice
429     }
430   >>
431   \oneVoice
432 }
433 @end lilypond
434
435 The correct definition of the voices allows the melody to be
436 slurred.
437
438 @lilypond[quote,ragged-right,verbatim]
439 \new Staff \relative c' {
440   c16^( d e f
441   \voiceOne
442   <<
443     { g4 f e | d2 e2) }
444     \context Voice="1" { \voiceTwo
445       r8 e4 d c8 ~ | c8 b16 a b8 g ~ g2
446       \oneVoice
447     }
448     \new Voice { \voiceThree
449       s2. s4 b4 c2
450       \oneVoice
451     }
452   >>
453   \oneVoice
454 }
455 @end lilypond
456
457 Avoiding the @code{\\} separator also allows nesting polyphony
458 constructs, which in some case might be a more natural way to
459 typeset the music.
460
461 @lilypond[quote,ragged-right,verbatim]
462 \new Staff \relative c' {
463   c16^( d e f
464   \voiceOne
465   <<
466     { g4 f e | d2 e2) }
467     \context Voice="1" { \voiceTwo
468       r8 e4 d c8 ~ |
469       <<
470         {c8 b16 a b8 g ~ g2}
471         \new Voice { \voiceThree
472           s4 b4 c2
473           \oneVoice
474         }
475       >>
476     \oneVoice
477     }
478   >>
479   \oneVoice
480 }
481 @end lilypond
482
483
484 @node Voices and vocals
485 @subsection Voices and vocals
486
487 Vocal music presents a special difficulty: we need to combine two
488 expressions -- notes and lyrics.
489
490 You have already seen the @code{\lyricsAdd@{@}} command, which
491 handles simple cases for you.  However, @code{\lyricsAdd@{@}} is
492 very limited.  For most music, you must explicitly link the lyrics
493 to the notes with @code{\lyricsTo@{@}}
494
495 @lilypond[quote,verbatim,fragment]
496 <<
497   \new Voice = "one" \relative c'' {
498     \autoBeamOff
499     \time 2/4
500     c4 b8. a16 g4. f8 e4 d c2
501   }
502   \new Lyrics \lyricsto "one" {
503     No more let sins and sor -- rows grow.
504   }
505 >>
506 @end lilypond
507
508 TODO: get some vocal person to write more.
509
510
511 @node TODO new sec fundamental
512 @section TODO new sec fundamental
513
514 blh blah
515
516
517 @menu
518 * On the un-nestedness of brackets and ties::  
519 @end menu
520
521 @c my name start sucking the more docs I write. -gp
522 @node On the un-nestedness of brackets and ties
523 @subsection On the un-nestedness of brackets and ties
524
525 Different kinds of brackets and ties may be mixed freely,
526
527 TODO: improve this example
528
529 @lilypond[quote,verbatim,fragment,ragged-right]
530 {
531   r16[ g16 \times 2/3 {r16 e'8] }
532   g16( a \times 2/3 {b d e') }
533   g8[( a \times 2/3 {b d') e'~]}
534   \times 4/5 {e'32\( a b d' e'} a'4.\)
535 }
536 @end lilypond
537
538 TODO... add more info?  Fluff up the first sentence?
539
540
541
542