3 LilyGuts - doco to the internals of LilyPond
7 This page documents some aspects of the internals of 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
11 You should use doc++ to take a peek at the sources.
13 [have to do more doco; see TODO]
17 LilyPond is a "5-pass" system:
23 No difficult algorithms. Associated datastructures have prefix Input
24 (eg Input_score, Input_command)
28 Requests are processed and granted. This is done by three cooperating
29 structeres: Staff, Staff_walker and Staff_column. In this step
30 data-structures for 3. are created and filled with data: PScore, PCol,
35 This step uses structures which have names starting with 'P'.
36 linebreaks and horizontal positions of PCols are determined. Line_of_*
39 =item 4. Postprocesing:
41 Some items and all spanners need computation after the PCol positions
46 Very simple, just walk all Line_of_* and follow the links over there
50 =head1 Request_register
52 In the previous section the idea of Request has been explained, but
53 this only solves one half of the problem. The other half is
54 deciding which requests should be honored, which should merged with
55 other requests, and which should be ignored. Consider this (pseudo)input
62 Both the c and e are part of a chord (they are in the same
63 Voice_group), so they should share the beams, and the two [ ] pairs
64 should be merged. The slurs OTOH are specific for each voice, so they
67 The judge in this "allocation" problem is Staff (actually, it's child
68 C<Complex_staff>). It uses the C<Request_register> to do most of the
69 work. For each request C<Complex_staff> queries so-called
70 C<Request_register>s if they want to accept a request eg, the
71 C<Notehead_register> will accept C<Note_req>s, and turn down
72 C<Slur_req>s. If C<Complex_staff> cannot find a register that wants
73 the request, it is junked (with a warning message).
75 After all requests have been either assigned, or junked, the Register
76 will process the requests (which usually means creating an C<Item> or
77 C<Spanner>). If a C<Request_register> creates something, it tells
78 C<Complex_staff>. If all requests have been processed, then each
79 Register is notified of any created Staff_element.
87 note_request (duration 1/4)
88 stem_request (duration 1/4)
90 note_request will be taken by a C<Notehead_register>, stem_request
91 will be taken by a C<Stem_beam_register>. C<Notehead_register> creates
92 a C<Notehead>, C<Stem_beam_register> creates a C<Stem>. Both announce
93 this to the Staff. Staff will tell C<Stem_beam_register> about the
94 C<Notehead>, which will add the C<Notehead> to the C<Stem> it just
97 To decide on merging, C<Complex_staff> has grouped several
98 registers. There are a few groups:
112 Voice group, contains
131 [Source files: F<command.hh>, F<scommands.cc>]
133 BREAKING, PREBREAK POSTBREAK, etc.
135 So what's the deal with PREBREAK and POSTBREAK and all this
138 Let's take text as an example. In German some compound
139 words change their spelling if they are broken: "backen" becomes
140 "bak-ken". TeX has a mechanism to deal with this, you would define
141 the spelling of "backen" in TeX in this way
143 \discretionary{bak-}{ken}{backen}
145 These 3 arguments are called "prebreak", "postbreak" and "nobreak"
148 The same problem exists when typesetting music. If a line of music is
149 broken, the next line usually gets a clef. So in TeX terms, the clef
150 is a postbreak. The same thing happens with meter signs: Normally the
151 meter follows the bar. If a line is broken at that bar, the bar along
152 with the meter stays on the "last" line, but the next line also gets a
153 meter sign after the clef. Using the previous notation,
155 \discretionary{bar meter}{clef meter}{ bar meter }
157 In Lilypond, we have the same concepts (and the same
158 terminology). Each (nonrhythmic) symbol is typeset using a Command
159 (code: TYPESET). At a breakpoint, TYPESET commands can be grouped
160 using separators (in lower case):
162 BREAK_PRE, typeset(bar), typeset(meter),
163 BREAK_MID, typeset(bar), typeset(meter),
164 BREAK_POST, typeset(clef), typeset(meter), BREAK_END
166 The BREAK command sequence is terminated with BREAK_END, so other
167 commands (like INTERPRET) may follow this sequence.
171 I think my method is the most elegant algorithm i've seen so far.
172 Some terminology: I call a vertical group of symbols (notes) which
173 start at the same time a "column". Each line of a score has notes in
174 it, grouped in columns. The difference in starting time between those
175 columns makes it possible to determine ideal distances between those
190 (1 is a whole note, 2 a half note.)
192 time_difference (col1 , col2) = 0.5 wholes,
193 time_difference (col1 , col3) = 1 wholes,
194 time_difference (col2 , col3) = 0.5 wholes,
197 these differences are translated into ideal distances (these translations
198 have been the subject of discussion in this thread).
200 distance (col1,col2) = 10 pt
201 distance (col1,col3) = 14.1 pt
202 distance (col2,col3) = 10 pt
205 as you can see, these distance are conflicting. So instead of
206 satisfying all those ideals simultaneously, a compromise is sought.
208 This is Columbus' egg: LilyPond attaches "springs" to each
209 column-pair. each spring has an equilibrium-position which is equal to
210 the above mentioned distance, so
212 spring (col1, col2) and spring (col2,col3) try to push column 1
213 and 3 away (to a distance of 20pt) from each other, whereas the spring
214 between col 1 and col 3 tries to pull those two together (to a
215 distance of 14.1 pt). The net result of this pushing and pulling is an
216 equilibrium situation (the pushing cancels the pulling), which can be
217 calculated as the solution of Quadratic program: it is the solution
218 with minimum potential energy, for you physicists out there.
220 This algorithm for doing one line, gives a "badness" parameter for
221 each line (the potential energy). Now one can use TeX's algorithm for
222 making paragraphs (using this new version of "badness"): one should
223 try to minimise the overall badness of a paragraph. LilyPond also uses the
224 concept of pre- and post-breaks.
226 (actually, it is a bit more complicated: each column also has a
227 minimum distance to other columns, to prevent symbols from running
228 into symbols of other columns.)
234 [partly by Mark Basinski <basinski@arizona.edu>]
238 W.A. Hegazy and J. S. Gourlay. Optimal line breaking in music. In
239 ``Document Manipulation and Typography'', J.C. van Vliet (ed) 1988.
241 Ross, Ted. ``Teach yourself the art of music engraving and processing''
242 (3rd edition). Hansen House, Miami Beach, FL.
249 [This is about I<engraving> i.e. professional music typesetting, and includes
250 some good spacing tables]
252 Read, Gardner. ``Modern Rhythmic Notation.'' Indiana University Press, 1978.
254 Read, Gardner. ``Music Notation'' (2nd edition). Taplinger Publishing,
257 [This is as close to the ``standard'' reference work for music notation issues
258 as one is likely to get.]
261 =head2 Further reading
263 (of varying usefulness):
265 Donato, Anthony. Preparing Music Manuscript. Englewood Cliffs:
268 Donemus. "Uitgeven van muziek". Donemus Amsterdam, 1900
270 Heussenstamm, George. The Norton Manual of Music Notation. New York:
273 Karkoshka, Erdhard. Notation in New Music. Trans. Ruth Koenig. New York:
274 Praeger Publishers, 1972. Out of print.
276 Roelofs, Ren\'e. ``Een Geautomatiseerd Systeem voor het Afdrukken van
277 Muziek'' afstudeerscriptie Bestuurlijke informatica, no 45327, Erasmus
278 universiteit Rotterdam, 1991. (``An automated system for printing
279 music'' Master's Thesis Management and Computer Science.)
281 C. Roemer, The Art of Music Copying. Roerick music co., Sherman Oaks
284 Rosecrans, Glen. Music Notation Primer. New York: Passantino, 1979.
286 Stone, Kurt. Music Notation in the Twentieth Century. New York: Norton, 1980.