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