]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
* input/regression/note-head-harmonic.ly: new file.
[lilypond.git] / Documentation / user / internals.itely
1 @c -*-texinfo-*-
2 @c Note:
3 @c
4 @c A menu is needed before every deeper *section nesting of @nodes
5 @c Run M-x texinfo-all-menus-update
6 @c to automagically fill in these menus
7 @c before saving changes
8
9
10 @node Technical manual
11 @chapter Technical manual
12
13
14 When LilyPond is run, it reads music from a file, translates that into
15 notation, and outputs the result to a file. The most important steps
16 are the first three. Consequently, there are three important basic
17 concepts within LilyPond: music, translation and layout.  The
18 following diagram illustrates the concepts, and list the terminology
19 associated with each step.
20
21  
22 @verbatim
23
24                   +-------------+         Translation      +----------+
25                   |             |                          |          |
26                   |    Music    |     ------------------>  | Layout   |
27                   |             |                          |          |
28                   +-------------+                          +----------+
29  
30  
31 Syntax:            c4                     \context           \set #'padding = 
32                                           \property                    
33  
34 Objects:           Music expressions     Contexts            Layout object
35                                          Engravers           (aka. Grob)
36                                          
37 Example objects:   NoteEvent             Voice               NoteHead
38                                          Note_heads_engraver
39                                          
40 Example properties: #'pitch              keySignature        #'line-count
41
42 User applications: none                  various             tuning layout
43
44 @end verbatim
45
46 The objects passed around in LilyPond have @emph{properties},
47 variables that can contain many different types of information. Users
48 can set these variables, to modify the default behavior.  Since there
49 are three different main concepts, there are also three types of
50 properties:
51
52 @cindex properties
53 @cindex concepts, main
54 @cindex context
55 @cindex music expressions
56 @cindex layout
57 @cindex grob 
58
59
60 @table @b
61 @item Music properties
62 These are used internally, and most users will not see or use them.
63
64 They use Scheme style naming: @code{pitch}, @code{tremolo-type}.
65
66 @item Translation properties
67 These influence the translation process, and most users will encounter them
68 regularly. For example, beaming behavior is tuned with
69 @code{autoBeamSettings}.
70
71 These use mixed-caps naming: @code{autoBeamSettings},
72 @code{ignoreMelismata}.
73
74
75 @item Layout properties
76 These are internally used in the formatting process. Consequently, to
77 tune formatting details, it is necessary to adjust these
78 properties. For example, some objects may be moved around vertically
79 by setting their @code{padding} property.
80
81 These properties use Scheme-style naming: @code{c0-position},
82 @code{break-align-symbol}.
83
84 @end table
85
86 This chapter discusses details of the three concepts in more detail,
87 and explains how they are glued together in LilyPond with the embedded
88 Scheme interpreter.
89
90 @menu
91 * Interpretation context::
92 * Scheme integration::
93 * Music storage format::
94 * Lexical details::
95 * Output details::
96 @end menu
97
98
99 @node Interpretation context
100 @section Interpretation context
101
102 @menu
103 * Creating contexts::
104 * Default contexts::
105 * Context properties::
106 * Context evaluation::
107 * Defining contexts::
108 * Changing contexts locally::
109 * Engravers and performers::
110 * Defining new contexts::
111 @end menu
112
113
114 Interpretation contexts are objects that only exist during program
115 run.  During the interpretation phase (when @code{interpreting music}
116 is printed on the standard output), the music expression in a
117 @code{\score} block is interpreted in time order, the same order in
118 which we hear and play the music.  During this phase, the interpretation
119 context holds the state for the current point within the music, for
120 example:
121 @itemize @bullet
122 @item What notes are playing at this point?
123
124 @item What symbols will be printed at this point?
125
126 @item What is the current key signature, time signature, point within
127 the measure, etc.?
128 @end itemize
129
130 Contexts are grouped hierarchically: A @internalsref{Voice} context is
131 contained in a @internalsref{Staff} context (because a staff can contain
132 multiple voices at any point), a @internalsref{Staff} context is contained in
133 @internalsref{Score}, @internalsref{StaffGroup}, or
134 @internalsref{ChoirStaff} context.
135
136 Contexts associated with sheet music output are called @emph{notation
137 contexts}, those for sound output are called @emph{performance
138 contexts}.  The default definitions of the standard notation and
139 performance contexts can be found in @file{ly/engraver-init.ly} and
140 @file{ly/performer-init.ly}, respectively.
141
142
143 @node Creating contexts
144 @subsection Creating contexts
145 @cindex @code{\context}
146 @cindex context selection
147
148 Contexts for a music expression can be selected manually, using one of
149 the following music expressions:
150
151 @example
152 \new @var{contexttype} @var{musicexpr}
153 \context @var{contexttype} [= @var{contextname}] @var{musicexpr}
154 @end example
155
156 @noindent
157 This means that @var{musicexpr} should be interpreted within a context
158 of type @var{contexttype} (with name @var{contextname} if specified).
159 If no such context exists, it will be created:
160
161 @lilypond[verbatim,singleline]
162 \score {
163   \notes \relative c'' {
164     c4 <<d4 \context Staff = "another" e4>> f
165   }
166 }
167 @end lilypond
168
169 @noindent
170 In this example, the @code{c} and @code{d} are printed on the default
171 staff.  For the @code{e}, a context @code{Staff} called @code{another}
172 is specified; since that does not exist, a new context is created.
173 Within @code{another}, a (default) Voice context is created for the
174 @code{e4}.  A context is ended when when all music referring it has
175 finished, so after the third quarter, @code{another} is removed.
176
177 The @code{\new} construction creates a context with a
178 generated, unique @var{contextname}. An expression with
179 @code{\new} always leads to a new context. This is convenient
180 for creating multiple staffs, multiple lyric lines, etc.
181
182 When using automatic staff changes, automatic phrasing, etc., the
183 context names have special meanings, so @code{\new} cannot be
184 used.
185
186
187 @node Default contexts
188 @subsection Default contexts
189
190 Every top level music is interpreted by the @code{Score} context; in
191 other words, you may think of @code{\score} working like
192
193 @example
194 \score @{
195   \context Score @var{music}
196 @}
197 @end example
198
199 Music expressions  inherit their context from the enclosing music
200 expression. Hence, it is not necessary to explicitly specify
201 @code{\context} for most expressions.  In
202 the following example, only the sequential expression has an explicit
203 context. The notes contained therein inherit the @code{goUp} context
204 from the enclosing music expression.
205
206 @lilypond[verbatim,singleline]
207   \notes \context Voice = goUp { c'4 d' e' }
208 @end lilypond
209
210
211 Second, contexts are created automatically to be able to interpret the
212 music expressions.  Consider the following example:
213
214 @lilypond[verbatim, singleline]
215   \score { \notes { c'4-( d' e'-) } }
216 @end lilypond
217
218 @noindent
219 The sequential music is interpreted by the Score context initially,
220 but when a note is encountered, contexts are setup to accept that
221 note.  In this case, a @code{Thread}, @code{Voice}, and @code{Staff}
222 context are created.  The rest of the sequential music is also
223 interpreted with the same @code{Thread}, @code{Voice}, and
224 @code{Staff} context, putting the notes on the same staff, in the same
225 voice.
226
227 @node Context properties
228 @subsection Context properties
229
230 Contexts have properties.  These properties are set from the @file{.ly}
231 file using the following expression:
232 @cindex @code{\property}
233 @cindex context properties
234 @cindex properties, context
235
236 @example
237 \property @var{contextname}.@var{propname} = @var{value}
238 @end example
239
240 @noindent
241 Sets the @var{propname} property of the context @var{contextname} to
242 the specified Scheme expression @var{value}.  Both @var{propname} and
243 @var{contextname} are strings, which can often be written unquoted.
244
245 @cindex inheriting
246 Properties that are set in one context are inherited by all of the
247 contained contexts.  This means that a property valid for the
248 @internalsref{Voice} context can be set in the @internalsref{Score} context
249 (for example) and thus take effect in all @internalsref{Voice} contexts.
250
251 @cindex @code{Current}
252 If you do not wish to specify the name of the context in the
253 @code{\property}-expression itself, you can refer to the abstract context
254 name, @code{Current}.  The @code{Current} context is the latest
255 used context.  This will typically mean the @internalsref{Thread} context,
256 but you can force another context with the
257 @code{\property}-command.  Hence the expressions
258
259 @example
260 \property @var{contextname}.@var{propname} = @var{value}
261 @end example
262
263 @noindent
264 and
265
266 @example
267 \context @var{contextname}
268 \property Current.@var{propname} = @var{value}
269 @end example
270
271 @noindent
272 do the same thing.  The main use for this is in predefined variables.
273 This construction allows the specification of a property-setting
274 without restriction to a specific context.
275
276 Properties can be unset using the following statement.
277 @example
278 \property @var{contextname}.@var{propname} \unset
279 @end example
280
281 @cindex properties, unsetting
282 @cindex @code{\unset}
283
284 @noindent
285 This removes the definition of @var{propname} in @var{contextname}.  If
286 @var{propname} was not defined in @var{contextname} (but was inherited
287 from a higher context), then this has no effect.
288
289 @refbugs
290
291 The syntax of @code{\unset} is asymmetric: @code{\property \unset} is not
292 the inverse of @code{\property \set}.
293
294
295 @node Context evaluation
296 @subsection Context evaluation
297
298 Contexts can be modified during interpretation with Scheme code. The
299 syntax for this is
300 @example
301   \applycontext @var{function}
302 @end example
303
304 @var{function} should be a Scheme function taking a single argument,
305 being the context to apply it to. The following code will print the
306 current bar number on the standard output during the compile:
307
308 @example
309     \applycontext
310       #(lambda (x)
311          (format #t "\nWe were called in barnumber ~a.\n"
312           (ly:get-context-property x 'currentBarNumber)))
313 @end example
314
315
316
317 @node Defining contexts
318 @subsection Defining contexts
319
320 @cindex context definition
321 @cindex translator definition
322
323 The most common way to create a new context definition is by extending
324 an existing one.  An existing context from the paper block is copied
325 by referencing a context identifier:
326
327 @example
328 \paper @{
329   \translator @{
330     @var{context-identifier}
331   @}
332 @}
333 @end example
334
335 @noindent
336 Every predefined context has a standard identifier. For example, the
337 @code{Staff} context can be referred to as @code{\StaffContext}.
338
339 The context can then be modified by setting or changing properties,
340 e.g.
341 @example
342 \translator @{
343   \StaffContext
344   Stem \set #'thickness = #2.0
345   defaultBarType = #"||"
346 @}
347 @end example
348 These assignments happen before interpretation starts, so a @code{\property}
349 command will override any predefined settings.
350
351 @cindex engraver
352
353 @refbugs
354
355 It is not possible to collect multiple property assignments in a
356 variable, and apply to one @code{\translator} definition by
357 referencing that variable.
358
359 @node Changing contexts locally
360 @subsection Changing contexts locally
361
362
363 Extending an existing context can also be done locally. A piece of
364 music can be interpreted in a changed context by using the following syntax
365
366 @example
367   \with @{
368      @var{context modifications}
369   @}
370 @end example
371
372 These statements comes between @code{\new} or @code{\context} and the
373 music to be interpreted. The @var{context modifications} property
374 settings and @code{\remove}, @code{\consists} and @code{\consistsend}
375 commands. The syntax is similar to the @code{\translator} block.
376
377 The following example shows how a staff is created with bigger spaces,
378 and without a @code{Clef_engraver}.
379
380 @lilypond[relative=1,fragment,verbatim]
381 <<
382   \new Staff { c4 es4 g2 }
383   \new Staff \with {
384         StaffSymbol \set #'staff-space = #(magstep 1.5)
385         fontSize = #1.5
386         \remove "Clef_engraver"
387   } {
388         c4 es4 g2
389   } >>
390 @end lilypond
391
392 @refbugs
393
394 The command @code{\with} has no effect on contexts that already
395 exist. Neither can it be used for @internalsref{Score} contexts.
396
397
398 @node Engravers and performers
399 @subsection  Engravers and performers
400
401
402 Each context is composed of a number of building blocks, or plug-ins
403 called engravers.  An engraver is a specialized C++ class that is
404 compiled into the executable. Typically, an engraver is responsible
405 for one function: the @code{Slur_engraver} creates only @code{Slur}
406 objects, and the @code{Skip_event_swallow_translator} only swallows
407 (silently gobbles) @code{SkipEvent}s.
408
409
410
411 @cindex engraver
412 @cindex plug-in
413
414 An existing context definition can be changed by adding or removing an
415 engraver. The syntax for these operations is
416 @example
417 \consists @var{engravername}
418 \remove @var{engravername}
419 @end example
420
421 @cindex \consists
422 @cindex \remove
423
424 @noindent
425 Here @var{engravername} is a string, the name of an engraver in the
426 system. In the following example, the @code{Clef_engraver} is removed
427 from the Staff context. The result is a staff without a clef, where
428 the central C is at its default position, the center line:
429
430 @lilypond[verbatim,singleline]
431 \score {
432   \notes {
433     c'4 f'4
434   }
435   \paper {
436     \translator {
437       \StaffContext
438       \remove Clef_engraver
439     }
440   }
441 }
442 @end lilypond
443
444 A list of all engravers is in the internal documentation,
445 see @internalsref{All engravers}.
446
447 @node Defining new contexts
448 @subsection Defining new contexts
449
450
451 It is also possible to define new contexts from scratch.  To do this,
452 you must define give the new context a name.  In the following
453 example, a very simple Staff context is created: one that will put
454 note heads on a staff symbol.
455
456 @example
457 \translator @{
458   \type "Engraver_group_engraver"
459   \name "SimpleStaff"
460   \alias "Staff"
461   \consists "Staff_symbol_engraver"
462   \consists "Note_head_engraver"
463   \consistsend "Axis_group_engraver"
464 @}
465 @end example
466
467 @noindent
468 The argument of @code{\type} is the name for a special engraver that
469 handles cooperation between simple engravers such as
470 @code{Note_head_engraver} and @code{Staff_symbol_engraver}.  This
471 should always be  @code{Engraver_group_engraver} (unless you are
472 defining a Score context from scratch, in which case
473 @code{Score_engraver}   must be used).
474
475 The complete list of context  modifiers is the following:
476 @itemize @bullet
477 @item @code{\alias} @var{alternate-name}:
478 This specifies a different name.  In the above example,
479 @code{\property Staff.X = Y} will also work on @code{SimpleStaff}s.
480
481 @item @code{\consistsend} @var{engravername}:
482 Analogous to @code{\consists}, but makes sure that
483 @var{engravername} is always added to the end of the list of
484 engravers.
485
486 Engravers that group context objects into axis groups or alignments
487 need to be at the end of the list. @code{\consistsend} insures that
488 engravers stay at the end even if a user adds or removes engravers.
489
490 @item @code{\accepts} @var{contextname}:
491 This context can contains @var{contextname} contexts.  The first
492 @code{\accepts} is created as a default context when events (e.g. notes
493 or rests) are encountered.
494
495 @item @code{\denies}:
496 The opposite of @code{\accepts}.
497
498 @item @code{\name} @var{contextname}:
499 This sets the type name of the context, e.g. @code{Staff},
500 @code{Voice}.  If the name is not specified, the translator will not
501 do anything.
502 @end itemize
503
504
505 @node Scheme integration
506 @section Scheme integration
507
508 @cindex Scheme
509 @cindex GUILE
510 @cindex Scheme, in-line code
511 @cindex accessing Scheme
512 @cindex evaluating Scheme
513 @cindex LISP
514
515 LilyPond internally uses GUILE, a Scheme-interpreter, to represent
516 data throughout the whole program, and glue together different program
517 modules. For advanced usage, it is sometimes necessary to access and
518 program the Scheme interpreter.
519
520 Scheme is a full-blown programming language, from the LISP
521 family. and a full discussion is outside the scope of this document.
522 Interested readers are referred to the website
523 @uref{http://www.schemers.org/} for more information on Scheme.
524
525 The GUILE library for extension is documented at
526 @uref{http://www.gnu.org/software/guile}.
527 @ifinfo
528 When it is installed, the following link should take you to its manual
529 @ref{(guile.info)guile}
530 @end ifinfo
531
532 @menu
533 * Inline Scheme::
534 * Input variables and Scheme::
535 * Scheme datatypes::
536 * Assignments::
537 @end menu
538
539 @node Inline Scheme
540 @subsection Inline Scheme
541
542 Scheme expressions can be entered in the input file by entering a
543 hash-sign (@code{#}).  The expression following the hash-sign is
544 evaluated as Scheme. For example, the boolean value @var{true} is
545 @code{#t} in Scheme, so for LilyPond @var{true} looks like @code{##t},
546 and can be used in property assignments:
547 @example
548   \property Staff.autoBeaming = ##f
549 @end example
550
551
552 @node Input variables and Scheme
553 @subsection Input variables and Scheme
554
555
556 The input format supports the notion of variable: in the following
557 example, a music expression is assigned to a variable with the name
558 @code{traLaLa}.
559 @example
560   traLaLa = \notes @{ c'4 d'4 @}
561 @end example
562
563 @noindent
564
565 There is also a form of scoping: in the following example, the
566 @code{\paper} block also contains a @code{traLaLa} variable, which is
567 independent of the outer @code{\traLaLa}.
568 @example
569   traLaLa = \notes @{ c'4 d'4 @}
570   \paper @{ traLaLa = 1.0 @}
571 @end example
572 @c
573 In effect, each input file is a scope, and all @code{\header},
574 @code{\midi} and @code{\paper} blocks are scopes nested inside that
575 toplevel scope.
576
577 Both variables and scoping are implemented in the GUILE module system.
578 An anonymous Scheme module is attached to each scope. An assignment of
579 the form
580 @example
581  traLaLa = \notes @{ c'4 d'4 @}
582 @end example
583
584 @noindent
585 is internally converted to a Scheme definition
586 @example
587  (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
588 @end example
589
590 This means that input variables and Scheme variables may be freely
591 mixed.  In the following example, a music fragment is stored in the
592 variable @code{traLaLa}, and duplicated using Scheme. The result is
593 imported in a @code{\score} by means of a second variable
594 @code{twice}:
595 @example
596   traLaLa = \notes @{ c'4 d'4 @}
597
598   #(define newLa (map ly:music-deep-copy
599     (list traLaLa traLaLa)))
600   #(define twice
601     (make-sequential-music newLa))
602
603   \score @{ \twice @}
604 @end example
605
606 In the above example, music expressions can be `exported' from the
607 input to the Scheme interpreter. The opposite is also possible. By
608 wrapping a Scheme value in the function @code{ly:export}, a Scheme
609 value is interpreted as if it were entered in LilyPond syntax: instead
610 of defining @code{\twice}, the example above could also have been
611 written as
612 @example
613   @dots{}
614   \score @{ #(ly:export (make-sequential-music newLa)) @}
615 @end example
616
617
618
619
620
621 @node Scheme datatypes
622 @subsection Scheme datatypes
623
624 Scheme is used to glue together different program modules. To aid this
625 glue function, many LilyPond specific object types can be passed as
626 Scheme value.
627
628 The following list are all LilyPond specific types, that
629 can exist during parsing:
630 @table @code
631 @item Duration
632 @item Input
633 @item Moment
634 @item Music
635 @item Event
636 In C++ terms, an @code{Event} is a subtype of @code{Music}. However,
637 both have different functions in the syntax.
638 @item Music_output_def
639 @item Pitch
640 @item Score
641 @item Translator_def
642 @end table
643
644
645 During a run, transient objects are also created and destroyed.
646
647 @table @code
648 @item Grob: short for `Graphical object'.
649 @item Scheme_hash_table
650 @item Music_iterator
651
652 @item Molecule: Device-independent page output object,
653 including dimensions.
654
655 @item Syllable_group
656
657 @item Spring_smob
658
659 @item Translator: An object that produces audio objects or Grobs.
660 It may be accessed with @code{\applyoutput}.
661
662 @item Font_metric: An object representing a font.
663 @end table
664
665 Many functions are defined to manipulate these data structures. They
666 are all listed and documented in the internals manual, see
667 @internalsref{All scheme functions}.
668
669
670 @node Assignments
671 @subsection Assignments
672 @cindex Assignments
673
674 Variables allow objects to be assigned to names during the parse
675 stage.  To assign a variable, use
676 @example
677 @var{name}@code{=}@var{value}
678 @end example
679 To refer to a variable, precede its name with a backslash:
680 `@code{\}@var{name}'.  @var{value} is any valid Scheme value or any of
681 the input-types listed above.  Variable assignments can appear at top
682 level in the LilyPond file, but also in @code{\paper} blocks.
683
684 A variable can be created with any string for its name, but for
685 accessing it in the LilyPond syntax, its name must consist of
686 alphabetic characters only, and may not be a keyword of the syntax.
687 There are no restrictions for naming and accessing variables in the
688 Scheme interpreter,
689
690 The right hand side of a variable assignment is parsed completely
691 before the assignment is done, so variables may be  redefined in terms
692 of its old value, e.g.
693 @c
694 @example
695 foo = \foo * 2.0
696 @end example
697
698 When a variable is referenced in LilyPond syntax, the information it
699 points to is copied.  For this reason, an variable reference must
700 always be the first item in a block.
701
702 @example
703 \paper @{
704   foo = 1.0
705   \paperIdent % wrong and invalid
706 @}
707 @end example
708
709 @example
710 \paper @{
711   \paperIdent % correct
712   foo = 1.0
713 @}
714 @end example
715
716
717
718 @node Music storage format
719 @section Music storage format
720
721 Music in LilyPond is entered as music expressions. This section
722 discusses different types of music expressions, and explains how
723 information is stored internally. This internal storage is accessible
724 through the Scheme interpreter, so music expressions may be
725 manipulated using Scheme functions.
726
727 @menu
728 * Music expressions::
729 * Internal music representation::
730 * Manipulating music expressions::
731 @end menu
732
733 @node Music expressions
734 @subsection Music expressions
735 @cindex music expressions
736
737 Notes, rests, lyric syllables are music expressions.  Small music
738 expressions may be combined to form larger ones, for example, by
739 enclosing a list of expressions in @code{\sequential @{ @}} or @code{<<
740 >>}.  In the following example, a compound expression is formed out of
741 the quarter note @code{c} and a quarter note @code{d}:
742
743 @example
744 \sequential @{ c4 d4 @}
745 @end example
746
747 @cindex Sequential music
748 @cindex @code{\sequential}
749 @cindex sequential music
750 @cindex @code{<<}
751 @cindex @code{>>}
752 @cindex Simultaneous music
753 @cindex @code{\simultaneous}
754
755 The two basic compound music expressions are simultaneous and
756 sequential music:
757
758 @example
759 \sequential @code{@{} @var{musicexprlist} @code{@}}
760 \simultaneous @code{@{} @var{musicexprlist} @code{@}}
761 @end example
762
763 For both, there is a shorthand:
764
765 @example
766 @code{@{} @var{musicexprlist} @code{@}}
767 @end example
768
769 @noindent
770 for sequential and
771
772 @example
773 @code{<<} @var{musicexprlist} @code{>>}
774 @end example
775
776 @noindent
777 for simultaneous music.
778 In principle, the way in which you nest sequential and simultaneous to
779 produce music is not relevant.  In the following example, three chords
780 are expressed in two different ways:
781
782 @lilypond[fragment,verbatim,center,quote]
783 \notes \context Voice {
784   <<a c'>> <<b d'>> <<c' e'>>
785   << { a b c' } { c' d' e' } >>
786 }
787 @end lilypond
788 However, using @code{<<} and @code{>>} for entering chords leads to
789 various peculiarities. For this reason, a special syntax
790 for chords was introduced in version 1.7: @code{< >}.
791
792
793
794
795
796 Other compound music expressions include:
797 @example
798 \repeat @var{expr}
799 \transpose @var{from} @var{to} @var{expr}
800 \apply @var{func} @var{expr}
801 \context @var{type} = @var{id} @var{expr}
802 \times @var{fraction} @var{expr}
803 @end example
804
805 @node Internal music representation
806 @subsection Internal music representation
807
808
809
810
811
812
813 When a music expression is parsed, it is converted into a set of
814 Scheme music objects. The defining property of a music object is that
815 it takes up time. Time is a rational number that measures the length
816 of a piece of music, in whole notes.
817
818 A music object has three kinds of types:
819 @itemize @bullet
820 @item
821   music name: Each music expression has a name, for example, a note
822 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
823 a @internalsref{SimultaneousMusic}. A list of all expressions
824 available is in the internals manual, under @internalsref{Music
825 expressions}.
826
827 @item
828   `type' or interface: Each music name has several `types' or interface,
829   for example, a note is an @code{event}, but it is also a @code{note-event},
830   a @code{rhythmic-event} and a @code{melodic-event}.
831
832   All classes of music are listed in the internals manual, under
833   @internalsref{Music classes}.
834 @item
835 C++ object: Each music object is represented by a C++ object. For technical
836 reasons, different music objects may be represented by different C++
837 object types. For example, a note is @code{Event} object, while
838 @code{\grace} creates a @code{Grace_music} object.
839
840 We expect that distinctions between different C++ types will disappear
841 in the future.
842 @end itemize
843
844 The actual information of a music expression is stored in properties.
845 For example, a @internalsref{NoteEvent} has @code{pitch} and
846 @code{duration} properties that store the pitch and duration of that
847 note.  A list of all properties available is in the internals manual,
848 under @internalsref{Music properties}.
849
850 A compound music expression is a music object that contains other
851 music objects in its properties. A list of objects can be stored in
852 the @code{elements} property of a music object, or a single `child'
853 music object in the @code{element} object. For example,
854 @internalsref{SequentialMusic} has its children in @code{elements},
855 and @internalsref{GraceMusic} has its single argument in
856 @code{element}. The body of a repeat is in @code{element} property of
857 @internalsref{RepeatedMusic}, and the alternatives in @code{elements}.
858
859 @node Manipulating music expressions
860 @subsection Manipulating music expressions
861
862 Music objects and their properties can be accessed and manipulated
863 directly, through the @code{\apply} mechanism.
864 The syntax for @code{\apply} is
865 @example
866 \apply #@var{func} @var{music}
867 @end example
868
869 @noindent
870 This means that the scheme function @var{func} is called with
871 @var{music} as its argument.  The return value of @var{func} is the
872 result of the entire expression.  @var{func} may read and write music
873 properties using the functions @code{ly:get-mus-property} and
874 @code{ly:set-mus-property!}.
875
876 An example is a function that reverses the order of elements in
877 its argument:
878 @lilypond[verbatim,singleline]
879   #(define (rev-music-1 m)
880      (ly:set-mus-property! m 'elements (reverse
881        (ly:get-mus-property m 'elements)))
882      m)
883   \score { \notes \apply #rev-music-1 { c4 d4 } }
884 @end lilypond
885
886 The use of such a function is very limited. The effect of this
887 function is void when applied to an argument which is does not have
888 multiple children.  The following function application has no effect:
889
890 @example
891   \apply #rev-music-1 \grace @{ c4 d4 @}
892 @end example
893
894 @noindent
895 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
896 @code{elements}, only a single @code{element}. Every generally
897 applicable function for @code{\apply} must -- like music expressions
898 themselves -- be recursive.
899
900 The following example is such a recursive function: It first extracts
901 the @code{elements} of an expression, reverses them and puts them
902 back. Then it recurses, both on @code{elements} and @code{element}
903 children.
904 @example
905 #(define (reverse-music music)
906   (let* ((elements (ly:get-mus-property music 'elements))
907          (child (ly:get-mus-property music 'element))
908          (reversed (reverse elements)))
909
910     ; set children
911     (ly:set-mus-property! music 'elements reversed)
912
913     ; recurse
914     (if (ly:music? child) (reverse-music child))
915     (map reverse-music reversed)
916
917     music))
918 @end example
919
920 A slightly more elaborate example is in
921 @inputfileref{input/test,reverse-music.ly}.
922
923 Some of the input syntax is also implemented as recursive music
924 functions. For example, the syntax for polyphony
925 @example
926   <<a \\ b>>
927 @end example
928
929 @noindent
930 is actually  implemented as a recursive function that replaces the
931 above by the internal equivalent of
932 @example
933   << \context Voice = "1" @{ \voiceOne a @}
934     \context Voice = "2" @{ \voiceTwo b @} >>
935 @end example
936
937 Other applications of @code{\apply} are writing out repeats
938 automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
939 saving keystrokes (@inputfileref{input/test,music-box.ly}) and
940 exporting
941 LilyPond input to other formats  (@inputfileref{input/test,to-xml.ly})
942
943 @seealso
944
945 @file{scm/music-functions.scm}, @file{scm/music-types.scm},
946 @inputfileref{input/test,add-staccato.ly},
947 @inputfileref{input/test,unfold-all-repeats.ly}, and
948 @inputfileref{input/test,music-box.ly}.
949
950 @node Lexical details
951 @section Lexical details
952
953
954 @cindex string
955 @cindex concatenate
956
957 By enclosing text in quotes (@code{"}), strings are formed.  To
958 include a @code{"} character in a string write @code{\"}.  Various
959 other backslash sequences have special interpretations as in the C
960 language.  A string that does not contain spaces or special characters
961 can be written without the quotes. The exact form of such unquoted
962 strings depends on the input mode; there are different rules for
963 lyrics, notes and markups.  Strings can be concatenated with the
964 @code{+} operator.
965
966
967 @node Output details
968 @section Output details
969
970 LilyPond's default output format is @TeX{}.  Using the option @option{-f}
971 (or @option{--format}) other output formats can be selected also, but
972 currently none of them work reliably.
973
974 At the beginning of the output file, various global parameters are defined.
975 It also contains a large @code{\special} call to define PostScript routines
976 to draw items not representable with @TeX{}, mainly slurs and ties.  A DVI
977 driver must be able to understand such embedded PostScript, or the output
978 will be rendered incompletely.
979
980 Then the file @file{lilyponddefs.tex} is loaded to define the macros used
981 in the code which follows.  @file{lilyponddefs.tex} includes various other
982 files, partially depending on the global parameters.
983
984 Now the music is output system by system (a `system' consists of all
985 staves belonging together).  From @TeX{}'s point of view, a system is an
986 @code{\hbox} which contains a lowered @code{\vbox} so that it is centered
987 vertically on the baseline of the text.  Between systems,
988 @code{\interscoreline} is inserted vertically to have stretchable space.
989 The horizontal dimension of the @code{\hbox} is given by the
990 @code{linewidth} parameter from LilyPond's @code{\paper} block.
991
992
993 After the last system LilyPond emits a stronger variant of
994 @code{\interscoreline} only if the macro
995 @code{\lilypondpaperlastpagefill} is not defined (flushing the systems
996 to the top of the page).  You can avoid that by setting the variable
997 @code{lastpagefill} in LilyPond's @code{\paper} block.
998
999 It is possible to fine-tune the vertical offset further by defining the
1000 macro @code{\lilypondscoreshift}:
1001
1002 @example
1003 \def\lilypondscoreshift@{0.25\baselineskip@}
1004 @end example
1005
1006 @noindent
1007 where @code{\baselineskip} is the distance from one text line to the next.
1008
1009 The code produced by LilyPond should be run through La@TeX{}, not
1010 plain @TeX{}.
1011
1012 Here an example how to embed a small LilyPond file @code{foo.ly} into
1013 running La@TeX{} text without using the @code{lilypond-book} script
1014 (@pxref{lilypond-book manual}):
1015
1016 @example
1017 \documentclass@{article@}
1018
1019 \def\lilypondpaperlastpagefill@{@}
1020 \lineskip 5pt
1021 \def\lilypondscoreshift@{0.25\baselineskip@}
1022
1023 \begin@{document@}
1024 This is running text which includes an example music file
1025 \input@{foo.tex@}
1026 right here.
1027 \end@{document@}
1028 @end example
1029
1030 The file @file{foo.tex} has been simply produced with
1031
1032 @example
1033 lilypond foo.ly
1034 @end example
1035
1036 It is important to set the @code{indent} parameter to zero in the
1037 @code{\paper} block of @file{foo.ly}.
1038
1039 The call to @code{\lineskip} assures that there is enough vertical space
1040 between the LilyPond box and the surrounding text lines.
1041
1042 @c EOF