]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/fundamental.itely
fa867e96b732ce532cbe204cdbee915d094ab4bd
[lilypond.git] / Documentation / user / fundamental.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
3 @node Fundamental concepts
4 @chapter Fundamental concepts
5
6 @menu
7 * File structure (introduction)::  
8 * How LilyPond files work::     
9 * Score is a single musical expression::  
10 @end menu
11
12
13 @node File structure (introduction)
14 @section File structure (introduction)
15
16 A basic example of a lilypond input file is
17
18 @example
19 \version "2.11.23"
20 \score @{
21   @{ @}     % this is a single music expression;
22             % all the music goes in here.
23   \header @{ @}
24   \layout @{ @}
25   \midi @{ @}
26 @}
27 @end example
28
29 @noindent
30 There are many variations of this basic pattern, but this
31 example serves as a useful starting place.
32
33 The major part of this manual is concerned with entering various
34 forms of music in LilyPond.  However, many music expressions are not
35 valid input on their own, for example, a @code{.ly} file containing
36 only a note
37 @example
38 c'4
39 @end example
40
41 @noindent
42 will result in a parsing error.  Instead, music should be inside other
43 expressions, which may be put in a file by themselves.  Such
44 expressions are called toplevel expressions; see @ruser{File structure}, for
45 a list of all such expressions.
46
47
48 @node How LilyPond files work
49 @section How LilyPond files work
50
51 The LilyPond input format is quite free-form, giving experienced
52 users a lot of flexibility to structure their files however they
53 wish.  However, this flexibility can make things confusing for
54 new users.  This section will explain some of this structure, but
55 may gloss over some details in favor of simplicity.  For a complete
56 description of the input format, see @ruser{File structure}.
57
58 Most examples in this manual are little snippets -- for example
59
60 @example
61 c4 a b c
62 @end example
63
64 As you are (hopefully) aware by now, this will not compile by
65 itself.  These examples are shorthand for complete
66 examples.  They all need at least curly braces to compile
67
68 @example
69 @{
70   c4 a b c
71 @}
72 @end example
73
74 Most examples also make use of the @code{\relative c'}
75 (or @code{c''}) command.  This is not necessary to merely
76 compile the examples, but in most cases the output will
77 look very odd if you omit the @code{\relative c'}.
78
79 @lilypond[quote,fragment,ragged-right,verbatim]
80 \relative c'' {
81   c4 a b c
82 }
83 @end lilypond
84
85 Now we get to the only real stumbling block: LilyPond
86 input in this form is actually @emph{another}
87 shorthand.  Although it compiles and displays the
88 correct output, it is shorthand for
89
90 @example
91 \score @{
92   \relative c'' @{
93     c4 a b c
94   @}
95 @}
96 @end example
97
98 A @code{\score} must begin with a single music
99 expression.  Remember that a music expression could
100 be anything from a single note to a huge
101
102 @example
103 @{
104   \new GrandStaff <<
105     insert the whole score of a Wagner opera in here
106   >>
107 @}
108 @end example
109
110 @noindent
111 Since everything is inside @code{@{ ... @}}, it counts
112 as one music expression.
113
114 The @code{\score} can contain other things, such as
115
116 @example
117 \score @{
118   @{ c'4 a b c' @}
119   \layout @{ @}
120   \midi @{ @}
121   \header @{ @}
122 @}
123 @end example
124
125 @noindent
126 Some people put some of those commands outside the
127 @code{\score} block -- for example, @code{\header} is
128 often placed above the @code{\score}.  That's just
129 another shorthand that LilyPond accepts.
130
131 @cindex variables
132 @cindex identifiers
133
134 Another great shorthand is the ability to define
135 variables.  All the templates use this
136
137 @example
138 melody = \relative c' @{
139   c4 a b c
140 @}
141
142 \score @{
143   @{ \melody @}
144 @}
145 @end example
146
147 When LilyPond looks at this file, it takes the value of
148 @code{melody} (everything after the equals sign) and
149 inserts it whenever it sees
150 @code{\melody}.  There's nothing special about the
151 names -- it could be @code{melody}, @code{global},
152 @code{pianorighthand}, or @code{foofoobarbaz}.  You
153 can use whatever variable names you want.  For
154 more details, see
155 @ruser{Saving typing with identifiers and functions}.
156
157 For a complete definition
158 of the input format, see @ruser{File structure}.
159
160
161 @node Score is a single musical expression
162 @section Score is a single musical expression
163
164 In the previous section, @ruser{How LilyPond files work},
165 we saw the general organization of LilyPond input
166 files.  But we seemed to skip over the most important
167 part: how do we figure out what to write after
168 @code{\score}?
169
170 We didn't skip over it at all.  The big mystery is simply
171 that there @emph{is} no mystery.  This line explains it
172 all:
173
174 @quotation
175 @emph{A @code{\score} must begin with a single music expression.}
176 @end quotation
177
178 @noindent
179 You may find it useful to review
180 @ruser{Music expressions explained}.  In that section, we
181 saw how to build big music expressions from small
182 pieces -- we started from notes, then chords, etc.  Now
183 we're going to start from a big music expression and
184 work our way down.
185
186 @example
187 \score @{
188   @{   % this brace begins the overall music expression
189     \new GrandStaff <<
190       insert the whole score of a Wagner opera in here
191     >>
192   @}   % this brace ends the overall music expression
193   \layout @{ @}
194 @}
195 @end example
196
197 A whole Wagner opera would easily double the length of
198 this manual, so let's just do a singer and piano.  We
199 don't need a @code{GrandStaff} for this ensemble, so we
200 shall remove it.  We @emph{do} need a singer and a piano,
201 though.
202
203 @example
204 \score @{
205   @{
206     <<
207       \new Staff = "singer" <<
208       >>
209       \new PianoStaff = piano <<
210       >>
211     >>
212   @}
213   \layout @{ @}
214 @}
215 @end example
216
217 Remember that we use @code{<<} and @code{>>} to show
218 simultaneous music.  And we definitely want to show
219 the vocal part and piano part at the same time!
220
221 @example
222 \score @{
223   @{
224     <<
225       \new Staff = "singer" <<
226         \new Voice = "vocal" @{ @}
227       >>
228       \new Lyrics \lyricsto vocal \new Lyrics @{ @}
229       \new PianoStaff = "piano" <<
230         \new Staff = "upper" @{ @}
231         \new Staff = "lower" @{ @}
232       >>
233     >>
234   @}
235   \layout @{ @}
236 @}
237 @end example
238
239 Now we have a lot more details.  We have the singer's
240 staff: it contains a @code{Voice} (in LilyPond, this
241 term refers to a set of notes, not necessarily vocal
242 notes -- for example, a violin generally plays one
243 voice) and some lyrics.  We also have a piano staff:
244 it contains an upper staff (right hand) and a lower
245 staff (left hand).
246
247 At this stage, we could start filling in notes.  Inside
248 the curly braces next to @code{\new Voice = vocal},
249 we could start writing
250
251 @example
252 \relative c'' @{
253   a4 b c d
254 @}
255 @end example
256
257 But if we did that, the @code{\score} section would
258 get pretty long, and it would be harder to understand
259 what was happening.  So let's use identifiers (or
260 variables) instead.
261
262 @example
263 melody = @{ @}
264 text = @{ @}
265 upper = @{ @}
266 lower = @{ @}
267 \score @{
268   @{
269     <<
270       \new Staff = "singer" <<
271         \new Voice = "vocal" @{ \melody @}
272       >>
273       \new Lyrics \lyricsto vocal \new Lyrics @{ \text @}
274       \new PianoStaff = "piano" <<
275         \new Staff = "upper" @{ \upper @}
276         \new Staff = "lower" @{ \lower @}
277       >>
278     >>
279   @}
280   \layout @{ @}
281 @}
282 @end example
283
284 @noindent
285 Remember that you can use almost any name you like.  The
286 limitations on identifier names are detailed in
287 @ruser{File structure}.
288
289 When writing a @code{\score} section, or when reading
290 one, just take it slowly and carefully.  Start with
291 the outer layer, then work on each smaller
292 layer.  It also really helps to be strict with
293 indentation -- make sure that each item on the same
294 layer starts on the same horizontal position in your
295 text editor!
296
297
298
299
300