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)
194 (define-public (make-grob-property-revert grob gprop)
195 "Revert the grob property GPROP for GROB."
196 (let* ((m (make-music-by-name 'OverrideProperty)))
197 (ly:set-mus-property! m 'symbol grob)
198 (ly:set-mus-property! m 'grob-property gprop)
205 (define-public (make-voice-props-set n)
206 (make-sequential-music
208 (map (lambda (x) (make-grob-property-set x 'direction
210 '(Tie Slur Script TextScript Stem Dots))
212 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
213 (make-grob-property-set 'MultiMeasureRest 'staff-position
222 (define-public (make-voice-props-revert)
223 (make-sequential-music
225 (make-grob-property-revert 'Tie 'direction)
226 (make-grob-property-revert 'Dots 'direction)
227 (make-grob-property-revert 'Stem 'direction)
228 (make-grob-property-revert 'Slur 'direction)
229 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
234 (define-public (context-spec-music m context . rest)
235 "Add \context CONTEXT = foo to M. "
237 (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
238 (ly:set-mus-property! cm 'element m)
239 (ly:set-mus-property! cm 'context-type context)
240 (if (and (pair? rest) (string? (car rest)))
241 (ly:set-mus-property! cm 'context-id (car rest))
246 (define-public (make-apply-context func)
248 ((m (make-music-by-name 'ApplyContext)))
250 (ly:set-mus-property! m 'procedure func)
254 (define-public (make-sequential-music elts)
255 (let* ((m (make-music-by-name 'SequentialMusic)))
256 (ly:set-mus-property! m 'elements elts)
260 (define-public (make-simultaneous-music elts)
261 (let* ((m (make-music-by-name 'SimultaneousMusic)))
262 (ly:set-mus-property! m 'elements elts)
266 (define-public (make-event-chord elts)
267 (let* ((m (make-music-by-name 'EventChord)))
268 (ly:set-mus-property! m 'elements elts)
273 (define-public (make-nonevent-skip dur)
274 (let* ((m (make-music-by-name 'NonEventSkip)))
275 (ly:set-mus-property! m 'duration dur)
282 (define-public (make-multi-measure-rest duration location)
285 (start (make-music-by-name 'MultiMeasureRestEvent))
286 (stop (make-music-by-name 'MultiMeasureRestEvent))
287 (skip ( make-music-by-name 'SkipEvent))
288 (ch (make-music-by-name 'BarCheck))
289 (ch2 (make-music-by-name 'BarCheck))
290 (seq (make-music-by-name 'MultiMeasureRestMusicGroup))
293 (map (lambda (x) (ly:set-mus-property! x 'origin location))
294 (list start stop skip ch ch2 seq))
295 (ly:set-mus-property! start 'span-direction START)
296 (ly:set-mus-property! stop 'span-direction STOP)
297 (ly:set-mus-property! skip 'duration duration)
298 (ly:set-mus-property! seq 'elements
301 (make-event-chord (list start))
302 (make-event-chord (list skip))
303 (make-event-chord (list stop))
310 (define-public (glue-mm-rest-texts music)
311 "Check if we have R1*4-\markup { .. }, and if applicable convert to
312 a property set for MultiMeasureRestNumber."
314 (define (script-to-mmrest-text script-music)
315 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
319 (text (ly:get-mus-property script-music 'text))
320 (dir (ly:get-mus-property script-music 'direction))
321 (p (make-music-by-name 'MultiMeasureTextEvent))
325 (ly:set-mus-property! p 'direction dir))
326 (ly:set-mus-property! p 'text text)
330 (if (eq? (ly:get-mus-property music 'name) 'MultiMeasureRestMusicGroup)
333 (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
334 (es (ly:get-mus-property music 'elements))
335 (texts (map script-to-mmrest-text (filter text? es)))
336 (others (remove text? es))
339 (ly:set-mus-property!
341 (cons (make-event-chord texts) others)
348 (define-public (make-property-set sym val)
351 (m (make-music-by-name 'PropertySet))
353 (ly:set-mus-property! m 'symbol sym)
354 (ly:set-mus-property! m 'value val)
360 (define-public (make-ottava-set octavation)
362 ((m (make-music-by-name 'ApplyContext)))
365 (define (ottava-modify context)
366 "Either reset centralCPosition to the stored original,
367 or remember old centralCPosition, add OCTAVATION to centralCPosition,
368 and set OTTAVATION to `8va', or whatever appropriate."
369 (if (number? (ly:get-context-property context 'centralCPosition))
373 ((where (ly:context-property-where-defined context 'centralCPosition))
374 (oc0 (ly:get-context-property context 'originalCentralCPosition)))
376 (ly:set-context-property! context 'centralCPosition oc0)
377 (ly:unset-context-property where 'originalCentralCPosition)
378 (ly:unset-context-property where 'ottavation))
381 ((where (ly:context-property-where-defined context 'centralCPosition))
382 (c0 (ly:get-context-property context 'centralCPosition))
383 (new-c0 (+ c0 (* -7 octavation)))
385 (assoc octavation '((2 . "15ma")
389 (-2 . "15ma bassa"))))))
391 (ly:set-context-property! context 'centralCPosition new-c0)
392 (ly:set-context-property! context 'originalCentralCPosition c0)
393 (ly:set-context-property! context 'ottavation string)
397 (ly:set-mus-property! m 'procedure ottava-modify)
398 (context-spec-music m 'Staff)
401 (define-public (set-octavation ottavation)
402 (ly:export (make-ottava-set ottavation)))
404 (define-public (make-time-signature-set num den . rest)
405 " Set properties for time signature NUM/DEN.
406 Rest can contain a list of beat groupings
412 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
413 (beat (ly:make-moment 1 den))
414 (len (ly:make-moment num den))
415 (set2 (make-property-set 'beatLength beat))
416 (set3 (make-property-set 'measureLength len))
417 (set4 (make-property-set 'beatGrouping (if (pair? rest)
420 (basic (list set1 set2 set3 set4)))
423 (make-sequential-music basic) 'Timing)))
425 (define-public (set-time-signature num den . rest)
426 (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
428 (define-public (make-penalty-music pen)
430 ((m (make-music-by-name 'BreakEvent)))
431 (ly:set-mus-property! m 'penalty pen)
434 (define-public (make-articulation name)
436 (m (make-music-by-name 'ArticulationEvent))
438 (ly:set-mus-property! m 'articulation-type name)
442 (define-public (make-lyric-event string duration)
443 (let* ((m (make-music-by-name 'LyricEvent)))
445 (ly:set-mus-property! m 'duration duration)
446 (ly:set-mus-property! m 'text string)
449 (define-public (make-span-event type spandir)
451 (m (make-music-by-name type))
453 (ly:set-mus-property! m 'span-direction spandir)
457 (define-public (set-mus-properties! m alist)
458 "Set all of ALIST as properties of M."
461 (ly:set-mus-property! m (caar alist) (cdar alist))
462 (set-mus-properties! m (cdr alist)))
467 (define-public (music-separator? m)
469 (let* ((ts (ly:get-mus-property m 'types )))
474 ;;; splitting chords into voices.
476 (define (voicify-list lst number)
477 "Make a list of Musics.
479 voicify-list :: [ [Music ] ] -> number -> [Music]
480 LST is a list music-lists.
484 (cons (context-spec-music
485 (make-sequential-music
487 (make-voice-props-set number)
488 (make-simultaneous-music (car lst))))
490 'Voice (number->string number))
491 (voicify-list (cdr lst) (+ number 1))
495 (define (voicify-chord ch)
496 "Split the parts of a chord into different Voices using separator"
497 (let* ((es (ly:get-mus-property ch 'elements)))
500 (ly:set-mus-property! ch 'elements
501 (voicify-list (split-list es music-separator?) 0))
505 (define (voicify-music m)
506 "Recursively split chords that are separated with \\ "
508 (if (not (ly:music? m))
510 (error "not music!"))
513 ((es (ly:get-mus-property m 'elements))
514 (e (ly:get-mus-property m 'element))
517 (ly:set-mus-property! m 'elements (map voicify-music es)))
519 (ly:set-mus-property! m 'element (voicify-music e)))
521 (and (equal? (ly:music-name m) "Simultaneous_music")
522 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
529 (define-public (empty-music)
530 (ly:export (make-music-by-name 'Music))
534 ; Make a function that checks score element for being of a specific type.
535 (define-public (make-type-checker symbol)
538 ;;(eq? #t (ly:get-grob-property elt symbol))
539 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
541 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
543 (ly:set-grob-property! grob sym val)))
546 (define-public (smart-bar-check n)
547 "Make a bar check that checks for a specific bar number.
551 (m (make-music-by-name 'ApplyContext))
555 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
559 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
563 (ly:set-mus-property! m 'procedure checker)
567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
568 ;; warn for bare chords at start.
570 (define (has-request-chord elts)
571 (reduce (lambda (x y) (or x y)) #f (map (lambda (x) (equal? (ly:music-name x)
572 "Request_chord")) elts)
575 (define (ly:music-message music msg)
578 (ip (ly:get-mus-property music 'origin))
581 (if (ly:input-location? ip)
582 (ly:input-message ip msg)
586 (define (check-start-chords music)
587 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
590 ((es (ly:get-mus-property music 'elements))
591 (e (ly:get-mus-property music 'element))
592 (name (ly:music-name music))
596 ((equal? name "Context_specced_music") #t)
597 ((equal? name "Simultaneous_music")
599 (if (has-request-chord es)
600 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
601 (map check-start-chords es)))
603 ((equal? name "Sequential_music")
605 (check-start-chords (car es))))
606 (else (if (ly:music? e) (check-start-chords e )))
614 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
616 ;; setting stuff for grace context.
619 (define (vector-extend v x)
620 "Make a new vector consisting of V, with X added to the end."
622 ((n (vector-length v))
623 (nv (make-vector (+ n 1) '())))
626 (vector-move-left! v 0 n nv 0)
631 (define (vector-map f v)
632 "Map F over V. This function returns nothing."
634 ((n (vector-length v))
638 (f (vector-ref v i))))
640 (define (vector-reverse-map f v)
641 "Map F over V, N to 0 order. This function returns nothing."
643 ((i (- (vector-length v) 1) (- i 1)))
646 (f (vector-ref v i))))
648 ;; TODO: make a remove-grace-property too.
649 (define-public (add-grace-property context-name grob sym val)
650 "Set SYM=VAL for GROB in CONTEXT-NAME. "
651 (define (set-prop context)
653 ((where (ly:context-property-where-defined context 'graceSettings))
654 (current (ly:get-context-property where 'graceSettings))
655 (new-settings (vector-extend current (list context-name grob sym val)))
657 (ly:set-context-property! where 'graceSettings new-settings)))
659 (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
662 (define-public (set-start-grace-properties context)
663 (define (execute-1 x)
665 ((tr (ly:translator-find context (car x))))
668 (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x))
672 ((props (ly:get-context-property context 'graceSettings)))
674 (vector-map execute-1 props))))
676 (define-public (set-stop-grace-properties context)
677 (define (execute-1 x)
679 ((tr (ly:translator-find context (car x))))
681 (ly:context-pushpop-property tr (cadr x) (caddr x))
685 ((props (ly:get-context-property context 'graceSettings)))
687 (vector-reverse-map execute-1 props))))
689 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
690 ;; switch it on here, so parsing and init isn't checked (too slow!)
692 ;; automatic music transformations.
694 (define (switch-on-debugging m)
695 (set-debug-cell-accesses! 15000)
698 (define-public toplevel-music-functions
699 (list check-start-chords
701 (lambda (x) (music-map glue-mm-rest-texts x))
702 ; switch-on-debugging