2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 (define-public (music-map function music)
5 "Apply @var{function} to @var{music} and all of the music it contains. "
6 (let* ((es (ly:get-mus-property music 'elements))
7 (e (ly:get-mus-property music 'element))
10 (ly:set-mus-property! music 'elements
11 (map (lambda (y) (music-map function y)) es))
13 (ly:set-mus-property! music 'element (music-map function e)))
17 (define-public (display-music music)
18 "Display music, not done with music-map for clarity of presentation."
22 (let* ((es (ly:get-mus-property music 'elements))
23 (e (ly:get-mus-property music 'element))
26 (display (ly:get-mutable-properties music))
29 (begin (display "\nElements: {\n")
30 (map display-music es)
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 (define (shift-one-duration-log music shift dot)
53 " add SHIFT to ly:duration-log and optionally
54 a dot to any note encountered. This scales the music up by a factor
55 2^shift * (2 - (1/2)^dot)"
59 (d (ly:get-mus-property music 'duration))
63 (cp (ly:duration-factor d))
64 (nd (ly:make-duration (+ shift (ly:duration-log d))
65 (+ dot (ly:duration-dot-count d))
70 (ly:set-mus-property! music 'duration nd)
76 (define-public (shift-duration-log music shift dot)
77 (music-map (lambda (x) (shift-one-duration-log x shift dot))
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 (define-public (unfold-repeats music)
88 This function replaces all repeats with unfold repeats. It was
89 written by Rune Zedeler. "
90 (let* ((es (ly:get-mus-property music 'elements))
91 (e (ly:get-mus-property music 'element))
92 (n (ly:music-name music)))
94 (if (equal? n "Repeated_music")
97 (ly:get-mus-property music 'iterator-ctor)
98 Chord_tremolo_iterator::constructor)
99 (shift-duration-log music (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
101 (ly:set-mus-property!
102 music 'length Repeated_music::unfolded_music_length)
103 (ly:set-mus-property!
104 music 'start-moment-function Repeated_music::first_start)
105 (ly:set-mus-property!
106 music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
109 (ly:set-mus-property!
111 (map unfold-repeats es)))
114 (ly:set-mus-property!
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;; property setting music objs.
124 (define-public (make-grob-property-set grob gprop val)
126 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
127 i.e. this is not an override"
129 (let* ((m (make-music-by-name 'OverrideProperty)))
130 (ly:set-mus-property! m 'symbol grob)
131 (ly:set-mus-property! m 'grob-property gprop)
132 (ly:set-mus-property! m 'grob-value val)
133 (ly:set-mus-property! m 'pop-first #t)
140 (define-public (make-grob-property-revert grob gprop)
141 "Revert the grob property GPROP for GROB."
142 (let* ((m (make-music-by-name 'OverrideProperty)))
143 (ly:set-mus-property! m 'symbol grob)
144 (ly:set-mus-property! m 'grob-property gprop)
151 (define-public (make-voice-props-set n)
152 (make-sequential-music
154 (map (lambda (x) (make-grob-property-set x 'direction
156 '(Tie Slur Stem Dots))
158 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
159 (make-grob-property-set 'MultiMeasureRest 'staff-position
168 (define-public (make-voice-props-revert)
169 (make-sequential-music
171 (make-grob-property-revert 'Tie 'direction)
172 (make-grob-property-revert 'Dots 'direction)
173 (make-grob-property-revert 'Stem 'direction)
174 (make-grob-property-revert 'Slur 'direction)
175 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
180 (define-public (context-spec-music m context . rest)
181 "Add \context CONTEXT = foo to M. "
183 (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
184 (ly:set-mus-property! cm 'element m)
185 (ly:set-mus-property! cm 'context-type context)
186 (if (and (pair? rest) (string? (car rest)))
187 (ly:set-mus-property! cm 'context-id (car rest))
192 (define-public (make-apply-context func)
195 (m (make-music-by-name 'ApplyContext))
198 (ly:set-mus-property! m 'procedure func)
202 (define-public (make-sequential-music elts)
203 (let* ((m (make-music-by-name 'SequentialMusic)))
204 (ly:set-mus-property! m 'elements elts)
208 (define-public (make-simultaneous-music elts)
209 (let* ((m (make-music-by-name 'SimultaneousMusic)))
210 (ly:set-mus-property! m 'elements elts)
214 (define-public (make-event-chord elts)
215 (let* ((m (make-music-by-name 'EventChord)))
216 (ly:set-mus-property! m 'elements elts)
222 (define-public (make-multi-measure-rest duration location)
225 (start (make-music-by-name 'MultiMeasureRestEvent))
226 (stop (make-music-by-name 'MultiMeasureRestEvent))
227 (skip ( make-music-by-name 'SkipEvent))
228 (ch (make-music-by-name 'BarCheck))
229 (ch2 (make-music-by-name 'BarCheck))
230 (seq (make-music-by-name 'MultiMeasureRestMusicGroup))
233 (map (lambda (x) (ly:set-mus-property! x 'origin location))
234 (list start stop skip ch ch2 seq))
235 (ly:set-mus-property! start 'span-direction START)
236 (ly:set-mus-property! stop 'span-direction STOP)
237 (ly:set-mus-property! skip 'duration duration)
238 (ly:set-mus-property! seq 'elements
241 (make-event-chord (list start))
242 (make-event-chord (list skip))
243 (make-event-chord (list stop))
250 (define-public (glue-mm-rest-texts music)
251 "Check if we have R1*4-\markup { .. }, and if applicable convert to
252 a property set for MultiMeasureRestNumber."
254 (define (script-to-mmrest-text script-music)
255 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
259 (text (ly:get-mus-property script-music 'text))
260 (dir (ly:get-mus-property script-music 'direction))
261 (p (make-music-by-name 'MultiMeasureTextEvent))
265 (ly:set-mus-property! p 'direction dir))
266 (ly:set-mus-property! p 'text text)
270 (if (eq? (ly:get-mus-property music 'name) 'MultiMeasureRestMusicGroup)
273 (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
274 (es (ly:get-mus-property music 'elements))
275 (texts (map script-to-mmrest-text (filter-list text? es)))
276 (others (filter-out-list text? es))
279 (ly:set-mus-property!
281 (cons (make-event-chord texts) others)
288 (define-public (make-property-set sym val)
291 (m (make-music-by-name 'PropertySet))
293 (ly:set-mus-property! m 'symbol sym)
294 (ly:set-mus-property! m 'value val)
300 (define-public (make-ottava-set octavation)
303 (m (make-music-by-name 'ApplyContext))
307 (define (ottava-modify context)
308 "Either reset centralCPosition to the stored original,
309 or remember old centralCPosition, add OCTAVATION to centralCPosition,
310 and set OTTAVATION to `8va', or whatever appropriate.
314 ((where (ly:context-property-where-defined context 'centralCPosition))
315 (oc0 (ly:get-context-property context 'originalCentralCPosition))
319 (ly:set-context-property context 'centralCPosition oc0)
320 (ly:unset-context-property where 'originalCentralCPosition)
321 (ly:unset-context-property where 'ottavation)
325 ((where (ly:context-property-where-defined context 'centralCPosition))
326 (c0 (ly:get-context-property context 'centralCPosition))
327 (new-c0 (+ c0 (* -7 octavation)))
329 (assoc octavation '((2 . "15ma")
333 (-2 . "15ma bassa")))))
336 (ly:set-context-property where 'centralCPosition new-c0)
337 (ly:set-context-property where 'originalCentralCPosition c0)
338 (ly:set-context-property where 'ottavation string)
342 (ly:set-mus-property! m 'procedure ottava-modify)
346 (define-public (set-octavation ottavation)
347 (ly:export (make-ottava-set ottavation)))
349 (define-public (make-time-signature-set num den . rest)
350 " Set properties for time signature NUM/DEN.
351 Rest can contain a list of beat groupings
357 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
358 (beat (ly:make-moment 1 den))
359 (len (ly:make-moment num den))
360 (set2 (make-property-set 'beatLength beat))
361 (set3 (make-property-set 'measureLength len))
362 (set4 (make-property-set 'beatGrouping (if (pair? rest)
365 (basic (list set1 set2 set3 set4))
370 (make-sequential-music basic) "Timing")))
372 (define-public (set-time-signature num den . rest)
373 (ly:export (apply make-time-signature-set `(,num ,den . ,rest)))
376 (define-public (make-penalty-music pen)
378 ((m (make-music-by-name 'BreakEvent)))
379 (ly:set-mus-property! m 'penalty pen)
382 (define-public (make-articulation name)
384 (m (make-music-by-name 'ArticulationEvent))
386 (ly:set-mus-property! m 'articulation-type name)
390 (define-public (make-span-event type spandir)
392 (m (make-music-by-name type))
394 (ly:set-mus-property! m 'span-direction spandir)
398 (define-public (set-mus-properties! m alist)
399 "Set all of ALIST as properties of M."
402 (ly:set-mus-property! m (caar alist) (cdar alist))
403 (set-mus-properties! m (cdr alist)))
406 (define-public (music-separator? m)
408 (let* ((ts (ly:get-mus-property m 'types )))
413 ;;; splitting chords into voices.
415 (define (voicify-list lst number)
416 "Make a list of Musics.
418 voicify-list :: [ [Music ] ] -> number -> [Music]
419 LST is a list music-lists.
423 (cons (context-spec-music
424 (make-sequential-music
426 (make-voice-props-set number)
427 (make-simultaneous-music (car lst))))
429 "Voice" (number->string number))
430 (voicify-list (cdr lst) (+ number 1))
434 (define (voicify-chord ch)
435 "Split the parts of a chord into different Voices using separator"
436 (let* ((es (ly:get-mus-property ch 'elements)))
439 (ly:set-mus-property! ch 'elements
440 (voicify-list (split-list es music-separator?) 0))
444 (define (voicify-music m)
445 "Recursively split chords that are separated with \\ "
447 (if (not (ly:music? m))
449 (error "not music!"))
452 ((es (ly:get-mus-property m 'elements))
453 (e (ly:get-mus-property m 'element))
456 (ly:set-mus-property! m 'elements (map voicify-music es)))
458 (ly:set-mus-property! m 'element (voicify-music e)))
460 (and (equal? (ly:music-name m) "Simultaneous_music")
461 (reduce (lambda (x y ) (or x y)) (map music-separator? es)))
468 (define-public (empty-music)
469 (ly:export (make-music-by-name 'Music))
473 ; Make a function that checks score element for being of a specific type.
474 (define-public (make-type-checker symbol)
477 ;;(eq? #t (ly:get-grob-property elt symbol))
478 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
482 (define-public (smart-bar-check n)
483 "Make a bar check that checks for a specific bar number.
487 (m (make-music-by-name 'ApplyContext))
491 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
495 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
499 (ly:set-mus-property! m 'procedure checker)
503 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504 ;; warn for bare chords at start.
506 (define (has-request-chord elts)
507 (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly:music-name x)
508 "Request_chord")) elts)
511 (define (ly:music-message music msg)
514 (ip (ly:get-mus-property music 'origin))
517 (if (ly:input-location? ip)
518 (ly:input-message ip msg)
522 (define (check-start-chords music)
523 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
526 ((es (ly:get-mus-property music 'elements))
527 (e (ly:get-mus-property music 'element))
528 (name (ly:music-name music))
532 ((equal? name "Context_specced_music") #t)
533 ((equal? name "Simultaneous_music")
535 (if (has-request-chord es)
536 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
537 (map check-start-chords es)))
539 ((equal? name "Sequential_music")
541 (check-start-chords (car es))))
542 (else (if (ly:music? e) (check-start-chords e )))
548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 ;; switch it on here, so parsing and init isn't checked (too slow!)
551 ;; automatic music transformations.
553 (define (switch-on-debugging m)
554 (set-debug-cell-accesses! 15000)
558 (define-public toplevel-music-functions
559 (list check-start-chords
561 (lambda (x) (music-map glue-mm-rest-texts x))
562 ; switch-on-debugging