Use cues in Choral and Vocal scores
[orchestrallily.git] / orchestrallily.ily
blobbe72da2717a95a5968e875bab0294d46d8e2f8ae
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24 %%%%%   SCORE STRUCTURE AND AUTOMATIC GENERATION
25 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 % Helper functions
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 % Helper function to filter all non-null entries
36 #(define (not-null? x) (not (null? x)))
38 % Helper function to extract a given variable, built from [Piece][Instrument]Identifier
39 #(define (namedPieceInstrObject piece instr name)
40   (let* (
41          (fullname  (string->symbol (string-append piece instr name)))
42          (instrname (string->symbol (string-append instr name)))
43          (piecename (string->symbol (string-append piece name)))
44         )
45     (cond
46       ((defined? fullname) (primitive-eval fullname))
47       ((defined? instrname) (primitive-eval instrname))
48       ((defined? piecename) (primitive-eval piecename))
49       (else '())
50     )
51   )
54 %% Print text as a justified paragraph, taken from the lilypond Notation Reference
55 #(define-markup-list-command (paragraph layout props args) (markup-list?)
56    (let ((indent (chain-assoc-get 'par-indent props 2)))
57      (interpret-markup-list layout props
58        (make-justified-lines-markup-list (cons (make-hspace-markup indent)
59                                                args)))))
61 conditionalBreak = #(define-music-function (parser location) ()
62    #{ \tag #'instrumental-score \pageBreak #}
65 #(define (oly:piece-title-markup title) (markup #:column (#:line (#:fontsize #'3 #:bold title))) )
67 #(define-markup-command (piece-title layout props title) (markup?)
68      (interpret-markup layout props (oly:piece-title-markup title))
71 #(define (oly:generate_object_name piece instr obj )
72   (if (and (string? piece) (string? instr) (string? obj))
73     (string-append piece instr obj)
74     #f
75   )
77 #(define (oly:generate_staff_name piece instr) (oly:generate_object_name piece instr "St"))
79 #(define (set-context-property context property value)
80   (set! (ly:music-property context property) value)
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % Score structure and voice types
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 #(define oly:LiedScoreStructure '(
89   ("SoloScore" "SimultaneousMusic" ("Singstimme"))
90   ("Pfe" "PianoStaff" ("PfeI" "PfeII"))
91   ;("PfeI" "ParallelVoicesStaff" ("OIa" "OIb"))
92   ;("PfeII" "ParallelVoicesStaff" ("OIIa" "OIIb"))
93   ("FullScore" "SimultaneousMusic" ("Singstimme" "Pfe"))
94   ("VocalScore" "SimultaneousMusic" ("Singstimme" "Pfe"))
97 #(define oly:fullOrchestraScoreStructure '(
98 ; Part-combined staves for full score
99   ("Fl" "PartCombinedStaff" ("FlI" "FlII"))
100   ("Ob" "PartCombinedStaff" ("ObI" "ObII"))
101   ("Cl" "PartCombinedStaff" ("ClI" "ClII"))
102   ("Fag" "PartCombinedStaff" ("FagI" "FagII"))
103   ("Wd" "StaffGroup" ("Fl" "Ob" "Cl" "Fag" "CFag"))
105   ("Cor" "PartCombinedStaff" ("CorI" "CorII"))
106   ("Trb" "PartCombinedStaff" ("TrbI" "TrbII"))
107   ("Tbe" "PartCombinedStaff" ("TbeI" "TbeII"))
108   ("Br" "StaffGroup" ("Cor" "Trb" "Tbe" "Tba"))
110 ; long score; no part-combined staves, but GrandStaves instead
111   ("FlLong" "GrandStaff" ("FlI" "FlII"))
112   ("ObLong" "GrandStaff" ("ObI" "ObII"))
113   ("ClLong" "GrandStaff" ("ClI" "ClII"))
114   ("FagLong" "GrandStaff" ("FagI" "FagII"))
115   ("WdLong" "StaffGroup" ("FlLong" "ObLong" "ClLong" "FagLong" "CFag"))
117   ("CorLong" "GrandStaff" ("CorI" "CorII"))
118   ("TrbLong" "GrandStaff" ("TrbI" "TrbII"))
119   ("TbeLong" "GrandStaff" ("TbeI" "TbeII" "TbeIII"))
120   ("BrLong" "StaffGroup" ("CorLong" "TrbLong" "TbeLong" "Tba"))
122 ; Percussion
123   ("Perc" "StaffGroup" ("Tim"))
125 ; Strings, they are the same in long and short full score  
126   ("V" "GrandStaff" ("VI" "VII"))
127   ("Str" "StaffGroup" ("V" "Va"))
128   ("VceB" "StaffGroup" ("Vc" "Cb" "VcB"))
129   ("FullStr" "StaffGroup" ("V" "Va" "Vc" "Cb" "VcB"))
131 ; Choral score
132   ("Solo" "SimultaneousMusic" ("SSolo" "ASolo" "TSolo" "BSolo"))
133   ("Ch" "ChoirStaff" ("S" "A" "T" "B"))
134   ("ChoralScore" "SimultaneousMusic" ("Ch"))
135   ("SoloScore" "SimultaneousMusic" ("Solo"))
136   ("SoloChoirScore" "SimultaneousMusic" ("Solo" "Ch"))
138 ; Organ score (inkl. Figured bass)
139   ("BCFb" "FiguredBass" ())
140   ("FiguredBass" "FiguredBass" ())
141   ;("Organ" "SimultaneousMusic" ("BCFb" "O"))
142   ("Organ" "ParallelVoicesStaff" ("BCFb" "BC" "FiguredBass"))
143   ("Continuo" "ParallelVoicesStaff" ("BCFb" "BC" "FiguredBass"))
144   
145   ("O" "PianoStaff" ("OI" "OII"))
146   ("OI" "ParallelVoicesStaff" ("OIa" "OIb"))
147   ("OII" "ParallelVoicesStaff" ("OIIa" "OIIb"))
148   ;("BassGroup" "ParallelVoicesStaff" ("Organ" "O" "BC" "VceB"))
149   ("BassGroup" "StaffGroup" ("O" "Continuo"))
151 ; Full Scores
152   ("FullScore" "SimultaneousMusic" ("Wd" "Br" "Perc" "Str" "SoloChoirScore" "BassGroup"))
153   ("LongScore" "SimultaneousMusic" ("WdLong" "BrLong" "Perc" "Str" "SoloChoirScore" "BassGroup"))
154   ("OriginalScore" "SimultaneousMusic" ("BrLong" "WdLong" "Perc" "Str" "SoloChoirScore" "BassGroup"))
156 ; Piano reduction
157   ;("Piano" "SimultaneousMusic" ("Organ"))
158   ("OrganScore" "SimultaneousMusic" ("ChoralScore" "O"))
159   ("VocalScore" "SimultaneousMusic" ("ChoralScore" "Piano"))
160   ("Particell"  "SimultaneousMusic" ("ChoralScore" "Continuo"))
162 ; Full scores: Orchestral score and long score including organ
163   ("ChStrQ" "SimultaneousMusic" ("Str" "Ch" "VceB"))
165 #(define oly:orchestral_score_structure oly:fullOrchestraScoreStructure)
167 #(define (oly:set_score_structure struct)
168   (if (list? struct)
169     (set! oly:orchestral_score_structure struct)
170     (ly:warning (_ "oly:set_score_structure needs an association list as argument!"))
171   )
174 #(define (oly:modify_score_structure entry)
175   (if (list? entry)
176     (set! oly:orchestral_score_structure (assoc-set! oly:orchestral_score_structure  (car entry) (cdr entry)))
177     (ly:warning (_ "oly:modify_score_structure expects a list (\"key\" \"type\" '(children)) as argument!"))))
179 #(define (oly:remove_from_score_structure entry)
180   (if (list? entry)
181     (map oly:remove_from_score_structure entry)
182     (set! oly:orchestral_score_structure (assoc-remove! oly:orchestral_score_structure  entry))))
184 orchestralScoreStructure = #(define-music-function (parser location structure) (list?) 
185   (oly:set_score_structure structure)
186   (make-music 'Music 'void #t)
189 #(define oly:voice_types '())
191 #(define (oly:set_voice_types types)
192   (if (list? types)
193     (set! oly:voice_types types)
194     (ly:warning (_ "oly:set_voice_types needs an association list as argument!"))
195   )
198 orchestralVoiceTypes = #(define-music-function (parser location types) (list?)
199   (oly:set_voice_types types)
200   (make-music 'Music 'void #t)
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 % Automatic staff and group generation
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 % Retrieve all music definitions for the given 
209 #(define (oly:get_music_object piece instrument)
210   (namedPieceInstrObject piece instrument "Music")
212 #(define (oly:get_music_objects piece instruments)
213   (filter not-null? (map (lambda (i) (oly:get_music_object piece i)) instruments))
216 % Given a property name and the extensions, either generate the pair to set 
217 % the property or an empty list, if no pre-defined variable could be found
218 #(define (oly:generate_property_pair prop piece instr type)
219   (let* ((val (namedPieceInstrObject piece instr type)))
220     (if (not-null? val) (list 'assign prop val) '() )
221   )
223   
224 #(define (oly:staff_type type)
225   (cond 
226     ((string? type) (string->symbol type))
227     ((symbol? type) type)
228     (else 'Staff)
229   )
232 #(define (oly:extractPitch music)
233   (let* (
234          (elems  (if (ly:music? music) (ly:music-property music 'elements)))
235          (note   (if (pair? elems) (car elems)))
236          (pitch  (if (ly:music? note) (ly:music-property note 'pitch)))
237         )
238     (if (and (not-null? music) (not (ly:pitch? pitch))) 
239       (ly:warning "Unable to interpret as a pitch!")
240     )
241     pitch
242   )
245 #(define (oly:extractTranspositionPitch piece name)
246   (let* (
247          (trpFromPitch (oly:extractPitch (namedPieceInstrObject piece name "TransposeFrom")))
248          (trpToPitch   (oly:extractPitch (namedPieceInstrObject piece name "TransposeTo")))
249         )
250     (if (ly:pitch? trpFromPitch)
251       (if (ly:pitch? trpToPitch)
252         ; Both pitches
253         (ly:pitch-diff trpFromPitch trpToPitch)
254         (ly:pitch-diff trpFromPitch (ly:make-pitch 0 0 0))
255       )
256       (if (ly:pitch? trpToPitch)
257         (ly:pitch-diff (ly:make-pitch 0 0 0) trpToPitch)
258         #f
259       )
260     )
261   )
264 #(define (oly:musiccontent_for_voice parser piece name music additional)
265   (let* ((musiccontent additional))
266     
267     ; Append the settings, key and clef (if defined)
268     (map 
269       (lambda (type) 
270         (let* ((object (namedPieceInstrObject piece name type)))
271           (if (ly:music? object)
272             (set! musiccontent (append musiccontent (list (ly:music-deep-copy object))))
273             (if (not-null? object) (ly:warning (_ "Wrong type (no ly:music) for ~S for instrument ~S in piece ~S") type name piece))
274           )
275         )
276       )
277       ; TODO: Does the "Tempo" work here???
278       '("Settings" "Key" "Clef" "TimeSignature" ;"Tempo"
279       )
280     )
282     (if (ly:music? music)
283       (begin
284         (set! musiccontent (make-simultaneous-music (append musiccontent (list music))))
285         ;(ly:message "Generating staff for ~a" name)
286         (let* ((trpPitch (oly:extractTranspositionPitch piece name)))
287           (if (ly:pitch? trpPitch) 
288             (set! musiccontent (ly:music-transpose musiccontent trpPitch))
289           )
290         )
291         musiccontent
292       )
293       ; For empty music, return empty 
294       '()
295     )
296   )
299 #(define (oly:lyrics_create_single_context parser piece name voicename lyricsid)
300   ; If we have lyrics, create a lyrics context containing LyricCombineMusic 
301   ; and add that as second element to the staff's elements list...
302   (let* ((id (string-append "Lyrics" lyricsid))
303          (lyrics (namedPieceInstrObject piece name id)))
304     (if (ly:music? lyrics)
305       (context-spec-music (make-music 'LyricCombineMusic 
306                                       'element lyrics 
307                                       'associated-context voicename) 
308                           'Lyrics (oly:generate_object_name piece name id))
309       '())
310   )
313 #(define (oly:lyrics_create_contexts parser piece name voicename)
314   (filter not-null? 
315     (map (lambda (str) 
316                  (oly:lyrics_create_single_context parser piece name voicename str))
317          (list "" "I" "II" "III"  "IV" "V" "VI"))))
319 #(define (oly:voice_handler_internal parser piece name type music)
320   (if (ly:music? music)
321     (let* (
322            (voicename    (oly:generate_object_name piece name "Voice" ))
323            (lyrics       (oly:lyrics_create_contexts parser piece name voicename))
324            (additional   (if (not-null? lyrics) (list dynamicUp) '()))
325            (musiccontent (oly:musiccontent_for_voice parser piece name music additional))
326            (voicetype    (oly:staff_type type))
327            (voice        (context-spec-music musiccontent voicetype voicename))
328           )
329       (cons voice lyrics)
330     )
331     ; For empty music, return empty 
332     '()
333   )
336 #(define (oly:voice_handler parser piece name type)
337   (oly:voice_handler_internal parser piece name type (oly:get_music_object piece name)))
340 #(define (oly:staff_handler_internal parser piece name type voices)
341   (if (not-null? voices)
342     (let* (
343            (staffname  (oly:generate_staff_name piece name))
344            (stafftype  (oly:staff_type type))
345            (staff      (make-simultaneous-music voices))
346            (propops    (oly:staff_handler_properties piece name))
347           )
348       (case stafftype
349         ((SimultaneousMusic ParallelMusic) #f)
350         (else (set! staff (context-spec-music staff stafftype staffname)))
351       )
352       (if (not-null? propops)
353         (set! (ly:music-property staff 'property-operations) propops)
354       )
355       staff
356     )
357     ; For empty music, return empty 
358     '()
359   )
362 #(define (oly:staff_handler parser piece name type children)
363   (let* ((c (if (not-null? children) children (list name)))
364          (voices (apply append (map (lambda (v) (oly:create_voice parser piece v)) c)) ) 
365         )
366     (if (not-null? voices)
367       (oly:staff_handler_internal parser piece name type voices)
368       '()
369     )
370   )
373 #(define (oly:devnull_handler parser piece name type children)
374   (oly:voice_handler parser piece name type)
376   
377 #(define (oly:parallel_voices_staff_handler parser piece name type children) 
378   (let* (
379          (voices (map (lambda (i) (oly:create_voice parser piece i)) children))
380          ; get the list of non-empty voices and flatten it!
381          (nonemptyvoices (apply append (filter not-null? voices)))
382         )
383     (if (not-null? nonemptyvoices)
384       (oly:staff_handler_internal parser piece name "Staff" nonemptyvoices)
385       '()
386     )
387   )
390 #(define (oly:part_combined_staff_handler parser piece name type children) 
391   (let* ((rawmusic (map (lambda (c) (oly:musiccontent_for_voice parser piece name (oly:get_music_object piece c) '())) children))
392          (music (filter not-null? rawmusic)))
393   (cond
394       ((and (pair? music) (ly:music? (car music)) (not-null? (cdr music)) (ly:music? (cadr music)))
395           ;(ly:message "Part-combine with two music expressions")
396           (oly:staff_handler_internal parser piece name "Staff" (list (make-part-combine-music parser music))))
397       ((null? music) 
398           (ly:warning "Part-combine without any music expressions")
399           '())
400       ; exactly one is a music expression, simply use that by joining 
401       ((list? music) 
402           (ly:message "Part-combine with only one music expressions")
403           (oly:staff_handler_internal parser piece name "Staff" (list (apply append music))))
404       (else 
405           ;(ly:message "make_part_combined_staff: ~S ~S ~a" piece instr instruments) 
406           '() )
407     )
408   )
411 % Generate the properties for the staff for piece and instr. Typically, these
412 % are the instrument name and the short instrument name (if defined).
413 % return a (possibly empty) list of all assignments.
414 #(define (oly:staff_handler_properties piece instr)
415   (let* (
416          (mapping '( 
417               (instrumentName . "InstrumentName") 
418               (shortInstrumentName . "ShortInstrumentName")
419               (midiInstrument . "MidiInstrument")
420             ))
421          (assignments (map 
422              (lambda (pr) 
423                  (oly:generate_property_pair (car pr) piece instr (cdr pr))
424              )
425              mapping))
426          (props (filter not-null? assignments))
427         )
428     props
429   )
431   
432 % Figured bass is a special case, as it can be voice- or staff-type. When
433 % given as a staff type, simply call the voice handler, instead
435 #(define (oly:figured_bass_staff_handler parser piece name type children)
436   (let* ((c (if (not-null? children) children (list name)))
437          (voice  (oly:voice_handler parser piece (car c) type)))
438     (if (pair? voice) (car voice) ())
439   )
441   
442 #(define (flatten lst)
443   (define (f remaining result)
444     (cond
445       ((null? remaining) result)
446       ((pair? (car remaining)) (f (cdr remaining) (f (car remaining) result)))
447       (else (f (cdr remaining) (cons (car remaining) result)))))
448   (reverse! (f lst '())))
450 #(define (oly:staff_group_handler parser piece name type children)
451   (let* (
452          (staves (flatten (map (lambda (i) (oly:create_staff_or_group parser piece i)) children)))
453          (nonemptystaves (filter not-null? staves))
454         )
455     (if (not-null? nonemptystaves)
456       (let* (
457              (musicexpr (if (= 1 (length nonemptystaves)) 
458                           (car nonemptystaves) 
459                           (make-simultaneous-music nonemptystaves)))
460              (groupname (oly:generate_staff_name piece name))
461              (grouptype (oly:staff_type type))
462              (group     musicexpr)
463              (propops   (oly:staff_handler_properties piece name))
464             )
465         (case grouptype
466           ((SimultaneousMusic ParallelMusic) #f)
467           (else (set! group (context-spec-music group grouptype groupname)))
468         )
469         (if (pair? propops)
470           (set! (ly:music-property group 'property-operations) propops))
471         group
472       )
473       ; Return empty list if no staves are generated
474       '()
475     )
476   )
479 #(define (oly:create_voice parser piece name)
480   (let* ( (voice (namedPieceInstrObject piece name "Voice"))
481           (type (assoc-ref oly:voice_types name)) )
482     (if (not-null? voice)
483       ; Explicit voice variable, use that
484       voice
485       
486       (if (not type)
487         ; No entry in structure found => simple voice
488         (oly:voice_handler parser piece name "Voice")
489         ; Entry found in structure => use the handler for the given type
490         (let* (
491                (voicetype (car type))
492                (handler (assoc-ref oly:voice_handlers voicetype))
493               )
494           (if handler
495             ((primitive-eval handler) parser piece name voicetype)
496             (begin
497               (ly:warning "No handler found for voice type ~a, using default voice handler" voicetype)
498               (oly:voice_handler parser piece name voicetype)
499             )
500           )
501         )
502       )
503     )
504   )
507 #(define (oly:create_staff_or_group parser piece name)
508   (let* ( (staff (namedPieceInstrObject piece name "Staff"))
509           (type_from_structure (assoc-ref oly:orchestral_score_structure name)) )
510     ;(if (not-null? staff)
511     ;  (ly:message "Found staff variable for instrument ~a in piece ~a"  instr piece)
512     ;  (ly:message "Staff variable for instrument ~a in piece ~a NOT FOUND"  instr piece)
513     ;)
514     (if (not-null? staff)
515       ; Explicit staff variable, use that
516       staff
517       
518       (if (not (list? type_from_structure)) 
519         ; No entry in structure found => simple staff
520         (oly:staff_handler parser piece name "Staff" '())
521         
522         ; Entry found in structure => use the handler for the given type
523         (let* ((type (car type_from_structure))
524                (handler (assoc-ref oly:staff_handlers type))
525                (children (cadr type_from_structure))
526               )
527           (if handler
528             ((primitive-eval handler) parser piece name type children)
529             (begin
530               (ly:warning "No handler found for staff type ~a, using default staff handler" type)
531               (oly:staff_handler parser piece name type children)
532             )
533           )
534         )
535       )
536     )
537   )
540 #(define oly:staff_handlers
541   (list
542     ; staff group types
543     '("GrandStaff" . oly:staff_group_handler )
544     '("PianoStaff" . oly:staff_group_handler )
545     '("ChoirStaff" . oly:staff_group_handler )
546     '("StaffGroup" . oly:staff_group_handler )
547     '("InnerChoirStaff" . oly:staff_group_handler )
548     '("InnerStaffGroup" . oly:staff_group_handler )
549     '("ParallelMusic" . oly:staff_group_handler )
550     '("SimultaneousMusic" . oly:staff_group_handler )
551     ; staff types
552     '("Staff" . oly:staff_handler )
553     '("DrumStaff" . oly:staff_handler )
554     '("RhythmicStaff" . oly:staff_handler )
555     '("TabStaff" . oly:staff_handler )
556     '("GregorianTranscriptionStaff" . oly:staff_handler )
557     '("MensuralStaff" . oly:staff_handler )
558     '("VaticanaStaff" . oly:staff_handler )
559     ; staves with multiple voices
560     '("PartCombinedStaff" . oly:part_combined_staff_handler )
561     '("ParallelVoicesStaff" . oly:parallel_voices_staff_handler )
562     ; special cases: Figured bass can be staff or voice type!
563     '("FiguredBass" . oly:figured_bass_staff_handler )
564     ; Devnull is like a staff, only that it doesn't craete output
565     '("Devnull" . oly:devnull_handler )
566   )
569 #(define oly:voice_handlers
570   (list
571     ; voice types
572     '("Voice" . oly:voice_handler )
573     '("CueVoice" . oly:voice_handler )
574     '("DrumVoice" . oly:voice_handler )
575     '("FiguredBass" . oly:voice_handler )
576     '("GregorianTranscriptionVoice" . oly:voice_handler )
577     '("NoteNames" . oly:voice_handler )
578     '("TabVoice" . oly:voice_handler )
579     '("VaticanaVoice" . oly:voice_handler )
580     ;'("Dynamics" . oly:dynamics_handler )
581   )
585 #(define (oly:register_staff_type_handler type func)
586 ;  (ly:message "Registering staff handler ~a for type ~a" func type)
587   (set! oly:staff_handlers (assoc-set! oly:staff_handlers type func))
590 #(define (oly:register_voice_type_handler type func)
591 ;  (ly:message "Registering voice type handler ~a for type ~a" func type)
592   (set! oly:voice_handlers (assoc-set! oly:voice_handlers type func))
595 % handlers for deprecated API
596 #(oly:register_staff_type_handler 'StaffGroup 'oly:staff_group_handler)
597 #(oly:register_staff_type_handler 'GrandStaff 'oly:staff_group_handler)
598 #(oly:register_staff_type_handler 'PianoStaff 'oly:staff_group_handler)
599 #(oly:register_staff_type_handler 'ChoirStaff 'oly:staff_group_handler)
600 #(oly:register_staff_type_handler 'Staff 'oly:staff_handler )
601 #(oly:register_staff_type_handler 'ParallelMusic 'oly:staff_group_handler)
602 #(oly:register_staff_type_handler 'SimultaneousMusic 'oly:staff_group_handler)
603 #(oly:register_staff_type_handler #t 'oly:part_combined_staff_handler )
604 #(oly:register_staff_type_handler #f 'oly:parallel_voices_staff_handler )
606     
608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609 % Automatic score generation
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
612 #(define oly:score_handler add-score)
613 #(define oly:music_handler add-music)
614 #(define oly:text_handler add-text)
615 % ~~~~~~~~~~~~~~~~~~
617 % TODO: deprecate
618 setUseBook = #(define-music-function (parser location usebook) (boolean?) 
619   (ly:warning "\\setUseBook has been deprecated! Books are now automatically handled without any hacks")
620   (make-music 'Music 'void #t)
624 % Two functions to handle midi-blocks: Either don't set one, or set an empty 
625 % one so that MIDI is generated
626 #(define (oly:set_no_midi_block score) '())
627 #(define (oly:set_midi_block score) 
628   (let* ((midiblock (if (defined? '$defaultmidi) 
629                         (ly:output-def-clone $defaultmidi) 
630                         (ly:make-output-def))))
631     (ly:output-def-set-variable! midiblock 'is-midi #t)
632     (ly:score-add-output-def! score midiblock)
633   )
636 % \setCreateMidi ##t/##f sets a flag to determine wheter MIDI output should
637 % be generated
638 #(define oly:apply_score_midi oly:set_no_midi_block)
639 setCreateMIDI = #(define-music-function (parser location createmidi) (boolean?)
640   (if createmidi
641     (set! oly:apply_score_midi oly:set_midi_block)
642     (set! oly:apply_score_midi oly:set_no_midi_block)
643   )
644   (make-music 'Music 'void #t)
648 % Two functions to handle layout-blocks: Either don't set one, or set an empty 
649 % one so that a PDF is generated
650 #(define (oly:set_no_layout_block score) '())
651 #(define (oly:set_layout_block score) 
652   (let* ((layoutblock (if (defined? '$defaultlayout) 
653                         (ly:output-def-clone $defaultlayout) 
654                         (ly:make-output-def))))
655     (ly:output-def-set-variable! layoutblock 'is-layout #t)
656     (ly:score-add-output-def! score layoutblock)
657   )
660 % \setCreatePDF ##t/##f sets a flag to determine wheter PDF output should
661 % be generated
662 #(define oly:apply_score_layout oly:set_no_layout_block)
663 setCreatePDF = #(define-music-function (parser location createlayout) (boolean?)
664   (if createlayout
665     (set! oly:apply_score_layout oly:set_layout_block)
666     (set! oly:apply_score_layout oly:set_no_layout_block)
667   )
668   (make-music 'Music 'void #t)
672 % Set the piece title in a new header block.
673 #(define (oly:set_piece_header score piecename)
674   (if (not-null? piecename)
675     (let* ((header (make-module)))
676       (module-define! header 'piece piecename)
677       (ly:score-set-header! score header)
678     )
679   )
683 % post-filter functions. By default, no filtering is done. However,
684 % for the *NoCues* function, the cue notes should be killed
685 identity = #(define-music-function (parser location music) (ly:music?) music)
686 cuefilter = #(define-music-function (parser location music) (ly:music?)
687   ((ly:music-function-extract removeWithTag) parser location 'cued ((ly:music-function-extract killCues) parser location music))
691 #(define (oly:create-toc-file layout pages)
692   (let* ((label-table (ly:output-def-lookup layout 'label-page-table)))
693     (if (not (null? label-table))
694       (let* ((format-line (lambda (toc-item)
695              (let* ((label (car toc-item))
696                     (text  (caddr toc-item))
697                     (label-page (and (list? label-table) 
698                                      (assoc label label-table)))
699                     (page (and label-page (cdr label-page))))
700                (format #f "~a, section, 1, {~a}, ~a" page text label))))
701              (formatted-toc-items (map format-line (toc-items)))
702              (whole-string (string-join formatted-toc-items ",\n"))
703              (output-name (ly:parser-output-name parser))
704              (outfilename (format "~a.toc" output-name))
705              (outfile (open-output-file outfilename)))
706         (if (output-port? outfile)
707             (display whole-string outfile)
708             (ly:warning (_ "Unable to open output file ~a for the TOC information") outfilename))
709         (close-output-port outfile)))))
712 #(define-public (oly:add-toc-item parser markup-symbol text)
713   (oly:music_handler parser (add-toc-item! markup-symbol text)))
716 #(define (oly:add-score parser score piecename)
717   (if (not-null? piecename)
718     (oly:add-toc-item parser 'tocItemMarkup piecename))
719   (oly:score_handler parser score)
721 % The helper function to build a score.
722 #(define (oly:createScoreHelper parser location piece children func)
723   (let* (
724          (staves    (oly:staff_group_handler parser piece "" "SimultaneousMusic" children))
725          (music     (if (not-null? staves)
726                         ((ly:music-function-extract func) parser location staves)
727                         '()
728                     ))
729          (score     '())
730          (piecename (namedPieceInstrObject piece (car children) "PieceName"))
731          (piecenametacet (namedPieceInstrObject piece (car children) "PieceNameTacet"))
732          (header    '())
733         )
734     (if (null? music)
735       ; No staves, print tacet
736       (begin 
737         (if (not-null? piecenametacet) (set! piecename piecenametacet))
738         (if (not-null? piecename)
739           (oly:add-score parser (list (oly:piece-title-markup piecename)) piecename)
740           (ly:warning (_ "No music and no score title found for part ~a and instrument ~a") piece children)
741         )
742       )
743       ; we have staves, apply the piecename to the score and add layout/midi blocks if needed
744       (begin
745         (set! score (scorify-music music parser))
746         (oly:set_piece_header score piecename)
747         (oly:apply_score_midi score)
748         (oly:apply_score_layout score)
749         ; Schedule the score for typesetting
750         (oly:add-score parser score piecename)
751       )
752     )
753   )
754   ; This is a void function, the score has been schedulled for typesetting already
755   (make-music 'Music 'void #t)
758 createScore = #(define-music-function (parser location piece children) (string? list?)
759   (oly:createScoreHelper parser location piece children identity)
761 createNoCuesScore = #(define-music-function (parser location piece children) (string? list?)
762   (oly:createScoreHelper parser location piece children cuefilter)
765 createHeadline = #(define-music-function (parser location headline) (string?)
766   (oly:add-toc-item parser 'tocItemMarkup headline)
767   (oly:score_handler parser (list (oly:piece-title-markup headline)))
768   (make-music 'Music 'void #t)
773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775 %%%%%   CUE NOTES
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 newInstrument = #(define-music-function (parser location instr) (string?)
781   \set Voice.instrumentCueName = #$(string-join (list "+" instr))
784 cueText = #(define-music-function (parser location instr) (string?)
786   \set Voice.instrumentCueName = $instr
790 % generate a cue music section with instrument names
791 % Parameters: \namedCueDuring NameOfQuote CueDirection CueInstrument OriginalInstrument music
792 %                 -) NameOfQuote CueDirection music are the parameters for \cueDuring
793 %                 -) CueInstrument and OriginalInstrument are the displayed instrument names
794 % typical call:
795 % \namedCueDuring #"vIQuote" #UP #"V.I" #"Sop." { R1*3 }
796 %      This adds the notes from vIQuote (defined via \addQuote) to three measures, prints "V.I" at
797 %      the beginning of the cue notes and "Sop." at the end
798 namedCueDuring = #(define-music-function (parser location cuevoice direction instrcue instr cuemusic) (string? number? string? string? ly:music?)
799    #{
800      \cueDuring #$cuevoice #$direction { \tag #'cued \cueText #$instrcue $cuemusic \tag #'cued \cueText #$instr }
801 %      \tag #'uncued $cuemusic
802    #}
804 namedTransposedCueDuring = #(define-music-function (parser location cuevoice direction instrcue instr trans cuemusic) (string? number? string? string? ly:music? ly:music?)
805    #{
806      \transposedCueDuring #$cuevoice #$direction $trans { \tag #'cued \cueText #$instrcue $cuemusic \tag #'cued \cueText #$instr }
807 %      \tag #'uncued $cuemusic
808    #}
811 % set the cue instrument name and clef
812 setClefCue = #(define-music-function (parser location instr clef) 
813                                                      (string? ly:music?)
814    #{
815      \once \override Staff.Clef #'font-size = #-3 $clef
816      \set Voice.instrumentCueName = $instr
817    #} )
819 % generate a cue music section with instrument names and clef changes
820 % Parameters: \cleffedCueDuring NameOfQuote CueDirection CueInstrument CueClef OriginalInstrument OriginalClef music
821 %                 -) NameOfQuote CueDirection music are the parameters for \cueDuring
822 %                 -) CueInstrument and OriginalInstrument are the displayed instrument names
823 %                 -) CueClef and OriginalClef are the clefs for the the cue notes and the clef of the containing voice
824 % typical call:
825 % \cleffedCueDuring #"vIQuote" #UP #"V.I" #"treble" #"Basso" #"bass" { R1*3 }
826 %      This adds the notes from vIQuote (defined via \addQuote) to three measures, prints "V.I" at
827 %      the beginning of the cue notes and "Basso" at the end. The clef is changed to treble at the 
828 %      beginning of the cue notes and reset to bass at the end
829 cleffedCueDuring = #(define-music-function (parser location cuevoice direction instrcue clefcue instr clefinstr cuemusic) 
830                                                         (string? number? string? ly:music? string? ly:music? ly:music?)
831    #{
832      \cueDuring #$cuevoice #$direction { \tag #'cued \setClefCue #$instrcue $clefcue $cuemusic \tag #'cued \setClefCue #$instr $clefinstr }
833 %      \tag #'uncued $cuemusic
834    #}
840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842 %%%%%   DYNAMICS
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 dynamicsX = #(define-music-function (parser location offset) (number?)
849     \once \override DynamicText #'X-offset = $offset
850     \once \override DynamicLineSpanner #'Y-offset = #0
853 % Move the dynamic sign inside the staff to a fixed staff-relative position 
854 % posY (where 0 means vertically starts at the middle staff line)
855 dynamicsAllInside = #(define-music-function (parser location offsetX posY)
856 (number? number?)
858   % Invalid y-extent -> hidden from skyline calculation and collisions
859   \once \override DynamicLineSpanner #'Y-extent = #(cons +0 -0.01)
860   % move by X offset and to fixed Y-position (use Y-offset of parent!)
861   \once \override DynamicText #'X-offset = $offsetX
862   \once \override DynamicText #'Y-offset =
863     $(lambda (grob)
864        (let* ((head (ly:grob-parent grob Y))
865               (offset (ly:grob-property head 'Y-offset)))
866          (- posY  offset (- 0.6))))
867   \once \override DynamicLineSpanner #'Y-offset = $posY
870 dynamicsUpInside = #(define-music-function (parser location offsetX) (number?)
871   ((ly:music-function-extract dynamicsAllInside) parser location offsetX 1.5)
874 dynamicsDownInside = #(define-music-function (parser location offsetX) (number?)
875   ((ly:music-function-extract dynamicsAllInside) parser location offsetX -3.5)
878 hairpinOffset = #(define-music-function (parser location posY) (number?)
880   \once \override DynamicLineSpanner #'Y-offset = $posY
881   \once \override DynamicLineSpanner #'Y-extent = #(cons +0 -0.01)
884 #(define ((line-break-offset before after) grob)
885   (let* ((orig (ly:grob-original grob))
886          ; All siblings if line-broken:
887          (siblings (if (ly:grob? orig) (ly:spanner-broken-into orig) '() )))
888     (if (>= (length siblings) 2)
889       ; We have been line-broken
890       (if (eq? (car (last-pair siblings)) grob)
891         ; Last sibling:
892         (ly:grob-set-property! grob 'Y-offset after)
893         ; Others get the before value:
894         (ly:grob-set-property! grob 'Y-offset before)
895       )
896     )
897   )
900 ffz = #(make-dynamic-script "ffz")
901 pf = #(make-dynamic-script "pf")
902 sempp = #(make-dynamic-script (markup #:line( #:with-dimensions '(0 . 0) 
903 '(0 . 0) #:right-align #:normal-text #:italic "sempre" #:dynamic "pp")))
904 parenf = #(make-dynamic-script (markup #:line(#:normal-text #:italic #:fontsize 2 "(" #:dynamic "f" #:normal-text #:italic #:fontsize 2 ")" )))
905 parenp = #(make-dynamic-script (markup #:line(#:normal-text #:italic #:fontsize 2 "(" #:dynamic "p" #:normal-text #:italic #:fontsize 2 ")" )))
906 pdolce = #(make-dynamic-script (markup #:line(#:dynamic "p" #:with-dimensions '(0 . 0) '(0 . 0) #:normal-text #:italic "dolce"  )))
907 sfpdolce = #(make-dynamic-script (markup #:line(#:dynamic "sfp" #:with-dimensions '(0 . 0) '(0 . 0) #:normal-text #:italic "dolce"  )))
908 bracketf = #(make-dynamic-script (markup #:line(#:concat(#:normal-text #:fontsize 3 "[" #:dynamic "f" #:hspace 0.1 #:normal-text #:fontsize 3 "]"))))
909 bracketp = #(make-dynamic-script (markup #:line(#:concat(#:normal-text #:fontsize 2 "[" #:hspace 0.2 #:dynamic "p" #:normal-text #:fontsize 2 "]"))))
913 % cresc = #(make-music 'CrescendoEvent 'span-direction START 'crescendoSpanner 'text 'crescendoText "cresc.")
914 % endcresc =  #(make-span-event 'CrescendoEvent STOP)
915 % dim = #(make-music 'DecrescendoEvent 'span-direction START 'decrescendoSpanner 'text 'decrescendoText "dim.")
916 % enddim =  #(make-span-event 'DecrescendoEvent STOP)
917 % decresc = #(make-music 'DecrescendoEvent 'span-direction START 'decrescendoSpanner 'text 'decrescendoText "decresc.")
918 % enddecresc =  #(make-span-event 'DecrescendoEvent STOP)
920 % setCresc = {}
921 % setDecresc = {}
922 % setDim = {}
923 cresc = #(make-music 'CrescendoEvent 'span-direction START
924                      'span-type 'text 'span-text "cresc.")
925 dim = #(make-music 'DecrescendoEvent 'span-direction START
926                    'span-type 'text 'span-text "dim.")
927 decresc = #(make-music 'DecrescendoEvent 'span-direction START
928                        'span-type 'text 'span-text "decresc.")
930 newOrOldClef = #(define-music-function (parser location new old ) (string? string?)
931     (if (ly:get-option 'old-clefs) #{ \clef $old #} #{ \clef $new #}) 
936 %%% Thanks to "Gilles THIBAULT" <gilles.thibault@free.fr>, there is a way
937 %   to remove also the fermata from R1-\fermataMarkup: By filtering the music
938 %   and removing the corresponding events.
939 %   Documented as an LSR snippet: http://lsr.dsi.unimi.it/LSR/Item?id=372
940 #(define (filterOneEventsMarkup event)
941 ( let ( (eventname (ly:music-property  event 'name)) )
942  (not
943   (or     ;; add here event name you do NOT want
944    (eq? eventname 'MultiMeasureTextEvent)
945    (eq? eventname 'AbsoluteDynamicEvent)
946    (eq? eventname 'TextScriptEvent)
947    (eq? eventname 'ArticulationEvent)
948    (eq? eventname 'CrescendoEvent)
949    (eq? eventname 'DecrescendoEvent)
950   )
954 filterArticulations = #(define-music-function (parser location music) (ly:music?)
955   Â (music-filter filterOneEventsMarkup music)
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 %%%%%   Tempo markings
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 rit = \markup {\italic "rit."}
971 pocorit = \markup {\italic "poco rit."}
972 ppmosso = \markup {\italic "poco più mosso"}
973 dolce = \markup {\italic "dolce."}
974 pizz = \markup {\italic "pizz."}
975 arco = \markup {\italic "arco"}
976 perd = \markup {\italic "perdend."}
981 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983 %%%%%   REST COMBINATION
984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 %% REST COMBINING, TAKEN FROM http://lsr.dsi.unimi.it/LSR/Item?id=336
991 %% Usage:
992 %%   \new Staff \with {
993 %%     \override RestCollision #'positioning-done = #merge-rests-on-positioning
994 %%   } << \somevoice \\ \othervoice >>
995 %% or (globally):
996 %%   \layout {
997 %%     \context {
998 %%       \Staff
999 %%       \override RestCollision #'positioning-done = #merge-rests-on-positioning
1000 %%     }
1001 %%   } 
1003 %% Limitations:
1004 %% - only handles two voices
1005 %% - does not handle multi-measure/whole-measure rests
1007 #(define (rest-score r)
1008   (let ((score 0)
1009   (yoff (ly:grob-property-data r 'Y-offset))
1010   (sp (ly:grob-property-data r 'staff-position)))
1011     (if (number? yoff)
1012   (set! score (+ score 2))
1013   (if (eq? yoff 'calculation-in-progress)
1014       (set! score (- score 3))))
1015     (and (number? sp)
1016    (<= 0 2 sp)
1017    (set! score (+ score 2))
1018    (set! score (- score (abs (- 1 sp)))))
1019     score))
1021 #(define (merge-rests-on-positioning grob)
1022   (let* ((can-merge #f)
1023    (elts (ly:grob-object grob 'elements))
1024    (num-elts (and (ly:grob-array? elts)
1025       (ly:grob-array-length elts)))
1026    (two-voice? (= num-elts 2)))
1027     (if two-voice?
1028   (let* ((v1-grob (ly:grob-array-ref elts 0))
1029          (v2-grob (ly:grob-array-ref elts 1))
1030          (v1-rest (ly:grob-object v1-grob 'rest))
1031          (v2-rest (ly:grob-object v2-grob 'rest)))
1032     (and
1033      (ly:grob? v1-rest)
1034      (ly:grob? v2-rest)          
1035      (let* ((v1-duration-log (ly:grob-property v1-rest 'duration-log))
1036       (v2-duration-log (ly:grob-property v2-rest 'duration-log))
1037       (v1-dot (ly:grob-object v1-rest 'dot))
1038       (v2-dot (ly:grob-object v2-rest 'dot))
1039       (v1-dot-count (and (ly:grob? v1-dot)
1040              (ly:grob-property v1-dot 'dot-count -1)))
1041       (v2-dot-count (and (ly:grob? v2-dot)
1042              (ly:grob-property v2-dot 'dot-count -1))))
1043        (set! can-merge
1044        (and 
1045         (number? v1-duration-log)
1046         (number? v2-duration-log)
1047         (= v1-duration-log v2-duration-log)
1048         (eq? v1-dot-count v2-dot-count)))
1049        (if can-merge
1050      ;; keep the rest that looks best:
1051      (let* ((keep-v1? (>= (rest-score v1-rest)
1052               (rest-score v2-rest)))
1053       (rest-to-keep (if keep-v1? v1-rest v2-rest))
1054       (dot-to-kill (if keep-v1? v2-dot v1-dot)))
1055        ;; uncomment if you're curious of which rest was chosen:
1056        ;;(ly:grob-set-property! v1-rest 'color green)
1057        ;;(ly:grob-set-property! v2-rest 'color blue)
1058        (ly:grob-suicide! (if keep-v1? v2-rest v1-rest))
1059        (if (ly:grob? dot-to-kill)
1060            (ly:grob-suicide! dot-to-kill))
1061        (ly:grob-set-property! rest-to-keep 'direction 0)
1062        (ly:rest::y-offset-callback rest-to-keep)))))))
1063     (if can-merge
1064   #t
1065   (ly:rest-collision::calc-positioning-done grob))))
1071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1073 %%%%%   TABLE OF CONTENTS
1074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078 contentsTitle = "Inhalt / Contents"
1080 \paper {
1081   tocTitleMarkup = \markup \fill-line{ 
1082     \null 
1083     \column {
1084       \override #(cons 'line-width (* 7 cm)) 
1085       \line{ \fill-line {\piece-title {\contentsTitle} \null }}
1086       \hspace #1
1087     }
1088     \null 
1089   }
1090   tocItemMarkup = \markup \fill-line { 
1091     \null 
1092     \column {
1093       \override #(cons 'line-width (* 7 cm )) 
1094       \line { \fill-line{\fromproperty #'toc:text \fromproperty #'toc:page }}
1095     }
1096     \null
1097   }
1101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103 %%%%%   TITLE PAGE / HEADER
1104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1107 #(define-markup-command (when-property layout props symbol markp) (symbol? markup?)
1108   (if (chain-assoc-get symbol props)
1109       (interpret-markup layout props markp)
1110       (ly:make-stencil '()  '(1 . -1) '(1 . -1))))
1112 #(define-markup-command (vspace layout props amount) (number?)
1113   "This produces a invisible object taking vertical space."
1114   (let ((amount (* amount 3.0)))
1115     (if (> amount 0)
1116         (ly:make-stencil "" (cons -1 1) (cons 0 amount))
1117         (ly:make-stencil "" (cons -1 1) (cons amount amount)))))
1121 titlePageMarkup = \markup \abs-fontsize #10 \when-property #'header:title \column {
1122     \vspace #4
1123     \fill-line { \fontsize #8 \fromproperty #'header:composer }
1124     \vspace #1
1125     \fill-line { \fontsize #8 \fromproperty #'header:poet }
1126     \vspace #4
1127     \fill-line { \fontsize #10 \bold \fromproperty #'header:titlepagetitle }
1128     \vspace #1
1129     \fontsize #2 \when-property #'header:titlepagesubtitle { 
1130       \fill-line { \fromproperty #'header:titlepagesubtitle }
1131       \vspace #1
1132     }
1133     \fill-line { \postscript #"-20 0 moveto 40 0 rlineto stroke" }
1134     \vspace #8
1135     \fill-line { \fontsize #5 \fromproperty #'header:ensemble }
1136     \vspace #0.02
1137     \fill-line { \fontsize #2 \fromproperty #'header:instruments }
1138     \vspace #9
1139     \fill-line { \fontsize #5 \fromproperty #'header:date }
1140     \vspace #1
1141     \fill-line { \fontsize #5 \fromproperty #'header:scoretype }
1142     \vspace #8
1143     \fontsize #2 \when-property #'header:enteredby { 
1144       \fill-line { "Herausgegeben von: / Edited by:"}
1145       \vspace #0.
1146       \fill-line { \fromproperty #'header:enteredby }
1147     }
1148     \fill-line {
1149       \when-property #'header:arrangement \column {
1150         \vspace #8
1151         \fill-line { \fontsize #3 \fromproperty #'header:arrangement }
1152       }
1153     }
1156 titleHeaderMarkup = \markup {
1157   \override #'(baseline-skip . 3.5)
1158   \column {
1159     \override #'(baseline-skip . 3.5)
1160     \column {
1161       \huge \larger \bold
1162       \fill-line {
1163         \larger \fromproperty #'header:title
1164       }
1165       \fill-line {
1166         \large \smaller \bold
1167         \larger \fromproperty #'header:subtitle
1168       }
1169       \fill-line {
1170         \smaller \bold
1171         \fromproperty #'header:subsubtitle
1172       }
1173       \fill-line {
1174         { \large \bold \fromproperty #'header:instrument }
1175       }
1176       \fill-line {
1177         \fromproperty #'header:poet
1178         \fromproperty #'header:composer
1179       }
1180       \fill-line {
1181         \fromproperty #'header:meter
1182         \fromproperty #'header:arranger
1183       }
1184     }
1185   }
1188 titleScoreMarkup = \markup \piece-title \fromproperty #'header:piece
1190 \paper {
1191   scoreTitleMarkup = \titleScoreMarkup
1192   bookTitleMarkup = \titleHeaderMarkup
1197 %%%%%%%%%%%%%% headers and footers %%%%%%%%%%%%%%%%%%%%%%%%%%
1199 #(define (first-score-page layout props arg)
1200   (let* ((label 'first-score-page)
1201          (table (ly:output-def-lookup layout 'label-page-table))
1202          (label-page (and (list? table) (assoc label table)))
1203          (page-number (and label-page (cdr label-page)))
1204         )
1205     (if (eq? (chain-assoc-get 'page:page-number props -1) page-number)
1206       (interpret-markup layout props arg)
1207       empty-stencil)))
1209 #(define no-header-table '())
1210 thisPageNoHeader = #(define-music-function (parser location) ()
1211   (let* ((label (gensym "header")))
1212     (set! no-header-table (cons label no-header-table))
1213     (make-music 'Music 
1214       'page-marker #t
1215       'page-label label)))
1218 % TODO: Use the no-header-table!
1219 #(define (is-header-page layout props arg)
1220   (let* ((page-number (chain-assoc-get 'page:page-number props -1))
1221         )
1222     ;(if (and (> page-number 2) (!= page-number 7))
1223     (if (> page-number 1)
1224       (interpret-markup layout props arg)
1225       empty-stencil)))
1227 #(define no-footer-table '())
1228 thisPageNoFooter = #(define-music-function (parser location) ()
1229   (let* ((label (gensym "footer")))
1230     (set! no-footer-table (cons label no-footer-table))
1231     (make-music 'Music 
1232       'page-marker #t
1233       'page-label label)))
1235 % TODO: Use the no-footer-table!
1236 #(define (is-footer-page layout props arg)
1237   (let* ((page-number (chain-assoc-get 'page:page-number props -1))
1238          (label 'first-score-page)
1239          (table (ly:output-def-lookup layout 'label-page-table))
1240          (label-page (and (list? table) (assoc label table)))
1241          ;(page-number (and label-page (cdr label-page)))
1242         )
1243     (if (and (> page-number 1))
1244       (interpret-markup layout props arg)
1245       empty-stencil)))
1248 #(define copyright-footer-table '())
1249 thisPageCopyrightFooter = #(define-music-function (parser location) ()
1250   (let* ((label (gensym "copyrightfooter")))
1251     (set! copyright-footer-table (cons label copyright-footer-table))
1252     (make-music 'Music 
1253       'page-marker #t
1254       'page-label label)))
1256 #(define copyright-pg 1)
1257 #(define (set-copyright-page page)
1258   (set! copyright-pg page)
1261 % TODO: Use the copyright-footer-table!
1262 #(define (copyright-page layout props arg)
1263     (if (= (chain-assoc-get 'page:page-number props -1) copyright-pg)
1264       (interpret-markup layout props arg)
1265       empty-stencil))
1268 \paper {
1269   oddHeaderMarkup = \markup \fill-line {
1270     %% force the header to take some space, otherwise the
1271     %% page layout becomes a complete mess.
1272     " "
1273     \on-the-fly #is-header-page \fromproperty #'header:title
1274     \on-the-fly #is-header-page \fromproperty #'page:page-number-string
1275   }
1276   evenHeaderMarkup = \markup \fill-line {
1277     \on-the-fly #is-header-page \fromproperty #'page:page-number-string
1278     \on-the-fly #is-header-page \fromproperty #'header:composer
1279     " "
1280   }
1282   oddFooterMarkup = \markup {
1283     \column {
1284       \fill-line {
1285         %% publisher header field only on title page.
1286         \on-the-fly #first-page \fromproperty #'header:publisher
1287       }
1288       \fill-line {
1289         %% copyright on the first real score page
1290         \on-the-fly #copyright-page \fromproperty #'header:copyright
1291         \on-the-fly #copyright-page \null
1292       }
1293       \fill-line {
1294         %% All other pages get the number of the edition centered
1295         \on-the-fly #is-footer-page \fromproperty #'header:scorenumber
1296       }
1297     }
1298   }
1312 % Interpret the given markup with the header fields added to the props. 
1313 % This way, one can re-use the same functions (using fromproperty 
1314 % #'header:field) in the header block and as top-level markup.
1316 % This function is originally copied from mark-up-title (file scm/titling.scm), 
1317 % which is lilypond's internal function to handle the title markups. I needed 
1318 % to replace the scopes and manually add the $defaultheader (which is internally 
1319 % done in paper-book.cc before calling mark-up-title. Also, I don't extract the 
1320 % markup from the header block, but use the given markup.
1322 % I'm not sure if I really need the page properties in props, too... But I 
1323 % suppose it does not hurt, either.
1324 #(define-markup-command (markupWithHeader layout props markup) (markup?)
1325   "Interpret the given markup with the header fields added to the props. 
1326    This way, one can re-use the same functions (using fromproperty 
1327    #'header:field) in the header block and as top-level markup."
1328   (let* (
1329       ; TODO: If we are inside a score, add the score's local header block, too!
1330       ; Currently, I only use the global header block, stored in $defaultheader
1331       (scopes (list $defaultheader))
1332       (alists (map ly:module->alist scopes))
1333   
1334       (prefixed-alist
1335         (map (lambda (alist)
1336           (map (lambda (entry)
1337             (cons
1338               (string->symbol (string-append "header:" (symbol->string (car entry))))
1339               (cdr entry)))
1340             alist))
1341           alists))
1342       (props (append prefixed-alist
1343               props
1344               (layout-extract-page-properties layout)))
1345     )
1346     (interpret-markup layout props markup)
1347   )
1355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1357 %%%%%   Equally spacing multiple columns (e.g. for translations)
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1361 % Credits: Nicolas Sceaux on the lilypond-user mailinglist
1362 #(define-markup-command (columns layout props args) (markup-list?)
1363    (let ((line-width (/ (chain-assoc-get 'line-width props
1364                          (ly:output-def-lookup layout 'line-width))
1365                         (max (length args) 1))))
1366      (interpret-markup layout props
1367        (make-line-markup (map (lambda (line)
1368                                 (markup #:pad-to-box `(0 . ,line-width) '(0 . 0)
1369                                   #:override `(line-width . ,line-width)
1370                                   line))
1371                                args)))))
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377 %%%%%   SCORE (HEADER / LAYOUT) SETTINGS
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 startSlashedGraceMusic =  {
1383   \override Stem  #'stroke-style = #"grace"
1386 stopSlashedGraceMusic =  {
1387   \revert Stem #'stroke-style
1390 slashedGrace =
1391 #(def-grace-function startSlashedGraceMusic stopSlashedGraceMusic
1392    (_i "Create slashed graces (slashes through stems, but no slur)from the following music expression"))
1395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 %%%%%   SCORE (HEADER / LAYOUT) SETTINGS
1398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 \paper {
1402   left-margin = 2\cm
1403   right-margin = 1.5\cm
1404   line-width = 17.5\cm
1405 %   bottom-margin = 1.5\cm
1406 %   top-margin = 1.5\cm
1407 %   after-title-space = 0.5\cm
1408   ragged-right = ##f
1409   ragged-last = ##f
1410   ragged-bottom = ##f
1411   ragged-last-bottom = ##f
1413 \layout {
1414   \context {
1415     \ChoirStaff 
1416     % If only one non-empty staff in a system exists, still print the backet
1417     \override SystemStartBracket #'collapse-height = #1
1418     \consists "Instrument_name_engraver"
1419   }
1420   \context {
1421     \StaffGroup
1422     % If only one non-empty staff in a system exists, still print the backet
1423     \override SystemStartBracket #'collapse-height = #1
1424     \consists "Instrument_name_engraver"
1425   }
1426   \context {
1427     \GrandStaff
1428     \override SystemStartBracket #'collapse-height = #1
1429     \consists "Instrument_name_engraver"
1430   }
1431   \context {
1432     \PianoStaff
1433 %     \override VerticalAxisGroup #'keep-fixed-while-stretching = ##t
1434   }
1435   \context {
1436     \FiguredBass
1437 %     \override VerticalAxisGroup #'keep-fixed-while-stretching = ##t
1438     \override VerticalAxisGroup #'minimum-Y-extent  = #'(0 . 1)
1439     \override VerticalAxisGroup #'padding = #0
1440   }
1441   \context {
1442     \Score
1443     % Force multi-measure rests to be written as one span
1444     \override MultiMeasureRest #'expand-limit = #3
1445     skipBars = ##t
1446     autoBeaming = ##f
1447 %     hairpinToBarline = ##f
1448     \override BarNumber #'break-visibility = #end-of-line-invisible
1449     \override BarNumber #'self-alignment-X = #0
1450     \override CombineTextScript #'avoid-slur = #'outside
1451     barNumberVisibility = #(every-nth-bar-number-visible 5)
1452     \override DynamicTextSpanner #'dash-period = #-1.0
1453     \override InstrumentSwitch #'font-size = #-1
1455     % Rest collision
1456     \override RestCollision #'positioning-done = #merge-rests-on-positioning
1457     % Auto-Accidentals: Use modern-cautionary style...
1458     extraNatural = ##f
1459     autoAccidentals = #`(Staff  ,(make-accidental-rule 'same-octave 0)
1460                                 ,(make-accidental-rule 'any-octave 0)
1461                                 ,(make-accidental-rule 'same-octave 1))
1462 %     autoCautionaries = #`(Staff ,(make-accidental-rule 'any-octave 0)
1463 %                                 ,(make-accidental-rule 'same-octave 1))
1464     printKeyCancellation = ##t
1465   }
1466   \context {
1467     \RemoveEmptyStaffContext 
1468   }
1469   \context {
1470     \Lyrics
1471     \override VerticalAxisGroup #'minimum-Y-extent = #'(0.5 . 0.5)
1472   }
1473   \context {
1474     \Staff
1475     \override VerticalAxisGroup #'minimum-Y-extent = #'(-1. . 3)
1476     quotedEventTypes = #'(StreamEvent)
1477     ignoreFiguredBassRest = ##f
1478     implicitBassFigures = #'(0 100)
1479   }
1484 \layout {
1485   \context {
1486     \type "Engraver_group"
1487     \name Dynamics
1488     % So that \cresc works, for example.
1489     \alias Voice
1490     \consists "Output_property_engraver"
1491     
1492     \override VerticalAxisGroup #'minimum-Y-extent = #'(-1 . 1)
1493     pedalSustainStrings = #'("Ped." "*Ped." "*")
1494     pedalUnaCordaStrings = #'("una corda" "" "tre corde")
1495     
1496     \consists "Piano_pedal_engraver"
1497     \consists "Script_engraver"
1498     \consists "Dynamic_engraver"
1499     \consists "Text_engraver"
1500     
1501     \override TextScript #'font-size = #2
1502     \override TextScript #'font-shape = #'italic
1503     \override DynamicText #'extra-offset = #'(0 . 2.5)
1504     \override Hairpin #'extra-offset = #'(0 . 2.5)
1505     
1506     \consists "Skip_event_swallow_translator"
1507     
1508     \consists "Axis_group_engraver"
1509   }
1510   \context {
1511     \PianoStaff
1512     \accepts Dynamics
1513 %     \override VerticalAlignment #'forced-distance = #7
1514   }
1518 bracketts = ^\markup{"[Solo]"}
1519 brackettt = ^\markup{"[Tutti]"}
1520 ts = ^\markup{"t.s."}
1521 tt = ^\markup{"Tutti"}
1522 solo = ^\markup{"Solo"}
1523 tutti = ^\markup{"Tutti"}
1525 dashedSlur = -\tweak #'dash-definition #'((0 1 0.4 0.75))(
1526 dashedTie = -\tweak #'dash-definition #'((0 1 0.4 0.75))~
1528 divisi = #(define-music-function (parser location vc1 vc2) (ly:music? ly:music?)
1530   << { \voiceOne $vc1 \oneVoice} \new Voice = "divisi2" { \voiceTwo $vc2 } >>
1534 #(define-public (bracket-stencils grob)
1535   (let ((lp (grob-interpret-markup grob (markup #:fontsize 3.5 #:translate (cons -0.3 -0.5) "[")))
1536         (rp (grob-interpret-markup grob (markup #:fontsize 3.5 #:translate (cons -0.3 -0.5) "]"))))
1537     (list lp rp)))
1539 bracketify = #(define-music-function (parser loc arg) (ly:music?)
1540    (_i "Tag @var{arg} to be parenthesized.")
1542   \once \override ParenthesesItem #'stencils = #bracket-stencils
1543   \parenthesize $arg
1548 #(define-markup-command (hat layout props arg) (markup?)
1549   "Draw a hat above the given string @var{arg}."
1550   (interpret-markup layout props (markup #:combine #:raise 1.5 "^" arg)))
1554 smallFlageolet =
1555 #(let ((m (make-music 'ArticulationEvent
1556                       'articulation-type "flageolet")))
1557    (ly:music-set-property! m 'tweaks
1558      (acons 'font-size -2
1559        (ly:music-property m 'tweaks)))
1560   m)
1561   
1562