]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/internals.texi
release: 1.2.6
[lilypond.git] / Documentation / internals.texi
1 \input texinfo @c -*-texinfo-*-
2 @setfilename internals.info
3 @settitle LilyPond internals
4
5 @node Top, , Spacing, (dir)
6 @top
7 @menu
8 * LilyPond internals::            LilyPond internals
9 @end menu
10
11
12
13 @node LilyPond internals, Overview, , Top
14 @menu
15 * Overview::                      Overview
16 * mudela::                        mudela
17 * Request_engraver::              Request_engraver
18 * Items and spanners::            Items and spanners
19 * Dependencies::                  Dependencies
20 * Breaking::                      Breaking
21 * Spacing::                       Spacing
22 @end menu
23 @chapter LilyPond internals
24
25
26 This documents some aspects of the internals of GNU LilyPond. Some of
27 this stuff comes from e-mail I wrote, some from e-mail others wrote,
28 some are large comments taken away from the headers. This page may be
29 a little incoherent.  Unfortunately, it is also quite outdated.  A
30 more thorough  and understandable document is in the works.
31
32 You should use @code{doc++} to take a peek at the sources.
33
34 @node Overview, mudela, LilyPond internals, LilyPond internals
35 @section Overview
36
37 GNU LilyPond is a "multi-pass" system. The different passes have been
38 created so that they do not depend on each other. In a later stage
39 some parts may be moved into libraries, or seperate programs, or they
40 might be integrated in larger systems.
41
42 @table @samp
43
44 @item Parsing:
45
46 No difficult algorithms. The .ly file is read, and converted to a list
47 of @code{Scores}, which each contain @code{Music} and paper/midi-definitions.
48
49 @item Interpreting music
50
51 The music is walked through in time-order. The iterators which do the
52 walking report Music to Translators which use this information to
53 create elements, either MIDI or "visual" elements. The translators
54 form a hierarchy; the ones for paper output are Engravers, for MIDI
55 Performers.
56
57 The translators swallow Music (mostly atomic gobs called Requests),
58 create elements, broadcast them to other translators on higher or same
59 level in the hierarchy:
60
61 The stem of a voice A is broadcast to the staff which contains A, but
62 not to the stems, beams and noteheads of a different voice (say B) or
63 a different staff. The stem and noteheads of A are coupled, because
64 the the Note_heads_engraver broadcasts its heads, and the Stem_engraver catches
65 these.
66
67 The engraver which agrees to handle a request decides whether to to
68 honor the request, ignore it, or merge it with other requests. Merging
69 of requests is preferably done with other requests done by members of
70 the same voicegroups (beams, brackets, stems). In this way you can put
71 the voices of 2 instruments in a conductor's score so they make chords
72 (the Beam requests of both instruments will be merged).
73
74 @item Prebreaking
75
76 Breakable stuff (eg. clefs and bars) are copied into pre and
77 postbreaks.
78
79 @item Preprocessing
80
81 Some dependencies are resolved, such as the direction of stems, beams,
82 and "horizontal" placement issues (the order of clefs,  keys etc,
83 placement of chords in multi-voice music), 
84
85 @item Break calculation:
86
87 The lines and horizontal positions of the columns are determined.
88
89 @item Breaking
90
91 Through some magical interactions with Line_of_score and Super_elem
92 (check out the source) the "lines" are produced.
93
94 All other spanners can figure across which lines they are spread. If
95 applicable, they break themselves into pieces. After this, each piece
96 (or, if there are no pieces, the original spanner itself) throws out
97 any dependencies which are in the wrong line.
98
99 @item Postprocesing:
100
101 Some items and all spanners need computation after the Paper_column
102 positions are determined. Examples: slurs, vertical positions of
103 staffs.
104
105 @item Output paper
106
107 @end table
108
109 @node mudela, Request_engraver, Overview, LilyPond internals
110 @section mudela
111
112 Most information is stored in the form of a request.  In music
113 typesetting, the user might want to cram a lot more symbols on the
114 paper than actually fits. To reflect this idea (the user asks more
115 than we can do), the container for this data is called Request.
116
117 In a lot of other formats this would be called an 'Event'
118
119 @table @samp
120 @item @code{Barcheck_req}
121     Checks during music processing if start of this voice element
122     coincides with the start of a measure. Handy to check if you left out
123     some voice elts.
124 @item @code{Note_req}
125     LilyPond has to decide if the ball should be hanging left or
126     right. This influences the horizontal dimensions of a column, and this
127     is why request processing should be done before horizontal spacing.
128     Other voices' frivolities may cause the need for accidentals, so this
129     is also for the to decide. The engraver can decide on positioning based on
130     ottava commands and the appropriate clef.
131 @item @code{Rest_req}
132     Typeset a rest.
133 @item @code{Span_req}
134     This type of request typically results in the creation of a @code{Spanner}
135 @item @code{Beam_req}
136     Start/stop a beam.
137     Engraver has to combine this request with the stem_request, since the
138     number of flags that a stem wants to carry will determine the
139     number of beams.
140 @item @code{Dynamic}
141     Each dynamic is bound to one note (a crescendo spanning multiple
142     notes is thought to be made of two "dynamics": a start and a stop).
143     Dynamic changes can occur in a smaller time than the length of its
144     note, therefore fore each  @code{Dynamic} request carries a time, measured
145     from the start of its note.
146 @end table
147
148 @node Request_engraver, Items and spanners, mudela, LilyPond internals
149 @section Request_engraver
150
151 In the previous section the idea of Request has been explained, but
152 this only solves one half of the problem. The other half is deciding
153 which requests should be honored, which should merged with other
154 requests, and which should be ignored. Consider this input
155
156 @example 
157
158         \type Staff < % chord
159                 @{ \meter 2/4; [c8 c8] @}
160                 @{\meter 2/4;  [e8 e8] @}
161         >
162  
163 @end example 
164
165 Both the cs and es are part of a staff (they are in the same
166 Voice_group), so they should share meters, but the two [ ] pairs
167 should be merged.
168
169 The judge in this "allocation" problem a set of brokers: the requests
170 are transmitted to so-called engravers which respond if they want to
171 accept a request eg, the @code{Notehead_engraver} will accept
172 @code{Note_req}s, and turn down @code{Slur_req}s. If the Music_iterator
173 cannot find a engraver that wants the request, it is junked (with a
174 warning message).
175
176 After all requests have been either assigned, or junked, the Engraver
177 will process the requests (which usually means creating an @code{Item}
178 or @code{Spanner}). If a @code{Request_engraver} creates something, it
179 tells the enclosing context. If all items/spanners have been created,
180 then each Engraver is notified of any created Score_element, via a
181 broadcasting system.
182
183 @unnumberedsubsec example:
184
185 @example 
186
187         c4
188  
189 @end example 
190
191 produces:
192
193 @example 
194
195         Note_request (duration 1/4)
196         Stem_request (duration 1/4)
197  
198 @end example 
199
200 Note_request will be taken by a @code{Notehead_engraver}, stem_request
201 will be taken by a @code{Stem_beam_engraver}. @code{Notehead_engraver}
202 creates a @code{Notehead}, @code{Stem_beam_engraver} creates a
203 @code{Stem}. Both announce this to the Staff_engraver. Staff_engraver
204 will tell @code{Stem_beam_engraver} about the @code{Notehead}, which
205 will add the @code{Notehead} to the @code{Stem} it just created.
206
207 To decide on merging, several engravers have been grouped. Please
208 check @file{init/engraver.ly}.
209
210 @node Items and spanners, Dependencies, Request_engraver, LilyPond internals
211 @section Items and spanners
212
213 The symbols that are printed, are generated by items and spanners
214 (staff-elements). An item has one horizontal position, whereas a
215 spanner spans several columns.
216
217 @node Dependencies, Breaking, Items and spanners, LilyPond internals
218 @section Dependencies
219
220 In music symbols depend on each other: the stems of a beam should
221 point in the same direction as the beam itself, so the stems of a beam
222 depend on the beam. In the same way do scripts depend on the direction
223 of the stem. To reflect this, LilyPond has the notion of dependency.
224 It works in the same fashion that @code{make} uses to build programs:
225 before a stem is calculated, its dependencies (the beam) should be
226 calculated. Before a slur is calculated, its dependencies (stems,
227 noteheads) should be calculated.
228
229 @node Breaking, Spacing, Dependencies, LilyPond internals
230 @section Breaking
231
232 So what is this PREBREAK and POSTBREAK stuff?
233
234 Let's take text as an example. In German some compound
235 words change their spelling if they are broken: "backen" becomes
236 "bak-ken".  TeX has a mechanism to deal with this, you would define
237 the spelling of "backen" in TeX in this way
238
239         \discretionary@{bak-@}@{ken@}@{backen@}
240
241 These 3 arguments are called "prebreak", "postbreak" and "nobreak"
242 text.
243
244 The same problem exists when typesetting music. If a line of music is
245 broken, the next line usually gets a clef. So in TeX terms, the clef
246 is a postbreak. The same thing happens with meter signs: Normally the
247 meter follows the bar. If a line is broken at that bar, the bar along
248 with the meter stays on the "last" line, but the next line also gets a
249 meter sign after the clef. Using the previous notation,
250
251         \discretionary@{bar meter@}@{clef meter@}@{ bar meter @}
252
253 In GNU Lilypond, we have the same concepts (and the same
254 terminology). Each (nonrhythmic) symbol is typeset in  a nonrhythmic column
255 At a breakpoint, multiple symbols are printed; symbols to be printed
256 if the line is not broken, symbols to appear on the previous line, and
257 on the next line if it is broken.
258
259 @node Spacing, Top, Breaking, LilyPond internals
260 @section Spacing
261
262 Some terminology: I call a vertical group of symbols (notes) which
263 start at the same time a "column".  Each line of a score has notes in
264 it, grouped in columns. The difference in starting time between those
265 columns makes it possible to determine ideal distances between those
266 columns.
267
268 Example:
269
270 @example 
271
272         time ----->
273
274         cols:   col1    col2    col3    col4
275
276         voice1  1               1
277
278         voice2  2       2       2       2
279
280         (1 is a whole note, 2 a half note.)
281
282         time_difference (col1 , col2) = 0.5 wholes,
283         time_difference (col1 , col3) = 1 wholes,
284         time_difference (col2 , col3) = 0.5 wholes,
285         etc.
286  
287 @end example 
288
289 these differences are translated into ideal distances 
290
291 @example 
292
293         distance (col1,col2) = 10 pt
294         distance (col1,col3) = 14.1 pt
295         distance (col2,col3) = 10 pt
296         etc.
297  
298 @end example 
299
300 as you can see, these distance are conflicting. So instead of
301 satisfying all those ideals simultaneously, a compromise is sought.
302
303 This is Columbus' egg: GNU LilyPond attaches "springs" to each
304 column-pair.  each spring has an equilibrium-position which is equal to
305 the above mentioned distance, so
306
307 spring (col1, col2) and spring (col2,col3) try to push column 1
308 and 3 away (to a distance of 20pt) from each other, whereas the spring
309 between col 1 and col 3 tries to pull those two together (to a
310 distance of 14.1 pt). The net result of this pushing and pulling is an
311 equilibrium situation (the pushing cancels the pulling), which can be
312 calculated as the solution of Quadratic program: it is the solution
313 with minimum potential energy, for you physicists out there.
314
315 This algorithm for doing one line, gives a "badness" parameter for
316 each line (the potential energy). Now one can use TeX's algorithm for
317 making paragraphs (using this new version of "badness"): one should
318 try to minimise the overall badness of a paragraph. GNU LilyPond also
319 uses the concept of pre- and post-breaks.
320
321 (actually, it is a bit more complicated: each column also has a
322 minimum distance to other columns, to prevent symbols from running
323 into symbols of other columns.)
324
325
326 @bye