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 (integer? mult))
303 (ly:warning (_ "invalid tremolo repeat count: ~a") times))
304 ;; Adjust the time of the notes
305 (ly:music-compress r (ly:make-moment 1 children))
306 ;; Adjust the displayed note durations
307 (shift-duration-log r shift dots))
310 (define (calc-repeat-slash-count music)
311 "Given the child-list @var{music} in @code{PercentRepeatMusic},
312 calculate the number of slashes based on the durations. Returns @code{0}
313 if durations in @var{music} vary, allowing slash beats and double-percent
314 beats to be distinguished."
315 (let* ((durs (map duration-of-note
316 (extract-named-music music '(EventChord NoteEvent
317 RestEvent SkipEvent))))
318 (first-dur (car durs)))
320 (if (every (lambda (d) (equal? d first-dur)) durs)
321 (max (- (ly:duration-log first-dur) 2) 1)
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 (define-public (note-to-cluster music)
328 "Replace @code{NoteEvents} by @code{ClusterNoteEvents}."
329 (if (eq? (ly:music-property music 'name) 'NoteEvent)
330 (make-music 'ClusterNoteEvent
331 'pitch (ly:music-property music 'pitch)
332 'duration (ly:music-property music 'duration))
335 (define-public (notes-to-clusters music)
336 (music-map note-to-cluster music))
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 (define-public (unfold-repeats music)
342 "Replace all repeats with unfolded repeats."
344 (let ((es (ly:music-property music 'elements))
345 (e (ly:music-property music 'element)))
347 (if (memq 'repeated-music (ly:music-property music 'types))
348 (let* ((props (ly:music-mutable-properties music))
349 (old-name (ly:music-property music 'name))
350 (flattened (flatten-alist props)))
351 (set! music (apply make-music (cons 'UnfoldedRepeatedMusic
354 (if (equal? old-name 'TremoloRepeatedMusic)
355 (let* ((seq-arg? (memq 'sequential-music
356 (ly:music-property e 'types)))
357 (count (ly:music-property music 'repeat-count))
358 (dot-shift (if (= 0 (remainder count 3))
360 (child-count (if seq-arg?
361 (length (ly:music-property e 'elements))
365 (set! count (* 2 (quotient count 3))))
367 (shift-duration-log music (+ (if (= 2 child-count)
369 (ly:intlog2 count)) dot-shift)
372 (ly:music-compress e (ly:make-moment child-count 1)))))))
375 (set! (ly:music-property music 'elements)
376 (map unfold-repeats es)))
378 (set! (ly:music-property music 'element)
382 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 ;; property setting music objs.
385 (define-public (make-grob-property-set grob gprop val)
386 "Make a @code{Music} expression that sets @var{gprop} to @var{val} in
387 @var{grob}. Does a pop first, i.e., this is not an override."
388 (make-music 'OverrideProperty
394 (define-public (make-grob-property-override grob gprop val)
395 "Make a @code{Music} expression that overrides @var{gprop} to @var{val}
397 (make-music 'OverrideProperty
402 (define-public (make-grob-property-revert grob gprop)
403 "Revert the grob property @var{gprop} for @var{grob}."
404 (make-music 'RevertProperty
406 'grob-property gprop))
408 (define direction-polyphonic-grobs
409 '(AccidentalSuggestion
426 (define-safe-public (make-voice-props-set n)
427 (make-sequential-music
429 (map (lambda (x) (make-grob-property-set x 'direction
431 direction-polyphonic-grobs)
433 (make-property-set 'graceSettings
434 ;; TODO: take this from voicedGraceSettings or similar.
435 '((Voice Stem font-size -3)
436 (Voice Flag font-size -3)
437 (Voice NoteHead font-size -3)
438 (Voice TabNoteHead font-size -4)
439 (Voice Dots font-size -3)
440 (Voice Stem length-fraction 0.8)
441 (Voice Stem no-stem-extend #t)
442 (Voice Beam beam-thickness 0.384)
443 (Voice Beam length-fraction 0.8)
444 (Voice Accidental font-size -4)
445 (Voice AccidentalCautionary font-size -4)
446 (Voice Script font-size -3)
447 (Voice Fingering font-size -8)
448 (Voice StringNumber font-size -8)))
450 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
451 (make-grob-property-set 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))))))
453 (define-safe-public (make-voice-props-override n)
454 (make-sequential-music
456 (map (lambda (x) (make-grob-property-override x 'direction
458 direction-polyphonic-grobs)
460 (make-property-set 'graceSettings
461 ;; TODO: take this from voicedGraceSettings or similar.
462 '((Voice Stem font-size -3)
463 (Voice Flag font-size -3)
464 (Voice NoteHead font-size -3)
465 (Voice TabNoteHead font-size -4)
466 (Voice Dots font-size -3)
467 (Voice Stem length-fraction 0.8)
468 (Voice Stem no-stem-extend #t)
469 (Voice Beam beam-thickness 0.384)
470 (Voice Beam length-fraction 0.8)
471 (Voice Accidental font-size -4)
472 (Voice AccidentalCautionary font-size -4)
473 (Voice Script font-size -3)
474 (Voice Fingering font-size -8)
475 (Voice StringNumber font-size -8)))
477 (make-grob-property-override 'NoteColumn 'horizontal-shift (quotient n 2))
478 (make-grob-property-override 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))))))
480 (define-safe-public (make-voice-props-revert)
481 (make-sequential-music
483 (map (lambda (x) (make-grob-property-revert x 'direction))
484 direction-polyphonic-grobs)
485 (list (make-property-unset 'graceSettings)
486 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
487 (make-grob-property-revert 'MultiMeasureRest 'staff-position)))))
490 (define-safe-public (context-spec-music m context #:optional id)
491 "Add \\context CONTEXT = ID to M."
492 (let ((cm (make-music 'ContextSpeccedMusic
494 'context-type context)))
496 (set! (ly:music-property cm 'context-id) id))
499 (define-public (descend-to-context m context)
500 "Like @code{context-spec-music}, but only descending."
501 (let ((cm (context-spec-music m context)))
502 (ly:music-set-property! cm 'descend-only #t)
505 (define-public (make-non-relative-music mus)
506 (make-music 'UnrelativableMusic
509 (define-public (make-apply-context func)
510 (make-music 'ApplyContext
513 (define-public (make-sequential-music elts)
514 (make-music 'SequentialMusic
517 (define-public (make-simultaneous-music elts)
518 (make-music 'SimultaneousMusic
521 (define-safe-public (make-event-chord elts)
522 (make-music 'EventChord
525 (define-public (make-skip-music dur)
526 (make-music 'SkipMusic
529 (define-public (make-grace-music music)
530 (make-music 'GraceMusic
536 (define-public (make-multi-measure-rest duration location)
537 (make-music 'MultiMeasureRestMusic
541 (define-public (make-property-set sym val)
542 (make-music 'PropertySet
546 (define-public (make-property-unset sym)
547 (make-music 'PropertyUnset
550 (define-safe-public (make-articulation name)
551 (make-music 'ArticulationEvent
552 'articulation-type name))
554 (define-public (make-lyric-event string duration)
555 (make-music 'LyricEvent
559 (define-safe-public (make-span-event type span-dir)
561 'span-direction span-dir))
563 (define-public (override-head-style heads style)
564 "Override style for @var{heads} to @var{style}."
565 (make-sequential-music
568 (make-grob-property-override h 'style style))
570 (list (make-grob-property-override heads 'style style)))))
572 (define-public (revert-head-style heads)
573 "Revert style for @var{heads}."
574 (make-sequential-music
577 (make-grob-property-revert h 'style))
579 (list (make-grob-property-revert heads 'style)))))
581 (define-public (style-note-heads heads style music)
582 "Set @var{style} for all @var{heads} in @var{music}. Works both
583 inside of and outside of chord construct."
584 ;; are we inside a <...>?
585 (if (eq? (ly:music-property music 'name) 'NoteEvent)
586 ;; yes -> use a tweak
588 (set! (ly:music-property music 'tweaks)
589 (acons 'style style (ly:music-property music 'tweaks)))
591 ;; not in <...>, so use overrides
592 (make-sequential-music
594 (override-head-style heads style)
596 (revert-head-style heads)))))
598 (define-public (set-mus-properties! m alist)
599 "Set all of @var{alist} as properties of @var{m}."
602 (set! (ly:music-property m (caar alist)) (cdar alist))
603 (set-mus-properties! m (cdr alist)))))
605 (define-public (music-separator? m)
606 "Is @var{m} a separator?"
607 (let ((ts (ly:music-property m 'types)))
608 (memq 'separator ts)))
610 ;;; expanding repeat chords
611 (define-public (copy-repeat-chord original-chord repeat-chord duration
613 "Copies all events in @var{event-types} (be sure to include
614 @code{rhythmic-events}) from @var{original-chord} over to
615 @var{repeat-chord} with their articulations filtered as well. Any
616 duration is replaced with the specified @var{duration}."
617 ;; First remove everything from event-types that can already be
618 ;; found in the repeated chord. We don't need to look for
619 ;; articulations on individual events since they can't actually get
620 ;; into a repeat chord given its input syntax.
621 (for-each (lambda (e)
622 (for-each (lambda (x)
623 (set! event-types (delq x event-types)))
624 (ly:music-property e 'types)))
625 (ly:music-property repeat-chord 'elements))
626 ;; now treat the elements
627 (set! (ly:music-property repeat-chord 'elements)
631 (and (any (lambda (t) (music-is-of-type? m t)) event-types)
633 (set! m (ly:music-deep-copy m))
634 (if (pair? (ly:music-property m 'articulations))
635 (set! (ly:music-property m 'articulations)
638 (any (lambda (t) (music-is-of-type? a t))
640 (ly:music-property m 'articulations))))
641 (if (ly:duration? (ly:music-property m 'duration))
642 (set! (ly:music-property m 'duration) duration))
644 (ly:music-property original-chord 'elements))
645 (ly:music-property repeat-chord 'elements))))
647 (define-public (expand-repeat-chords! event-types music)
648 "Walks through @var{music} and fills repeated chords (notable by
649 having a duration in @code{duration}) with the notes from their
650 respective predecessor chord."
651 (let loop ((music music) (last-chord #f))
652 (if (music-is-of-type? music 'event-chord)
653 (let ((chord-repeat (ly:music-property music 'duration)))
655 ((not (ly:duration? chord-repeat))
658 (set! (ly:music-property music 'duration) '())
659 (copy-repeat-chord last-chord music chord-repeat event-types)
662 (ly:music-warning music (_ "Bad chord repetition"))
664 (let ((elt (ly:music-property music 'element)))
665 (fold loop (if (ly:music? elt) (loop elt last-chord) last-chord)
666 (ly:music-property music 'elements)))))
669 ;;; splitting chords into voices.
670 (define (voicify-list lst number)
671 "Make a list of Musics.
673 voicify-list :: [ [Music ] ] -> number -> [Music]
674 LST is a list music-lists.
676 NUMBER is 0-base, i.e., Voice=1 (upstems) has number 0.
680 (cons (context-spec-music
681 (make-sequential-music
682 (list (make-voice-props-set number)
683 (make-simultaneous-music (car lst))))
684 'Bottom (number->string (1+ number)))
685 (voicify-list (cdr lst) (1+ number)))))
687 (define (voicify-chord ch)
688 "Split the parts of a chord into different Voices using separator"
689 (let ((es (ly:music-property ch 'elements)))
690 (set! (ly:music-property ch 'elements)
691 (voicify-list (split-list-by-separator es music-separator?) 0))
694 (define-public (voicify-music m)
695 "Recursively split chords that are separated with @code{\\\\}."
696 (if (not (ly:music? m))
697 (ly:error (_ "music expected: ~S") m))
698 (let ((es (ly:music-property m 'elements))
699 (e (ly:music-property m 'element)))
702 (set! (ly:music-property m 'elements) (map voicify-music es)))
704 (set! (ly:music-property m 'element) (voicify-music e)))
705 (if (and (equal? (ly:music-property m 'name) 'SimultaneousMusic)
706 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
707 (set! m (context-spec-music (voicify-chord m) 'Staff)))
710 (define-public (empty-music)
713 ;; Make a function that checks score element for being of a specific type.
714 (define-public (make-type-checker symbol)
716 (grob::has-interface elt symbol)))
718 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
720 (set! (ly:grob-property grob sym) val)))
723 (define-public ((set-output-property grob-name symbol val) grob grob-c context)
725 @code{\\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))}"
726 (let ((meta (ly:grob-property grob 'meta)))
727 (if (equal? (assoc-get 'name meta) grob-name)
728 (set! (ly:grob-property grob symbol) val))))
731 (define-public (skip->rest mus)
732 "Replace @var{mus} by @code{RestEvent} of the same duration if it is a
733 @code{SkipEvent}. Useful for extracting parts from crowded scores."
735 (if (memq (ly:music-property mus 'name) '(SkipEvent SkipMusic))
736 (make-music 'RestEvent 'duration (ly:music-property mus 'duration))
740 (define-public (music-has-type music type)
741 (memq type (ly:music-property music 'types)))
743 (define-public (music-clone music)
744 (define (alist->args alist acc)
747 (alist->args (cdr alist)
748 (cons (caar alist) (cons (cdar alist) acc)))))
752 (ly:music-property music 'name)
753 (alist->args (ly:music-mutable-properties music) '())))
755 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
756 ;; warn for bare chords at start.
758 (define-public (ly:music-message music msg)
759 (let ((ip (ly:music-property music 'origin)))
760 (if (ly:input-location? ip)
761 (ly:input-message ip msg)
764 (define-public (ly:music-warning music msg)
765 (let ((ip (ly:music-property music 'origin)))
766 (if (ly:input-location? ip)
767 (ly:input-warning ip msg)
770 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
772 ;; setting stuff for grace context.
775 (define (vector-extend v x)
776 "Make a new vector consisting of V, with X added to the end."
777 (let* ((n (vector-length v))
778 (nv (make-vector (+ n 1) '())))
779 (vector-move-left! v 0 n nv 0)
783 (define (vector-map f v)
784 "Map F over V. This function returns nothing."
785 (do ((n (vector-length v))
788 (f (vector-ref v i))))
790 (define (vector-reverse-map f v)
791 "Map F over V, N to 0 order. This function returns nothing."
792 (do ((i (- (vector-length v) 1) (- i 1)))
794 (f (vector-ref v i))))
796 (define-public (add-grace-property context-name grob sym val)
797 "Set @var{sym}=@var{val} for @var{grob} in @var{context-name}."
798 (define (set-prop context)
799 (let* ((where (ly:context-property-where-defined context 'graceSettings))
800 (current (ly:context-property where 'graceSettings))
801 (new-settings (append current
802 (list (list context-name grob sym val)))))
803 (ly:context-set-property! where 'graceSettings new-settings)))
804 (context-spec-music (make-apply-context set-prop) 'Voice))
806 (define-public (remove-grace-property context-name grob sym)
807 "Remove all @var{sym} for @var{grob} in @var{context-name}."
808 (define (sym-grob-context? property sym grob context-name)
809 (and (eq? (car property) context-name)
810 (eq? (cadr property) grob)
811 (eq? (caddr property) sym)))
812 (define (delete-prop context)
813 (let* ((where (ly:context-property-where-defined context 'graceSettings))
814 (current (ly:context-property where 'graceSettings))
815 (prop-settings (filter
816 (lambda(x) (sym-grob-context? x sym grob context-name))
818 (new-settings current))
820 (set! new-settings (delete x new-settings)))
822 (ly:context-set-property! where 'graceSettings new-settings)))
823 (context-spec-music (make-apply-context delete-prop) 'Voice))
827 (defmacro-public def-grace-function (start stop . docstring)
828 "Helper macro for defining grace music"
829 `(define-music-function (parser location music) (ly:music?)
831 (make-music 'GraceMusic
833 'element (make-music 'SequentialMusic
834 'elements (list (ly:music-deep-copy ,start)
836 (ly:music-deep-copy ,stop))))))
838 (defmacro-public define-syntax-function (type args signature . body)
839 "Helper macro for `ly:make-music-function'.
841 (define-syntax-function (result-type? parser location arg1 arg2 ...) (result-type? arg1-type arg2-type ...)
844 argX-type can take one of the forms @code{predicate?} for mandatory
845 arguments satisfying the predicate, @code{(predicate?)} for optional
846 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
847 value)}} for optional parameters with a specified default
848 value (evaluated at definition time). An optional parameter can be
849 omitted in a call only when it can't get confused with a following
850 parameter of different type.
852 Predicates with syntactical significance are @code{ly:pitch?},
853 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
854 predicates require the parameter to be entered as Scheme expression.
856 @code{result-type?} can specify a default in the same manner as
857 predicates, to be used in case of a type error in arguments or
860 (set! signature (map (lambda (pred)
863 ,(and (pair? (cdr pred)) (cadr pred)))
865 (cons type signature)))
866 (if (and (pair? body) (pair? (car body)) (eqv? '_i (caar body)))
867 ;; When the music function definition contains a i10n doc string,
868 ;; (_i "doc string"), keep the literal string only
869 (let ((docstring (cadar body))
871 `(ly:make-music-function (list ,@signature)
875 `(ly:make-music-function (list ,@signature)
879 (defmacro-public define-music-function rest
880 "Defining macro returning music functions.
882 (define-music-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
885 argX-type can take one of the forms @code{predicate?} for mandatory
886 arguments satisfying the predicate, @code{(predicate?)} for optional
887 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
888 value)}} for optional parameters with a specified default
889 value (evaluated at definition time). An optional parameter can be
890 omitted in a call only when it can't get confused with a following
891 parameter of different type.
893 Predicates with syntactical significance are @code{ly:pitch?},
894 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
895 predicates require the parameter to be entered as Scheme expression.
897 Must return a music expression. The @code{origin} is automatically
898 set to the @code{location} parameter."
900 `(define-syntax-function (ly:music? (make-music 'Music 'void #t)) ,@rest))
903 (defmacro-public define-scheme-function rest
904 "Defining macro returning Scheme functions.
906 (define-scheme-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
909 argX-type can take one of the forms @code{predicate?} for mandatory
910 arguments satisfying the predicate, @code{(predicate?)} for optional
911 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
912 value)}} for optional parameters with a specified default
913 value (evaluated at definition time). An optional parameter can be
914 omitted in a call only when it can't get confused with a following
915 parameter of different type.
917 Predicates with syntactical significance are @code{ly:pitch?},
918 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
919 predicates require the parameter to be entered as Scheme expression.
921 Can return arbitrary expressions. If a music expression is returned,
922 its @code{origin} is automatically set to the @code{location}
925 `(define-syntax-function scheme? ,@rest))
927 (defmacro-public define-void-function rest
928 "This defines a Scheme function like @code{define-scheme-function} with
929 void return value (i.e., what most Guile functions with `unspecified'
930 value return). Use this when defining functions for executing actions
931 rather than returning values, to keep Lilypond from trying to interpret
933 `(define-syntax-function (void? *unspecified*) ,@rest *unspecified*))
935 (defmacro-public define-event-function rest
936 "Defining macro returning event functions.
938 (define-event-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
941 argX-type can take one of the forms @code{predicate?} for mandatory
942 arguments satisfying the predicate, @code{(predicate?)} for optional
943 parameters of that type defaulting to @code{#f}, @code{@w{(predicate?
944 value)}} for optional parameters with a specified default
945 value (evaluated at definition time). An optional parameter can be
946 omitted in a call only when it can't get confused with a following
947 parameter of different type.
949 Predicates with syntactical significance are @code{ly:pitch?},
950 @code{ly:duration?}, @code{ly:music?}, @code{markup?}. Other
951 predicates require the parameter to be entered as Scheme expression.
953 Must return an event expression. The @code{origin} is automatically
954 set to the @code{location} parameter."
956 `(define-syntax-function (ly:event? (make-music 'Event 'void #t)) ,@rest))
958 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
960 (define-public (cue-substitute quote-music)
961 "Must happen after @code{quote-substitute}."
963 (if (vector? (ly:music-property quote-music 'quoted-events))
964 (let* ((dir (ly:music-property quote-music 'quoted-voice-direction))
965 (clef (ly:music-property quote-music 'quoted-music-clef))
966 (main-voice (if (eq? 1 dir) 1 0))
967 (cue-voice (if (eq? 1 dir) 0 1))
968 (main-music (ly:music-property quote-music 'element))
969 (return-value quote-music))
971 (if (or (eq? 1 dir) (eq? -1 dir))
973 ;; if we have stem dirs, change both quoted and main music
974 ;; to have opposite stems.
977 ;; cannot context-spec Quote-music, since context
978 ;; for the quotes is determined in the iterator.
979 (make-sequential-music
983 (make-cue-clef-set clef))
984 (context-spec-music (make-voice-props-override cue-voice) 'CueVoice "cue")
986 (context-spec-music (make-voice-props-revert) 'CueVoice "cue")
989 (make-cue-clef-unset)))))
991 (make-sequential-music
993 (make-voice-props-override main-voice)
995 (make-voice-props-revert))))
996 (set! (ly:music-property quote-music 'element) main-music)))
1001 (define-public ((quote-substitute quote-tab) music)
1002 (let* ((quoted-name (ly:music-property music 'quoted-music-name))
1003 (quoted-vector (and (string? quoted-name)
1004 (hash-ref quote-tab quoted-name #f))))
1007 (if (string? quoted-name)
1008 (if (vector? quoted-vector)
1010 (set! (ly:music-property music 'quoted-events) quoted-vector)
1011 (set! (ly:music-property music 'iterator-ctor)
1012 ly:quote-iterator::constructor))
1013 (ly:music-warning music (ly:format (_ "cannot find quoted music: `~S'") quoted-name))))
1017 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1018 ;; switch it on here, so parsing and init isn't checked (too slow!)
1020 ;; automatic music transformations.
1022 (define (switch-on-debugging m)
1023 (if (defined? 'set-debug-cell-accesses!)
1024 (set-debug-cell-accesses! 15000))
1027 (define (music-check-error music)
1030 (if (and (ly:music? m)
1031 (eq? (ly:music-property m 'error-found) #t))
1034 (for-each signal (ly:music-property music 'elements))
1035 (signal (ly:music-property music 'element))
1038 (set! (ly:music-property music 'error-found) #t))
1041 (define (precompute-music-length music)
1042 (set! (ly:music-property music 'length)
1043 (ly:music-length music))
1046 (define-public (make-duration-of-length moment)
1047 "Make duration of the given @code{moment} length."
1048 (ly:make-duration 0 0
1049 (ly:moment-main-numerator moment)
1050 (ly:moment-main-denominator moment)))
1052 (define (make-skipped moment bool)
1053 "Depending on BOOL, set or unset skipTypesetting,
1054 then make SkipMusic of the given MOMENT length, and
1055 then revert skipTypesetting."
1056 (make-sequential-music
1058 (context-spec-music (make-property-set 'skipTypesetting bool)
1060 (make-music 'SkipMusic 'duration
1061 (make-duration-of-length moment))
1062 (context-spec-music (make-property-set 'skipTypesetting (not bool))
1065 (define (skip-as-needed music parser)
1067 << { \\set skipTypesetting = ##f
1068 LENGTHOF(\\showFirstLength)
1069 \\set skipTypesetting = ##t
1070 LENGTHOF(\\showLastLength) }
1074 When only showFirstLength is set,
1075 the 'length property of the music is
1076 overridden to speed up compiling."
1078 ((show-last (ly:parser-lookup parser 'showLastLength))
1079 (show-first (ly:parser-lookup parser 'showFirstLength))
1080 (show-last-length (and (ly:music? show-last)
1081 (ly:music-length show-last)))
1082 (show-first-length (and (ly:music? show-first)
1083 (ly:music-length show-first)))
1084 (orig-length (ly:music-length music)))
1086 ;;FIXME: if using either showFirst- or showLastLength,
1087 ;; make sure that skipBars is not set.
1091 ;; both properties may be set.
1092 ((and show-first-length show-last-length)
1094 ((skip-length (ly:moment-sub orig-length show-last-length)))
1095 (make-simultaneous-music
1097 (make-sequential-music
1099 (make-skipped skip-length #t)
1100 ;; let's draw a separator between the beginning and the end
1101 (context-spec-music (make-property-set 'whichBar "||")
1103 (make-skipped show-first-length #f)
1106 ;; we may only want to print the last length
1109 ((skip-length (ly:moment-sub orig-length show-last-length)))
1110 (make-simultaneous-music
1112 (make-skipped skip-length #t)
1115 ;; we may only want to print the beginning; in this case
1116 ;; only the first length will be processed (much faster).
1118 ;; the first length must not exceed the original length.
1119 (if (ly:moment<? show-first-length orig-length)
1120 (set! (ly:music-property music 'length)
1127 (define-public toplevel-music-functions
1129 (lambda (music parser) (expand-repeat-chords!
1130 (cons 'rhythmic-event
1131 (ly:parser-lookup parser '$chord-repeat-events))
1133 (lambda (music parser) (voicify-music music))
1134 (lambda (x parser) (music-map music-check-error x))
1135 (lambda (x parser) (music-map precompute-music-length x))
1136 (lambda (music parser)
1138 (music-map (quote-substitute (ly:parser-lookup parser 'musicQuotes)) music))
1140 ;; switch-on-debugging
1141 (lambda (x parser) (music-map cue-substitute x))
1144 (skip-as-needed x parser)
1148 ;;; general purpose music functions
1150 (define (shift-octave pitch octave-shift)
1151 (_i "Add @var{octave-shift} to the octave of @var{pitch}.")
1153 (+ (ly:pitch-octave pitch) octave-shift)
1154 (ly:pitch-notename pitch)
1155 (ly:pitch-alteration pitch)))
1161 (define (apply-durations lyric-music durations)
1162 (define (apply-duration music)
1163 (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
1164 (ly:duration? (ly:music-property music 'duration)))
1166 (set! (ly:music-property music 'duration) (car durations))
1167 (set! durations (cdr durations)))))
1169 (music-map apply-duration lyric-music))
1172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1175 (define (recent-enough? bar-number alteration-def laziness)
1176 (or (number? alteration-def)
1177 (equal? laziness #t)
1178 (<= bar-number (+ (cadr alteration-def) laziness))))
1180 (define (accidental-invalid? alteration-def)
1181 "Checks an alteration entry for being invalid.
1183 Non-key alterations are invalidated when tying into the next bar or
1184 when there is a clef change, since neither repetition nor cancellation
1185 can be omitted when the same note occurs again.
1187 Returns @code{#f} or the reason for the invalidation, a symbol."
1188 (let* ((def (if (pair? alteration-def)
1189 (car alteration-def)
1191 (and (symbol? def) def)))
1193 (define (extract-alteration alteration-def)
1194 (cond ((number? alteration-def)
1196 ((pair? alteration-def)
1197 (car alteration-def))
1200 (define (check-pitch-against-signature context pitch barnum laziness octaveness)
1201 "Checks the need for an accidental and a @q{restore} accidental against
1202 @code{localKeySignature}. The @var{laziness} is the number of measures
1203 for which reminder accidentals are used (i.e., if @var{laziness} is zero,
1204 only cancel accidentals in the same measure; if @var{laziness} is three,
1205 we cancel accidentals up to three measures after they first appear.
1206 @var{octaveness} is either @code{'same-octave} or @code{'any-octave} and
1207 specifies whether accidentals should be canceled in different octaves."
1208 (let* ((ignore-octave (cond ((equal? octaveness 'any-octave) #t)
1209 ((equal? octaveness 'same-octave) #f)
1211 (ly:warning (_ "Unknown octaveness type: ~S ") octaveness)
1212 (ly:warning (_ "Defaulting to 'any-octave."))
1214 (key-sig (ly:context-property context 'keySignature))
1215 (local-key-sig (ly:context-property context 'localKeySignature))
1216 (notename (ly:pitch-notename pitch))
1217 (octave (ly:pitch-octave pitch))
1218 (pitch-handle (cons octave notename))
1220 (need-accidental #f)
1221 (previous-alteration #f)
1222 (from-other-octaves #f)
1223 (from-same-octave (assoc-get pitch-handle local-key-sig))
1224 (from-key-sig (or (assoc-get notename local-key-sig)
1226 ;; If no key signature match is found from localKeySignature, we may have a custom
1227 ;; type with octave-specific entries of the form ((octave . pitch) alteration)
1228 ;; instead of (pitch . alteration). Since this type cannot coexist with entries in
1229 ;; localKeySignature, try extracting from keySignature instead.
1230 (assoc-get pitch-handle key-sig))))
1232 ;; loop through localKeySignature to search for a notename match from other octaves
1233 (let loop ((l local-key-sig))
1235 (let ((entry (car l)))
1236 (if (and (pair? (car entry))
1237 (= (cdar entry) notename))
1238 (set! from-other-octaves (cdr entry))
1241 ;; find previous alteration-def for comparison with pitch
1243 ;; from same octave?
1244 ((and (not ignore-octave)
1246 (recent-enough? barnum from-same-octave laziness))
1247 (set! previous-alteration from-same-octave))
1252 (recent-enough? barnum from-other-octaves laziness))
1253 (set! previous-alteration from-other-octaves))
1255 ;; not recent enough, extract from key signature/local key signature
1257 (set! previous-alteration from-key-sig)))
1259 (if (accidental-invalid? previous-alteration)
1260 (set! need-accidental #t)
1262 (let* ((prev-alt (extract-alteration previous-alteration))
1263 (this-alt (ly:pitch-alteration pitch)))
1265 (if (not (= this-alt prev-alt))
1267 (set! need-accidental #t)
1268 (if (and (not (= this-alt 0))
1269 (and (< (abs this-alt) (abs prev-alt))
1270 (> (* prev-alt this-alt) 0)))
1271 (set! need-restore #t))))))
1273 (cons need-restore need-accidental)))
1275 (define-public ((make-accidental-rule octaveness laziness) context pitch barnum measurepos)
1276 "Create an accidental rule that makes its decision based on the octave of
1277 the note and a laziness value.
1279 @var{octaveness} is either @code{'same-octave} or @code{'any-octave} and
1280 defines whether the rule should respond to accidental changes in other
1281 octaves than the current. @code{'same-octave} is the normal way to typeset
1282 accidentals -- an accidental is made if the alteration is different from the
1283 last active pitch in the same octave. @code{'any-octave} looks at the last
1284 active pitch in any octave.
1286 @var{laziness} states over how many bars an accidental should be remembered.
1287 @code{0}@tie{}is the default -- accidental lasts over 0@tie{}bar lines, that
1288 is, to the end of current measure. A positive integer means that the
1289 accidental lasts over that many bar lines. @w{@code{-1}} is `forget
1290 immediately', that is, only look at key signature. @code{#t} is `forever'."
1292 (check-pitch-against-signature context pitch barnum laziness octaveness))
1294 (define (key-entry-notename entry)
1295 "Return the pitch of an entry in localKeySignature. The entry is either of the form
1296 '(notename . alter) or '((octave . notename) . (alter barnum . measurepos))."
1297 (if (number? (car entry))
1301 (define (key-entry-octave entry)
1302 "Return the octave of an entry in localKeySignature (or #f if the entry does not have
1304 (and (pair? (car entry)) (caar entry)))
1306 (define (key-entry-bar-number entry)
1307 "Return the bar number of an entry in localKeySignature (or #f if the entry does not
1308 have a bar number)."
1309 (and (pair? (car entry)) (caddr entry)))
1311 (define (key-entry-measure-position entry)
1312 "Return the measure position of an entry in localKeySignature (or #f if the entry does
1313 not have a measure position)."
1314 (and (pair? (car entry)) (cdddr entry)))
1316 (define (key-entry-alteration entry)
1317 "Return the alteration of an entry in localKeySignature.
1319 For convenience, returns @code{0} if entry is @code{#f}."
1321 (if (number? (car entry))
1326 (define-public (find-pitch-entry keysig pitch accept-global accept-local)
1327 "Return the first entry in @var{keysig} that matches @var{pitch}.
1328 @var{accept-global} states whether key signature entries should be included.
1329 @var{accept-local} states whether local accidentals should be included.
1330 If no matching entry is found, @var{#f} is returned."
1332 (let* ((entry (car keysig))
1333 (entryoct (key-entry-octave entry))
1334 (entrynn (key-entry-notename entry))
1335 (oct (ly:pitch-octave pitch))
1336 (nn (ly:pitch-notename pitch)))
1337 (if (and (equal? nn entrynn)
1338 (or (and accept-global (not entryoct))
1339 (and accept-local (equal? oct entryoct))))
1341 (find-pitch-entry (cdr keysig) pitch accept-global accept-local)))))
1343 (define-public (neo-modern-accidental-rule context pitch barnum measurepos)
1344 "An accidental rule that typesets an accidental if it differs from the
1345 key signature @emph{and} does not directly follow a note on the same
1346 staff line. This rule should not be used alone because it does neither
1347 look at bar lines nor different accidentals at the same note name."
1348 (let* ((keysig (ly:context-property context 'localKeySignature))
1349 (entry (find-pitch-entry keysig pitch #t #t)))
1352 (let* ((global-entry (find-pitch-entry keysig pitch #t #f))
1353 (key-acc (key-entry-alteration global-entry))
1354 (acc (ly:pitch-alteration pitch))
1355 (entrymp (key-entry-measure-position entry))
1356 (entrybn (key-entry-bar-number entry)))
1357 (cons #f (not (or (equal? acc key-acc)
1358 (and (equal? entrybn barnum) (equal? entrymp measurepos)))))))))
1360 (define-public (teaching-accidental-rule context pitch barnum measurepos)
1361 "An accidental rule that typesets a cautionary accidental if it is
1362 included in the key signature @emph{and} does not directly follow a note
1363 on the same staff line."
1364 (let* ((keysig (ly:context-property context 'localKeySignature))
1365 (entry (find-pitch-entry keysig pitch #t #t)))
1368 (let* ((global-entry (find-pitch-entry keysig pitch #f #f))
1369 (key-acc (key-entry-alteration global-entry))
1370 (acc (ly:pitch-alteration pitch))
1371 (entrymp (key-entry-measure-position entry))
1372 (entrybn (key-entry-bar-number entry)))
1373 (cons #f (not (or (equal? acc key-acc)
1374 (and (equal? entrybn barnum) (equal? entrymp measurepos)))))))))
1376 (define-public (set-accidentals-properties extra-natural
1377 auto-accs auto-cauts
1380 (make-sequential-music
1381 (append (if (boolean? extra-natural)
1382 (list (make-property-set 'extraNatural extra-natural))
1384 (list (make-property-set 'autoAccidentals auto-accs)
1385 (make-property-set 'autoCautionaries auto-cauts))))
1388 (define-public (set-accidental-style style . rest)
1389 "Set accidental style to @var{style}. Optionally take a context
1390 argument, e.g. @code{'Staff} or @code{'Voice}. The context defaults
1391 to @code{Staff}, except for piano styles, which use @code{GrandStaff}
1393 (let ((context (if (pair? rest)
1395 (pcontext (if (pair? rest)
1396 (car rest) 'GrandStaff)))
1398 ;; accidentals as they were common in the 18th century.
1399 ((equal? style 'default)
1400 (set-accidentals-properties #t
1401 `(Staff ,(make-accidental-rule 'same-octave 0))
1404 ;; accidentals from one voice do NOT get canceled in other voices
1405 ((equal? style 'voice)
1406 (set-accidentals-properties #t
1407 `(Voice ,(make-accidental-rule 'same-octave 0))
1410 ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
1411 ;; This includes all the default accidentals, but accidentals also needs canceling
1412 ;; in other octaves and in the next measure.
1413 ((equal? style 'modern)
1414 (set-accidentals-properties #f
1415 `(Staff ,(make-accidental-rule 'same-octave 0)
1416 ,(make-accidental-rule 'any-octave 0)
1417 ,(make-accidental-rule 'same-octave 1))
1420 ;; the accidentals that Stone adds to the old standard as cautionaries
1421 ((equal? style 'modern-cautionary)
1422 (set-accidentals-properties #f
1423 `(Staff ,(make-accidental-rule 'same-octave 0))
1424 `(Staff ,(make-accidental-rule 'any-octave 0)
1425 ,(make-accidental-rule 'same-octave 1))
1427 ;; same as modern, but accidentals different from the key signature are always
1428 ;; typeset - unless they directly follow a note of the same pitch.
1429 ((equal? style 'neo-modern)
1430 (set-accidentals-properties #f
1431 `(Staff ,(make-accidental-rule 'same-octave 0)
1432 ,(make-accidental-rule 'any-octave 0)
1433 ,(make-accidental-rule 'same-octave 1)
1434 ,neo-modern-accidental-rule)
1437 ((equal? style 'neo-modern-cautionary)
1438 (set-accidentals-properties #f
1439 `(Staff ,(make-accidental-rule 'same-octave 0))
1440 `(Staff ,(make-accidental-rule 'any-octave 0)
1441 ,(make-accidental-rule 'same-octave 1)
1442 ,neo-modern-accidental-rule)
1444 ((equal? style 'neo-modern-voice)
1445 (set-accidentals-properties #f
1446 `(Voice ,(make-accidental-rule 'same-octave 0)
1447 ,(make-accidental-rule 'any-octave 0)
1448 ,(make-accidental-rule 'same-octave 1)
1449 ,neo-modern-accidental-rule
1450 Staff ,(make-accidental-rule 'same-octave 0)
1451 ,(make-accidental-rule 'any-octave 0)
1452 ,(make-accidental-rule 'same-octave 1)
1453 ,neo-modern-accidental-rule)
1456 ((equal? style 'neo-modern-voice-cautionary)
1457 (set-accidentals-properties #f
1458 `(Voice ,(make-accidental-rule 'same-octave 0))
1459 `(Voice ,(make-accidental-rule 'any-octave 0)
1460 ,(make-accidental-rule 'same-octave 1)
1461 ,neo-modern-accidental-rule
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)
1467 ;; Accidentals as they were common in dodecaphonic music with no tonality.
1468 ;; Each note gets one accidental.
1469 ((equal? style 'dodecaphonic)
1470 (set-accidentals-properties #f
1471 `(Staff ,(lambda (c p bn mp) '(#f . #t)))
1474 ;; Multivoice accidentals to be read both by musicians playing one voice
1475 ;; and musicians playing all voices.
1476 ;; Accidentals are typeset for each voice, but they ARE canceled across voices.
1477 ((equal? style 'modern-voice)
1478 (set-accidentals-properties #f
1479 `(Voice ,(make-accidental-rule 'same-octave 0)
1480 ,(make-accidental-rule 'any-octave 0)
1481 ,(make-accidental-rule 'same-octave 1)
1482 Staff ,(make-accidental-rule 'same-octave 0)
1483 ,(make-accidental-rule 'any-octave 0)
1484 ,(make-accidental-rule 'same-octave 1))
1487 ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
1489 ((equal? style 'modern-voice-cautionary)
1490 (set-accidentals-properties #f
1491 `(Voice ,(make-accidental-rule 'same-octave 0))
1492 `(Voice ,(make-accidental-rule 'any-octave 0)
1493 ,(make-accidental-rule 'same-octave 1)
1494 Staff ,(make-accidental-rule 'same-octave 0)
1495 ,(make-accidental-rule 'any-octave 0)
1496 ,(make-accidental-rule 'same-octave 1))
1498 ;; stone's suggestions for accidentals on grand staff.
1499 ;; Accidentals are canceled across the staves in the same grand staff as well
1500 ((equal? style 'piano)
1501 (set-accidentals-properties #f
1502 `(Staff ,(make-accidental-rule 'same-octave 0)
1503 ,(make-accidental-rule 'any-octave 0)
1504 ,(make-accidental-rule 'same-octave 1)
1506 ,(make-accidental-rule 'any-octave 0)
1507 ,(make-accidental-rule 'same-octave 1))
1510 ((equal? style 'piano-cautionary)
1511 (set-accidentals-properties #f
1512 `(Staff ,(make-accidental-rule 'same-octave 0))
1513 `(Staff ,(make-accidental-rule 'any-octave 0)
1514 ,(make-accidental-rule 'same-octave 1)
1516 ,(make-accidental-rule 'any-octave 0)
1517 ,(make-accidental-rule 'same-octave 1))
1520 ;; same as modern, but cautionary accidentals are printed for all sharp or flat
1521 ;; tones specified by the key signature.
1522 ((equal? style 'teaching)
1523 (set-accidentals-properties #f
1524 `(Staff ,(make-accidental-rule 'same-octave 0))
1525 `(Staff ,(make-accidental-rule 'same-octave 1)
1526 ,teaching-accidental-rule)
1529 ;; do not set localKeySignature when a note alterated differently from
1530 ;; localKeySignature is found.
1531 ;; Causes accidentals to be printed at every note instead of
1532 ;; remembered for the duration of a measure.
1533 ;; accidentals not being remembered, causing accidentals always to
1534 ;; be typeset relative to the time signature
1535 ((equal? style 'forget)
1536 (set-accidentals-properties '()
1537 `(Staff ,(make-accidental-rule 'same-octave -1))
1540 ;; Do not reset the key at the start of a measure. Accidentals will be
1541 ;; printed only once and are in effect until overridden, possibly many
1543 ((equal? style 'no-reset)
1544 (set-accidentals-properties '()
1545 `(Staff ,(make-accidental-rule 'same-octave #t))
1549 (ly:warning (_ "unknown accidental style: ~S") style)
1550 (make-sequential-music '())))))
1552 (define-public (invalidate-alterations context)
1553 "Invalidate alterations in @var{context}.
1555 Elements of @code{'localKeySignature} corresponding to local
1556 alterations of the key signature have the form
1557 @code{'((octave . notename) . (alter barnum . measurepos))}.
1558 Replace them with a version where @code{alter} is set to @code{'clef}
1559 to force a repetition of accidentals.
1561 Entries that conform with the current key signature are not invalidated."
1562 (let* ((keysig (ly:context-property context 'keySignature)))
1563 (set! (ly:context-property context 'localKeySignature)
1566 (let* ((localalt (key-entry-alteration entry))
1567 (localoct (key-entry-octave entry)))
1568 (if (or (accidental-invalid? localalt)
1571 (key-entry-alteration
1574 (ly:make-pitch localoct
1575 (key-entry-notename entry)
1579 (cons (car entry) (cons 'clef (cddr entry))))))
1580 (ly:context-property context 'localKeySignature)))))
1582 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1584 (define-public (skip-of-length mus)
1585 "Create a skip of exactly the same length as @var{mus}."
1589 'duration (ly:make-duration 0 0))))
1591 (make-event-chord (list (ly:music-compress skip (ly:music-length mus))))))
1593 (define-public (mmrest-of-length mus)
1594 "Create a multi-measure rest of exactly the same length as @var{mus}."
1597 (make-multi-measure-rest
1598 (ly:make-duration 0 0) '())))
1599 (ly:music-compress skip (ly:music-length mus))
1602 (define-public (pitch-of-note event-chord)
1603 (let ((evs (filter (lambda (x)
1604 (music-has-type x 'note-event))
1605 (ly:music-property event-chord 'elements))))
1608 (ly:music-property (car evs) 'pitch))))
1610 (define-public (duration-of-note event-chord)
1612 ((pair? event-chord)
1613 (or (duration-of-note (car event-chord))
1614 (duration-of-note (cdr event-chord))))
1615 ((ly:music? event-chord)
1616 (let ((dur (ly:music-property event-chord 'duration)))
1617 (if (ly:duration? dur)
1619 (duration-of-note (ly:music-property event-chord 'elements)))))
1622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1624 (define-public (map-some-music map? music)
1625 "Walk through @var{music}, transform all elements calling @var{map?}
1626 and only recurse if this returns @code{#f}."
1627 (let loop ((music music))
1629 (let ((elt (ly:music-property music 'element))
1630 (elts (ly:music-property music 'elements))
1631 (arts (ly:music-property music 'articulations)))
1633 (set! (ly:music-property music 'element)
1636 (set! (ly:music-property music 'elements)
1639 (set! (ly:music-property music 'articulations)
1643 (define-public (extract-music music pred?)
1644 "Return a flat list of all music matching @var{pred?} inside of
1645 @var{music}, not recursing into matches themselves."
1647 (let loop ((music music) (res '()))
1652 (let ((elt (ly:music-property music 'element)))
1656 (ly:music-property music 'elements))
1657 (ly:music-property music 'articulations))))))
1659 (define-public (extract-named-music music music-name)
1660 "Return a flat list of all music named @var{music-name} (either a
1661 single event symbol or a list of alternatives) inside of @var{music},
1662 not recursing into matches themselves."
1665 (if (cheap-list? music-name)
1666 (lambda (m) (memq (ly:music-property m 'name) music-name))
1667 (lambda (m) (eq? (ly:music-property m 'name) music-name)))))
1669 (define-public (extract-typed-music music type)
1670 "Return a flat list of all music with @var{type} (either a single
1671 type symbol or a list of alternatives) inside of @var{music}, not
1672 recursing into matches themselves."
1675 (if (cheap-list? type)
1677 (any (lambda (t) (music-is-of-type? m t)) type))
1678 (lambda (m) (music-is-of-type? m type)))))
1680 (define*-public (event-chord-wrap! music #:optional parser)
1681 "Wrap isolated rhythmic events and non-postevent events in
1682 @var{music} inside of an @code{EventChord}. If the optional
1683 @var{parser} argument is given, chord repeats @samp{q} are expanded
1684 using the default settings. Otherwise, you need to cater for them
1688 (cond ((music-is-of-type? m 'event-chord)
1689 (if (pair? (ly:music-property m 'articulations))
1691 (set! (ly:music-property m 'elements)
1692 (append (ly:music-property m 'elements)
1693 (ly:music-property m 'articulations)))
1694 (set! (ly:music-property m 'articulations) '())))
1696 ((music-is-of-type? m 'rhythmic-event)
1697 (let ((arts (ly:music-property m 'articulations)))
1699 (set! (ly:music-property m 'articulations) '()))
1700 (make-event-chord (cons m arts))))
1703 (expand-repeat-chords!
1704 (cons 'rhythmic-event
1705 (ly:parser-lookup parser '$chord-repeat-events))
1709 (define-public (event-chord-notes event-chord)
1710 "Return a list of all notes from @var{event-chord}."
1712 (lambda (m) (eq? 'NoteEvent (ly:music-property m 'name)))
1713 (ly:music-property event-chord 'elements)))
1715 (define-public (event-chord-pitches event-chord)
1716 "Return a list of all pitches from @var{event-chord}."
1717 (map (lambda (x) (ly:music-property x 'pitch))
1718 (event-chord-notes event-chord)))