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