Add orchestrallily to the latex search path, so we can directly load editionkainhofer.sty
[orchestrallily.git] / orchestrallily.ily
bloba2bf0a7b5a6b87f78ddc67369c4a8bdb205a6f31
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"))
145   ("Pfe" "PianoStaff" ("PfeI" "PfeII"))
146   ("O" "PianoStaff" ("OI" "OII"))
147   ("OI" "ParallelVoicesStaff" ("OIa" "OIb"))
148   ("OII" "ParallelVoicesStaff" ("OIIa" "OIIb"))
149   ;("BassGroup" "ParallelVoicesStaff" ("Organ" "O" "BC" "VceB"))
150   ("BassGroup" "StaffGroup" ("O" "Continuo"))
152 ; Full Scores
153   ("FullScore" "SimultaneousMusic" ("Wd" "Br" "Perc" "Str" "SoloChoirScore" "BassGroup"))
154   ("LongScore" "SimultaneousMusic" ("WdLong" "BrLong" "Perc" "Str" "SoloChoirScore" "BassGroup"))
155   ("OriginalScore" "SimultaneousMusic" ("BrLong" "WdLong" "Perc" "Str" "SoloChoirScore" "BassGroup"))
157 ; Piano reduction
158   ;("Piano" "SimultaneousMusic" ("Organ"))
159   ("OrganScore" "SimultaneousMusic" ("ChoralScore" "O"))
160   ("VocalScore" "SimultaneousMusic" ("ChoralScore" "Piano"))
161   ("Particell"  "SimultaneousMusic" ("ChoralScore" "Continuo"))
163 ; Full scores: Orchestral score and long score including organ
164   ("ChStrQ" "SimultaneousMusic" ("Str" "Ch" "VceB"))
166 #(define oly:orchestral_score_structure oly:fullOrchestraScoreStructure)
168 #(define (oly:set_score_structure struct)
169   (if (list? struct)
170     (set! oly:orchestral_score_structure struct)
171     (ly:warning (_ "oly:set_score_structure needs an association list as argument!"))
172   )
175 #(define (oly:modify_score_structure entry)
176   (if (list? entry)
177     (set! oly:orchestral_score_structure (assoc-set! oly:orchestral_score_structure  (car entry) (cdr entry)))
178     (ly:warning (_ "oly:modify_score_structure expects a list (\"key\" \"type\" '(children)) as argument!"))))
180 #(define (oly:remove_from_score_structure entry)
181   (if (list? entry)
182     (map oly:remove_from_score_structure entry)
183     (set! oly:orchestral_score_structure (assoc-remove! oly:orchestral_score_structure  entry))))
185 orchestralScoreStructure = #(define-music-function (parser location structure) (list?)
186   (oly:set_score_structure structure)
187   (make-music 'Music 'void #t)
190 #(define oly:voice_types '())
192 #(define (oly:set_voice_types types)
193   (if (list? types)
194     (set! oly:voice_types types)
195     (ly:warning (_ "oly:set_voice_types needs an association list as argument!"))
196   )
199 orchestralVoiceTypes = #(define-music-function (parser location types) (list?)
200   (oly:set_voice_types types)
201   (make-music 'Music 'void #t)
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 % Automatic staff and group generation
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % Retrieve all music definitions for the given
210 #(define (oly:get_music_object piece instrument)
211   (namedPieceInstrObject piece instrument "Music")
213 #(define (oly:get_music_objects piece instruments)
214   (filter not-null? (map (lambda (i) (oly:get_music_object piece i)) instruments))
217 % Given a property name and the extensions, either generate the pair to set
218 % the property or an empty list, if no pre-defined variable could be found
219 #(define (oly:generate_property_pair prop piece instr type)
220   (let* ((val (namedPieceInstrObject piece instr type)))
221     (if (not-null? val) (list 'assign prop val) '() )
222   )
225 #(define (oly:staff_type type)
226   (cond
227     ((string? type) (string->symbol type))
228     ((symbol? type) type)
229     (else 'Staff)
230   )
233 #(define (oly:extractPitch music)
234   (let* (
235          (elems  (if (ly:music? music) (ly:music-property music 'elements)))
236          (note   (if (pair? elems) (car elems)))
237          (pitch  (if (ly:music? note) (ly:music-property note 'pitch)))
238         )
239     (if (and (not-null? music) (not (ly:pitch? pitch)))
240       (ly:warning "Unable to interpret as a pitch!")
241     )
242     pitch
243   )
246 #(define (oly:extractTranspositionPitch piece name)
247   (let* (
248          (trpFromPitch (oly:extractPitch (namedPieceInstrObject piece name "TransposeFrom")))
249          (trpToPitch   (oly:extractPitch (namedPieceInstrObject piece name "TransposeTo")))
250         )
251     (if (ly:pitch? trpFromPitch)
252       (if (ly:pitch? trpToPitch)
253         ; Both pitches
254         (ly:pitch-diff trpFromPitch trpToPitch)
255         (ly:pitch-diff trpFromPitch (ly:make-pitch 0 0 0))
256       )
257       (if (ly:pitch? trpToPitch)
258         (ly:pitch-diff (ly:make-pitch 0 0 0) trpToPitch)
259         #f
260       )
261     )
262   )
265 #(define (oly:musiccontent_for_voice parser piece name music additional)
266   (let* ((musiccontent additional))
268     ; Append the settings, key and clef (if defined)
269     (map
270       (lambda (type)
271         (let* ((object (namedPieceInstrObject piece name type)))
272           (if (ly:music? object)
273             (set! musiccontent (append musiccontent (list (ly:music-deep-copy object))))
274             (if (not-null? object) (ly:warning (_ "Wrong type (no ly:music) for ~S for instrument ~S in piece ~S") type name piece))
275           )
276         )
277       )
278       ; TODO: Does the "Tempo" work here???
279       '("Settings" "Key" "Clef" "TimeSignature" ;"Tempo"
280       )
281     )
283     (if (ly:music? music)
284       (begin
285         (set! musiccontent (make-simultaneous-music (append musiccontent (list music))))
286         ;(ly:message "Generating staff for ~a" name)
287         (let* ((trpPitch (oly:extractTranspositionPitch piece name)))
288           (if (ly:pitch? trpPitch)
289             (set! musiccontent (ly:music-transpose musiccontent trpPitch))
290           )
291         )
292         musiccontent
293       )
294       ; For empty music, return empty
295       '()
296     )
297   )
300 #(define (oly:lyrics_create_single_context parser piece name voicename lyricsid)
301   ; If we have lyrics, create a lyrics context containing LyricCombineMusic
302   ; and add that as second element to the staff's elements list...
303   (let* ((id (string-append "Lyrics" lyricsid))
304          (lyrics (namedPieceInstrObject piece name id)))
305     (if (ly:music? lyrics)
306       (context-spec-music (make-music 'LyricCombineMusic
307                                       'element lyrics
308                                       'associated-context voicename)
309                           'Lyrics (oly:generate_object_name piece name id))
310       '())
311   )
314 #(define (oly:lyrics_create_contexts parser piece name voicename)
315   (filter not-null?
316     (map (lambda (str)
317                  (oly:lyrics_create_single_context parser piece name voicename str))
318          (list "" "I" "II" "III"  "IV" "V" "VI"))))
320 #(define (oly:voice_handler_internal parser piece name type music)
321   (if (ly:music? music)
322     (let* (
323            (voicename    (oly:generate_object_name piece name "Voice" ))
324            (lyrics       (oly:lyrics_create_contexts parser piece name voicename))
325            (additional   (if (not-null? lyrics) (list dynamicUp) '()))
326            (musiccontent (oly:musiccontent_for_voice parser piece name music additional))
327            (voicetype    (oly:staff_type type))
328            (voice        (context-spec-music musiccontent voicetype voicename))
329           )
330       (cons voice lyrics)
331     )
332     ; For empty music, return empty
333     '()
334   )
337 #(define (oly:voice_handler parser piece name type)
338   (oly:voice_handler_internal parser piece name type (oly:get_music_object piece name)))
341 #(define (oly:staff_handler_internal parser piece name type voices)
342   (if (not-null? voices)
343     (let* (
344            (staffname  (oly:generate_staff_name piece name))
345            (stafftype  (oly:staff_type type))
346            (staff      (make-simultaneous-music voices))
347            (propops    (oly:staff_handler_properties piece name))
348           )
349       (case stafftype
350         ((SimultaneousMusic ParallelMusic) #f)
351         (else (set! staff (context-spec-music staff stafftype staffname)))
352       )
353       (if (not-null? propops)
354         (set! (ly:music-property staff 'property-operations) propops)
355       )
356       staff
357     )
358     ; For empty music, return empty
359     '()
360   )
363 #(define (oly:staff_handler parser piece name type children)
364   (let* ((c (if (not-null? children) children (list name)))
365          (voices (apply append (map (lambda (v) (oly:create_voice parser piece v)) c)) )
366         )
367     (if (not-null? voices)
368       (oly:staff_handler_internal parser piece name type voices)
369       '()
370     )
371   )
374 #(define (oly:devnull_handler parser piece name type children)
375   (oly:voice_handler parser piece name type)
378 #(define (oly:parallel_voices_staff_handler parser piece name type children)
379   (let* (
380          (voices (map (lambda (i) (oly:create_voice parser piece i)) children))
381          ; get the list of non-empty voices and flatten it!
382          (nonemptyvoices (apply append (filter not-null? voices)))
383         )
384     (if (not-null? nonemptyvoices)
385       (oly:staff_handler_internal parser piece name "Staff" nonemptyvoices)
386       '()
387     )
388   )
391 #(define (oly:part_combined_staff_handler parser piece name type children)
392   (let* ((rawmusic (map (lambda (c) (oly:musiccontent_for_voice parser piece name (oly:get_music_object piece c) '())) children))
393          (music (filter not-null? rawmusic)))
394   (cond
395       ((and (pair? music) (ly:music? (car music)) (not-null? (cdr music)) (ly:music? (cadr music)))
396           ;(ly:message "Part-combine with two music expressions")
397           (oly:staff_handler_internal parser piece name "Staff" (list (make-part-combine-music parser music))))
398       ((null? music)
399           (ly:warning "Part-combine without any music expressions")
400           '())
401       ; exactly one is a music expression, simply use that by joining
402       ((list? music)
403           (ly:message "Part-combine with only one music expressions")
404           (oly:staff_handler_internal parser piece name "Staff" (list (apply append music))))
405       (else
406           ;(ly:message "make_part_combined_staff: ~S ~S ~a" piece instr instruments)
407           '() )
408     )
409   )
412 % Generate the properties for the staff for piece and instr. Typically, these
413 % are the instrument name and the short instrument name (if defined).
414 % return a (possibly empty) list of all assignments.
415 #(define (oly:staff_handler_properties piece instr)
416   (let* (
417          (mapping '(
418               (instrumentName . "InstrumentName")
419               (shortInstrumentName . "ShortInstrumentName")
420               (midiInstrument . "MidiInstrument")
421             ))
422          (assignments (map
423              (lambda (pr)
424                  (oly:generate_property_pair (car pr) piece instr (cdr pr))
425              )
426              mapping))
427          (props (filter not-null? assignments))
428         )
429     props
430   )
433 % Figured bass is a special case, as it can be voice- or staff-type. When
434 % given as a staff type, simply call the voice handler, instead
436 #(define (oly:figured_bass_staff_handler parser piece name type children)
437   (let* ((c (if (not-null? children) children (list name)))
438          (voice  (oly:voice_handler parser piece (car c) type)))
439     (if (pair? voice) (car voice) ())
440   )
443 #(define (flatten lst)
444   (define (f remaining result)
445     (cond
446       ((null? remaining) result)
447       ((pair? (car remaining)) (f (cdr remaining) (f (car remaining) result)))
448       (else (f (cdr remaining) (cons (car remaining) result)))))
449   (reverse! (f lst '())))
451 #(define (oly:staff_group_handler parser piece name type children)
452   (let* (
453          (staves (flatten (map (lambda (i) (oly:create_staff_or_group parser piece i)) children)))
454          (nonemptystaves (filter not-null? staves))
455         )
456     (if (not-null? nonemptystaves)
457       (let* (
458              (musicexpr (if (= 1 (length nonemptystaves))
459                           (car nonemptystaves)
460                           (make-simultaneous-music nonemptystaves)))
461              (groupname (oly:generate_staff_name piece name))
462              (grouptype (oly:staff_type type))
463              (group     musicexpr)
464              (propops   (oly:staff_handler_properties piece name))
465             )
466         (case grouptype
467           ((SimultaneousMusic ParallelMusic) #f)
468           (else (set! group (context-spec-music group grouptype groupname)))
469         )
470         (if (pair? propops)
471           (set! (ly:music-property group 'property-operations) propops))
472         group
473       )
474       ; Return empty list if no staves are generated
475       '()
476     )
477   )
480 #(define (oly:create_voice parser piece name)
481   (let* ( (voice (namedPieceInstrObject piece name "Voice"))
482           (type (assoc-ref oly:voice_types name)) )
483     (if (not-null? voice)
484       ; Explicit voice variable, use that
485       voice
487       (if (not type)
488         ; No entry in structure found => simple voice
489         (oly:voice_handler parser piece name "Voice")
490         ; Entry found in structure => use the handler for the given type
491         (let* (
492                (voicetype (car type))
493                (handler (assoc-ref oly:voice_handlers voicetype))
494               )
495           (if handler
496             ((primitive-eval handler) parser piece name voicetype)
497             (begin
498               (ly:warning "No handler found for voice type ~a, using default voice handler" voicetype)
499               (oly:voice_handler parser piece name voicetype)
500             )
501           )
502         )
503       )
504     )
505   )
508 #(define (oly:create_staff_or_group parser piece name)
509   (let* ( (staff (namedPieceInstrObject piece name "Staff"))
510           (type_from_structure (assoc-ref oly:orchestral_score_structure name)) )
511     ;(if (not-null? staff)
512     ;  (ly:message "Found staff variable for instrument ~a in piece ~a"  instr piece)
513     ;  (ly:message "Staff variable for instrument ~a in piece ~a NOT FOUND"  instr piece)
514     ;)
515     (if (not-null? staff)
516       ; Explicit staff variable, use that
517       staff
519       (if (not (list? type_from_structure))
520         ; No entry in structure found => simple staff
521         (oly:staff_handler parser piece name "Staff" '())
523         ; Entry found in structure => use the handler for the given type
524         (let* ((type (car type_from_structure))
525                (handler (assoc-ref oly:staff_handlers type))
526                (children (cadr type_from_structure))
527               )
528           (if handler
529             ((primitive-eval handler) parser piece name type children)
530             (begin
531               (ly:warning "No handler found for staff type ~a, using default staff handler" type)
532               (oly:staff_handler parser piece name type children)
533             )
534           )
535         )
536       )
537     )
538   )
541 #(define oly:staff_handlers
542   (list
543     ; staff group types
544     '("GrandStaff" . oly:staff_group_handler )
545     '("PianoStaff" . oly:staff_group_handler )
546     '("ChoirStaff" . oly:staff_group_handler )
547     '("StaffGroup" . oly:staff_group_handler )
548     '("InnerChoirStaff" . oly:staff_group_handler )
549     '("InnerStaffGroup" . oly:staff_group_handler )
550     '("ParallelMusic" . oly:staff_group_handler )
551     '("SimultaneousMusic" . oly:staff_group_handler )
552     ; staff types
553     '("Staff" . oly:staff_handler )
554     '("DrumStaff" . oly:staff_handler )
555     '("RhythmicStaff" . oly:staff_handler )
556     '("TabStaff" . oly:staff_handler )
557     '("GregorianTranscriptionStaff" . oly:staff_handler )
558     '("MensuralStaff" . oly:staff_handler )
559     '("VaticanaStaff" . oly:staff_handler )
560     ; staves with multiple voices
561     '("PartCombinedStaff" . oly:part_combined_staff_handler )
562     '("ParallelVoicesStaff" . oly:parallel_voices_staff_handler )
563     ; special cases: Figured bass can be staff or voice type!
564     '("FiguredBass" . oly:figured_bass_staff_handler )
565     ; Devnull is like a staff, only that it doesn't craete output
566     '("Devnull" . oly:devnull_handler )
567   )
570 #(define oly:voice_handlers
571   (list
572     ; voice types
573     '("Voice" . oly:voice_handler )
574     '("CueVoice" . oly:voice_handler )
575     '("DrumVoice" . oly:voice_handler )
576     '("FiguredBass" . oly:voice_handler )
577     '("GregorianTranscriptionVoice" . oly:voice_handler )
578     '("NoteNames" . oly:voice_handler )
579     '("TabVoice" . oly:voice_handler )
580     '("VaticanaVoice" . oly:voice_handler )
581     ;'("Dynamics" . oly:dynamics_handler )
582   )
586 #(define (oly:register_staff_type_handler type func)
587 ;  (ly:message "Registering staff handler ~a for type ~a" func type)
588   (set! oly:staff_handlers (assoc-set! oly:staff_handlers type func))
591 #(define (oly:register_voice_type_handler type func)
592 ;  (ly:message "Registering voice type handler ~a for type ~a" func type)
593   (set! oly:voice_handlers (assoc-set! oly:voice_handlers type func))
596 % handlers for deprecated API
597 #(oly:register_staff_type_handler 'StaffGroup 'oly:staff_group_handler)
598 #(oly:register_staff_type_handler 'GrandStaff 'oly:staff_group_handler)
599 #(oly:register_staff_type_handler 'PianoStaff 'oly:staff_group_handler)
600 #(oly:register_staff_type_handler 'ChoirStaff 'oly:staff_group_handler)
601 #(oly:register_staff_type_handler 'Staff 'oly:staff_handler )
602 #(oly:register_staff_type_handler 'ParallelMusic 'oly:staff_group_handler)
603 #(oly:register_staff_type_handler 'SimultaneousMusic 'oly:staff_group_handler)
604 #(oly:register_staff_type_handler #t 'oly:part_combined_staff_handler )
605 #(oly:register_staff_type_handler #f 'oly:parallel_voices_staff_handler )
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610 % Automatic score generation
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 #(define oly:score_handler add-score)
614 #(define oly:music_handler add-music)
615 #(define oly:text_handler add-text)
616 % ~~~~~~~~~~~~~~~~~~
618 % TODO: deprecate
619 setUseBook = #(define-music-function (parser location usebook) (boolean?)
620   (ly:warning "\\setUseBook has been deprecated! Books are now automatically handled without any hacks")
621   (make-music 'Music 'void #t)
625 % Two functions to handle midi-blocks: Either don't set one, or set an empty
626 % one so that MIDI is generated
627 #(define (oly:set_no_midi_block score) '())
628 #(define (oly:set_midi_block score)
629   (let* ((midiblock (if (defined? '$defaultmidi)
630                         (ly:output-def-clone $defaultmidi)
631                         (ly:make-output-def))))
632     (ly:output-def-set-variable! midiblock 'is-midi #t)
633     (ly:score-add-output-def! score midiblock)
634   )
637 % \setCreateMidi ##t/##f sets a flag to determine wheter MIDI output should
638 % be generated
639 #(define oly:apply_score_midi oly:set_no_midi_block)
640 setCreateMIDI = #(define-music-function (parser location createmidi) (boolean?)
641   (if createmidi
642     (set! oly:apply_score_midi oly:set_midi_block)
643     (set! oly:apply_score_midi oly:set_no_midi_block)
644   )
645   (make-music 'Music 'void #t)
649 % Two functions to handle layout-blocks: Either don't set one, or set an empty
650 % one so that a PDF is generated
651 #(define (oly:set_no_layout_block score) '())
652 #(define (oly:set_layout_block score)
653   (let* ((layoutblock (if (defined? '$defaultlayout)
654                         (ly:output-def-clone $defaultlayout)
655                         (ly:make-output-def))))
656     (ly:output-def-set-variable! layoutblock 'is-layout #t)
657     (ly:score-add-output-def! score layoutblock)
658   )
661 % \setCreatePDF ##t/##f sets a flag to determine wheter PDF output should
662 % be generated
663 #(define oly:apply_score_layout oly:set_no_layout_block)
664 setCreatePDF = #(define-music-function (parser location createlayout) (boolean?)
665   (if createlayout
666     (set! oly:apply_score_layout oly:set_layout_block)
667     (set! oly:apply_score_layout oly:set_no_layout_block)
668   )
669   (make-music 'Music 'void #t)
673 % Set the piece title in a new header block.
674 #(define (oly:set_piece_header score piecename)
675   (if (not-null? piecename)
676     (let* ((header (make-module)))
677       (module-define! header 'piece piecename)
678       (ly:score-set-header! score header)
679     )
680   )
684 % post-filter functions. By default, no filtering is done. However,
685 % for the *NoCues* function, the cue notes should be killed
686 identity = #(define-music-function (parser location music) (ly:music?) music)
687 cuefilter = #(define-music-function (parser location music) (ly:music?)
688   ((ly:music-function-extract removeWithTag) parser location 'cued ((ly:music-function-extract killCues) parser location music))
692 #(define (oly:create-toc-file layout pages)
693   (let* ((label-table (ly:output-def-lookup layout 'label-page-table)))
694     (if (not (null? label-table))
695       (let* ((format-line (lambda (toc-item)
696              (let* ((label (car toc-item))
697                     (text  (caddr toc-item))
698                     (label-page (and (list? label-table)
699                                      (assoc label label-table)))
700                     (page (and label-page (cdr label-page))))
701                (format #f "~a, section, 1, {~a}, ~a" page text label))))
702              (formatted-toc-items (map format-line (toc-items)))
703              (whole-string (string-join formatted-toc-items ",\n"))
704              (output-name (ly:parser-output-name parser))
705              (outfilename (format "~a.toc" output-name))
706              (outfile (open-output-file outfilename)))
707         (if (output-port? outfile)
708             (display whole-string outfile)
709             (ly:warning (_ "Unable to open output file ~a for the TOC information") outfilename))
710         (close-output-port outfile)))))
713 #(define-public (oly:add-toc-item parser markup-symbol text)
714   (oly:music_handler parser (add-toc-item! markup-symbol text)))
717 #(define (oly:add-score parser score piecename)
718   (if (not-null? piecename)
719     (oly:add-toc-item parser 'tocItemMarkup piecename))
720   (oly:score_handler parser score)
722 % The helper function to build a score.
723 #(define (oly:createScoreHelper parser location piece children func)
724   (let* (
725          (staves    (oly:staff_group_handler parser piece "" "SimultaneousMusic" children))
726          (music     (if (not-null? staves)
727                         ((ly:music-function-extract func) parser location staves)
728                         '()
729                     ))
730          (score     '())
731          (piecename (namedPieceInstrObject piece (car children) "PieceName"))
732          (piecenametacet (namedPieceInstrObject piece (car children) "PieceNameTacet"))
733          (header    '())
734         )
735     (if (null? music)
736       ; No staves, print tacet
737       (begin
738         (if (not-null? piecenametacet) (set! piecename piecenametacet))
739         (if (not-null? piecename)
740           (oly:add-score parser (list (oly:piece-title-markup piecename)) piecename)
741           (ly:warning (_ "No music and no score title found for part ~a and instrument ~a") piece children)
742         )
743       )
744       ; we have staves, apply the piecename to the score and add layout/midi blocks if needed
745       (begin
746         (set! score (scorify-music music parser))
747         (oly:set_piece_header score piecename)
748         (oly:apply_score_midi score)
749         (oly:apply_score_layout score)
750         ; Schedule the score for typesetting
751         (oly:add-score parser score piecename)
752       )
753     )
754   )
755   ; This is a void function, the score has been schedulled for typesetting already
756   (make-music 'Music 'void #t)
759 createScore = #(define-music-function (parser location piece children) (string? list?)
760   (oly:createScoreHelper parser location piece children identity)
762 createNoCuesScore = #(define-music-function (parser location piece children) (string? list?)
763   (oly:createScoreHelper parser location piece children cuefilter)
766 createHeadline = #(define-music-function (parser location headline) (string?)
767   (oly:add-toc-item parser 'tocItemMarkup headline)
768   (oly:score_handler parser (list (oly:piece-title-markup headline)))
769   (make-music 'Music 'void #t)
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 %%%%%   CUE NOTES
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 newInstrument = #(define-music-function (parser location instr) (string?)
782   \set Voice.instrumentCueName = #$(string-join (list "+" instr))
785 cueText = #(define-music-function (parser location instr) (string?)
787   \set Voice.instrumentCueName = $instr
791 clearCueText = #(define-music-function (parser location) ()
793   \unset Voice.instrumentCueName
797 insertCueText = #(define-music-function (parser location instr) (string?)
798   (if (string-null? instr)
799     #{ \tag #'cued \clearCueText #}
800     #{ \tag #'cued \cueText #$instr #}
803 % generate a cue music section with instrument names
804 % Parameters: \namedCueDuring NameOfQuote CueDirection CueInstrument OriginalInstrument music
805 %                 -) NameOfQuote CueDirection music are the parameters for \cueDuring
806 %                 -) CueInstrument and OriginalInstrument are the displayed instrument names
807 % typical call:
808 % \namedCueDuring #"vIQuote" #UP #"V.I" #"Sop." { R1*3 }
809 %      This adds the notes from vIQuote (defined via \addQuote) to three measures, prints "V.I" at
810 %      the beginning of the cue notes and "Sop." at the end
811 namedCueDuring = #(define-music-function (parser location cuevoice direction instrcue instr cuemusic) (string? number? string? string? ly:music?)
813   \cueDuring #$cuevoice #$direction {
814     \insertCueText #$instrcue
815     $cuemusic
816     \insertCueText #$instr
817   }
820 namedTransposedCueDuring = #(define-music-function (parser location cuevoice direction instrcue instr trans cuemusic) (string? number? string? string? ly:music? ly:music?)
821    #{
822      \transposedCueDuring #$cuevoice #$direction $trans {
823        \insertCueText #$instrcue
824        $cuemusic
825        \insertCueText #$instr
826      }
827    #}
830 % set the cue instrument name and clef
831 setClefCue = #(define-music-function (parser location instr clef)
832                                                      (string? ly:music?)
833    #{
834      \once \override Staff.Clef #'font-size = #-3 $clef
835      \insertCueText $instr
836    #} )
838 % generate a cue music section with instrument names and clef changes
839 % Parameters: \cleffedCueDuring NameOfQuote CueDirection CueInstrument CueClef OriginalInstrument OriginalClef music
840 %                 -) NameOfQuote CueDirection music are the parameters for \cueDuring
841 %                 -) CueInstrument and OriginalInstrument are the displayed instrument names
842 %                 -) CueClef and OriginalClef are the clefs for the the cue notes and the clef of the containing voice
843 % typical call:
844 % \cleffedCueDuring #"vIQuote" #UP #"V.I" #"treble" #"Basso" #"bass" { R1*3 }
845 %      This adds the notes from vIQuote (defined via \addQuote) to three measures, prints "V.I" at
846 %      the beginning of the cue notes and "Basso" at the end. The clef is changed to treble at the
847 %      beginning of the cue notes and reset to bass at the end
848 cleffedCueDuring = #(define-music-function (parser location cuevoice direction instrcue clefcue instr clefinstr cuemusic)
849                                                         (string? number? string? ly:music? string? ly:music? ly:music?)
850    #{
851      \cueDuring #$cuevoice #$direction {
852        \tag #'cued \setClefCue #$instrcue $clefcue
853        $cuemusic
854        \tag #'cued \setClefCue #$instr $clefinstr
855      }
856    #}
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 %%%%%   DYNAMICS
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 dynamicsX = #(define-music-function (parser location offset) (number?)
871     \once \override DynamicText #'X-offset = $offset
872     \once \override DynamicLineSpanner #'Y-offset = #0
875 % Move the dynamic sign inside the staff to a fixed staff-relative position
876 % posY (where 0 means vertically starts at the middle staff line)
877 dynamicsAllInside = #(define-music-function (parser location offsetX posY)
878 (number? number?)
880   % Invalid y-extent -> hidden from skyline calculation and collisions
881   \once \override DynamicLineSpanner #'Y-extent = #(cons +0 -0.01)
882   % move by X offset and to fixed Y-position (use Y-offset of parent!)
883   \once \override DynamicText #'X-offset = $offsetX
884   \once \override DynamicText #'Y-offset =
885     $(lambda (grob)
886        (let* ((head (ly:grob-parent grob Y))
887               (offset (ly:grob-property head 'Y-offset)))
888          (- posY  offset (- 0.6))))
889   \once \override DynamicLineSpanner #'Y-offset = $posY
892 dynamicsUpInside = #(define-music-function (parser location offsetX) (number?)
893   ((ly:music-function-extract dynamicsAllInside) parser location offsetX 1.5)
896 dynamicsDownInside = #(define-music-function (parser location offsetX) (number?)
897   ((ly:music-function-extract dynamicsAllInside) parser location offsetX -3.5)
900 hairpinOffset = #(define-music-function (parser location posY) (number?)
902   \once \override DynamicLineSpanner #'Y-offset = $posY
903   \once \override DynamicLineSpanner #'Y-extent = #(cons +0 -0.01)
906 #(define ((line-break-offset before after) grob)
907   (let* ((orig (ly:grob-original grob))
908          ; All siblings if line-broken:
909          (siblings (if (ly:grob? orig) (ly:spanner-broken-into orig) '() )))
910     (if (>= (length siblings) 2)
911       ; We have been line-broken
912       (if (eq? (car (last-pair siblings)) grob)
913         ; Last sibling:
914         (ly:grob-set-property! grob 'Y-offset after)
915         ; Others get the before value:
916         (ly:grob-set-property! grob 'Y-offset before)
917       )
918     )
919   )
922 ffz = #(make-dynamic-script "ffz")
923 pf = #(make-dynamic-script "pf")
924 sempp = #(make-dynamic-script (markup #:line( #:with-dimensions '(0 . 0)
925 '(0 . 0) #:right-align #:normal-text #:italic "sempre" #:dynamic "pp")))
926 parenf = #(make-dynamic-script (markup #:line(#:normal-text #:italic #:fontsize 2 "(" #:dynamic "f" #:normal-text #:italic #:fontsize 2 ")" )))
927 parenp = #(make-dynamic-script (markup #:line(#:normal-text #:italic #:fontsize 2 "(" #:dynamic "p" #:normal-text #:italic #:fontsize 2 ")" )))
928 pdolce = #(make-dynamic-script (markup #:line(#:dynamic "p" #:with-dimensions '(0 . 0) '(0 . 0) #:normal-text #:italic "dolce"  )))
929 sfpdolce = #(make-dynamic-script (markup #:line(#:dynamic "sfp" #:with-dimensions '(0 . 0) '(0 . 0) #:normal-text #:italic "dolce"  )))
930 bracketf = #(make-dynamic-script (markup #:line(#:concat(#:normal-text #:fontsize 3 "[" #:dynamic "f" #:hspace 0.1 #:normal-text #:fontsize 3 "]"))))
931 bracketp = #(make-dynamic-script (markup #:line(#:concat(#:normal-text #:fontsize 2 "[" #:hspace 0.2 #:dynamic "p" #:normal-text #:fontsize 2 "]"))))
935 % cresc = #(make-music 'CrescendoEvent 'span-direction START 'crescendoSpanner 'text 'crescendoText "cresc.")
936 % endcresc =  #(make-span-event 'CrescendoEvent STOP)
937 % dim = #(make-music 'DecrescendoEvent 'span-direction START 'decrescendoSpanner 'text 'decrescendoText "dim.")
938 % enddim =  #(make-span-event 'DecrescendoEvent STOP)
939 % decresc = #(make-music 'DecrescendoEvent 'span-direction START 'decrescendoSpanner 'text 'decrescendoText "decresc.")
940 % enddecresc =  #(make-span-event 'DecrescendoEvent STOP)
942 % setCresc = {}
943 % setDecresc = {}
944 % setDim = {}
945 cresc = #(make-music 'CrescendoEvent 'span-direction START
946                      'span-type 'text 'span-text "cresc.")
947 dim = #(make-music 'DecrescendoEvent 'span-direction START
948                    'span-type 'text 'span-text "dim.")
949 decresc = #(make-music 'DecrescendoEvent 'span-direction START
950                        'span-type 'text 'span-text "decresc.")
952 newOrOldClef = #(define-music-function (parser location new old ) (string? string?)
953     (if (ly:get-option 'old-clefs) #{ \clef $old #} #{ \clef $new #})
958 %%% Thanks to "Gilles THIBAULT" <gilles.thibault@free.fr>, there is a way
959 %   to remove also the fermata from R1-\fermataMarkup: By filtering the music
960 %   and removing the corresponding events.
961 %   Documented as an LSR snippet: http://lsr.dsi.unimi.it/LSR/Item?id=372
962 #(define (filterOneEventsMarkup event)
963 ( let ( (eventname (ly:music-property  event 'name)) )
964  (not
965   (or     ;; add here event name you do NOT want
966    (eq? eventname 'MultiMeasureTextEvent)
967    (eq? eventname 'AbsoluteDynamicEvent)
968    (eq? eventname 'TextScriptEvent)
969    (eq? eventname 'ArticulationEvent)
970    (eq? eventname 'CrescendoEvent)
971    (eq? eventname 'DecrescendoEvent)
972   )
976 filterArticulations = #(define-music-function (parser location music) (ly:music?)
977   Â (music-filter filterOneEventsMarkup music)
984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 %%%%%   Tempo markings
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 rit = \markup {\italic "rit."}
993 pocorit = \markup {\italic "poco rit."}
994 ppmosso = \markup {\italic "poco più mosso"}
995 dolce = \markup {\italic "dolce."}
996 pizz = \markup {\italic "pizz."}
997 arco = \markup {\italic "arco"}
998 perd = \markup {\italic "perdend."}
1003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 %%%%%   REST COMBINATION
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1011 %% REST COMBINING, TAKEN FROM http://lsr.dsi.unimi.it/LSR/Item?id=336
1013 %% Usage:
1014 %%   \new Staff \with {
1015 %%     \override RestCollision #'positioning-done = #merge-rests-on-positioning
1016 %%   } << \somevoice \\ \othervoice >>
1017 %% or (globally):
1018 %%   \layout {
1019 %%     \context {
1020 %%       \Staff
1021 %%       \override RestCollision #'positioning-done = #merge-rests-on-positioning
1022 %%     }
1023 %%   }
1025 %% Limitations:
1026 %% - only handles two voices
1027 %% - does not handle multi-measure/whole-measure rests
1029 #(define (rest-score r)
1030   (let ((score 0)
1031   (yoff (ly:grob-property-data r 'Y-offset))
1032   (sp (ly:grob-property-data r 'staff-position)))
1033     (if (number? yoff)
1034   (set! score (+ score 2))
1035   (if (eq? yoff 'calculation-in-progress)
1036       (set! score (- score 3))))
1037     (and (number? sp)
1038    (<= 0 2 sp)
1039    (set! score (+ score 2))
1040    (set! score (- score (abs (- 1 sp)))))
1041     score))
1043 #(define (merge-rests-on-positioning grob)
1044   (let* ((can-merge #f)
1045    (elts (ly:grob-object grob 'elements))
1046    (num-elts (and (ly:grob-array? elts)
1047       (ly:grob-array-length elts)))
1048    (two-voice? (= num-elts 2)))
1049     (if two-voice?
1050   (let* ((v1-grob (ly:grob-array-ref elts 0))
1051          (v2-grob (ly:grob-array-ref elts 1))
1052          (v1-rest (ly:grob-object v1-grob 'rest))
1053          (v2-rest (ly:grob-object v2-grob 'rest)))
1054     (and
1055      (ly:grob? v1-rest)
1056      (ly:grob? v2-rest)
1057      (let* ((v1-duration-log (ly:grob-property v1-rest 'duration-log))
1058       (v2-duration-log (ly:grob-property v2-rest 'duration-log))
1059       (v1-dot (ly:grob-object v1-rest 'dot))
1060       (v2-dot (ly:grob-object v2-rest 'dot))
1061       (v1-dot-count (and (ly:grob? v1-dot)
1062              (ly:grob-property v1-dot 'dot-count -1)))
1063       (v2-dot-count (and (ly:grob? v2-dot)
1064              (ly:grob-property v2-dot 'dot-count -1))))
1065        (set! can-merge
1066        (and
1067         (number? v1-duration-log)
1068         (number? v2-duration-log)
1069         (= v1-duration-log v2-duration-log)
1070         (eq? v1-dot-count v2-dot-count)))
1071        (if can-merge
1072      ;; keep the rest that looks best:
1073      (let* ((keep-v1? (>= (rest-score v1-rest)
1074               (rest-score v2-rest)))
1075       (rest-to-keep (if keep-v1? v1-rest v2-rest))
1076       (dot-to-kill (if keep-v1? v2-dot v1-dot)))
1077        ;; uncomment if you're curious of which rest was chosen:
1078        ;;(ly:grob-set-property! v1-rest 'color green)
1079        ;;(ly:grob-set-property! v2-rest 'color blue)
1080        (ly:grob-suicide! (if keep-v1? v2-rest v1-rest))
1081        (if (ly:grob? dot-to-kill)
1082            (ly:grob-suicide! dot-to-kill))
1083        (ly:grob-set-property! rest-to-keep 'direction 0)
1084        (ly:rest::y-offset-callback rest-to-keep)))))))
1085     (if can-merge
1086   #t
1087   (ly:rest-collision::calc-positioning-done grob))))
1093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1095 %%%%%   TABLE OF CONTENTS
1096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1100 contentsTitle = "Inhalt / Contents"
1102 \paper {
1103   tocTitleMarkup = \markup \fill-line{
1104     \null
1105     \column {
1106       \override #(cons 'line-width (* 7 cm))
1107       \line{ \fill-line {\piece-title {\contentsTitle} \null }}
1108       \hspace #1
1109     }
1110     \null
1111   }
1112   tocItemMarkup = \markup \fill-line {
1113     \null
1114     \column {
1115       \override #(cons 'line-width (* 7 cm ))
1116       \line { \fill-line{\fromproperty #'toc:text \fromproperty #'toc:page }}
1117     }
1118     \null
1119   }
1123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1125 %%%%%   TITLE PAGE / HEADER
1126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1129 #(define-markup-command (when-property layout props symbol markp) (symbol? markup?)
1130   (if (chain-assoc-get symbol props)
1131       (interpret-markup layout props markp)
1132       (ly:make-stencil '()  '(1 . -1) '(1 . -1))))
1134 #(define-markup-command (vspace layout props amount) (number?)
1135   "This produces a invisible object taking vertical space."
1136   (let ((amount (* amount 3.0)))
1137     (if (> amount 0)
1138         (ly:make-stencil "" (cons -1 1) (cons 0 amount))
1139         (ly:make-stencil "" (cons -1 1) (cons amount amount)))))
1143 titlePageMarkup = \markup \abs-fontsize #10 \when-property #'header:title \column {
1144     \vspace #4
1145     \fill-line { \fontsize #8 \fromproperty #'header:composer }
1146     \vspace #1
1147     \fill-line { \fontsize #8 \fromproperty #'header:poet }
1148     \vspace #4
1149     \fill-line { \fontsize #10 \bold \fromproperty #'header:titlepagetitle }
1150     \vspace #1
1151     \fontsize #2 \when-property #'header:titlepagesubtitle {
1152       \fill-line { \fromproperty #'header:titlepagesubtitle }
1153       \vspace #1
1154     }
1155     \fill-line { \postscript #"-20 0 moveto 40 0 rlineto stroke" }
1156     \vspace #8
1157     \fill-line { \fontsize #5 \fromproperty #'header:ensemble }
1158     \vspace #0.02
1159     \fill-line { \fontsize #2 \fromproperty #'header:instruments }
1160     \vspace #9
1161     \fill-line { \fontsize #5 \fromproperty #'header:date }
1162     \vspace #1
1163     \fill-line { \fontsize #5 \fromproperty #'header:scoretype }
1164     \vspace #8
1165     \fontsize #2 \when-property #'header:enteredby {
1166       \fill-line { "Herausgegeben von: / Edited by:"}
1167       \vspace #0.
1168       \fill-line { \fromproperty #'header:enteredby }
1169     }
1170     \fill-line {
1171       \when-property #'header:arrangement \column {
1172         \vspace #8
1173         \fill-line { \fontsize #3 \fromproperty #'header:arrangement }
1174       }
1175     }
1178 titleHeaderMarkup = \markup {
1179   \override #'(baseline-skip . 3.5)
1180   \column {
1181     \fill-line {
1182       \fromproperty #'header:logo
1183       \center-column {
1184         \huge \larger \bold \larger \fromproperty #'header:title
1185         \large \smaller \bold \larger \fromproperty #'header:subtitle
1186         \smaller \bold \fromproperty #'header:subsubtitle
1187       }
1188       \bold \when-property #'header:instrument \rounded-box \fromproperty #'header:instrument
1189     }
1190     \fill-line {
1191       \with-dimensions #'( 0 . 0) #'( 0 . 1 ) \null
1192     }
1194 %     \fill-line {
1195 %       { \large \bold \fromproperty #'header:instrument }
1196 %     }
1197     \fill-line {
1198       \fromproperty #'header:poet
1199       \fromproperty #'header:composer
1200     }
1201     \fill-line {
1202       \fromproperty #'header:meter
1203       \fromproperty #'header:arranger
1204     }
1205   }
1208 titleScoreMarkup = \markup \piece-title \fromproperty #'header:piece
1210 \paper {
1211   scoreTitleMarkup = \titleScoreMarkup
1212   bookTitleMarkup = \titleHeaderMarkup
1217 %%%%%%%%%%%%%% headers and footers %%%%%%%%%%%%%%%%%%%%%%%%%%
1219 #(define (first-score-page layout props arg)
1220   (let* ((label 'first-score-page)
1221          (table (ly:output-def-lookup layout 'label-page-table))
1222          (label-page (and (list? table) (assoc label table)))
1223          (page-number (and label-page (cdr label-page)))
1224         )
1225     (if (eq? (chain-assoc-get 'page:page-number props -1) page-number)
1226       (interpret-markup layout props arg)
1227       empty-stencil)))
1229 #(define no-header-table '())
1230 thisPageNoHeader = #(define-music-function (parser location) ()
1231   (let* ((label (gensym "header")))
1232     (set! no-header-table (cons label no-header-table))
1233     (make-music 'Music
1234       'page-marker #t
1235       'page-label label)))
1238 % TODO: Use the no-header-table!
1239 #(define (is-header-page layout props arg)
1240   (let* ((page-number (chain-assoc-get 'page:page-number props -1))
1241         )
1242     ;(if (and (> page-number 2) (!= page-number 7))
1243     (if (> page-number 1)
1244       (interpret-markup layout props arg)
1245       empty-stencil)))
1247 #(define no-footer-table '())
1248 thisPageNoFooter = #(define-music-function (parser location) ()
1249   (let* ((label (gensym "footer")))
1250     (set! no-footer-table (cons label no-footer-table))
1251     (make-music 'Music
1252       'page-marker #t
1253       'page-label label)))
1255 % TODO: Use the no-footer-table!
1256 #(define (is-footer-page layout props arg)
1257   (let* ((page-number (chain-assoc-get 'page:page-number props -1))
1258          (label 'first-score-page)
1259          (table (ly:output-def-lookup layout 'label-page-table))
1260          (label-page (and (list? table) (assoc label table)))
1261          ;(page-number (and label-page (cdr label-page)))
1262         )
1263     (if (and (> page-number 1))
1264       (interpret-markup layout props arg)
1265       empty-stencil)))
1268 #(define copyright-footer-table '())
1269 thisPageCopyrightFooter = #(define-music-function (parser location) ()
1270   (let* ((label (gensym "copyrightfooter")))
1271     (set! copyright-footer-table (cons label copyright-footer-table))
1272     (make-music 'Music
1273       'page-marker #t
1274       'page-label label)))
1276 #(define copyright-pg 1)
1277 #(define (set-copyright-page page)
1278   (set! copyright-pg page)
1281 % TODO: Use the copyright-footer-table!
1282 #(define (copyright-page layout props arg)
1283     (if (= (chain-assoc-get 'page:page-number props -1) copyright-pg)
1284       (interpret-markup layout props arg)
1285       empty-stencil))
1288 \paper {
1289   oddHeaderMarkup = \markup \fill-line {
1290     %% force the header to take some space, otherwise the
1291     %% page layout becomes a complete mess.
1292     " "
1293     \on-the-fly #is-header-page \fromproperty #'header:title
1294     \on-the-fly #is-header-page \fromproperty #'page:page-number-string
1295   }
1296   evenHeaderMarkup = \markup \fill-line {
1297     \on-the-fly #is-header-page \fromproperty #'page:page-number-string
1298     \on-the-fly #is-header-page \fromproperty #'header:composer
1299     " "
1300   }
1302   oddFooterMarkup = \markup {
1303     \column {
1304       \fill-line {
1305         %% publisher header field only on title page.
1306         \on-the-fly #first-page \fromproperty #'header:publisher
1307       }
1308       \fill-line {
1309         %% copyright on the first real score page
1310         \on-the-fly #copyright-page \fromproperty #'header:copyright
1311         \on-the-fly #copyright-page \null
1312       }
1313       \fill-line {
1314         %% All other pages get the number of the edition centered
1315         \on-the-fly #is-footer-page \fromproperty #'header:scorenumber
1316       }
1317     }
1318   }
1332 % Interpret the given markup with the header fields added to the props.
1333 % This way, one can re-use the same functions (using fromproperty
1334 % #'header:field) in the header block and as top-level markup.
1336 % This function is originally copied from mark-up-title (file scm/titling.scm),
1337 % which is lilypond's internal function to handle the title markups. I needed
1338 % to replace the scopes and manually add the $defaultheader (which is internally
1339 % done in paper-book.cc before calling mark-up-title. Also, I don't extract the
1340 % markup from the header block, but use the given markup.
1342 % I'm not sure if I really need the page properties in props, too... But I
1343 % suppose it does not hurt, either.
1344 #(define-markup-command (markupWithHeader layout props markup) (markup?)
1345   "Interpret the given markup with the header fields added to the props.
1346    This way, one can re-use the same functions (using fromproperty
1347    #'header:field) in the header block and as top-level markup."
1348   (let* (
1349       ; TODO: If we are inside a score, add the score's local header block, too!
1350       ; Currently, I only use the global header block, stored in $defaultheader
1351       (scopes (list $defaultheader))
1352       (alists (map ly:module->alist scopes))
1354       (prefixed-alist
1355         (map (lambda (alist)
1356           (map (lambda (entry)
1357             (cons
1358               (string->symbol (string-append "header:" (symbol->string (car entry))))
1359               (cdr entry)))
1360             alist))
1361           alists))
1362       (props (append prefixed-alist
1363               props
1364               (layout-extract-page-properties layout)))
1365     )
1366     (interpret-markup layout props markup)
1367   )
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377 %%%%%   Equally spacing multiple columns (e.g. for translations)
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 % Credits: Nicolas Sceaux on the lilypond-user mailinglist
1382 #(define-markup-command (columns layout props args) (markup-list?)
1383    (let ((line-width (/ (chain-assoc-get 'line-width props
1384                          (ly:output-def-lookup layout 'line-width))
1385                         (max (length args) 1))))
1386      (interpret-markup layout props
1387        (make-line-markup (map (lambda (line)
1388                                 (markup #:pad-to-box `(0 . ,line-width) '(0 . 0)
1389                                   #:override `(line-width . ,line-width)
1390                                   line))
1391                                args)))))
1395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 %%%%%   SCORE (HEADER / LAYOUT) SETTINGS
1398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 startSlashedGraceMusic =  {
1403   \override Stem  #'stroke-style = #"grace"
1406 stopSlashedGraceMusic =  {
1407   \revert Stem #'stroke-style
1410 slashedGrace =
1411 #(def-grace-function startSlashedGraceMusic stopSlashedGraceMusic
1412    (_i "Create slashed graces (slashes through stems, but no slur)from the following music expression"))
1415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 %%%%%   SCORE (HEADER / LAYOUT) SETTINGS
1418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1421 \paper {
1422   left-margin = 2\cm
1423   right-margin = 1.5\cm
1424   line-width = 17.5\cm
1425 %   bottom-margin = 1.5\cm
1426   top-margin = 1.2\cm
1427 %   after-title-space = 0.5\cm
1428   ragged-right = ##f
1429   ragged-last = ##f
1430   ragged-bottom = ##f
1431   ragged-last-bottom = ##f
1433 \layout {
1434   \context {
1435     \ChoirStaff
1436     % If only one non-empty staff in a system exists, still print the backet
1437     \override SystemStartBracket #'collapse-height = #1
1438     \consists "Instrument_name_engraver"
1439   }
1440   \context {
1441     \StaffGroup
1442     % If only one non-empty staff in a system exists, still print the backet
1443     \override SystemStartBracket #'collapse-height = #1
1444     \consists "Instrument_name_engraver"
1445   }
1446   \context {
1447     \GrandStaff
1448     \override SystemStartBracket #'collapse-height = #1
1449     \consists "Instrument_name_engraver"
1450   }
1451   \context {
1452     \PianoStaff
1453 %     \override VerticalAxisGroup #'keep-fixed-while-stretching = ##t
1454   }
1455   \context {
1456     \FiguredBass
1457 %     \override VerticalAxisGroup #'keep-fixed-while-stretching = ##t
1458     \override VerticalAxisGroup #'minimum-Y-extent  = #'(0 . 1)
1459     \override VerticalAxisGroup #'padding = #0
1460   }
1461   \context {
1462     \Score
1463     % Force multi-measure rests to be written as one span
1464     \override MultiMeasureRest #'expand-limit = #3
1465     skipBars = ##t
1466     autoBeaming = ##f
1467 %     hairpinToBarline = ##f
1468     \override BarNumber #'break-visibility = #end-of-line-invisible
1469     \override BarNumber #'self-alignment-X = #0
1470     \override CombineTextScript #'avoid-slur = #'outside
1471     barNumberVisibility = #(every-nth-bar-number-visible 5)
1472     \override DynamicTextSpanner #'dash-period = #-1.0
1473     \override InstrumentSwitch #'font-size = #-1
1475     % Rest collision
1476     \override RestCollision #'positioning-done = #merge-rests-on-positioning
1477     % Auto-Accidentals: Use modern-cautionary style...
1478     extraNatural = ##f
1479     autoAccidentals = #`(Staff  ,(make-accidental-rule 'same-octave 0)
1480                                 ,(make-accidental-rule 'any-octave 0)
1481                                 ,(make-accidental-rule 'same-octave 1))
1482 %     autoCautionaries = #`(Staff ,(make-accidental-rule 'any-octave 0)
1483 %                                 ,(make-accidental-rule 'same-octave 1))
1484     printKeyCancellation = ##t
1485     quotedEventTypes = #'(StreamEvent)
1486     implicitBassFigures = #'(0 100)
1487   }
1488   \context {
1489     \RemoveEmptyStaffContext
1490   }
1491   \context {
1492     \Lyrics
1493     \override VerticalAxisGroup #'minimum-Y-extent = #'(0.5 . 0.5)
1494   }
1495   \context {
1496     \Staff
1497     \override VerticalAxisGroup #'minimum-Y-extent = #'(-1. . 3)
1498     ignoreFiguredBassRest = ##f
1499   }
1504 \layout {
1505   \context {
1506     \type "Engraver_group"
1507     \name Dynamics
1508     % So that \cresc works, for example.
1509     \alias Voice
1510     \consists "Output_property_engraver"
1512     \override VerticalAxisGroup #'minimum-Y-extent = #'(-1 . 1)
1513     pedalSustainStrings = #'("Ped." "*Ped." "*")
1514     pedalUnaCordaStrings = #'("una corda" "" "tre corde")
1516     \consists "Piano_pedal_engraver"
1517     \consists "Script_engraver"
1518     \consists "Dynamic_engraver"
1519     \consists "Text_engraver"
1521     \override TextScript #'font-size = #2
1522     \override TextScript #'font-shape = #'italic
1523     \override DynamicText #'extra-offset = #'(0 . 2.5)
1524     \override Hairpin #'extra-offset = #'(0 . 2.5)
1526     \consists "Skip_event_swallow_translator"
1528     \consists "Axis_group_engraver"
1529   }
1530   \context {
1531     \PianoStaff
1532     \accepts Dynamics
1533 %     \override VerticalAlignment #'forced-distance = #7
1534   }
1538 bracketts = ^\markup{"[Solo]"}
1539 brackettt = ^\markup{"[Tutti]"}
1540 ts = ^\markup{"t.s."}
1541 tt = ^\markup{"Tutti"}
1542 solo = ^\markup{"Solo"}
1543 tutti = ^\markup{"Tutti"}
1545 dashedSlur = -\tweak #'dash-definition #'((0 1 0.4 0.75))(
1546 dashedTie = -\tweak #'dash-definition #'((0 1 0.4 0.75))~
1548 divisi = #(define-music-function (parser location vc1 vc2) (ly:music? ly:music?)
1550   << { \voiceOne $vc1 \oneVoice} \new Voice = "divisi2" { \voiceTwo $vc2 } >>
1554 #(define-public (bracket-stencils grob)
1555   (let ((lp (grob-interpret-markup grob (markup #:fontsize 3.5 #:translate (cons -0.3 -0.5) "[")))
1556         (rp (grob-interpret-markup grob (markup #:fontsize 3.5 #:translate (cons -0.3 -0.5) "]"))))
1557     (list lp rp)))
1559 bracketify = #(define-music-function (parser loc arg) (ly:music?)
1560    (_i "Tag @var{arg} to be parenthesized.")
1562   \once \override ParenthesesItem #'stencils = #bracket-stencils
1563   \parenthesize $arg
1568 #(define-markup-command (hat layout props arg) (markup?)
1569   "Draw a hat above the given string @var{arg}."
1570   (interpret-markup layout props (markup #:combine #:raise 1.5 "^" arg)))
1574 smallFlageolet =
1575 #(let ((m (make-music 'ArticulationEvent
1576                       'articulation-type "flageolet")))
1577    (ly:music-set-property! m 'tweaks
1578      (acons 'font-size -2
1579        (ly:music-property m 'tweaks)))
1580   m)