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