1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 ; for define-safe-public when byte-compiling using Guile V2
20 (use-modules (scm safe-utility-defs))
22 (use-modules (ice-9 optargs))
24 ;;; ly:music-property with setter
25 ;;; (ly:music-property my-music 'elements)
26 ;;; ==> the 'elements property
27 ;;; (set! (ly:music-property my-music 'elements) value)
28 ;;; ==> set the 'elements property and return it
29 (define-public ly:music-property
30 (make-procedure-with-setter ly:music-property
31 ly:music-set-property!))
33 (define-safe-public (music-is-of-type? mus type)
34 "Does @code{mus} belong to the music class @code{type}?"
35 (memq type (ly:music-property mus 'types)))
38 (define-public ly:grob-property
39 (make-procedure-with-setter ly:grob-property
40 ly:grob-set-property!))
42 (define-public ly:grob-object
43 (make-procedure-with-setter ly:grob-object
46 (define-public ly:grob-parent
47 (make-procedure-with-setter ly:grob-parent
50 (define-public ly:prob-property
51 (make-procedure-with-setter ly:prob-property
52 ly:prob-set-property!))
54 (define-public ly:context-property
55 (make-procedure-with-setter ly:context-property
56 ly:context-set-property!))
58 (define-public (music-map function music)
59 "Apply @var{function} to @var{music} and all of the music it contains.
61 First it recurses over the children, then the function is applied to
63 (let ((es (ly:music-property music 'elements))
64 (e (ly:music-property music 'element)))
66 (set! (ly:music-property music 'elements)
67 (map (lambda (y) (music-map function y)) es)))
69 (set! (ly:music-property music 'element)
70 (music-map function e)))
73 (define-public (music-filter pred? music)
74 "Filter out music expressions that do not satisfy @var{pred?}."
76 (define (inner-music-filter pred? music)
78 (let* ((es (ly:music-property music 'elements))
79 (e (ly:music-property music 'element))
80 (as (ly:music-property music 'articulations))
81 (filtered-as (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) as)))
82 (filtered-e (if (ly:music? e)
83 (inner-music-filter pred? e)
85 (filtered-es (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) es))))
87 (set! (ly:music-property music 'element) filtered-e))
89 (set! (ly:music-property music 'elements) filtered-es))
91 (set! (ly:music-property music 'articulations) filtered-as))
92 ;; if filtering emptied the expression, we remove it completely.
93 (if (or (not (pred? music))
94 (and (eq? filtered-es '()) (not (ly:music? e))
95 (or (not (eq? es '()))
100 (set! music (inner-music-filter pred? music))
101 (if (ly:music? music)
103 (make-music 'Music))) ;must return music.
105 (define*-public (display-music music #:optional (port (current-output-port)))
106 "Display music, not done with @code{music-map} for clarity of
109 (display ": { " port)
110 (let ((es (ly:music-property music 'elements))
111 (e (ly:music-property music 'element)))
112 (display (ly:music-mutable-properties music) port)
114 (begin (display "\nElements: {\n" port)
115 (for-each (lambda (m) (display-music m port)) es)
116 (display "}\n" port)))
119 (display "\nChild:" port)
120 (display-music e port))))
121 (display " }\n" port)
125 ;;; A scheme music pretty printer
127 (define (markup-expression->make-markup markup-expression)
128 "Transform `markup-expression' into an equivalent, hopefuly readable, scheme expression.
130 \\markup \\bold \\italic hello
132 (markup #:line (#:bold (#:italic (#:simple \"hello\"))))"
133 (define (proc->command-keyword proc)
134 "Return a keyword, eg. `#:bold', from the `proc' function, eg. #<procedure bold-markup (layout props arg)>"
135 (let ((cmd-markup (symbol->string (procedure-name proc))))
136 (symbol->keyword (string->symbol (substring cmd-markup 0 (- (string-length cmd-markup)
137 (string-length "-markup")))))))
138 (define (transform-arg arg)
139 (cond ((and (pair? arg) (markup? (car arg))) ;; a markup list
140 (apply append (map inner-markup->make-markup arg)))
141 ((and (not (string? arg)) (markup? arg)) ;; a markup
142 (inner-markup->make-markup arg))
144 (music->make-music arg))))
145 (define (inner-markup->make-markup mrkup)
148 (let ((cmd (proc->command-keyword (car mrkup)))
149 (args (map transform-arg (cdr mrkup))))
152 (if (string? markup-expression)
154 `(markup ,@(inner-markup->make-markup markup-expression))))
156 (define-public (music->make-music obj)
157 "Generate an expression that, once evaluated, may return an object
158 equivalent to @var{obj}, that is, for a music expression, a
159 @code{(make-music ...)} form."
160 (cond (;; markup expression
162 (markup-expression->make-markup obj))
166 ',(ly:music-property obj 'name)
167 ,@(apply append (map (lambda (prop)
169 ,(music->make-music (cdr prop))))
170 (remove (lambda (prop)
171 (eqv? (car prop) 'origin))
172 (ly:music-mutable-properties obj))))))
175 `(ly:make-moment ,(ly:moment-main-numerator obj)
176 ,(ly:moment-main-denominator obj)
177 ,(ly:moment-grace-numerator obj)
178 ,(ly:moment-grace-denominator obj)))
181 `(ly:make-duration ,(ly:duration-log obj)
182 ,(ly:duration-dot-count obj)
183 ,(car (ly:duration-factor obj))
184 ,(cdr (ly:duration-factor obj))))
187 `(ly:make-pitch ,(ly:pitch-octave obj)
188 ,(ly:pitch-notename obj)
189 ,(ly:pitch-alteration obj)))
192 (or (procedure-name obj) obj))
193 (;; a symbol (avoid having an unquoted symbol)
196 (;; an empty list (avoid having an unquoted empty list)
201 `(list ,@(map music->make-music obj)))
204 `(cons ,(music->make-music (car obj))
205 ,(music->make-music (cdr obj))))
209 (use-modules (ice-9 pretty-print))
210 (define*-public (display-scheme-music obj #:optional (port (current-output-port)))
211 "Displays `obj', typically a music expression, in a friendly fashion,
212 which often can be read back in order to generate an equivalent expression."
213 (pretty-print (music->make-music obj) port)
217 ;;; Scheme music expression --> Lily-syntax-using string translator
219 (use-modules (srfi srfi-39)
222 (define*-public (display-lily-music expr parser #:optional (port (current-output-port))
223 #:key force-duration)
224 "Display the music expression using LilyPond syntax"
225 (memoize-clef-names supported-clefs)
226 (parameterize ((*indent* 0)
227 (*previous-duration* (ly:make-duration 2))
228 (*force-duration* force-duration))
229 (display (music->lily-string expr parser) port)
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 (define-public (shift-one-duration-log music shift dot)
235 "Add @var{shift} to @code{duration-log} of @code{'duration} in
236 @var{music} and optionally @var{dot} to any note encountered.
237 The number of dots in the shifted music may not be less than zero."
238 (let ((d (ly:music-property music 'duration)))
240 (let* ((cp (ly:duration-factor d))
241 (nd (ly:make-duration
242 (+ shift (ly:duration-log d))
243 (max 0 (+ dot (ly:duration-dot-count d)))
246 (set! (ly:music-property music 'duration) nd)))
249 (define-public (shift-duration-log music shift dot)
250 (music-map (lambda (x) (shift-one-duration-log x shift dot))
253 (define-public (make-repeat name times main alts)
254 "Create a repeat music expression, with all properties initialized
256 (define (first-note-duration music)
257 "Finds the duration of the first NoteEvent by searching depth-first
259 ;; NoteEvent or a non-expanded chord-repetition
260 ;; We just take anything that actually sports an announced duration.
261 (if (ly:duration? (ly:music-property music 'duration))
262 (ly:music-property music 'duration)
263 (let loop ((elts (if (ly:music? (ly:music-property music 'element))
264 (list (ly:music-property music 'element))
265 (ly:music-property music 'elements))))
267 (let ((dur (first-note-duration (car elts))))
268 (if (ly:duration? dur)
270 (loop (cdr elts))))))))
272 (let ((talts (if (< times (length alts))
274 (ly:warning (_ "More alternatives than repeats. Junking excess alternatives"))
277 (r (make-repeated-music name)))
278 (set! (ly:music-property r 'element) main)
279 (set! (ly:music-property r 'repeat-count) (max times 1))
280 (set! (ly:music-property r 'elements) talts)
281 (if (and (equal? name "tremolo")
282 (pair? (extract-named-music main '(EventChord NoteEvent))))
283 ;; This works for single-note and multi-note tremolos!
284 (let* ((children (if (music-is-of-type? main 'sequential-music)
285 ;; \repeat tremolo n { ... }
286 (length (extract-named-music main '(EventChord
288 ;; \repeat tremolo n c4
290 ;; # of dots is equal to the 1 in bitwise representation (minus 1)!
291 (dots (1- (logcount (* times children))))
292 ;; The remaining missing multiplicator to scale the notes by
294 (mult (/ (* times children (ash 1 dots)) (1- (ash 2 dots))))
295 (shift (- (ly:intlog2 (floor mult))))
296 (note-duration (first-note-duration r))
297 (duration-log (if (ly:duration? note-duration)
298 (ly:duration-log note-duration)
300 (tremolo-type (ash 1 duration-log)))
301 (set! (ly:music-property r 'tremolo-type) tremolo-type)
302 (if (not (and (integer? mult) (= (logcount mult) 1)))
305 (ly:format (_ "invalid tremolo repeat count: ~a") times)))
306 ;; Adjust the time of the notes
307 (ly:music-compress r (ly:make-moment 1 children))
308 ;; Adjust the displayed note durations
309 (shift-duration-log r shift dots))
312 (define (calc-repeat-slash-count music)
313 "Given the child-list @var{music} in @code{PercentRepeatMusic},
314 calculate the number of slashes based on the durations. Returns @code{0}
315 if durations in @var{music} vary, allowing slash beats and double-percent
316 beats to be distinguished."
317 (let* ((durs (map duration-of-note
318 (extract-named-music music '(EventChord NoteEvent
319 RestEvent SkipEvent))))
320 (first-dur (car durs)))
322 (if (every (lambda (d) (equal? d first-dur)) durs)
323 (max (- (ly:duration-log first-dur) 2) 1)
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 (define-public (note-to-cluster music)
330 "Replace @code{NoteEvents} by @code{ClusterNoteEvents}."
331 (if (eq? (ly:music-property music 'name) 'NoteEvent)
332 (make-music 'ClusterNoteEvent
333 'pitch (ly:music-property music 'pitch)
334 'duration (ly:music-property music 'duration))
337 (define-public (notes-to-clusters music)
338 (music-map note-to-cluster music))
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 (define-public (unfold-repeats music)
344 "Replace all repeats with unfolded repeats."
346 (let ((es (ly:music-property music 'elements))
347 (e (ly:music-property music 'element)))
349 (if (music-is-of-type? music 'repeated-music)
350 (let* ((props (ly:music-mutable-properties music))
351 (old-name (ly:music-property music 'name))
352 (flattened (flatten-alist props)))
353 (set! music (apply make-music (cons 'UnfoldedRepeatedMusic
356 (if (and (equal? old-name 'TremoloRepeatedMusic)
357 (pair? (extract-named-music e '(EventChord NoteEvent))))
358 ;; This works for single-note and multi-note tremolos!
359 (let* ((children (if (music-is-of-type? e 'sequential-music)
360 ;; \repeat tremolo n { ... }
361 (length (extract-named-music e '(EventChord
363 ;; \repeat tremolo n c4
365 (times (ly:music-property music 'repeat-count))
367 ;; # of dots is equal to the 1 in bitwise representation (minus 1)!
368 (dots (1- (logcount (* times children))))
369 ;; The remaining missing multiplicator to scale the notes by
371 (mult (/ (* times children (ash 1 dots)) (1- (ash 2 dots))))
372 (shift (- (ly:intlog2 (floor mult)))))
374 ;; Adjust the time of the notes
375 (ly:music-compress music (ly:make-moment children 1))
376 ;; Adjust the displayed note durations
377 (shift-duration-log music (- shift) (- dots))))))
380 (set! (ly:music-property music 'elements)
381 (map unfold-repeats es)))
383 (set! (ly:music-property music 'element)
387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ;; property setting music objs.
390 (define-public (make-grob-property-set grob gprop val)
391 "Make a @code{Music} expression that sets @var{gprop} to @var{val} in
392 @var{grob}. Does a pop first, i.e., this is not an override."
393 (make-music 'OverrideProperty
399 (define-public (make-grob-property-override grob gprop val)
400 "Make a @code{Music} expression that overrides @var{gprop} to @var{val}
402 (make-music 'OverrideProperty
407 (define-public (make-grob-property-revert grob gprop)
408 "Revert the grob property @var{gprop} for @var{grob}."
409 (make-music 'RevertProperty
411 'grob-property gprop))
413 (define direction-polyphonic-grobs
414 '(AccidentalSuggestion
431 (define-safe-public (make-voice-props-set n)
432 (make-sequential-music
434 (map (lambda (x) (make-grob-property-set x 'direction
436 direction-polyphonic-grobs)
438 (make-property-set 'graceSettings
439 ;; TODO: take this from voicedGraceSettings or similar.
440 '((Voice Stem font-size -3)
441 (Voice Flag font-size -3)
442 (Voice NoteHead font-size -3)
443 (Voice TabNoteHead font-size -4)
444 (Voice Dots font-size -3)
445 (Voice Stem length-fraction 0.8)
446 (Voice Stem no-stem-extend #t)
447 (Voice Beam beam-thickness 0.384)
448 (Voice Beam length-fraction 0.8)
449 (Voice Accidental font-size -4)
450 (Voice AccidentalCautionary font-size -4)
451 (Voice Script font-size -3)
452 (Voice Fingering font-size -8)
453 (Voice StringNumber font-size -8)))
455 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
456 (make-grob-property-set 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))))))
458 (define-safe-public (make-voice-props-override n)
459 (make-sequential-music
461 (map (lambda (x) (make-grob-property-override x 'direction
463 direction-polyphonic-grobs)
465 (make-property-set 'graceSettings
466 ;; TODO: take this from voicedGraceSettings or similar.
467 '((Voice Stem font-size -3)
468 (Voice Flag font-size -3)
469 (Voice NoteHead font-size -3)
470 (Voice TabNoteHead font-size -4)
471 (Voice Dots font-size -3)
472 (Voice Stem length-fraction 0.8)
473 (Voice Stem no-stem-extend #t)
474 (Voice Beam beam-thickness 0.384)
475 (Voice Beam length-fraction 0.8)
476 (Voice Accidental font-size -4)
477 (Voice AccidentalCautionary font-size -4)
478 (Voice Script font-size -3)
479 (Voice Fingering font-size -8)
480 (Voice StringNumber font-size -8)))
482 (make-grob-property-override 'NoteColumn 'horizontal-shift (quotient n 2))
483 (make-grob-property-override 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))))))
485 (define-safe-public (make-voice-props-revert)
486 (make-sequential-music
488 (map (lambda (x) (make-grob-property-revert x 'direction))
489 direction-polyphonic-grobs)
490 (list (make-property-unset 'graceSettings)
491 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
492 (make-grob-property-revert 'MultiMeasureRest 'staff-position)))))
495 (define-safe-public (context-spec-music m context #:optional id)
496 "Add \\context CONTEXT = ID to M."
497 (let ((cm (make-music 'ContextSpeccedMusic
499 'context-type context)))
501 (set! (ly:music-property cm 'context-id) id))
504 (define-public (descend-to-context m context)
505 "Like @code{context-spec-music}, but only descending."
506 (let ((cm (context-spec-music m context)))
507 (ly:music-set-property! cm 'descend-only #t)
510 (define-public (make-non-relative-music mus)
511 (make-music 'UnrelativableMusic
514 (define-public (make-apply-context func)
515 (make-music 'ApplyContext
518 (define-public (make-sequential-music elts)
519 (make-music 'SequentialMusic
522 (define-public (make-simultaneous-music elts)
523 (make-music 'SimultaneousMusic
526 (define-safe-public (make-event-chord elts)
527 (make-music 'EventChord
530 (define-public (make-skip-music dur)
531 (make-music 'SkipMusic
534 (define-public (make-grace-music music)
535 (make-music 'GraceMusic
541 (define-public (make-multi-measure-rest duration location)
542 (make-music 'MultiMeasureRestMusic
546 (define-public (make-property-set sym val)
547 (make-music 'PropertySet
551 (define-public (make-property-unset sym)
552 (make-music 'PropertyUnset
555 (define-safe-public (make-articulation name)
556 (make-music 'ArticulationEvent
557 'articulation-type name))
559 (define-public (make-lyric-event string duration)
560 (make-music 'LyricEvent
564 (define-safe-public (make-span-event type span-dir)
566 'span-direction span-dir))
568 (define-public (override-head-style heads style)
569 "Override style for @var{heads} to @var{style}."
570 (make-sequential-music
573 (make-grob-property-override h 'style style))
575 (list (make-grob-property-override heads 'style style)))))
577 (define-public (revert-head-style heads)
578 "Revert style for @var{heads}."
579 (make-sequential-music
582 (make-grob-property-revert h 'style))
584 (list (make-grob-property-revert heads 'style)))))
586 (define-public (style-note-heads heads style music)
587 "Set @var{style} for all @var{heads} in @var{music}. Works both
588 inside of and outside of chord construct."
589 ;; are we inside a <...>?
590 (if (eq? (ly:music-property music 'name) 'NoteEvent)
591 ;; yes -> use a tweak
593 (set! (ly:music-property music 'tweaks)
594 (acons 'style style (ly:music-property music 'tweaks)))
596 ;; not in <...>, so use overrides
597 (make-sequential-music
599 (override-head-style heads style)
601 (revert-head-style heads)))))
603 (define-public (set-mus-properties! m alist)
604 "Set all of @var{alist} as properties of @var{m}."
607 (set! (ly:music-property m (caar alist)) (cdar alist))
608 (set-mus-properties! m (cdr alist)))))
610 (define-public (music-separator? m)
611 "Is @var{m} a separator?"
612 (let ((ts (ly:music-property m 'types)))
613 (memq 'separator ts)))
615 ;;; expanding repeat chords
616 (define-public (copy-repeat-chord original-chord repeat-chord duration
618 "Copies all events in @var{event-types} (be sure to include
619 @code{rhythmic-events}) from @var{original-chord} over to
620 @var{repeat-chord} with their articulations filtered as well. Any
621 duration is replaced with the specified @var{duration}."
622 ;; First remove everything from event-types that can already be
623 ;; found in the repeated chord. We don't need to look for
624 ;; articulations on individual events since they can't actually get
625 ;; into a repeat chord given its input syntax.
627 (define (keep-element? m)
628 (any (lambda (t) (music-is-of-type? m t))
630 (define origin (ly:music-property repeat-chord 'origin #f))
631 (define (set-origin! l)
633 (for-each (lambda (m) (set! (ly:music-property m 'origin) origin)) l))
638 (for-each (lambda (e)
639 (for-each (lambda (x)
640 (set! event-types (delq x event-types)))
641 (ly:music-property e 'types)))
642 (ly:music-property repeat-chord field)))
643 '(elements articulations))
645 ;; now treat the elements
646 (set! (ly:music-property repeat-chord 'elements)
648 (set-origin! (ly:music-deep-copy
649 (filter keep-element?
650 (ly:music-property original-chord
654 (let ((arts (ly:music-property m 'articulations)))
656 (set! (ly:music-property m 'articulations)
657 (set-origin! (filter! keep-element? arts))))
658 (if (ly:duration? (ly:music-property m 'duration))
659 (set! (ly:music-property m 'duration) duration))))
661 (append! elts (ly:music-property repeat-chord 'elements))))
662 (let ((arts (filter keep-element?
663 (ly:music-property original-chord
666 (set! (ly:music-property repeat-chord 'articulations)
668 (set-origin! (ly:music-deep-copy arts))
669 (ly:music-property repeat-chord 'articulations))))))
672 (define-public (expand-repeat-chords! event-types music)
673 "Walks through @var{music} and fills repeated chords (notable by
674 having a duration in @code{duration}) with the notes from their
675 respective predecessor chord."
676 (let loop ((music music) (last-chord #f))
677 (if (music-is-of-type? music 'event-chord)
678 (let ((chord-repeat (ly:music-property music 'duration)))
680 ((not (ly:duration? chord-repeat))
681 (if (any (lambda (m) (ly:duration?
682 (ly:music-property m 'duration)))
683 (ly:music-property music 'elements))
687 (set! (ly:music-property music 'duration) '())
688 (copy-repeat-chord last-chord music chord-repeat event-types)
691 (ly:music-warning music (_ "Bad chord repetition"))
693 (let ((elt (ly:music-property music 'element)))
694 (fold loop (if (ly:music? elt) (loop elt last-chord) last-chord)
695 (ly:music-property music 'elements)))))
698 ;;; splitting chords into voices.
699 (define (voicify-list lst number)
700 "Make a list of Musics.
702 voicify-list :: [ [Music ] ] -> number -> [Music]
703 LST is a list music-lists.
705 NUMBER is 0-base, i.e., Voice=1 (upstems) has number 0.
709 (cons (context-spec-music
710 (make-sequential-music
711 (list (make-voice-props-set number)
712 (make-simultaneous-music (car lst))))
713 'Bottom (number->string (1+ number)))
714 (voicify-list (cdr lst) (1+ number)))))
716 (define (voicify-chord ch)
717 "Split the parts of a chord into different Voices using separator"
718 (let ((es (ly:music-property ch 'elements)))
719 (set! (ly:music-property ch 'elements)
720 (voicify-list (split-list-by-separator es music-separator?) 0))
723 (define-public (voicify-music m)
724 "Recursively split chords that are separated with @code{\\\\}."
725 (if (not (ly:music? m))
726 (ly:error (_ "music expected: ~S") m))
727 (let ((es (ly:music-property m 'elements))
728 (e (ly:music-property m 'element)))
731 (set! (ly:music-property m 'elements) (map voicify-music es)))
733 (set! (ly:music-property m 'element) (voicify-music e)))
734 (if (and (equal? (ly:music-property m 'name) 'SimultaneousMusic)
735 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
736 (set! m (context-spec-music (voicify-chord m) 'Staff)))
739 (define-public (empty-music)
742 ;; Make a function that checks score element for being of a specific type.
743 (define-public (make-type-checker symbol)
745 (grob::has-interface elt symbol)))
747 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
749 (set! (ly:grob-property grob sym) val)))
752 (define-public ((set-output-property grob-name symbol val) grob grob-c context)
754 @code{\\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))}"
755 (let ((meta (ly:grob-property grob 'meta)))
756 (if (equal? (assoc-get 'name meta) grob-name)
757 (set! (ly:grob-property grob symbol) val))))
760 (define-public (skip->rest mus)
761 "Replace @var{mus} by @code{RestEvent} of the same duration if it is a
762 @code{SkipEvent}. Useful for extracting parts from crowded scores."
764 (if (memq (ly:music-property mus 'name) '(SkipEvent SkipMusic))
765 (make-music 'RestEvent 'duration (ly:music-property mus 'duration))
769 (define-public (music-has-type music type)
770 (memq type (ly:music-property music 'types)))
772 (define-public (music-clone music)
773 (define (alist->args alist acc)
776 (alist->args (cdr alist)
777 (cons (caar alist) (cons (cdar alist) acc)))))
781 (ly:music-property music 'name)
782 (alist->args (ly:music-mutable-properties music) '())))
784 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
785 ;; warn for bare chords at start.
787 (define-public (ly:music-message music msg)
788 (let ((ip (ly:music-property music 'origin)))
789 (if (ly:input-location? ip)
790 (ly:input-message ip msg)
793 (define-public (ly:music-warning music msg)
794 (let ((ip (ly:music-property music 'origin)))
795 (if (ly:input-location? ip)
796 (ly:input-warning ip msg)
799 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
801 ;; setting stuff for grace context.
804 (define (vector-extend v x)
805 "Make a new vector consisting of V, with X added to the end."
806 (let* ((n (vector-length v))
807 (nv (make-vector (+ n 1) '())))
808 (vector-move-left! v 0 n nv 0)
812 (define (vector-map f v)
813 "Map F over V. This function returns nothing."
814 (do ((n (vector-length v))
817 (f (vector-ref v i))))
819 (define (vector-reverse-map f v)
820 "Map F over V, N to 0 order. This function returns nothing."
821 (do ((i (- (vector-length v) 1) (- i 1)))
823 (f (vector-ref v i))))
825 (define-public (add-grace-property context-name grob sym val)
826 "Set @var{sym}=@var{val} for @var{grob} in @var{context-name}."
827 (define (set-prop context)
828 (let* ((where (ly:context-property-where-defined context 'graceSettings))
829 (current (ly:context-property where 'graceSettings))
830 (new-settings (append current
831 (list (list context-name grob sym val)))))
832 (ly:context-set-property! where 'graceSettings new-settings)))
833 (context-spec-music (make-apply-context set-prop) 'Voice))
835 (define-public (remove-grace-property context-name grob sym)
836 "Remove all @var{sym} for @var{grob} in @var{context-name}."
837 (define (sym-grob-context? property sym grob context-name)
838 (and (eq? (car property) context-name)
839 (eq? (cadr property) grob)
840 (eq? (caddr property) sym)))
841 (define (delete-prop context)
842 (let* ((where (ly:context-property-where-defined context 'graceSettings))
843 (current (ly:context-property where 'graceSettings))
844 (prop-settings (filter
845 (lambda(x) (sym-grob-context? x sym grob context-name))
847 (new-settings current))
849 (set! new-settings (delete x new-settings)))
851 (ly:context-set-property! where 'graceSettings new-settings)))
852 (context-spec-music (make-apply-context delete-prop) 'Voice))
856 (defmacro-public def-grace-function (start stop . docstring)
857 "Helper macro for defining grace music"
858 `(define-music-function (parser location music) (ly:music?)
860 (make-music 'GraceMusic
862 'element (make-music 'SequentialMusic
863 'elements (list (ly:music-deep-copy ,start)
865 (ly:music-deep-copy ,stop))))))
867 (defmacro-public define-syntax-function (type args signature . body)
868 "Helper macro for `ly:make-music-function'.
870 (define-syntax-function (result-type? parser location arg1 arg2 ...) (result-type? arg1-type arg2-type ...)
873 argX-type can take one of the forms @code{predicate?} for mandatory
874 arguments satisfying the predicate, @code{(predicate?)} for optional
875 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
876 value)}} for optional parameters with a specified default
877 value (evaluated at definition time). An optional parameter can be
878 omitted in a call only when it can't get confused with a following
879 parameter of different type.
881 Predicates with syntactical significance are @code{ly:pitch?},
882 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
883 predicates require the parameter to be entered as Scheme expression.
885 @code{result-type?} can specify a default in the same manner as
886 predicates, to be used in case of a type error in arguments or
889 (set! signature (map (lambda (pred)
892 ,(and (pair? (cdr pred)) (cadr pred)))
894 (cons type signature)))
895 (if (and (pair? body) (pair? (car body)) (eqv? '_i (caar body)))
896 ;; When the music function definition contains a i10n doc string,
897 ;; (_i "doc string"), keep the literal string only
898 (let ((docstring (cadar body))
900 `(ly:make-music-function (list ,@signature)
904 `(ly:make-music-function (list ,@signature)
908 (defmacro-public define-music-function rest
909 "Defining macro returning music functions.
911 (define-music-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
914 argX-type can take one of the forms @code{predicate?} for mandatory
915 arguments satisfying the predicate, @code{(predicate?)} for optional
916 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
917 value)}} for optional parameters with a specified default
918 value (evaluated at definition time). An optional parameter can be
919 omitted in a call only when it can't get confused with a following
920 parameter of different type.
922 Predicates with syntactical significance are @code{ly:pitch?},
923 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
924 predicates require the parameter to be entered as Scheme expression.
926 Must return a music expression. The @code{origin} is automatically
927 set to the @code{location} parameter."
929 `(define-syntax-function (ly:music? (make-music 'Music 'void #t)) ,@rest))
932 (defmacro-public define-scheme-function rest
933 "Defining macro returning Scheme functions.
935 (define-scheme-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
938 argX-type can take one of the forms @code{predicate?} for mandatory
939 arguments satisfying the predicate, @code{(predicate?)} for optional
940 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
941 value)}} for optional parameters with a specified default
942 value (evaluated at definition time). An optional parameter can be
943 omitted in a call only when it can't get confused with a following
944 parameter of different type.
946 Predicates with syntactical significance are @code{ly:pitch?},
947 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
948 predicates require the parameter to be entered as Scheme expression.
950 Can return arbitrary expressions. If a music expression is returned,
951 its @code{origin} is automatically set to the @code{location}
954 `(define-syntax-function scheme? ,@rest))
956 (defmacro-public define-void-function rest
957 "This defines a Scheme function like @code{define-scheme-function} with
958 void return value (i.e., what most Guile functions with `unspecified'
959 value return). Use this when defining functions for executing actions
960 rather than returning values, to keep Lilypond from trying to interpret
962 `(define-syntax-function (void? *unspecified*) ,@rest *unspecified*))
964 (defmacro-public define-event-function rest
965 "Defining macro returning event functions.
967 (define-event-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
970 argX-type can take one of the forms @code{predicate?} for mandatory
971 arguments satisfying the predicate, @code{(predicate?)} for optional
972 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
973 value)}} for optional parameters with a specified default
974 value (evaluated at definition time). An optional parameter can be
975 omitted in a call only when it can't get confused with a following
976 parameter of different type.
978 Predicates with syntactical significance are @code{ly:pitch?},
979 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
980 predicates require the parameter to be entered as Scheme expression.
982 Must return an event expression. The @code{origin} is automatically
983 set to the @code{location} parameter."
985 `(define-syntax-function (ly:event? (make-music 'Event 'void #t)) ,@rest))
987 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
989 (define-public (cue-substitute quote-music)
990 "Must happen after @code{quote-substitute}."
992 (if (vector? (ly:music-property quote-music 'quoted-events))
993 (let* ((dir (ly:music-property quote-music 'quoted-voice-direction))
994 (clef (ly:music-property quote-music 'quoted-music-clef #f))
995 (main-voice (case dir ((1) 1) ((-1) 0) (else #f)))
996 (cue-voice (and main-voice (- 1 main-voice)))
997 (main-music (ly:music-property quote-music 'element))
998 (return-value quote-music))
1001 (set! (ly:music-property quote-music 'element)
1002 (make-sequential-music
1004 (make-voice-props-override main-voice)
1006 (make-voice-props-revert)))))
1008 ;; if we have stem dirs, change both quoted and main music
1009 ;; to have opposite stems.
1011 ;; cannot context-spec Quote-music, since context
1012 ;; for the quotes is determined in the iterator.
1014 (make-sequential-music
1017 (and clef (make-cue-clef-set clef))
1019 ;; Need to establish CueVoice context even in #CENTER case
1022 (make-voice-props-override cue-voice)
1023 (make-music 'Music))
1028 (make-voice-props-revert) 'CueVoice "cue"))
1029 (and clef (make-cue-clef-unset))))))
1032 (define-public ((quote-substitute quote-tab) music)
1033 (let* ((quoted-name (ly:music-property music 'quoted-music-name))
1034 (quoted-vector (and (string? quoted-name)
1035 (hash-ref quote-tab quoted-name #f))))
1038 (if (string? quoted-name)
1039 (if (vector? quoted-vector)
1041 (set! (ly:music-property music 'quoted-events) quoted-vector)
1042 (set! (ly:music-property music 'iterator-ctor)
1043 ly:quote-iterator::constructor))
1044 (ly:music-warning music (ly:format (_ "cannot find quoted music: `~S'") quoted-name))))
1048 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1049 ;; switch it on here, so parsing and init isn't checked (too slow!)
1051 ;; automatic music transformations.
1053 (define (switch-on-debugging m)
1054 (if (defined? 'set-debug-cell-accesses!)
1055 (set-debug-cell-accesses! 15000))
1058 (define (music-check-error music)
1061 (if (and (ly:music? m)
1062 (eq? (ly:music-property m 'error-found) #t))
1065 (for-each signal (ly:music-property music 'elements))
1066 (signal (ly:music-property music 'element))
1069 (set! (ly:music-property music 'error-found) #t))
1072 (define (precompute-music-length music)
1073 (set! (ly:music-property music 'length)
1074 (ly:music-length music))
1077 (define-public (make-duration-of-length moment)
1078 "Make duration of the given @code{moment} length."
1079 (ly:make-duration 0 0
1080 (ly:moment-main-numerator moment)
1081 (ly:moment-main-denominator moment)))
1083 (define (make-skipped moment bool)
1084 "Depending on BOOL, set or unset skipTypesetting,
1085 then make SkipMusic of the given MOMENT length, and
1086 then revert skipTypesetting."
1087 (make-sequential-music
1089 (context-spec-music (make-property-set 'skipTypesetting bool)
1091 (make-music 'SkipMusic 'duration
1092 (make-duration-of-length moment))
1093 (context-spec-music (make-property-set 'skipTypesetting (not bool))
1096 (define (skip-as-needed music parser)
1098 << { \\set skipTypesetting = ##f
1099 LENGTHOF(\\showFirstLength)
1100 \\set skipTypesetting = ##t
1101 LENGTHOF(\\showLastLength) }
1105 When only showFirstLength is set,
1106 the 'length property of the music is
1107 overridden to speed up compiling."
1109 ((show-last (ly:parser-lookup parser 'showLastLength))
1110 (show-first (ly:parser-lookup parser 'showFirstLength))
1111 (show-last-length (and (ly:music? show-last)
1112 (ly:music-length show-last)))
1113 (show-first-length (and (ly:music? show-first)
1114 (ly:music-length show-first)))
1115 (orig-length (ly:music-length music)))
1117 ;;FIXME: if using either showFirst- or showLastLength,
1118 ;; make sure that skipBars is not set.
1122 ;; both properties may be set.
1123 ((and show-first-length show-last-length)
1125 ((skip-length (ly:moment-sub orig-length show-last-length)))
1126 (make-simultaneous-music
1128 (make-sequential-music
1130 (make-skipped skip-length #t)
1131 ;; let's draw a separator between the beginning and the end
1132 (context-spec-music (make-property-set 'whichBar "||")
1134 (make-skipped show-first-length #f)
1137 ;; we may only want to print the last length
1140 ((skip-length (ly:moment-sub orig-length show-last-length)))
1141 (make-simultaneous-music
1143 (make-skipped skip-length #t)
1146 ;; we may only want to print the beginning; in this case
1147 ;; only the first length will be processed (much faster).
1149 ;; the first length must not exceed the original length.
1150 (if (ly:moment<? show-first-length orig-length)
1151 (set! (ly:music-property music 'length)
1158 (define-public toplevel-music-functions
1160 (lambda (music parser) (expand-repeat-chords!
1161 (cons 'rhythmic-event
1162 (ly:parser-lookup parser '$chord-repeat-events))
1164 (lambda (music parser) (voicify-music music))
1165 (lambda (x parser) (music-map music-check-error x))
1166 (lambda (x parser) (music-map precompute-music-length x))
1167 (lambda (music parser)
1169 (music-map (quote-substitute (ly:parser-lookup parser 'musicQuotes)) music))
1171 ;; switch-on-debugging
1172 (lambda (x parser) (music-map cue-substitute x))
1175 (skip-as-needed x parser)
1179 ;;; general purpose music functions
1181 (define (shift-octave pitch octave-shift)
1182 (_i "Add @var{octave-shift} to the octave of @var{pitch}.")
1184 (+ (ly:pitch-octave pitch) octave-shift)
1185 (ly:pitch-notename pitch)
1186 (ly:pitch-alteration pitch)))
1192 (define (apply-durations lyric-music durations)
1193 (define (apply-duration music)
1194 (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
1195 (ly:duration? (ly:music-property music 'duration)))
1197 (set! (ly:music-property music 'duration) (car durations))
1198 (set! durations (cdr durations)))))
1200 (music-map apply-duration lyric-music))
1203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1206 (define (recent-enough? bar-number alteration-def laziness)
1207 (or (number? alteration-def)
1208 (equal? laziness #t)
1209 (<= bar-number (+ (cadr alteration-def) laziness))))
1211 (define (accidental-invalid? alteration-def)
1212 "Checks an alteration entry for being invalid.
1214 Non-key alterations are invalidated when tying into the next bar or
1215 when there is a clef change, since neither repetition nor cancellation
1216 can be omitted when the same note occurs again.
1218 Returns @code{#f} or the reason for the invalidation, a symbol."
1219 (let* ((def (if (pair? alteration-def)
1220 (car alteration-def)
1222 (and (symbol? def) def)))
1224 (define (extract-alteration alteration-def)
1225 (cond ((number? alteration-def)
1227 ((pair? alteration-def)
1228 (car alteration-def))
1231 (define (check-pitch-against-signature context pitch barnum laziness octaveness)
1232 "Checks the need for an accidental and a @q{restore} accidental against
1233 @code{localKeySignature}. The @var{laziness} is the number of measures
1234 for which reminder accidentals are used (i.e., if @var{laziness} is zero,
1235 only cancel accidentals in the same measure; if @var{laziness} is three,
1236 we cancel accidentals up to three measures after they first appear.
1237 @var{octaveness} is either @code{'same-octave} or @code{'any-octave} and
1238 specifies whether accidentals should be canceled in different octaves."
1239 (let* ((ignore-octave (cond ((equal? octaveness 'any-octave) #t)
1240 ((equal? octaveness 'same-octave) #f)
1242 (ly:warning (_ "Unknown octaveness type: ~S ") octaveness)
1243 (ly:warning (_ "Defaulting to 'any-octave."))
1245 (key-sig (ly:context-property context 'keySignature))
1246 (local-key-sig (ly:context-property context 'localKeySignature))
1247 (notename (ly:pitch-notename pitch))
1248 (octave (ly:pitch-octave pitch))
1249 (pitch-handle (cons octave notename))
1251 (need-accidental #f)
1252 (previous-alteration #f)
1253 (from-other-octaves #f)
1254 (from-same-octave (assoc-get pitch-handle local-key-sig))
1255 (from-key-sig (or (assoc-get notename local-key-sig)
1257 ;; If no key signature match is found from localKeySignature, we may have a custom
1258 ;; type with octave-specific entries of the form ((octave . pitch) alteration)
1259 ;; instead of (pitch . alteration). Since this type cannot coexist with entries in
1260 ;; localKeySignature, try extracting from keySignature instead.
1261 (assoc-get pitch-handle key-sig))))
1263 ;; loop through localKeySignature to search for a notename match from other octaves
1264 (let loop ((l local-key-sig))
1266 (let ((entry (car l)))
1267 (if (and (pair? (car entry))
1268 (= (cdar entry) notename))
1269 (set! from-other-octaves (cdr entry))
1272 ;; find previous alteration-def for comparison with pitch
1274 ;; from same octave?
1275 ((and (not ignore-octave)
1277 (recent-enough? barnum from-same-octave laziness))
1278 (set! previous-alteration from-same-octave))
1283 (recent-enough? barnum from-other-octaves laziness))
1284 (set! previous-alteration from-other-octaves))
1286 ;; not recent enough, extract from key signature/local key signature
1288 (set! previous-alteration from-key-sig)))
1290 (if (accidental-invalid? previous-alteration)
1291 (set! need-accidental #t)
1293 (let* ((prev-alt (extract-alteration previous-alteration))
1294 (this-alt (ly:pitch-alteration pitch)))
1296 (if (not (= this-alt prev-alt))
1298 (set! need-accidental #t)
1299 (if (and (not (= this-alt 0))
1300 (and (< (abs this-alt) (abs prev-alt))
1301 (> (* prev-alt this-alt) 0)))
1302 (set! need-restore #t))))))
1304 (cons need-restore need-accidental)))
1306 (define-public ((make-accidental-rule octaveness laziness) context pitch barnum measurepos)
1307 "Create an accidental rule that makes its decision based on the octave of
1308 the note and a laziness value.
1310 @var{octaveness} is either @code{'same-octave} or @code{'any-octave} and
1311 defines whether the rule should respond to accidental changes in other
1312 octaves than the current. @code{'same-octave} is the normal way to typeset
1313 accidentals -- an accidental is made if the alteration is different from the
1314 last active pitch in the same octave. @code{'any-octave} looks at the last
1315 active pitch in any octave.
1317 @var{laziness} states over how many bars an accidental should be remembered.
1318 @code{0}@tie{}is the default -- accidental lasts over 0@tie{}bar lines, that
1319 is, to the end of current measure. A positive integer means that the
1320 accidental lasts over that many bar lines. @w{@code{-1}} is `forget
1321 immediately', that is, only look at key signature. @code{#t} is `forever'."
1323 (check-pitch-against-signature context pitch barnum laziness octaveness))
1325 (define (key-entry-notename entry)
1326 "Return the pitch of an entry in localKeySignature. The entry is either of the form
1327 '(notename . alter) or '((octave . notename) . (alter barnum . measurepos))."
1328 (if (number? (car entry))
1332 (define (key-entry-octave entry)
1333 "Return the octave of an entry in localKeySignature (or #f if the entry does not have
1335 (and (pair? (car entry)) (caar entry)))
1337 (define (key-entry-bar-number entry)
1338 "Return the bar number of an entry in localKeySignature (or #f if the entry does not
1339 have a bar number)."
1340 (and (pair? (car entry)) (caddr entry)))
1342 (define (key-entry-measure-position entry)
1343 "Return the measure position of an entry in localKeySignature (or #f if the entry does
1344 not have a measure position)."
1345 (and (pair? (car entry)) (cdddr entry)))
1347 (define (key-entry-alteration entry)
1348 "Return the alteration of an entry in localKeySignature.
1350 For convenience, returns @code{0} if entry is @code{#f}."
1352 (if (number? (car entry))
1357 (define-public (find-pitch-entry keysig pitch accept-global accept-local)
1358 "Return the first entry in @var{keysig} that matches @var{pitch}.
1359 @var{accept-global} states whether key signature entries should be included.
1360 @var{accept-local} states whether local accidentals should be included.
1361 If no matching entry is found, @var{#f} is returned."
1363 (let* ((entry (car keysig))
1364 (entryoct (key-entry-octave entry))
1365 (entrynn (key-entry-notename entry))
1366 (oct (ly:pitch-octave pitch))
1367 (nn (ly:pitch-notename pitch)))
1368 (if (and (equal? nn entrynn)
1369 (or (and accept-global (not entryoct))
1370 (and accept-local (equal? oct entryoct))))
1372 (find-pitch-entry (cdr keysig) pitch accept-global accept-local)))))
1374 (define-public (neo-modern-accidental-rule context pitch barnum measurepos)
1375 "An accidental rule that typesets an accidental if it differs from the
1376 key signature @emph{and} does not directly follow a note on the same
1377 staff line. This rule should not be used alone because it does neither
1378 look at bar lines nor different accidentals at the same note name."
1379 (let* ((keysig (ly:context-property context 'localKeySignature))
1380 (entry (find-pitch-entry keysig pitch #t #t)))
1383 (let* ((global-entry (find-pitch-entry keysig pitch #t #f))
1384 (key-acc (key-entry-alteration global-entry))
1385 (acc (ly:pitch-alteration pitch))
1386 (entrymp (key-entry-measure-position entry))
1387 (entrybn (key-entry-bar-number entry)))
1388 (cons #f (not (or (equal? acc key-acc)
1389 (and (equal? entrybn barnum) (equal? entrymp measurepos)))))))))
1391 (define-public (teaching-accidental-rule context pitch barnum measurepos)
1392 "An accidental rule that typesets a cautionary accidental if it is
1393 included in the key signature @emph{and} does not directly follow a note
1394 on the same staff line."
1395 (let* ((keysig (ly:context-property context 'localKeySignature))
1396 (entry (find-pitch-entry keysig pitch #t #t)))
1399 (let* ((global-entry (find-pitch-entry keysig pitch #f #f))
1400 (key-acc (key-entry-alteration global-entry))
1401 (acc (ly:pitch-alteration pitch))
1402 (entrymp (key-entry-measure-position entry))
1403 (entrybn (key-entry-bar-number entry)))
1404 (cons #f (not (or (equal? acc key-acc)
1405 (and (equal? entrybn barnum) (equal? entrymp measurepos)))))))))
1407 (define-public (set-accidentals-properties extra-natural
1408 auto-accs auto-cauts
1411 (make-sequential-music
1412 (append (if (boolean? extra-natural)
1413 (list (make-property-set 'extraNatural extra-natural))
1415 (list (make-property-set 'autoAccidentals auto-accs)
1416 (make-property-set 'autoCautionaries auto-cauts))))
1419 (define-public (set-accidental-style style . rest)
1420 "Set accidental style to @var{style}. Optionally take a context
1421 argument, e.g. @code{'Staff} or @code{'Voice}. The context defaults
1422 to @code{Staff}, except for piano styles, which use @code{GrandStaff}
1424 (let ((context (if (pair? rest)
1426 (pcontext (if (pair? rest)
1427 (car rest) 'GrandStaff)))
1429 ;; accidentals as they were common in the 18th century.
1430 ((equal? style 'default)
1431 (set-accidentals-properties #t
1432 `(Staff ,(make-accidental-rule 'same-octave 0))
1435 ;; accidentals from one voice do NOT get canceled in other voices
1436 ((equal? style 'voice)
1437 (set-accidentals-properties #t
1438 `(Voice ,(make-accidental-rule 'same-octave 0))
1441 ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
1442 ;; This includes all the default accidentals, but accidentals also needs canceling
1443 ;; in other octaves and in the next measure.
1444 ((equal? style 'modern)
1445 (set-accidentals-properties #f
1446 `(Staff ,(make-accidental-rule 'same-octave 0)
1447 ,(make-accidental-rule 'any-octave 0)
1448 ,(make-accidental-rule 'same-octave 1))
1451 ;; the accidentals that Stone adds to the old standard as cautionaries
1452 ((equal? style 'modern-cautionary)
1453 (set-accidentals-properties #f
1454 `(Staff ,(make-accidental-rule 'same-octave 0))
1455 `(Staff ,(make-accidental-rule 'any-octave 0)
1456 ,(make-accidental-rule 'same-octave 1))
1458 ;; same as modern, but accidentals different from the key signature are always
1459 ;; typeset - unless they directly follow a note of the same pitch.
1460 ((equal? style 'neo-modern)
1461 (set-accidentals-properties #f
1462 `(Staff ,(make-accidental-rule 'same-octave 0)
1463 ,(make-accidental-rule 'any-octave 0)
1464 ,(make-accidental-rule 'same-octave 1)
1465 ,neo-modern-accidental-rule)
1468 ((equal? style 'neo-modern-cautionary)
1469 (set-accidentals-properties #f
1470 `(Staff ,(make-accidental-rule 'same-octave 0))
1471 `(Staff ,(make-accidental-rule 'any-octave 0)
1472 ,(make-accidental-rule 'same-octave 1)
1473 ,neo-modern-accidental-rule)
1475 ((equal? style 'neo-modern-voice)
1476 (set-accidentals-properties #f
1477 `(Voice ,(make-accidental-rule 'same-octave 0)
1478 ,(make-accidental-rule 'any-octave 0)
1479 ,(make-accidental-rule 'same-octave 1)
1480 ,neo-modern-accidental-rule
1481 Staff ,(make-accidental-rule 'same-octave 0)
1482 ,(make-accidental-rule 'any-octave 0)
1483 ,(make-accidental-rule 'same-octave 1)
1484 ,neo-modern-accidental-rule)
1487 ((equal? style 'neo-modern-voice-cautionary)
1488 (set-accidentals-properties #f
1489 `(Voice ,(make-accidental-rule 'same-octave 0))
1490 `(Voice ,(make-accidental-rule 'any-octave 0)
1491 ,(make-accidental-rule 'same-octave 1)
1492 ,neo-modern-accidental-rule
1493 Staff ,(make-accidental-rule 'same-octave 0)
1494 ,(make-accidental-rule 'any-octave 0)
1495 ,(make-accidental-rule 'same-octave 1)
1496 ,neo-modern-accidental-rule)
1498 ;; Accidentals as they were common in dodecaphonic music with no tonality.
1499 ;; Each note gets one accidental.
1500 ((equal? style 'dodecaphonic)
1501 (set-accidentals-properties #f
1502 `(Staff ,(lambda (c p bn mp) '(#f . #t)))
1505 ;; Multivoice accidentals to be read both by musicians playing one voice
1506 ;; and musicians playing all voices.
1507 ;; Accidentals are typeset for each voice, but they ARE canceled across voices.
1508 ((equal? style 'modern-voice)
1509 (set-accidentals-properties #f
1510 `(Voice ,(make-accidental-rule 'same-octave 0)
1511 ,(make-accidental-rule 'any-octave 0)
1512 ,(make-accidental-rule 'same-octave 1)
1513 Staff ,(make-accidental-rule 'same-octave 0)
1514 ,(make-accidental-rule 'any-octave 0)
1515 ,(make-accidental-rule 'same-octave 1))
1518 ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
1520 ((equal? style 'modern-voice-cautionary)
1521 (set-accidentals-properties #f
1522 `(Voice ,(make-accidental-rule 'same-octave 0))
1523 `(Voice ,(make-accidental-rule 'any-octave 0)
1524 ,(make-accidental-rule 'same-octave 1)
1525 Staff ,(make-accidental-rule 'same-octave 0)
1526 ,(make-accidental-rule 'any-octave 0)
1527 ,(make-accidental-rule 'same-octave 1))
1529 ;; stone's suggestions for accidentals on grand staff.
1530 ;; Accidentals are canceled across the staves in the same grand staff as well
1531 ((equal? style 'piano)
1532 (set-accidentals-properties #f
1533 `(Staff ,(make-accidental-rule 'same-octave 0)
1534 ,(make-accidental-rule 'any-octave 0)
1535 ,(make-accidental-rule 'same-octave 1)
1537 ,(make-accidental-rule 'any-octave 0)
1538 ,(make-accidental-rule 'same-octave 1))
1541 ((equal? style 'piano-cautionary)
1542 (set-accidentals-properties #f
1543 `(Staff ,(make-accidental-rule 'same-octave 0))
1544 `(Staff ,(make-accidental-rule 'any-octave 0)
1545 ,(make-accidental-rule 'same-octave 1)
1547 ,(make-accidental-rule 'any-octave 0)
1548 ,(make-accidental-rule 'same-octave 1))
1551 ;; same as modern, but cautionary accidentals are printed for all sharp or flat
1552 ;; tones specified by the key signature.
1553 ((equal? style 'teaching)
1554 (set-accidentals-properties #f
1555 `(Staff ,(make-accidental-rule 'same-octave 0))
1556 `(Staff ,(make-accidental-rule 'same-octave 1)
1557 ,teaching-accidental-rule)
1560 ;; do not set localKeySignature when a note alterated differently from
1561 ;; localKeySignature is found.
1562 ;; Causes accidentals to be printed at every note instead of
1563 ;; remembered for the duration of a measure.
1564 ;; accidentals not being remembered, causing accidentals always to
1565 ;; be typeset relative to the time signature
1566 ((equal? style 'forget)
1567 (set-accidentals-properties '()
1568 `(Staff ,(make-accidental-rule 'same-octave -1))
1571 ;; Do not reset the key at the start of a measure. Accidentals will be
1572 ;; printed only once and are in effect until overridden, possibly many
1574 ((equal? style 'no-reset)
1575 (set-accidentals-properties '()
1576 `(Staff ,(make-accidental-rule 'same-octave #t))
1580 (ly:warning (_ "unknown accidental style: ~S") style)
1581 (make-sequential-music '())))))
1583 (define-public (invalidate-alterations context)
1584 "Invalidate alterations in @var{context}.
1586 Elements of @code{'localKeySignature} corresponding to local
1587 alterations of the key signature have the form
1588 @code{'((octave . notename) . (alter barnum . measurepos))}.
1589 Replace them with a version where @code{alter} is set to @code{'clef}
1590 to force a repetition of accidentals.
1592 Entries that conform with the current key signature are not invalidated."
1593 (let* ((keysig (ly:context-property context 'keySignature)))
1594 (set! (ly:context-property context 'localKeySignature)
1597 (let* ((localalt (key-entry-alteration entry))
1598 (localoct (key-entry-octave entry)))
1599 (if (or (accidental-invalid? localalt)
1602 (key-entry-alteration
1605 (ly:make-pitch localoct
1606 (key-entry-notename entry)
1610 (cons (car entry) (cons 'clef (cddr entry))))))
1611 (ly:context-property context 'localKeySignature)))))
1613 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1615 (define-public (skip-of-length mus)
1616 "Create a skip of exactly the same length as @var{mus}."
1620 'duration (ly:make-duration 0 0))))
1622 (make-event-chord (list (ly:music-compress skip (ly:music-length mus))))))
1624 (define-public (mmrest-of-length mus)
1625 "Create a multi-measure rest of exactly the same length as @var{mus}."
1628 (make-multi-measure-rest
1629 (ly:make-duration 0 0) '())))
1630 (ly:music-compress skip (ly:music-length mus))
1633 (define-public (pitch-of-note event-chord)
1634 (let ((evs (filter (lambda (x)
1635 (music-has-type x 'note-event))
1636 (ly:music-property event-chord 'elements))))
1639 (ly:music-property (car evs) 'pitch))))
1641 (define-public (duration-of-note event-chord)
1643 ((pair? event-chord)
1644 (or (duration-of-note (car event-chord))
1645 (duration-of-note (cdr event-chord))))
1646 ((ly:music? event-chord)
1647 (let ((dur (ly:music-property event-chord 'duration)))
1648 (if (ly:duration? dur)
1650 (duration-of-note (ly:music-property event-chord 'elements)))))
1653 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1655 (define-public (map-some-music map? music)
1656 "Walk through @var{music}, transform all elements calling @var{map?}
1657 and only recurse if this returns @code{#f}."
1658 (let loop ((music music))
1660 (let ((elt (ly:music-property music 'element))
1661 (elts (ly:music-property music 'elements))
1662 (arts (ly:music-property music 'articulations)))
1664 (set! (ly:music-property music 'element)
1667 (set! (ly:music-property music 'elements)
1670 (set! (ly:music-property music 'articulations)
1674 (define-public (for-some-music stop? music)
1675 "Walk through @var{music}, process all elements calling @var{stop?}
1676 and only recurse if this returns @code{#f}."
1677 (let loop ((music music))
1678 (if (not (stop? music))
1679 (let ((elt (ly:music-property music 'element)))
1682 (for-each loop (ly:music-property music 'elements))
1683 (for-each loop (ly:music-property music 'articulations))))))
1685 (define-public (fold-some-music pred? proc init music)
1686 "This works recursively on music like @code{fold} does on a list,
1687 calling @samp{(@var{pred?} music)} on every music element. If
1688 @code{#f} is returned for an element, it is processed recursively
1689 with the same initial value of @samp{previous}, otherwise
1690 @samp{(@var{proc} music previous)} replaces @samp{previous}
1691 and no recursion happens.
1692 The top @var{music} is processed using @var{init} for @samp{previous}."
1693 (let loop ((music music) (previous init))
1695 (proc music previous)
1698 (let ((elt (ly:music-property music 'element)))
1701 (loop elt previous)))
1702 (ly:music-property music 'elements))
1703 (ly:music-property music 'articulations)))))
1705 (define-public (extract-music music pred?)
1706 "Return a flat list of all music matching @var{pred?} inside of
1707 @var{music}, not recursing into matches themselves."
1708 (reverse! (fold-some-music pred? cons '() music)))
1710 (define-public (extract-named-music music music-name)
1711 "Return a flat list of all music named @var{music-name} (either a
1712 single event symbol or a list of alternatives) inside of @var{music},
1713 not recursing into matches themselves."
1716 (if (cheap-list? music-name)
1717 (lambda (m) (memq (ly:music-property m 'name) music-name))
1718 (lambda (m) (eq? (ly:music-property m 'name) music-name)))))
1720 (define-public (extract-typed-music music type)
1721 "Return a flat list of all music with @var{type} (either a single
1722 type symbol or a list of alternatives) inside of @var{music}, not
1723 recursing into matches themselves."
1726 (if (cheap-list? type)
1728 (any (lambda (t) (music-is-of-type? m t)) type))
1729 (lambda (m) (music-is-of-type? m type)))))
1731 (define*-public (event-chord-wrap! music #:optional parser)
1732 "Wrap isolated rhythmic events and non-postevent events in
1733 @var{music} inside of an @code{EventChord}. If the optional
1734 @var{parser} argument is given, chord repeats @samp{q} are expanded
1735 using the default settings. Otherwise, you need to cater for them
1739 (cond ((music-is-of-type? m 'event-chord)
1740 (if (pair? (ly:music-property m 'articulations))
1742 (set! (ly:music-property m 'elements)
1743 (append (ly:music-property m 'elements)
1744 (ly:music-property m 'articulations)))
1745 (set! (ly:music-property m 'articulations) '())))
1747 ((music-is-of-type? m 'rhythmic-event)
1748 (let ((arts (ly:music-property m 'articulations)))
1750 (set! (ly:music-property m 'articulations) '()))
1751 (make-event-chord (cons m arts))))
1754 (expand-repeat-chords!
1755 (cons 'rhythmic-event
1756 (ly:parser-lookup parser '$chord-repeat-events))
1760 (define-public (event-chord-notes event-chord)
1761 "Return a list of all notes from @var{event-chord}."
1763 (lambda (m) (eq? 'NoteEvent (ly:music-property m 'name)))
1764 (ly:music-property event-chord 'elements)))
1766 (define-public (event-chord-pitches event-chord)
1767 "Return a list of all pitches from @var{event-chord}."
1768 (map (lambda (x) (ly:music-property x 'pitch))
1769 (event-chord-notes event-chord)))
1771 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1772 ; The following functions are all associated with the crossStaff
1775 (define (close-enough? x y)
1776 "Values are close enough to ignore the difference"
1777 (< (abs (- x y)) 0.0001))
1779 (define (extent-combine extents)
1780 "Combine a list of extents"
1781 (if (pair? (cdr extents))
1782 (interval-union (car extents) (extent-combine (cdr extents)))
1785 (define ((stem-connectable? ref root) stem)
1786 "Check if the stem is connectable to the root"
1787 ; The root is always connectable to itself
1790 ; Horizontal positions of the stems must be almost the same
1791 (close-enough? (car (ly:grob-extent root ref X))
1792 (car (ly:grob-extent stem ref X)))
1793 ; The stem must be in the direction away from the root's notehead
1794 (positive? (* (ly:grob-property root 'direction)
1795 (- (car (ly:grob-extent stem ref Y))
1796 (car (ly:grob-extent root ref Y))))))))
1798 (define (stem-span-stencil span)
1799 "Connect stems if we have at least one stem connectable to the root"
1800 (let* ((system (ly:grob-system span))
1801 (root (ly:grob-parent span X))
1802 (stems (filter (stem-connectable? system root)
1803 (ly:grob-object span 'stems))))
1804 (if (<= 2 (length stems))
1805 (let* ((yextents (map (lambda (st)
1806 (ly:grob-extent st system Y)) stems))
1807 (yextent (extent-combine yextents))
1808 (layout (ly:grob-layout root))
1809 (blot (ly:output-def-lookup layout 'blot-diameter)))
1810 ; Hide spanned stems
1812 (set! (ly:grob-property st 'transparent) #t))
1814 ; Draw a nice looking stem with rounded corners
1815 (ly:round-filled-box (ly:grob-extent root root X) yextent blot))
1816 ; Nothing to connect, don't draw the span
1819 (define ((make-stem-span! stems trans) root)
1820 "Create a stem span as a child of the cross-staff stem (the root)"
1821 (let ((span (ly:engraver-make-grob trans 'Stem '())))
1822 (ly:grob-set-parent! span X root)
1823 (set! (ly:grob-object span 'stems) stems)
1824 ; Suppress positioning, the stem code is confused by this weird stem
1825 (set! (ly:grob-property span 'X-offset) 0)
1826 (set! (ly:grob-property span 'stencil) stem-span-stencil)))
1828 (define-public (cross-staff-connect stem)
1829 "Set cross-staff property of the stem to this function to connect it to
1830 other stems automatically"
1833 (define (stem-is-root? stem)
1834 "Check if automatic connecting of the stem was requested. Stems connected
1835 to cross-staff beams are cross-staff, but they should not be connected to
1836 other stems just because of that."
1837 (eq? cross-staff-connect (ly:grob-property-data stem 'cross-staff)))
1839 (define (make-stem-spans! ctx stems trans)
1840 "Create stem spans for cross-staff stems"
1841 ; Cannot do extensive checks here, just make sure there are at least
1842 ; two stems at this musical moment
1843 (if (<= 2 (length stems))
1844 (let ((roots (filter stem-is-root? stems)))
1845 (map (make-stem-span! stems trans) roots))))
1847 (define-public (Span_stem_engraver ctx)
1848 "Connect cross-staff stems to the stems above in the system"
1851 ; Record all stems for the given moment
1853 ((stem-interface trans grob source)
1854 (set! stems (cons grob stems))))
1855 ; Process stems and reset the stem list to empty
1856 ((process-acknowledged trans)
1857 (make-stem-spans! ctx stems trans)
1858 (set! stems '())))))
1860 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1861 ;; The following is used by the alterBroken function.
1863 (define-public ((value-for-spanner-piece arg) grob)
1864 "Associate a piece of broken spanner @var{grob} with an element
1866 (let* ((orig (ly:grob-original grob))
1867 (siblings (ly:spanner-broken-into orig)))
1869 (define (helper sibs arg)
1872 (if (eq? (car sibs) grob)
1874 (helper (cdr sibs) (cdr arg)))))
1876 (if (>= (length siblings) 2)
1877 (helper siblings arg)