Add *ExtraSettings variable to be inserted at begin of voice
[orchestrallily.git] / orchestrallily.ily
blob874932dcdbc3019f3477c68bac0fcce989123179
1 \version "2.11.41"
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 % OrchestralLily
5 % ==============
6 % Desciption:  Lilypond package to make writing large orchestral scores easier.
7 % Documentation: http://wiki.kainhofer.com/lilypond/orchestrallily
8 % Version: 0.02, 2008-03-06
9 % Author: Reinhold Kainhofer, reinhold@kainhofer.com
10 % Copyright: (C) 2008 by Reinhold Kainhofer
11 % License: GPL v3.0, http://www.gnu.org/licenses/gpl.html
13 % Version History:
14 % 0.01 (2008-03-02): Initial Version
15 % 0.02 (2008-03-06): Added basic MIDI support (*MidiInstrument and \setCreateMIDI)
16 % 0.03 (2008-07-xx): General staff/voice types, title pages, etc.
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19 #(use-modules (ice-9 match))
22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23 % GLOBAL OPTIONS
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26 % Use relative include pathes!
27 #(ly:set-option 'relative-includes #t)
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 %%%%%   SCORE STRUCTURE AND AUTOMATIC GENERATION
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 % Helper functions
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 % Helper function to filter all non-null entries
44 #(define (not-null? x) (not (null? x)))
46 % Helper function to extract a given variable, built from [Piece][Instrument]Identifier
47 #(define (namedPieceInstrObject piece instr name)
48   (let* (
49          (fullname  (string->symbol (string-append piece instr name)))
50          (instrname (string->symbol (string-append instr name)))
51          (piecename (string->symbol (string-append piece name)))
52          (fallback  (string->symbol name))
53         )
54     (cond
55       ((defined? fullname) (primitive-eval fullname))
56       ((defined? instrname) (primitive-eval instrname))
57       ((defined? piecename) (primitive-eval piecename))
58       ((defined? fallback) (primitive-eval fallback))
59       (else '())
60     )
61   )
64 %% Print text as a justified paragraph, taken from the lilypond Notation Reference
65 #(define-markup-list-command (paragraph layout props args) (markup-list?)
66    (let ((indent (chain-assoc-get 'par-indent props 2)))
67      (interpret-markup-list layout props
68        (make-justified-lines-markup-list (cons (make-hspace-markup indent)
69                                                args)))))
71 conditionalBreak = #(define-music-function (parser location) ()
72    #{ \tag #'instrumental-score \pageBreak #}
75 #(define (oly:piece-title-markup title) (markup #:column (#:line (#:fontsize #'3 #:bold title))) )
77 #(define-markup-command (piece-title layout props title) (markup?)
78      (interpret-markup layout props (oly:piece-title-markup title))
81 #(define (oly:generate_object_name piece instr obj )
82   (if (and (string? piece) (string? instr) (string? obj))
83     (string-append piece instr obj)
84     #f
85   )
87 #(define (oly:generate_staff_name piece instr) (oly:generate_object_name piece instr "St"))
89 #(define (set-context-property context property value)
90   (set! (ly:music-property context property) value)
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % Score structure and voice types
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 #(define oly:LiedScoreStructure '(
99   ("SoloScore" "SimultaneousMusic" ("Singstimme"))
100   ("Pfe" "PianoStaff" ("PfeI" "PfeII"))
101   ;("PfeI" "ParallelVoicesStaff" ("OIa" "OIb"))
102   ;("PfeII" "ParallelVoicesStaff" ("OIIa" "OIIb"))
103   ("FullScore" "SimultaneousMusic" ("Singstimme" "Pfe"))
104   ("VocalScore" "SimultaneousMusic" ("Singstimme" "Pfe"))
107 #(define oly:fullOrchestraScoreStructure '(
108 ; Part-combined staves for full score
109   ("Fl" "PartCombinedStaff" ("FlI" "FlII"))
110   ("Ob" "PartCombinedStaff" ("ObI" "ObII"))
111   ("Cl" "PartCombinedStaff" ("ClI" "ClII"))
112   ("Fag" "PartCombinedStaff" ("FagI" "FagII"))
113   ("Wd" "StaffGroup" ("Fl" "Ob" "Cl" "Fag" "CFag"))
115   ("Cor" "PartCombinedStaff" ("CorI" "CorII"))
116   ("Trb" "PartCombinedStaff" ("TrbI" "TrbII"))
117   ("Tbe" "PartCombinedStaff" ("TbeI" "TbeII"))
118   ("Br" "StaffGroup" ("Cor" "Trb" "Tbe" "Tba"))
120 ; long score; no part-combined staves, but GrandStaves instead
121   ("FlLong" "GrandStaff" ("FlI" "FlII"))
122   ("ObLong" "GrandStaff" ("ObI" "ObII"))
123   ("ClLong" "GrandStaff" ("ClI" "ClII"))
124   ("FagLong" "GrandStaff" ("FagI" "FagII"))
125   ("WdLong" "StaffGroup" ("FlLong" "ObLong" "ClLong" "FagLong" "CFag"))
127   ("CorLong" "GrandStaff" ("CorI" "CorII"))
128   ("TrbLong" "GrandStaff" ("TrbI" "TrbII"))
129   ("TbeLong" "GrandStaff" ("TbeI" "TbeII" "TbeIII"))
130   ("BrLong" "StaffGroup" ("CorLong" "TrbLong" "TbeLong" "Tba"))
132 ; Percussion
133   ("Perc" "StaffGroup" ("Tim"))
135 ; Strings, they are the same in long and short full score
136   ("V" "GrandStaff" ("VI" "VII"))
137   ("Str" "StaffGroup" ("V" "Va"))
138   ("VceB" "StaffGroup" ("Vc" "Cb" "VcB"))
139   ("FullStr" "StaffGroup" ("V" "Va" "Vc" "Cb" "VcB"))
141 ; Choral score
142   ("Solo" "SimultaneousMusic" ("SSolo" "ASolo" "TSolo" "BSolo"))
143   ("Ch" "ChoirStaff" ("S" "A" "T" "B"))
144   ("ChoralScore" "SimultaneousMusic" ("Ch"))
145   ("SoloScore" "SimultaneousMusic" ("Solo"))
146   ("SoloChoirScore" "SimultaneousMusic" ("Solo" "Ch"))
148 ; Organ score (inkl. Figured bass)
149   ("BCFb" "FiguredBass" ())
150   ("FiguredBass" "FiguredBass" ())
151   ;("Organ" "SimultaneousMusic" ("BCFb" "O"))
152   ("Continuo" "ParallelVoicesStaff" ("BCFb" "BC" "FiguredBass"))
153   ("RealizedContinuo" "PianoStaff" ("BCRealization" "Continuo"))
154   ("BassGroup" "StaffGroup" ("Continuo"))
156   ("P" "PianoStaff" ("PI" "PII"))
157   ("O" "PianoStaff" ("OI" "OII"))
158   ("OI" "ParallelVoicesStaff" ("OIa" "OIb"))
159   ("OII" "ParallelVoicesStaff" ("OIIa" "OIIb"))
160   ("Organ" "SimultaneousMusic" ("OGroup" "RealizedContinuo"))
161   ;("BassGroup" "ParallelVoicesStaff" ("Organ" "O" "BC" "VceB"))
163 ; Full Scores
164   ("FullScore" "SimultaneousMusic" ("Wd" "Br" "Perc" "Str" "SoloChoirScore" "O" "RealizedContinuo"))
165   ("LongScore" "SimultaneousMusic" ("WdLong" "BrLong" "Perc" "Str" "SoloChoirScore" "O" "RealizedContinuo"))
166   ("OriginalScore" "SimultaneousMusic" ("BrLong" "WdLong" "Perc" "Str" "SoloChoirScore" "O" "BassGroup"))
168 ; Piano reduction
169   ;("Piano" "SimultaneousMusic" ("Organ"))
170   ("OrganScore" "SimultaneousMusic" ("ChoralScore" "O"))
171   ("VocalScore" "SimultaneousMusic" ("ChoralScore" "P"))
172   ("Particell"  "SimultaneousMusic" ("ChoralScore" "BassGroup"))
174 ; Full scores: Orchestral score and long score including organ
175   ("ChStrQ" "SimultaneousMusic" ("Str" "Ch" "VceB"))
177 #(define oly:orchestral_score_structure oly:fullOrchestraScoreStructure)
179 #(define (oly:set_score_structure struct)
180   (if (list? struct)
181     (set! oly:orchestral_score_structure struct)
182     (ly:warning (_ "oly:set_score_structure needs an association list as argument!"))
183   )
186 #(define (oly:modify_score_structure entry)
187   (if (list? entry)
188     (set! oly:orchestral_score_structure (assoc-set! oly:orchestral_score_structure  (car entry) (cdr entry)))
189     (ly:warning (_ "oly:modify_score_structure expects a list (\"key\" \"type\" '(children)) as argument!"))))
191 #(define (oly:remove_from_score_structure entry)
192   (if (list? entry)
193     (map oly:remove_from_score_structure entry)
194     (set! oly:orchestral_score_structure (assoc-remove! oly:orchestral_score_structure  entry))))
196 orchestralScoreStructure = #(define-music-function (parser location structure) (list?)
197   (oly:set_score_structure structure)
198   (make-music 'Music 'void #t)
201 #(define oly:voice_types '())
203 #(define (oly:set_voice_types types)
204   (if (list? types)
205     (set! oly:voice_types types)
206     (ly:warning (_ "oly:set_voice_types needs an association list as argument!"))
207   )
210 orchestralVoiceTypes = #(define-music-function (parser location types) (list?)
211   (oly:set_voice_types types)
212   (make-music 'Music 'void #t)
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % Automatic staff and group generation
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 % Retrieve all music definitions for the given
221 #(define (oly:get_music_object piece instrument)
222   (namedPieceInstrObject piece instrument "Music")
224 #(define (oly:get_music_objects piece instruments)
225   (filter not-null? (map (lambda (i) (oly:get_music_object piece i)) instruments))
228 % Given a property name and the extensions, either generate the pair to set
229 % the property or an empty list, if no pre-defined variable could be found
230 #(define (oly:generate_property_pair prop piece instr type)
231   (let* ((val (namedPieceInstrObject piece instr type)))
232     (if (not-null? val) (list 'assign prop val) '() )
233   )
236 #(define (oly:staff_type type)
237   (cond
238     ((string? type) (string->symbol type))
239     ((symbol? type) type)
240     (else 'Staff)
241   )
244 #(define (oly:extractPitch music)
245   (let* (
246          (elems  (if (ly:music? music) (ly:music-property music 'elements)))
247          (note   (if (pair? elems) (car elems)))
248          (pitch  (if (ly:music? note) (ly:music-property note 'pitch)))
249         )
250     (if (and (not-null? music) (not (ly:pitch? pitch)))
251       (ly:warning "Unable to interpret as a pitch!")
252     )
253     pitch
254   )
257 #(define (oly:extractTranspositionPitch piece name)
258   (let* (
259          (trpFromPitch (oly:extractPitch (namedPieceInstrObject piece name "TransposeFrom")))
260          (trpToPitch   (oly:extractPitch (namedPieceInstrObject piece name "TransposeTo")))
261         )
262     (if (ly:pitch? trpFromPitch)
263       (if (ly:pitch? trpToPitch)
264         ; Both pitches
265         (ly:pitch-diff trpFromPitch trpToPitch)
266         (ly:pitch-diff trpFromPitch (ly:make-pitch 0 0 0))
267       )
268       (if (ly:pitch? trpToPitch)
269         (ly:pitch-diff (ly:make-pitch 0 0 0) trpToPitch)
270         #f
271       )
272     )
273   )
276 #(define (oly:musiccontent_for_voice parser piece name music additional)
277   (let* ((musiccontent additional))
279     ; Append the settings, key and clef (if defined)
280     (map
281       (lambda (type)
282         (let* ((object (namedPieceInstrObject piece name type)))
283           (if (ly:music? object)
284             (set! musiccontent (append musiccontent (list (ly:music-deep-copy object))))
285             (if (not-null? object) (ly:warning (_ "Wrong type (no ly:music) for ~S for instrument ~S in piece ~S") type name piece))
286           )
287         )
288       )
289       ; TODO: Does the "Tempo" work here???
290       '("Settings" "Key" "Clef" "TimeSignature" "ExtraSettings";"Tempo"
291       )
292     )
294     (if (ly:music? music)
295       (begin
296         (set! musiccontent (make-simultaneous-music (append musiccontent (list music))))
297         ;(ly:message "Generating staff for ~a" name)
298         (let* ((trpPitch (oly:extractTranspositionPitch piece name)))
299           (if (ly:pitch? trpPitch)
300             (set! musiccontent (ly:music-transpose musiccontent trpPitch))
301           )
302         )
303         musiccontent
304       )
305       ; For empty music, return empty
306       '()
307     )
308   )
311 #(define (oly:lyrics_create_single_context parser piece name voicename lyricsid)
312   ; If we have lyrics, create a lyrics context containing LyricCombineMusic
313   ; and add that as second element to the staff's elements list...
314   (let* ((id (string-append "Lyrics" lyricsid))
315          (lyrics (namedPieceInstrObject piece name id)))
316     (if (ly:music? lyrics)
317       (context-spec-music (make-music 'LyricCombineMusic
318                                       'element lyrics
319                                       'associated-context voicename)
320                           'Lyrics (oly:generate_object_name piece name id))
321       '())
322   )
325 #(define (oly:lyrics_create_contexts parser piece name voicename)
326   (filter not-null?
327     (map (lambda (str)
328                  (oly:lyrics_create_single_context parser piece name voicename str))
329          (list "" "I" "II" "III"  "IV" "V" "VI"))))
331 #(define (oly:voice_handler_internal parser piece name type music)
332   (if (ly:music? music)
333     (let* (
334            (voicename    (oly:generate_object_name piece name "Voice" ))
335            (lyrics       (oly:lyrics_create_contexts parser piece name voicename))
336            (additional   (if (not-null? lyrics) (list dynamicUp) '()))
337            (musiccontent (oly:musiccontent_for_voice parser piece name music additional))
338            (voicetype    (oly:staff_type type))
339            (voice        (context-spec-music musiccontent voicetype voicename))
340           )
341       (cons voice lyrics)
342     )
343     ; For empty music, return empty
344     '()
345   )
348 #(define (oly:voice_handler parser piece name type)
349   (oly:voice_handler_internal parser piece name type (oly:get_music_object piece name)))
351 #(define (oly:staff_handler_internal parser piece name type voices)
352   (if (not-null? voices)
353     (let* (
354            (staffname  (oly:generate_staff_name piece name))
355            (stafftype  (oly:staff_type type))
356            (staff      (make-simultaneous-music voices))
357            (propops    (oly:staff_handler_properties piece name))
358           )
359       (case stafftype
360         ((SimultaneousMusic ParallelMusic) #f)
361         (else (set! staff (context-spec-music staff stafftype staffname)))
362       )
363       (if (not-null? propops)
364         (set! (ly:music-property staff 'property-operations) propops)
365       )
366       staff
367     )
368     ; For empty music, return empty
369     '()
370   )
373 #(define (oly:staff_handler parser piece name type children)
374   (let* ((c (if (not-null? children) children (list name)))
375          (voices (apply append (map (lambda (v) (oly:create_voice parser piece v)) c)) )
376         )
377     (if (not-null? voices)
378       (oly:staff_handler_internal parser piece name type voices)
379       '()
380     )
381   )
384 #(define (oly:devnull_handler parser piece name type children)
385   (oly:voice_handler parser piece name type)
388 #(define (oly:parallel_voices_staff_handler parser piece name type children)
389   (let* (
390          (voices (map (lambda (i) (oly:create_voice parser piece i)) children))
391          ; get the list of non-empty voices and flatten it!
392          (nonemptyvoices (apply append (filter not-null? voices)))
393         )
394     (if (not-null? nonemptyvoices)
395       (oly:staff_handler_internal parser piece name "Staff" nonemptyvoices)
396       '()
397     )
398   )
401 #(define (oly:part_combined_staff_handler parser piece name type children)
402   (let* ((rawmusic (map (lambda (c) (oly:musiccontent_for_voice parser piece name (oly:get_music_object piece c) '())) children))
403          (music (filter not-null? rawmusic)))
404   (cond
405       ((and (pair? music) (ly:music? (car music)) (not-null? (cdr music)) (ly:music? (cadr music)))
406           ;(ly:message "Part-combine with two music expressions")
407           (oly:staff_handler_internal parser piece name "Staff" (list (make-part-combine-music parser music))))
408       ((null? music)
409           ;;(ly:warning "Part-combine without any music expressions")
410           '())
411       ; exactly one is a music expression, simply use that by joining
412       ((list? music)
413           ;;(ly:message "Part-combine with only one music expressions")
414           (oly:staff_handler_internal parser piece name "Staff" (list (apply append music))))
415       (else
416           ;(ly:message "make_part_combined_staff: ~S ~S ~a" piece instr instruments)
417           '() )
418     )
419   )
422 % Generate the properties for the staff for piece and instr. Typically, these
423 % are the instrument name and the short instrument name (if defined).
424 % return a (possibly empty) list of all assignments.
425 #(define (oly:staff_handler_properties piece instr)
426   (let* (
427          (mapping '(
428               (instrumentName . "InstrumentName")
429               (shortInstrumentName . "ShortInstrumentName")
430               (midiInstrument . "MidiInstrument")
431             ))
432          (assignments (map
433              (lambda (pr)
434                  (oly:generate_property_pair (car pr) piece instr (cdr pr))
435              )
436              mapping))
437          (props (filter not-null? assignments))
438         )
439     props
440   )
443 % Figured bass is a special case, as it can be voice- or staff-type. When
444 % given as a staff type, simply call the voice handler, instead
446 #(define (oly:figured_bass_staff_handler parser piece name type children)
447   (let* ((c (if (not-null? children) children (list name)))
448          (voice  (oly:voice_handler parser piece (car c) type)))
449     (if (pair? voice) (car voice) ())
450   )
453 #(define (flatten lst)
454   (define (f remaining result)
455     (cond
456       ((null? remaining) result)
457       ((pair? (car remaining)) (f (cdr remaining) (f (car remaining) result)))
458       (else (f (cdr remaining) (cons (car remaining) result)))))
459   (reverse! (f lst '())))
461 #(define (oly:staff_group_handler parser piece name type children)
462   (let* (
463          (staves (flatten (map (lambda (i) (oly:create_staff_or_group parser piece i)) children)))
464          (nonemptystaves (filter not-null? staves))
465         )
466     (if (not-null? nonemptystaves)
467       (let* (
468              (musicexpr (if (= 1 (length nonemptystaves))
469                           (car nonemptystaves)
470                           (make-simultaneous-music nonemptystaves)))
471              (groupname (oly:generate_staff_name piece name))
472              (grouptype (oly:staff_type type))
473              (group     musicexpr)
474              (propops   (oly:staff_handler_properties piece name))
475             )
476         (case grouptype
477           ((SimultaneousMusic ParallelMusic) #f)
478           (else (set! group (context-spec-music group grouptype groupname)))
479         )
480         (if (pair? propops)
481           (set! (ly:music-property group 'property-operations) propops))
482         group
483       )
484       ; Return empty list if no staves are generated
485       '()
486     )
487   )
490 #(define (oly:create_voice parser piece name)
491   (let* ( (voice (namedPieceInstrObject piece name "Voice"))
492           (type (assoc-ref oly:voice_types name)) )
493     (if (not-null? voice)
494       ; Explicit voice variable, use that
495       voice
497       (if (not type)
498         ; No entry in structure found => simple voice
499         (oly:voice_handler parser piece name "Voice")
500         ; Entry found in structure => use the handler for the given type
501         (let* (
502                (voicetype (car type))
503                (handler (assoc-ref oly:voice_handlers voicetype))
504               )
505           (if handler
506             ((primitive-eval handler) parser piece name voicetype)
507             (begin
508               (ly:warning "No handler found for voice type ~a, using default voice handler" voicetype)
509               (oly:voice_handler parser piece name voicetype)
510             )
511           )
512         )
513       )
514     )
515   )
518 #(define (oly:create_staff_or_group parser piece name)
519   (let* ( (staff (namedPieceInstrObject piece name "Staff"))
520           (type_from_structure (assoc-ref oly:orchestral_score_structure name)) )
521     ;(if (not-null? staff)
522     ;  (ly:message "Found staff variable for instrument ~a in piece ~a"  instr piece)
523     ;  (ly:message "Staff variable for instrument ~a in piece ~a NOT FOUND"  instr piece)
524     ;)
525     (if (not-null? staff)
526       ; Explicit staff variable, use that
527       staff
529       (if (not (list? type_from_structure))
530         ; No entry in structure found => simple staff
531         (oly:staff_handler parser piece name "Staff" '())
533         ; Entry found in structure => use the handler for the given type
534         (let* ((type (car type_from_structure))
535                (handler (assoc-ref oly:staff_handlers type))
536                (children (cadr type_from_structure))
537               )
538           (if handler
539             ((primitive-eval handler) parser piece name type children)
540             (begin
541               (ly:warning "No handler found for staff type ~a, using default staff handler" type)
542               (oly:staff_handler parser piece name type children)
543             )
544           )
545         )
546       )
547     )
548   )
551 #(define oly:staff_handlers
552   (list
553     ; staff group types
554     '("GrandStaff" . oly:staff_group_handler )
555     '("PianoStaff" . oly:staff_group_handler )
556     '("ChoirStaff" . oly:staff_group_handler )
557     '("StaffGroup" . oly:staff_group_handler )
558     '("InnerChoirStaff" . oly:staff_group_handler )
559     '("InnerStaffGroup" . oly:staff_group_handler )
560     '("ParallelMusic" . oly:staff_group_handler )
561     '("SimultaneousMusic" . oly:staff_group_handler )
562     ; staff types
563     '("Staff" . oly:staff_handler )
564     '("DrumStaff" . oly:staff_handler )
565     '("RhythmicStaff" . oly:staff_handler )
566     '("TabStaff" . oly:staff_handler )
567     '("GregorianTranscriptionStaff" . oly:staff_handler )
568     '("MensuralStaff" . oly:staff_handler )
569     '("VaticanaStaff" . oly:staff_handler )
570     ; staves with multiple voices
571     '("PartCombinedStaff" . oly:part_combined_staff_handler )
572     '("ParallelVoicesStaff" . oly:parallel_voices_staff_handler )
573     ; special cases: Figured bass can be staff or voice type!
574     '("FiguredBass" . oly:figured_bass_staff_handler )
575     ; Devnull is like a staff, only that it doesn't craete output
576     '("Devnull" . oly:devnull_handler )
577   )
580 #(define oly:voice_handlers
581   (list
582     ; voice types
583     '("Voice" . oly:voice_handler )
584     '("CueVoice" . oly:voice_handler )
585     '("DrumVoice" . oly:voice_handler )
586     '("FiguredBass" . oly:voice_handler )
587     '("GregorianTranscriptionVoice" . oly:voice_handler )
588     '("NoteNames" . oly:voice_handler )
589     '("TabVoice" . oly:voice_handler )
590     '("VaticanaVoice" . oly:voice_handler )
591     ;'("Dynamics" . oly:dynamics_handler )
592   )
596 #(define (oly:register_staff_type_handler type func)
597 ;  (ly:message "Registering staff handler ~a for type ~a" func type)
598   (set! oly:staff_handlers (assoc-set! oly:staff_handlers type func))
601 #(define (oly:register_voice_type_handler type func)
602 ;  (ly:message "Registering voice type handler ~a for type ~a" func type)
603   (set! oly:voice_handlers (assoc-set! oly:voice_handlers type func))
606 % handlers for deprecated API
607 #(oly:register_staff_type_handler 'StaffGroup 'oly:staff_group_handler)
608 #(oly:register_staff_type_handler 'GrandStaff 'oly:staff_group_handler)
609 #(oly:register_staff_type_handler 'PianoStaff 'oly:staff_group_handler)
610 #(oly:register_staff_type_handler 'ChoirStaff 'oly:staff_group_handler)
611 #(oly:register_staff_type_handler 'Staff 'oly:staff_handler )
612 #(oly:register_staff_type_handler 'ParallelMusic 'oly:staff_group_handler)
613 #(oly:register_staff_type_handler 'SimultaneousMusic 'oly:staff_group_handler)
614 #(oly:register_staff_type_handler #t 'oly:part_combined_staff_handler )
615 #(oly:register_staff_type_handler #f 'oly:parallel_voices_staff_handler )
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % Automatic score generation
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 #(define oly:score_handler add-score)
624 #(define oly:music_handler add-music)
625 #(define oly:text_handler add-text)
626 % ~~~~~~~~~~~~~~~~~~
628 % TODO: deprecate
629 setUseBook = #(define-music-function (parser location usebook) (boolean?)
630   (ly:warning "\\setUseBook has been deprecated! Books are now automatically handled without any hacks")
631   (make-music 'Music 'void #t)
635 % Two functions to handle midi-blocks: Either don't set one, or set an empty
636 % one so that MIDI is generated
637 #(define (oly:set_no_midi_block score) '())
638 #(define (oly:set_midi_block score)
639   (let* ((midiblock (if (defined? '$defaultmidi)
640                         (ly:output-def-clone $defaultmidi)
641                         (ly:make-output-def))))
642     (ly:output-def-set-variable! midiblock 'is-midi #t)
643     (ly:score-add-output-def! score midiblock)
644   )
647 % \setCreateMidi ##t/##f sets a flag to determine wheter MIDI output should
648 % be generated
649 #(define oly:apply_score_midi oly:set_no_midi_block)
650 setCreateMIDI = #(define-music-function (parser location createmidi) (boolean?)
651   (if createmidi
652     (set! oly:apply_score_midi oly:set_midi_block)
653     (set! oly:apply_score_midi oly:set_no_midi_block)
654   )
655   (make-music 'Music 'void #t)
659 % Two functions to handle layout-blocks: Either don't set one, or set an empty
660 % one so that a PDF is generated
661 #(define (oly:set_no_layout_block score) '())
662 #(define (oly:set_layout_block score)
663   (let* ((layoutblock (if (defined? '$defaultlayout)
664                         (ly:output-def-clone $defaultlayout)
665                         (ly:make-output-def))))
666     (ly:output-def-set-variable! layoutblock 'is-layout #t)
667     (ly:score-add-output-def! score layoutblock)
668   )
671 % \setCreatePDF ##t/##f sets a flag to determine wheter PDF output should
672 % be generated
673 #(define oly:apply_score_layout oly:set_no_layout_block)
674 setCreatePDF = #(define-music-function (parser location createlayout) (boolean?)
675   (if createlayout
676     (set! oly:apply_score_layout oly:set_layout_block)
677     (set! oly:apply_score_layout oly:set_no_layout_block)
678   )
679   (make-music 'Music 'void #t)
683 % Set the piece title in a new header block.
684 #(define (oly:set_piece_header score piecename)
685   (if (not-null? piecename)
686     (let* ((header (make-module)))
687       (module-define! header 'piece piecename)
688       (ly:score-set-header! score header)
689     )
690   )
694 % post-filter functions. By default, no filtering is done. However,
695 % for the *NoCues* function, the cue notes should be killed
696 identity = #(define-music-function (parser location music) (ly:music?) music)
697 cuefilter = #(define-music-function (parser location music) (ly:music?)
698   ((ly:music-function-extract removeWithTag) parser location 'cued ((ly:music-function-extract killCues) parser location music))
702 #(define (oly:create-toc-file layout pages)
703   (let* ((label-table (ly:output-def-lookup layout 'label-page-table)))
704     (if (not (null? label-table))
705       (let* ((format-line (lambda (toc-item)
706              (let* ((label (car toc-item))
707                     (text  (caddr toc-item))
708                     (label-page (and (list? label-table)
709                                      (assoc label label-table)))
710                     (page (and label-page (cdr label-page))))
711                (format #f "~a, section, 1, {~a}, ~a" page text label))))
712              (formatted-toc-items (map format-line (toc-items)))
713              (whole-string (string-join formatted-toc-items ",\n"))
714              (output-name (ly:parser-output-name parser))
715              (outfilename (format "~a.toc" output-name))
716              (outfile (open-output-file outfilename)))
717         (if (output-port? outfile)
718             (display whole-string outfile)
719             (ly:warning (_ "Unable to open output file ~a for the TOC information") outfilename))
720         (close-output-port outfile)))))
723 #(define-public (oly:add-toc-item parser markup-symbol text)
724   (oly:music_handler parser (add-toc-item! markup-symbol text)))
727 #(define (oly:add-score parser score piecename)
728   (if (not-null? piecename)
729     (oly:add-toc-item parser 'tocItemMarkup piecename))
730   (oly:score_handler parser score)
732 % The helper function to build a score.
733 #(define (oly:createScoreHelper parser location piece children func)
734   (let* (
735          (staves    (oly:staff_group_handler parser piece "" "SimultaneousMusic" children))
736          (music     (if (not-null? staves)
737                         ((ly:music-function-extract func) parser location staves)
738                         '()
739                     ))
740          (score     '())
741          (piecename (namedPieceInstrObject piece (car children) "PieceName"))
742          (piecenametacet (namedPieceInstrObject piece (car children) "PieceNameTacet"))
743          (header    '())
744         )
745     (if (null? music)
746       ; No staves, print tacet
747       (begin
748         (if (not-null? piecenametacet) (set! piecename piecenametacet))
749         (if (not-null? piecename)
750           (oly:add-score parser (list (oly:piece-title-markup piecename)) piecename)
751           (ly:warning (_ "No music and no score title found for part ~a and instrument ~a") piece children)
752         )
753       )
754       ; we have staves, apply the piecename to the score and add layout/midi blocks if needed
755       (begin
756         (set! score (scorify-music music parser))
757         (oly:set_piece_header score piecename)
758         (oly:apply_score_midi score)
759         (oly:apply_score_layout score)
760         ; Schedule the score for typesetting
761         (oly:add-score parser score piecename)
762       )
763     )
764   )
765   ; This is a void function, the score has been schedulled for typesetting already
766   (make-music 'Music 'void #t)
769 createScore = #(define-music-function (parser location piece children) (string? list?)
770   (oly:createScoreHelper parser location piece children identity)
772 createNoCuesScore = #(define-music-function (parser location piece children) (string? list?)
773   (oly:createScoreHelper parser location piece children cuefilter)
776 createHeadline = #(define-music-function (parser location headline) (string?)
777   (oly:add-toc-item parser 'tocItemMarkup headline)
778   (oly:score_handler parser (list (oly:piece-title-markup headline)))
779   (make-music 'Music 'void #t)
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 %%%%%   CUE NOTES
787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 newInstrument = #(define-music-function (parser location instr) (string?)
792   \set Voice.instrumentCueName = #$(string-join (list "+" instr))
795 cueText = #(define-music-function (parser location instr) (string?)
797   \set Voice.instrumentCueName = $instr
801 clearCueText = #(define-music-function (parser location) ()
803   \unset Voice.instrumentCueName
807 insertCueText = #(define-music-function (parser location instr) (string?)
808   (if (string-null? instr)
809     #{ \tag #'cued \clearCueText #}
810     #{ \tag #'cued \cueText #$instr #}
813 % generate a cue music section with instrument names
814 % Parameters: \namedCueDuring NameOfQuote CueDirection CueInstrument OriginalInstrument music
815 %                 -) NameOfQuote CueDirection music are the parameters for \cueDuring
816 %                 -) CueInstrument and OriginalInstrument are the displayed instrument names
817 % typical call:
818 % \namedCueDuring #"vIQuote" #UP #"V.I" #"Sop." { R1*3 }
819 %      This adds the notes from vIQuote (defined via \addQuote) to three measures, prints "V.I" at
820 %      the beginning of the cue notes and "Sop." at the end
821 namedCueDuring = #(define-music-function (parser location cuevoice direction instrcue instr cuemusic) (string? number? string? string? ly:music?)
823   \cueDuring #$cuevoice #$direction {
824     \insertCueText #$instrcue
825     $cuemusic
826     \insertCueText #$instr
827   }
830 namedTransposedCueDuring = #(define-music-function (parser location cuevoice direction instrcue instr trans cuemusic) (string? number? string? string? ly:music? ly:music?)
831    #{
832      \transposedCueDuring #$cuevoice #$direction $trans {
833        \insertCueText #$instrcue
834        $cuemusic
835        \insertCueText #$instr
836      }
837    #}
840 % set the cue instrument name and clef
841 setClefCue = #(define-music-function (parser location instr clef)
842                                                      (string? ly:music?)
843    #{
844      \once \override Staff.Clef #'font-size = #-3 $clef
845      \insertCueText $instr
846    #} )
848 % generate a cue music section with instrument names and clef changes
849 % Parameters: \cleffedCueDuring NameOfQuote CueDirection CueInstrument CueClef OriginalInstrument OriginalClef music
850 %                 -) NameOfQuote CueDirection music are the parameters for \cueDuring
851 %                 -) CueInstrument and OriginalInstrument are the displayed instrument names
852 %                 -) CueClef and OriginalClef are the clefs for the the cue notes and the clef of the containing voice
853 % typical call:
854 % \cleffedCueDuring #"vIQuote" #UP #"V.I" #"treble" #"Basso" #"bass" { R1*3 }
855 %      This adds the notes from vIQuote (defined via \addQuote) to three measures, prints "V.I" at
856 %      the beginning of the cue notes and "Basso" at the end. The clef is changed to treble at the
857 %      beginning of the cue notes and reset to bass at the end
858 cleffedCueDuring = #(define-music-function (parser location cuevoice direction instrcue clefcue instr clefinstr cuemusic)
859                                                         (string? number? string? ly:music? string? ly:music? ly:music?)
860    #{
861      \cueDuring #$cuevoice #$direction {
862        \tag #'cued \setClefCue #$instrcue $clefcue
863        $cuemusic
864        \tag #'cued \setClefCue #$instr $clefinstr
865      }
866    #}
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 %%%%%   DYNAMICS
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 dynamicsX = #(define-music-function (parser location offset) (number?)
881     \once \override DynamicText #'X-offset = $offset
882     \once \override DynamicLineSpanner #'Y-offset = #0
885 % Move the dynamic sign inside the staff to a fixed staff-relative position
886 % posY (where 0 means vertically starts at the middle staff line)
887 dynamicsAllInside = #(define-music-function (parser location offsetX posY)
888 (number? number?)
890   % Invalid y-extent -> hidden from skyline calculation and collisions
891   \once \override DynamicLineSpanner #'Y-extent = #(cons +0 -0.01)
892   % move by X offset and to fixed Y-position (use Y-offset of parent!)
893   \once \override DynamicText #'X-offset = $offsetX
894   \once \override DynamicText #'Y-offset =
895     $(lambda (grob)
896        (let* ((head (ly:grob-parent grob Y))
897               (offset (ly:grob-property head 'Y-offset)))
898          (- posY  offset (- 0.6))))
899   \once \override DynamicLineSpanner #'Y-offset = $posY
902 dynamicsUpInside = #(define-music-function (parser location offsetX) (number?)
903   ((ly:music-function-extract dynamicsAllInside) parser location offsetX 1.5)
906 dynamicsDownInside = #(define-music-function (parser location offsetX) (number?)
907   ((ly:music-function-extract dynamicsAllInside) parser location offsetX -3.5)
910 hairpinOffset = #(define-music-function (parser location posY) (number?)
912   \once \override DynamicLineSpanner #'Y-offset = $posY
913   \once \override DynamicLineSpanner #'Y-extent = #(cons +0 -0.01)
916 #(define ((line-break-offset before after) grob)
917   (let* ((orig (ly:grob-original grob))
918          ; All siblings if line-broken:
919          (siblings (if (ly:grob? orig) (ly:spanner-broken-into orig) '() )))
920     (if (>= (length siblings) 2)
921       ; We have been line-broken
922       (if (eq? (car (last-pair siblings)) grob)
923         ; Last sibling:
924         (ly:grob-set-property! grob 'Y-offset after)
925         ; Others get the before value:
926         (ly:grob-set-property! grob 'Y-offset before)
927       )
928     )
929   )
932 ffz = #(make-dynamic-script "ffz")
933 pf = #(make-dynamic-script "pf")
934 sempp = #(make-dynamic-script (markup #:line( #:with-dimensions '(0 . 0)
935 '(0 . 0) #:right-align #:normal-text #:italic "sempre" #:dynamic "pp")))
936 parenf = #(make-dynamic-script (markup #:line(#:normal-text #:italic #:fontsize 2 "(" #:dynamic "f" #:normal-text #:italic #:fontsize 2 ")" )))
937 parenp = #(make-dynamic-script (markup #:line(#:normal-text #:italic #:fontsize 2 "(" #:dynamic "p" #:normal-text #:italic #:fontsize 2 ")" )))
938 pdolce = #(make-dynamic-script (markup #:line(#:dynamic "p" #:with-dimensions '(0 . 0) '(0 . 0) #:normal-text #:italic "dolce"  )))
939 sfpdolce = #(make-dynamic-script (markup #:line(#:dynamic "sfp" #:with-dimensions '(0 . 0) '(0 . 0) #:normal-text #:italic "dolce"  )))
940 bracketf = #(make-dynamic-script (markup #:line(#:concat(#:normal-text #:fontsize 3 "[" #:dynamic "f" #:hspace 0.1 #:normal-text #:fontsize 3 "]"))))
941 bracketp = #(make-dynamic-script (markup #:line(#:concat(#:normal-text #:fontsize 2 "[" #:hspace 0.2 #:dynamic "p" #:normal-text #:fontsize 2 "]"))))
945 % cresc = #(make-music 'CrescendoEvent 'span-direction START 'crescendoSpanner 'text 'crescendoText "cresc.")
946 % endcresc =  #(make-span-event 'CrescendoEvent STOP)
947 % dim = #(make-music 'DecrescendoEvent 'span-direction START 'decrescendoSpanner 'text 'decrescendoText "dim.")
948 % enddim =  #(make-span-event 'DecrescendoEvent STOP)
949 % decresc = #(make-music 'DecrescendoEvent 'span-direction START 'decrescendoSpanner 'text 'decrescendoText "decresc.")
950 % enddecresc =  #(make-span-event 'DecrescendoEvent STOP)
952 % setCresc = {}
953 % setDecresc = {}
954 % setDim = {}
955 cresc = #(make-music 'CrescendoEvent 'span-direction START
956                      'span-type 'text 'span-text "cresc.")
957 dim = #(make-music 'DecrescendoEvent 'span-direction START
958                    'span-type 'text 'span-text "dim.")
959 decresc = #(make-music 'DecrescendoEvent 'span-direction START
960                        'span-type 'text 'span-text "decresc.")
962 newOrOldClef = #(define-music-function (parser location new old ) (string? string?)
963     (if (ly:get-option 'old-clefs) #{ \clef $old #} #{ \clef $new #})
968 %%% Thanks to "Gilles THIBAULT" <gilles.thibault@free.fr>, there is a way
969 %   to remove also the fermata from R1-\fermataMarkup: By filtering the music
970 %   and removing the corresponding events.
971 %   Documented as an LSR snippet: http://lsr.dsi.unimi.it/LSR/Item?id=372
972 #(define (filterOneEventsMarkup event)
973 ( let ( (eventname (ly:music-property  event 'name)) )
974  (not
975   (or     ;; add here event name you do NOT want
976    (eq? eventname 'MultiMeasureTextEvent)
977    (eq? eventname 'AbsoluteDynamicEvent)
978    (eq? eventname 'TextScriptEvent)
979    (eq? eventname 'ArticulationEvent)
980    (eq? eventname 'CrescendoEvent)
981    (eq? eventname 'DecrescendoEvent)
982   )
986 filterArticulations = #(define-music-function (parser location music) (ly:music?)
987    (music-filter filterOneEventsMarkup music)
994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996 %%%%%   Tempo markings
997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1002 rit = \markup {\italic "rit."}
1003 pocorit = \markup {\italic "poco rit."}
1004 ppmosso = \markup {\italic "poco più mosso"}
1005 dolce = \markup {\italic "dolce"}
1006 pizz = \markup {\italic "pizz."}
1007 arco = \markup {\italic "arco"}
1008 perd = \markup {\italic "perdend."}
1013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 %%%%%   REST COMBINATION
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1021 %% REST COMBINING, TAKEN FROM http://lsr.dsi.unimi.it/LSR/Item?id=336
1023 %% Usage:
1024 %%   \new Staff \with {
1025 %%     \override RestCollision #'positioning-done = #merge-rests-on-positioning
1026 %%   } << \somevoice \\ \othervoice >>
1027 %% or (globally):
1028 %%   \layout {
1029 %%     \context {
1030 %%       \Staff
1031 %%       \override RestCollision #'positioning-done = #merge-rests-on-positioning
1032 %%     }
1033 %%   }
1035 %% Limitations:
1036 %% - only handles two voices
1037 %% - does not handle multi-measure/whole-measure rests
1039 #(define (rest-score r)
1040   (let ((score 0)
1041   (yoff (ly:grob-property-data r 'Y-offset))
1042   (sp (ly:grob-property-data r 'staff-position)))
1043     (if (number? yoff)
1044   (set! score (+ score 2))
1045   (if (eq? yoff 'calculation-in-progress)
1046       (set! score (- score 3))))
1047     (and (number? sp)
1048    (<= 0 2 sp)
1049    (set! score (+ score 2))
1050    (set! score (- score (abs (- 1 sp)))))
1051     score))
1053 #(define (merge-rests-on-positioning grob)
1054   (let* ((can-merge #f)
1055    (elts (ly:grob-object grob 'elements))
1056    (num-elts (and (ly:grob-array? elts)
1057       (ly:grob-array-length elts)))
1058    (two-voice? (= num-elts 2)))
1059     (if two-voice?
1060   (let* ((v1-grob (ly:grob-array-ref elts 0))
1061          (v2-grob (ly:grob-array-ref elts 1))
1062          (v1-rest (ly:grob-object v1-grob 'rest))
1063          (v2-rest (ly:grob-object v2-grob 'rest)))
1064     (and
1065      (ly:grob? v1-rest)
1066      (ly:grob? v2-rest)
1067      (let* ((v1-duration-log (ly:grob-property v1-rest 'duration-log))
1068       (v2-duration-log (ly:grob-property v2-rest 'duration-log))
1069       (v1-dot (ly:grob-object v1-rest 'dot))
1070       (v2-dot (ly:grob-object v2-rest 'dot))
1071       (v1-dot-count (and (ly:grob? v1-dot)
1072              (ly:grob-property v1-dot 'dot-count -1)))
1073       (v2-dot-count (and (ly:grob? v2-dot)
1074              (ly:grob-property v2-dot 'dot-count -1))))
1075        (set! can-merge
1076        (and
1077         (number? v1-duration-log)
1078         (number? v2-duration-log)
1079         (= v1-duration-log v2-duration-log)
1080         (eq? v1-dot-count v2-dot-count)))
1081        (if can-merge
1082      ;; keep the rest that looks best:
1083      (let* ((keep-v1? (>= (rest-score v1-rest)
1084               (rest-score v2-rest)))
1085       (rest-to-keep (if keep-v1? v1-rest v2-rest))
1086       (dot-to-kill (if keep-v1? v2-dot v1-dot)))
1087        ;; uncomment if you're curious of which rest was chosen:
1088        ;;(ly:grob-set-property! v1-rest 'color green)
1089        ;;(ly:grob-set-property! v2-rest 'color blue)
1090        (ly:grob-suicide! (if keep-v1? v2-rest v1-rest))
1091        (if (ly:grob? dot-to-kill)
1092            (ly:grob-suicide! dot-to-kill))
1093        (ly:grob-set-property! rest-to-keep 'direction 0)
1094        (ly:rest::y-offset-callback rest-to-keep)))))))
1095     (if can-merge
1096   #t
1097   (ly:rest-collision::calc-positioning-done grob))))
1103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1105 %%%%%   TABLE OF CONTENTS
1106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1110 contentsTitle = "Inhalt / Contents"
1112 \paper {
1113   tocTitleMarkup = \markup \fill-line{
1114     \null
1115     \column {
1116       \override #(cons 'line-width (* 7 cm))
1117       \line{ \fill-line {\piece-title {\contentsTitle} \null }}
1118       \hspace #1
1119     }
1120     \null
1121   }
1122   tocItemMarkup = \markup \fill-line {
1123     \null
1124     \column {
1125       \override #(cons 'line-width (* 7 cm ))
1126       \line { \fill-line{\fromproperty #'toc:text \fromproperty #'toc:page }}
1127     }
1128     \null
1129   }
1133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1135 %%%%%   TITLE PAGE / HEADER
1136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1139 #(define-markup-command (when-property layout props symbol markp) (symbol? markup?)
1140   (if (chain-assoc-get symbol props)
1141       (interpret-markup layout props markp)
1142       (ly:make-stencil '()  '(1 . -1) '(1 . -1))))
1144 #(define-markup-command (vspace layout props amount) (number?)
1145   "This produces a invisible object taking vertical space."
1146   (let ((amount (* amount 3.0)))
1147     (if (> amount 0)
1148         (ly:make-stencil "" (cons -1 1) (cons 0 amount))
1149         (ly:make-stencil "" (cons -1 1) (cons amount amount)))))
1153 titlePageMarkup = \markup \abs-fontsize #10 \when-property #'header:title \column {
1154     \vspace #4
1155     \fill-line { \fontsize #8 \fromproperty #'header:composer }
1156     \vspace #1
1157     \fill-line { \fontsize #8 \fromproperty #'header:poet }
1158     \vspace #4
1159     \fill-line { \fontsize #10 \bold \fromproperty #'header:titlepagetitle }
1160     \vspace #1
1161     \fontsize #2 \when-property #'header:titlepagesubtitle {
1162       \fill-line { \fromproperty #'header:titlepagesubtitle }
1163       \vspace #1
1164     }
1165     \fill-line { \postscript #"-20 0 moveto 40 0 rlineto stroke" }
1166     \vspace #8
1167     \fill-line { \fontsize #5 \fromproperty #'header:ensemble }
1168     \vspace #0.02
1169     \fill-line { \fontsize #2 \fromproperty #'header:instruments }
1170     \vspace #9
1171     \fill-line { \fontsize #5 \fromproperty #'header:date }
1172     \vspace #1
1173     \fill-line { \fontsize #5 \fromproperty #'header:scoretype }
1174     \vspace #8
1175     \fontsize #2 \when-property #'header:enteredby {
1176       \fill-line { "Herausgegeben von: / Edited by:"}
1177       \vspace #0.
1178       \fill-line { \fromproperty #'header:enteredby }
1179     }
1180     \fill-line {
1181       \when-property #'header:arrangement \column {
1182         \vspace #8
1183         \fill-line { \fontsize #3 \fromproperty #'header:arrangement }
1184       }
1185     }
1188 titleHeaderMarkup = \markup {
1189   \override #'(baseline-skip . 3.5)
1190   \column {
1191     \fill-line {
1192       \fromproperty #'header:logo
1193       \center-column {
1194         \huge \larger \bold \larger \fromproperty #'header:title
1195         \large \smaller \bold \larger \fromproperty #'header:subtitle
1196         \smaller \bold \fromproperty #'header:subsubtitle
1197       }
1198       \bold \when-property #'header:instrument \rounded-box \fromproperty #'header:instrument
1199     }
1200     \fill-line {
1201       \with-dimensions #'( 0 . 0) #'( 0 . 1 ) \null
1202     }
1204 %     \fill-line {
1205 %       { \large \bold \fromproperty #'header:instrument }
1206 %     }
1207     \fill-line {
1208       \fromproperty #'header:poet
1209       \fromproperty #'header:composer
1210     }
1211     \fill-line {
1212       \fromproperty #'header:meter
1213       \fromproperty #'header:arranger
1214     }
1215   }
1218 titleScoreMarkup = \markup \piece-title \fromproperty #'header:piece
1220 \paper {
1221   scoreTitleMarkup = \titleScoreMarkup
1222   bookTitleMarkup = \titleHeaderMarkup
1227 %%%%%%%%%%%%%% headers and footers %%%%%%%%%%%%%%%%%%%%%%%%%%
1229 #(define (first-score-page layout props arg)
1230   (let* ((label 'first-score-page)
1231          (table (ly:output-def-lookup layout 'label-page-table))
1232          (label-page (and (list? table) (assoc label table)))
1233          (page-number (and label-page (cdr label-page)))
1234         )
1235     (if (eq? (chain-assoc-get 'page:page-number props -1) page-number)
1236       (interpret-markup layout props arg)
1237       empty-stencil)))
1239 #(define no-header-table '())
1240 thisPageNoHeader = #(define-music-function (parser location) ()
1241   (let* ((label (gensym "header")))
1242     (set! no-header-table (cons label no-header-table))
1243     (make-music 'Music
1244       'page-marker #t
1245       'page-label label)))
1248 % TODO: Use the no-header-table!
1249 #(define (is-header-page layout props arg)
1250   (let* ((page-number (chain-assoc-get 'page:page-number props -1))
1251         )
1252     ;(if (and (> page-number 2) (!= page-number 7))
1253     (if (> page-number 1)
1254       (interpret-markup layout props arg)
1255       empty-stencil)))
1257 #(define no-footer-table '())
1258 thisPageNoFooter = #(define-music-function (parser location) ()
1259   (let* ((label (gensym "footer")))
1260     (set! no-footer-table (cons label no-footer-table))
1261     (make-music 'Music
1262       'page-marker #t
1263       'page-label label)))
1265 % TODO: Use the no-footer-table!
1266 #(define (is-footer-page layout props arg)
1267   (let* ((page-number (chain-assoc-get 'page:page-number props -1))
1268          (label 'first-score-page)
1269          (table (ly:output-def-lookup layout 'label-page-table))
1270          (label-page (and (list? table) (assoc label table)))
1271          ;(page-number (and label-page (cdr label-page)))
1272         )
1273     (if (and (> page-number 1))
1274       (interpret-markup layout props arg)
1275       empty-stencil)))
1278 #(define copyright-footer-table '())
1279 thisPageCopyrightFooter = #(define-music-function (parser location) ()
1280   (let* ((label (gensym "copyrightfooter")))
1281     (set! copyright-footer-table (cons label copyright-footer-table))
1282     (make-music 'Music
1283       'page-marker #t
1284       'page-label label)))
1286 #(define copyright-pg 1)
1287 #(define (set-copyright-page page)
1288   (set! copyright-pg page)
1291 % TODO: Use the copyright-footer-table!
1292 #(define (copyright-page layout props arg)
1293     (if (= (chain-assoc-get 'page:page-number props -1) copyright-pg)
1294       (interpret-markup layout props arg)
1295       empty-stencil))
1298 \paper {
1299   oddHeaderMarkup = \markup \fill-line {
1300     %% force the header to take some space, otherwise the
1301     %% page layout becomes a complete mess.
1302     " "
1303     \on-the-fly #is-header-page \fromproperty #'header:title
1304     \on-the-fly #is-header-page \fromproperty #'page:page-number-string
1305   }
1306   evenHeaderMarkup = \markup \fill-line {
1307     \on-the-fly #is-header-page \fromproperty #'page:page-number-string
1308     \on-the-fly #is-header-page \fromproperty #'header:composer
1309     " "
1310   }
1312   oddFooterMarkup = \markup {
1313     \column {
1314       \fill-line {
1315         %% publisher header field only on title page.
1316         \on-the-fly #first-page \fromproperty #'header:publisher
1317       }
1318       \fill-line {
1319         %% copyright on the first real score page
1320         \on-the-fly #copyright-page \fromproperty #'header:copyright
1321         \on-the-fly #copyright-page \null
1322       }
1323       \fill-line {
1324         %% All other pages get the number of the edition centered
1325         \on-the-fly #is-footer-page \fromproperty #'header:scorenumber
1326       }
1327     }
1328   }
1342 % Interpret the given markup with the header fields added to the props.
1343 % This way, one can re-use the same functions (using fromproperty
1344 % #'header:field) in the header block and as top-level markup.
1346 % This function is originally copied from mark-up-title (file scm/titling.scm),
1347 % which is lilypond's internal function to handle the title markups. I needed
1348 % to replace the scopes and manually add the $defaultheader (which is internally
1349 % done in paper-book.cc before calling mark-up-title. Also, I don't extract the
1350 % markup from the header block, but use the given markup.
1352 % I'm not sure if I really need the page properties in props, too... But I
1353 % suppose it does not hurt, either.
1354 #(define-markup-command (markupWithHeader layout props markup) (markup?)
1355   "Interpret the given markup with the header fields added to the props.
1356    This way, one can re-use the same functions (using fromproperty
1357    #'header:field) in the header block and as top-level markup."
1358   (let* (
1359       ; TODO: If we are inside a score, add the score's local header block, too!
1360       ; Currently, I only use the global header block, stored in $defaultheader
1361       (scopes (list $defaultheader))
1362       (alists (map ly:module->alist scopes))
1364       (prefixed-alist
1365         (map (lambda (alist)
1366           (map (lambda (entry)
1367             (cons
1368               (string->symbol (string-append "header:" (symbol->string (car entry))))
1369               (cdr entry)))
1370             alist))
1371           alists))
1372       (props (append prefixed-alist
1373               props
1374               (layout-extract-page-properties layout)))
1375     )
1376     (interpret-markup layout props markup)
1377   )
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 %%%%%   Equally spacing multiple columns (e.g. for translations)
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1391 % Credits: Nicolas Sceaux on the lilypond-user mailinglist
1392 #(define-markup-command (columns layout props args) (markup-list?)
1393    (let ((line-width (/ (chain-assoc-get 'line-width props
1394                          (ly:output-def-lookup layout 'line-width))
1395                         (max (length args) 1))))
1396      (interpret-markup layout props
1397        (make-line-markup (map (lambda (line)
1398                                 (markup #:pad-to-box `(0 . ,line-width) '(0 . 0)
1399                                   #:override `(line-width . ,line-width)
1400                                   line))
1401                                args)))))
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407 %%%%%   SCORE (HEADER / LAYOUT) SETTINGS
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1412 startSlashedGraceMusic =  {
1413   \override Stem  #'stroke-style = #"grace"
1416 stopSlashedGraceMusic =  {
1417   \revert Stem #'stroke-style
1420 slashedGrace =
1421 #(def-grace-function startSlashedGraceMusic stopSlashedGraceMusic
1422    (_i "Create slashed graces (slashes through stems, but no slur)from the following music expression"))
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1427 %%%%%   SCORE (HEADER / LAYOUT) SETTINGS
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 \paper {
1432   left-margin = 2\cm
1433   right-margin = 1.5\cm
1434   line-width = 17.5\cm
1435 %   bottom-margin = 1.5\cm
1436   top-margin = 1.2\cm
1437 %   after-title-space = 0.5\cm
1438   ragged-right = ##f
1439   ragged-last = ##f
1440   ragged-bottom = ##f
1441   ragged-last-bottom = ##f
1443 \layout {
1444   \context {
1445     \ChoirStaff
1446     % If only one non-empty staff in a system exists, still print the backet
1447     \override SystemStartBracket #'collapse-height = #1
1448     \consists "Instrument_name_engraver"
1449   }
1450   \context {
1451     \StaffGroup
1452     % If only one non-empty staff in a system exists, still print the backet
1453     \override SystemStartBracket #'collapse-height = #1
1454     \consists "Instrument_name_engraver"
1455   }
1456   \context {
1457     \GrandStaff
1458     \override SystemStartBracket #'collapse-height = #1
1459     \consists "Instrument_name_engraver"
1460   }
1461 %   \context {
1462 %     \PianoStaff
1463 %     \override VerticalAxisGroup #'keep-fixed-while-stretching = ##t
1464 %   }
1465   \context {
1466     \FiguredBass
1467 %     \override VerticalAxisGroup #'keep-fixed-while-stretching = ##t
1468     \override VerticalAxisGroup #'minimum-Y-extent  = #'(0 . 1)
1469     \override VerticalAxisGroup #'padding = #0
1470   }
1471   \context {
1472     \Score
1473     % Force multi-measure rests to be written as one span
1474     \override MultiMeasureRest #'expand-limit = #3
1475     skipBars = ##t
1476     autoBeaming = ##f
1477 %     hairpinToBarline = ##f
1478     \override BarNumber #'break-visibility = #end-of-line-invisible
1479     \override BarNumber #'self-alignment-X = #0
1480     \override CombineTextScript #'avoid-slur = #'outside
1481     barNumberVisibility = #(every-nth-bar-number-visible 5)
1482     \override DynamicTextSpanner #'dash-period = #-1.0
1483     \override InstrumentSwitch #'font-size = #-1
1485     % Rest collision
1486     \override RestCollision #'positioning-done = #merge-rests-on-positioning
1487     % Auto-Accidentals: Use modern-cautionary style...
1488     extraNatural = ##f
1489     autoAccidentals = #`(Staff  ,(make-accidental-rule 'same-octave 0)
1490                                 ,(make-accidental-rule 'any-octave 0)
1491                                 ,(make-accidental-rule 'same-octave 1))
1492 %     autoCautionaries = #`(Staff ,(make-accidental-rule 'any-octave 0)
1493 %                                 ,(make-accidental-rule 'same-octave 1))
1494     printKeyCancellation = ##t
1495     quotedEventTypes = #'(StreamEvent)
1496     implicitBassFigures = #'(0 100)
1497   }
1498   \context {
1499     \RemoveEmptyStaffContext
1500   }
1501 %   \context {
1502 %     \Lyrics
1503 %     \override VerticalAxisGroup #'minimum-Y-extent = #'(0.5 . 0.5)
1504 %   }
1505   \context {
1506     \Staff
1507 %     \override VerticalAxisGroup #'minimum-Y-extent = #'(-1. . 3)
1508     ignoreFiguredBassRest = ##f
1509   }
1514 \layout {
1515   \context {
1516     \type "Engraver_group"
1517     \name Dynamics
1518     % So that \cresc works, for example.
1519     \alias Voice
1520     \consists "Output_property_engraver"
1522     \override VerticalAxisGroup #'minimum-Y-extent = #'(-1 . 1)
1523     pedalSustainStrings = #'("Ped." "*Ped." "*")
1524     pedalUnaCordaStrings = #'("una corda" "" "tre corde")
1526     \consists "Piano_pedal_engraver"
1527     \consists "Script_engraver"
1528     \consists "Dynamic_engraver"
1529     \consists "Text_engraver"
1531     \override TextScript #'font-size = #2
1532     \override TextScript #'font-shape = #'italic
1533     \override DynamicText #'extra-offset = #'(0 . 2.5)
1534     \override Hairpin #'extra-offset = #'(0 . 2.5)
1536     \consists "Skip_event_swallow_translator"
1538     \consists "Axis_group_engraver"
1539   }
1540   \context {
1541     \PianoStaff
1542     \accepts Dynamics
1543 %     \override VerticalAlignment #'forced-distance = #7
1544   }
1548 bracketts = ^\markup{"[Solo]"}
1549 brackettt = ^\markup{"[Tutti]"}
1550 ts = ^\markup{"t.s."}
1551 tt = ^\markup{"Tutti"}
1552 solo = ^\markup{"Solo"}
1553 bracketsolo = ^\markup{"[Solo]"}
1554 tutti = ^\markup{"Tutti"}
1556 dashedSlur = -\tweak #'dash-definition #'((0 1 0.4 0.75))(
1557 dashedTie = -\tweak #'dash-definition #'((0 1 0.4 0.75))~
1559 divisi = #(define-music-function (parser location vc1 vc2) (ly:music? ly:music?)
1561   << { \voiceOne $vc1 \oneVoice} \new Voice = "divisi2" { \voiceTwo $vc2 } >>
1565 #(define twoVoice divisi)
1567 #(define-public (bracket-stencils grob)
1568   (let ((lp (grob-interpret-markup grob (markup #:fontsize 3.5 #:translate (cons -0.3 -0.5) "[")))
1569         (rp (grob-interpret-markup grob (markup #:fontsize 3.5 #:translate (cons -0.3 -0.5) "]"))))
1570     (list lp rp)))
1572 bracketify = #(define-music-function (parser loc arg) (ly:music?)
1573    (_i "Tag @var{arg} to be parenthesized.")
1575   \once \override ParenthesesItem #'stencils = #bracket-stencils
1576   \parenthesize $arg
1581 #(define-markup-command (hat layout props arg) (markup?)
1582   "Draw a hat above the given string @var{arg}."
1583   (interpret-markup layout props (markup #:combine #:raise 1.5 "^" arg)))
1587 smallFlageolet =
1588 #(let ((m (make-music 'ArticulationEvent
1589                       'articulation-type "flageolet")))
1590    (ly:music-set-property! m 'tweaks
1591      (acons 'font-size -2
1592        (ly:music-property m 'tweaks)))
1593   m)
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598 %%%%%   LICENSE TEXTS
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602 LicenseCCBY = \markup {Lizensiert unter / Licensed under: Creative Commons BY \with-url #"http://creativecommons.org/licenses/by/3.0/at/" {\translate #'(0 . -0.7) \epsfile #Y #3 #"orchestrallily/cc-by.eps" }}
1603 LicenseCCBYNC = \markup {Lizensiert unter / Licensed under: Creative Commons BY-NC \with-url #"http://creativecommons.org/licenses/by-nc/3.0/at/" {\translate #'(0 . -0.7) \epsfile #Y #3 #"orchestrallily/cc-by-nc.eps" }}
1604 LicenseNoRestrictions = \markup{\line {Die Ausgabe darf kopiert und ohne Einschränkungen aufgeführt werden. / May be copied and performed without restriction.}}
1606 \include "sceaux_clef-key.ily"
1609 startUnremovableSection = \set Staff.keepAliveInterfaces =
1610  #'(rhythmic-grob-interface
1611     rest-interface
1612     lyric-interface
1613     percent-repeat-item-interface
1614     percent-repeat-interface
1615     stanza-number-interface)
1617 endUnremovableSection = \unset Staff.keepAliveInterfaces