1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 (define-public (music-map function music)
4 "Apply @var{function} to @var{music} and all of the music it contains. "
5 (let* ((es (ly:get-mus-property music 'elements))
6 (e (ly:get-mus-property music 'element))
9 (ly:set-mus-property! music 'elements
10 (map (lambda (y) (music-map function y)) es))
12 (ly:set-mus-property! music 'element (music-map function e)))
16 (define-public (music-filter pred? music)
17 "Filter out music expressions that do not satisfy PRED."
19 (define (inner-music-filter pred? music)
21 (let* ((es (ly:get-mus-property music 'elements))
22 (e (ly:get-mus-property music 'element))
23 (as (ly:get-mus-property music 'articulations))
24 (filtered-as (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) as)))
25 (filtered-e (if (ly:music? e)
26 (inner-music-filter pred? e)
28 (filtered-es (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) es)))
31 (ly:set-mus-property! music 'element filtered-e)
32 (ly:set-mus-property! music 'elements filtered-es)
33 (ly:set-mus-property! music 'articulations filtered-as)
35 ;; if filtering emptied the expression, we remove it completely.
37 (and (eq? filtered-es '()) (not (ly:music? e))
38 (or (not (eq? es '()))
44 (set! music (inner-music-filter pred? music))
47 (make-music-by-name 'Music) ;must return music.
50 (define-public (remove-tag tag)
54 (let* ((tags (ly:get-mus-property m 'tags))
55 (res (memq tag tags)))
58 (define-public (display-music music)
59 "Display music, not done with music-map for clarity of presentation."
63 (let* ((es (ly:get-mus-property music 'elements))
64 (e (ly:get-mus-property music 'element))
67 (display (ly:get-mutable-properties music))
70 (begin (display "\nElements: {\n")
71 (map display-music es)
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 (define (shift-one-duration-log music shift dot)
93 " add SHIFT to ly:duration-log and optionally
94 a dot to any note encountered. This scales the music up by a factor
95 2^shift * (2 - (1/2)^dot)"
99 (d (ly:get-mus-property music 'duration))
103 (cp (ly:duration-factor d))
104 (nd (ly:make-duration (+ shift (ly:duration-log d))
105 (+ dot (ly:duration-dot-count d))
110 (ly:set-mus-property! music 'duration nd)
116 (define-public (shift-duration-log music shift dot)
117 (music-map (lambda (x) (shift-one-duration-log x shift dot))
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 (define-public (note-to-cluster music)
125 "Replace NoteEvents by ClusterNoteEvents."
126 (if (eq? (ly:get-mus-property music 'name) 'NoteEvent)
127 (let* ((cn (make-music-by-name 'ClusterNoteEvent)))
129 (ly:set-mus-property! cn 'pitch (ly:get-mus-property music 'pitch))
130 (ly:set-mus-property! cn 'duration (ly:get-mus-property music 'duration))
134 (define-public (notes-to-clusters music)
135 (music-map note-to-cluster music))
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
140 (define-public (unfold-repeats music)
142 This function replaces all repeats with unfold repeats. It was
143 written by Rune Zedeler. "
144 (let* ((es (ly:get-mus-property music 'elements))
145 (e (ly:get-mus-property music 'element))
146 (n (ly:music-name music)))
148 (if (equal? n "Repeated_music")
151 (ly:get-mus-property music 'iterator-ctor)
152 Chord_tremolo_iterator::constructor)
153 (shift-duration-log music (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
155 (ly:set-mus-property!
156 music 'length Repeated_music::unfolded_music_length)
157 (ly:set-mus-property!
158 music 'start-moment-function Repeated_music::first_start)
159 (ly:set-mus-property!
160 music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
163 (ly:set-mus-property!
165 (map unfold-repeats es)))
168 (ly:set-mus-property!
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;; property setting music objs.
178 (define-public (make-grob-property-set grob gprop val)
180 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
181 i.e. this is not an override"
183 (let* ((m (make-music-by-name 'OverrideProperty)))
184 (ly:set-mus-property! m 'symbol grob)
185 (ly:set-mus-property! m 'grob-property gprop)
186 (ly:set-mus-property! m 'grob-value val)
187 (ly:set-mus-property! m 'pop-first #t)
192 (define-public (make-grob-property-override grob gprop val)
194 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
195 i.e. this is not an override"
197 (let* ((m (make-music-by-name 'OverrideProperty)))
198 (ly:set-mus-property! m 'symbol grob)
199 (ly:set-mus-property! m 'grob-property gprop)
200 (ly:set-mus-property! m 'grob-value val)
207 (define-public (make-grob-property-revert grob gprop)
208 "Revert the grob property GPROP for GROB."
209 (let* ((m (make-music-by-name 'OverrideProperty)))
210 (ly:set-mus-property! m 'symbol grob)
211 (ly:set-mus-property! m 'grob-property gprop)
217 (define direction-polyphonic-grobs
218 '(Tie Slur Script TextScript Stem Dots DotColumn))
220 (define-public (make-voice-props-set n)
221 (make-sequential-music
223 (map (lambda (x) (make-grob-property-set x 'direction
225 direction-polyphonic-grobs)
227 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
228 (make-grob-property-set 'MultiMeasureRest 'staff-position
236 (define-public (make-voice-props-revert)
237 (make-sequential-music
239 (map (lambda (x) (make-grob-property-revert x 'direction))
240 direction-polyphonic-grobs)
242 (list (make-grob-property-revert 'NoteColumn 'horizontal-shift))
247 (define-public (context-spec-music m context . rest)
248 "Add \\context CONTEXT = foo to M. "
250 (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
251 (ly:set-mus-property! cm 'element m)
252 (ly:set-mus-property! cm 'context-type context)
253 (if (and (pair? rest) (string? (car rest)))
254 (ly:set-mus-property! cm 'context-id (car rest))
259 (define-public (make-apply-context func)
261 ((m (make-music-by-name 'ApplyContext)))
263 (ly:set-mus-property! m 'procedure func)
267 (define-public (make-sequential-music elts)
268 (let* ((m (make-music-by-name 'SequentialMusic)))
269 (ly:set-mus-property! m 'elements elts)
273 (define-public (make-simultaneous-music elts)
274 (let* ((m (make-music-by-name 'SimultaneousMusic)))
275 (ly:set-mus-property! m 'elements elts)
279 (define-public (make-event-chord elts)
280 (let* ((m (make-music-by-name 'EventChord)))
281 (ly:set-mus-property! m 'elements elts)
286 (define-public (make-nonevent-skip dur)
287 (let* ((m (make-music-by-name 'NonEventSkip)))
288 (ly:set-mus-property! m 'duration dur)
295 (define-public (make-multi-measure-rest duration location)
298 (start (make-music-by-name 'MultiMeasureRestEvent))
299 (stop (make-music-by-name 'MultiMeasureRestEvent))
300 (skip ( make-music-by-name 'SkipEvent))
301 (ch (make-music-by-name 'BarCheck))
302 (ch2 (make-music-by-name 'BarCheck))
303 (seq (make-music-by-name 'MultiMeasureRestMusicGroup))
306 (map (lambda (x) (ly:set-mus-property! x 'origin location))
307 (list start stop skip ch ch2 seq))
308 (ly:set-mus-property! start 'span-direction START)
309 (ly:set-mus-property! stop 'span-direction STOP)
310 (ly:set-mus-property! skip 'duration duration)
311 (ly:set-mus-property! seq 'elements
314 (make-event-chord (list start))
315 (make-event-chord (list skip))
316 (make-event-chord (list stop))
323 (define-public (glue-mm-rest-texts music)
324 "Check if we have R1*4-\\markup { .. }, and if applicable convert to
325 a property set for MultiMeasureRestNumber."
327 (define (script-to-mmrest-text script-music)
328 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
332 (text (ly:get-mus-property script-music 'text))
333 (dir (ly:get-mus-property script-music 'direction))
334 (p (make-music-by-name 'MultiMeasureTextEvent))
338 (ly:set-mus-property! p 'direction dir))
339 (ly:set-mus-property! p 'text text)
343 (if (eq? (ly:get-mus-property music 'name) 'MultiMeasureRestMusicGroup)
346 (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
347 (es (ly:get-mus-property music 'elements))
348 (texts (map script-to-mmrest-text (filter text? es)))
349 (others (remove text? es))
352 (ly:set-mus-property!
354 (cons (make-event-chord texts) others)
361 (define-public (make-property-set sym val)
364 (m (make-music-by-name 'PropertySet))
366 (ly:set-mus-property! m 'symbol sym)
367 (ly:set-mus-property! m 'value val)
373 (define-public (make-ottava-set octavation)
375 ((m (make-music-by-name 'ApplyContext)))
378 (define (ottava-modify context)
379 "Either reset centralCPosition to the stored original, or remember
380 old centralCPosition, add OCTAVATION to centralCPosition, and set
381 OTTAVATION to `8va', or whatever appropriate."
383 (if (number? (ly:get-context-property context 'centralCPosition))
387 ((where (ly:context-property-where-defined context 'centralCPosition))
388 (oc0 (ly:get-context-property context 'originalCentralCPosition)))
390 (ly:set-context-property! context 'centralCPosition oc0)
391 (ly:unset-context-property where 'originalCentralCPosition)
392 (ly:unset-context-property where 'ottavation))
395 ((where (ly:context-property-where-defined context 'centralCPosition))
396 (c0 (ly:get-context-property context 'centralCPosition))
397 (new-c0 (+ c0 (* -7 octavation)))
399 (assoc octavation '((2 . "15ma")
403 (-2 . "15ma bassa"))))))
405 (ly:set-context-property! context 'centralCPosition new-c0)
406 (ly:set-context-property! context 'originalCentralCPosition c0)
407 (ly:set-context-property! context 'ottavation string)
411 (ly:set-mus-property! m 'procedure ottava-modify)
412 (context-spec-music m 'Staff)
415 (define-public (set-octavation ottavation)
416 (ly:export (make-ottava-set ottavation)))
418 (define-public (make-time-signature-set num den . rest)
419 " Set properties for time signature NUM/DEN.
420 Rest can contain a list of beat groupings
426 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
427 (beat (ly:make-moment 1 den))
428 (len (ly:make-moment num den))
429 (set2 (make-property-set 'beatLength beat))
430 (set3 (make-property-set 'measureLength len))
431 (set4 (make-property-set 'beatGrouping (if (pair? rest)
434 (basic (list set1 set2 set3 set4)))
437 (make-sequential-music basic) 'Timing)))
439 (define-public (make-mark-set label)
440 " Set properties for time signature NUM/DEN.
441 Rest can contain a list of beat groupings
446 ((set (if (integer? label)
447 (context-spec-music (make-property-set 'rehearsalMark label)
450 (ev (make-music-by-name 'MarkEvent))
451 (ch (make-event-chord (list ev)))
456 (make-sequential-music (list set ch))
458 (ly:set-mus-property! ev 'label label)
463 (define-public (set-time-signature num den . rest)
464 (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
466 (define-public (make-penalty-music pen)
468 ((m (make-music-by-name 'BreakEvent)))
469 (ly:set-mus-property! m 'penalty pen)
472 (define-public (make-articulation name)
474 (m (make-music-by-name 'ArticulationEvent))
476 (ly:set-mus-property! m 'articulation-type name)
480 (define-public (make-lyric-event string duration)
481 (let* ((m (make-music-by-name 'LyricEvent)))
483 (ly:set-mus-property! m 'duration duration)
484 (ly:set-mus-property! m 'text string)
487 (define-public (make-span-event type spandir)
489 (m (make-music-by-name type))
491 (ly:set-mus-property! m 'span-direction spandir)
495 (define-public (set-mus-properties! m alist)
496 "Set all of ALIST as properties of M."
499 (ly:set-mus-property! m (caar alist) (cdar alist))
500 (set-mus-properties! m (cdr alist)))
505 (define-public (music-separator? m)
507 (let* ((ts (ly:get-mus-property m 'types )))
512 ;;; splitting chords into voices.
514 (define (voicify-list lst number)
515 "Make a list of Musics.
517 voicify-list :: [ [Music ] ] -> number -> [Music]
518 LST is a list music-lists.
522 (cons (context-spec-music
523 (make-sequential-music
525 (make-voice-props-set number)
526 (make-simultaneous-music (car lst))))
528 'Voice (number->string number))
529 (voicify-list (cdr lst) (+ number 1))
533 (define (voicify-chord ch)
534 "Split the parts of a chord into different Voices using separator"
535 (let* ((es (ly:get-mus-property ch 'elements)))
537 (ly:set-mus-property! ch 'elements
538 (voicify-list (split-list es music-separator?) 0))
542 (define (voicify-music m)
543 "Recursively split chords that are separated with \\ "
545 (if (not (ly:music? m))
547 (error "not music!"))
550 ((es (ly:get-mus-property m 'elements))
551 (e (ly:get-mus-property m 'element))
554 (ly:set-mus-property! m 'elements (map voicify-music es)))
556 (ly:set-mus-property! m 'element (voicify-music e)))
558 (and (equal? (ly:music-name m) "Simultaneous_music")
559 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
566 (define-public (empty-music)
567 (ly:export (make-music-by-name 'Music))
571 ; Make a function that checks score element for being of a specific type.
572 (define-public (make-type-checker symbol)
575 ;;(eq? #t (ly:get-grob-property elt symbol))
576 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
578 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
580 (ly:set-grob-property! grob sym val)))
583 (define-public ((set-output-property grob-name symbol val) grob grob-c context)
586 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
591 ((meta (ly:get-grob-property grob 'meta)))
593 (if (equal? (cdr (assoc 'name meta)) grob-name)
594 (ly:set-grob-property! grob symbol val)
599 (define-public (smart-bar-check n)
600 "Make a bar check that checks for a specific bar number.
604 (m (make-music-by-name 'ApplyContext))
608 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
612 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
616 (ly:set-mus-property! m 'procedure checker)
620 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
621 ;; warn for bare chords at start.
623 (define (has-request-chord elts)
624 (reduce (lambda (x y) (or x y)) #f (map (lambda (x) (equal? (ly:music-name x)
625 "Request_chord")) elts)
628 (define (ly:music-message music msg)
631 (ip (ly:get-mus-property music 'origin))
634 (if (ly:input-location? ip)
635 (ly:input-message ip msg)
639 (define (check-start-chords music)
640 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
643 ((es (ly:get-mus-property music 'elements))
644 (e (ly:get-mus-property music 'element))
645 (name (ly:music-name music))
649 ((equal? name "Context_specced_music") #t)
650 ((equal? name "Simultaneous_music")
652 (if (has-request-chord es)
653 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
654 (map check-start-chords es)))
656 ((equal? name "Sequential_music")
658 (check-start-chords (car es))))
659 (else (if (ly:music? e) (check-start-chords e )))
667 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
669 ;; setting stuff for grace context.
672 (define (vector-extend v x)
673 "Make a new vector consisting of V, with X added to the end."
675 ((n (vector-length v))
676 (nv (make-vector (+ n 1) '())))
679 (vector-move-left! v 0 n nv 0)
684 (define (vector-map f v)
685 "Map F over V. This function returns nothing."
687 ((n (vector-length v))
691 (f (vector-ref v i))))
693 (define (vector-reverse-map f v)
694 "Map F over V, N to 0 order. This function returns nothing."
696 ((i (- (vector-length v) 1) (- i 1)))
699 (f (vector-ref v i))))
701 ;; TODO: make a remove-grace-property too.
702 (define-public (add-grace-property context-name grob sym val)
703 "Set SYM=VAL for GROB in CONTEXT-NAME. "
704 (define (set-prop context)
706 ((where (ly:context-property-where-defined context 'graceSettings))
707 (current (ly:get-context-property where 'graceSettings))
708 (new-settings (vector-extend current (list context-name grob sym val)))
710 (ly:set-context-property! where 'graceSettings new-settings)))
712 (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
715 (define-public (set-start-grace-properties context)
716 (define (execute-1 x)
718 ((tr (ly:translator-find context (car x))))
721 (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x))
725 ((props (ly:get-context-property context 'graceSettings)))
727 (vector-map execute-1 props))))
729 (define-public (set-stop-grace-properties context)
730 (define (execute-1 x)
732 ((tr (ly:translator-find context (car x))))
734 (ly:context-pushpop-property tr (cadr x) (caddr x))
738 ((props (ly:get-context-property context 'graceSettings)))
740 (vector-reverse-map execute-1 props))))
742 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
743 ;; switch it on here, so parsing and init isn't checked (too slow!)
745 ;; automatic music transformations.
747 (define (switch-on-debugging m)
748 (set-debug-cell-accesses! 15000)
751 (define-public toplevel-music-functions
753 ;; check-start-chords ; ; no longer needed with chord syntax.
755 (lambda (x) (music-map glue-mm-rest-texts x))
756 ; switch-on-debugging
765 (define (apply-durations lyric-music durations)
766 (define (apply-duration music)
767 (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
768 (ly:duration? (ly:get-mus-property music 'duration)))
770 (ly:set-mus-property! music 'duration (car durations))
771 (set! durations (cdr durations))
774 (music-map apply-duration lyric-music))
777 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
781 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
782 "Usage: see input/regression/balloon.ly "
784 ((meta (ly:get-grob-property grob 'meta))
785 (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
786 (cb (ly:get-grob-property grob 'molecule-callback)))
788 (if (equal? nm object-name)
790 (ly:set-grob-property! grob 'molecule-callback Balloon_interface::brew_molecule)
791 (ly:set-grob-property! grob 'balloon-original-callback cb)
792 (ly:set-grob-property! grob 'balloon-text text)
793 (ly:set-grob-property! grob 'balloon-text-offset off)
794 (ly:set-grob-property! grob 'balloon-text-props '((font-family . roman)))