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