1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 (define-public (denominator-tuplet-formatter mus)
5 (number->string (ly:get-mus-property mus 'denominator)))
7 (define-public (fraction-tuplet-formatter mus)
8 (string-append (number->string (ly:get-mus-property mus 'numerator))
10 (number->string (ly:get-mus-property mus 'denominator))
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 (define-public (music-map function music)
16 "Apply @var{function} to @var{music} and all of the music it contains. "
17 (let* ((es (ly:get-mus-property music 'elements))
18 (e (ly:get-mus-property music 'element))
21 (ly:set-mus-property! music 'elements
22 (map (lambda (y) (music-map function y)) es))
24 (ly:set-mus-property! music 'element (music-map function e)))
28 (define-public (display-music music)
29 "Display music, not done with music-map for clarity of presentation."
33 (let* ((es (ly:get-mus-property music 'elements))
34 (e (ly:get-mus-property music 'element))
37 (display (ly:get-mutable-properties music))
40 (begin (display "\nElements: {\n")
41 (map display-music es)
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 (define (shift-one-duration-log music shift dot)
64 " add SHIFT to ly:duration-log and optionally
65 a dot to any note encountered. This scales the music up by a factor
66 2^shift * (2 - (1/2)^dot)"
70 (d (ly:get-mus-property music 'duration))
74 (cp (ly:duration-factor d))
75 (nd (ly:make-duration (+ shift (ly:duration-log d))
76 (+ dot (ly:duration-dot-count d))
81 (ly:set-mus-property! music 'duration nd)
87 (define-public (shift-duration-log music shift dot)
88 (music-map (lambda (x) (shift-one-duration-log x shift dot))
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 (define-public (unfold-repeats music)
97 This function replaces all repeats with unfold repeats. It was
98 written by Rune Zedeler. "
99 (let* ((es (ly:get-mus-property music 'elements))
100 (e (ly:get-mus-property music 'element))
101 (n (ly:music-name music)))
103 (if (equal? n "Repeated_music")
106 (ly:get-mus-property music 'iterator-ctor)
107 Chord_tremolo_iterator::constructor)
108 (shift-duration-log music (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
110 (ly:set-mus-property!
111 music 'length Repeated_music::unfolded_music_length)
112 (ly:set-mus-property!
113 music 'start-moment-function Repeated_music::first_start)
114 (ly:set-mus-property!
115 music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
118 (ly:set-mus-property!
120 (map unfold-repeats es)))
123 (ly:set-mus-property!
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; property setting music objs.
133 (define-public (make-grob-property-set grob gprop val)
135 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
136 i.e. this is not an override"
138 (let* ((m (make-music-by-name 'OverrideProperty)))
139 (ly:set-mus-property! m 'symbol grob)
140 (ly:set-mus-property! m 'grob-property gprop)
141 (ly:set-mus-property! m 'grob-value val)
142 (ly:set-mus-property! m 'pop-first #t)
149 (define-public (make-grob-property-revert grob gprop)
150 "Revert the grob property GPROP for GROB."
151 (let* ((m (make-music-by-name 'OverrideProperty)))
152 (ly:set-mus-property! m 'symbol grob)
153 (ly:set-mus-property! m 'grob-property gprop)
160 (define-public (make-voice-props-set n)
161 (make-sequential-music
163 (map (lambda (x) (make-grob-property-set x 'direction
165 '(Tie Slur Stem Dots))
167 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
168 (make-grob-property-set 'MultiMeasureRest 'staff-position
177 (define-public (make-voice-props-revert)
178 (make-sequential-music
180 (make-grob-property-revert 'Tie 'direction)
181 (make-grob-property-revert 'Dots 'direction)
182 (make-grob-property-revert 'Stem 'direction)
183 (make-grob-property-revert 'Slur 'direction)
184 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
189 (define-public (context-spec-music m context . rest)
190 "Add \context CONTEXT = foo to M. "
192 (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
193 (ly:set-mus-property! cm 'element m)
194 (ly:set-mus-property! cm 'context-type context)
195 (if (and (pair? rest) (string? (car rest)))
196 (ly:set-mus-property! cm 'context-id (car rest))
201 (define-public (make-sequential-music elts)
202 (let* ((m (make-music-by-name 'SequentialMusic)))
203 (ly:set-mus-property! m 'elements elts)
207 (define-public (make-simultaneous-music elts)
208 (let* ((m (make-music-by-name 'SimultaneousMusic)))
209 (ly:set-mus-property! m 'elements elts)
213 (define-public (make-event-chord elts)
214 (let* ((m (make-music-by-name 'EventChord)))
215 (ly:set-mus-property! m 'elements elts)
220 (define-public (make-multi-measure-rest duration location)
223 (start (make-music-by-name 'MultiMeasureRestEvent))
224 (stop (make-music-by-name 'MultiMeasureRestEvent))
225 (skip ( make-music-by-name 'SkipEvent))
226 (ch (make-music-by-name 'BarCheck))
227 (ch2 (make-music-by-name 'BarCheck))
228 (seq (make-music-by-name 'MultiMeasureRestMusicGroup))
231 (ly:set-mus-property! start 'span-direction START)
232 (ly:set-mus-property! stop 'span-direction STOP)
233 (ly:set-mus-property! skip 'duration duration)
234 (ly:set-mus-property! seq 'elements
237 (make-event-chord (list start))
238 (make-event-chord (list skip))
239 (make-event-chord (list stop))
246 (define-public (glue-mm-rest-texts music)
247 "Check if we have R1*4-\markup { .. }, and if applicable convert to
248 a property set for MultiMeasureRestNumber."
250 (define (script-to-mmrest-text script-music)
251 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
255 (text (ly:get-mus-property script-music 'text))
256 (dir (ly:get-mus-property script-music 'direction))
257 (p (make-grob-property-set 'MultiMeasureRestNumber 'text text))
259 (make-grob-property-set 'MultiMeasureRestNumber 'direction dir)
263 (ly:set-mus-property! p 'once #t)
266 (ly:set-mus-property! d 'once #t)
267 (set! l (cons d l))))
269 (context-spec-music (make-sequential-music l) "Voice")
272 (if (eq? (ly:get-mus-property music 'name) 'MultiMeasureRestMusicGroup)
275 (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
276 (es (ly:get-mus-property music 'elements))
277 (texts (filter-list text? es))
278 (others (filter-out-list text? es))
281 (ly:set-mus-property!
283 (cons (script-to-mmrest-text (car texts))
291 (define-public (make-property-set sym val)
294 (m (make-music-by-name 'PropertySet))
296 (ly:set-mus-property! m 'symbol sym)
297 (ly:set-mus-property! m 'value val)
301 (define-public (make-time-signature-set num den . rest)
302 " Set properties for time signature NUM/DEN.
303 Rest can contain a list of beat groupings
309 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
310 (beat (ly:make-moment 1 den))
311 (len (ly:make-moment num den))
312 (set2 (make-property-set 'beatLength beat))
313 (set3 (make-property-set 'measureLength len))
314 (set4 (make-property-set 'beatGrouping (if (pair? rest)
317 (basic (list set1 set2 set3 set4))
322 (make-sequential-music basic) "Timing")))
324 (define-public (set-time-signature num den . rest)
325 (ly:export (apply make-time-signature-set `(,num ,den . ,rest)))
328 (define-public (make-penalty-music pen)
330 ((m (make-music-by-name 'BreakEvent)))
331 (ly:set-mus-property! m 'penalty pen)
334 (define-public (make-articulation name)
336 (m (make-music-by-name 'ArticulationEvent))
338 (ly:set-mus-property! m 'articulation-type name)
342 (define-public (make-span-event type spandir)
344 (m (make-music-by-name type))
346 (ly:set-mus-property! m 'span-direction spandir)
350 (define-public (set-mus-properties! m alist)
351 "Set all of ALIST as properties of M."
354 (ly:set-mus-property! m (caar alist) (cdar alist))
355 (set-mus-properties! m (cdr alist)))
358 (define-public (music-separator? m)
360 (let* ((ts (ly:get-mus-property m 'types )))
365 ;;; splitting chords into voices.
367 (define (voicify-list lst number)
368 "Make a list of Musics.
370 voicify-list :: [ [Music ] ] -> number -> [Music]
371 LST is a list music-lists.
375 (cons (context-spec-music
376 (make-sequential-music
378 (make-voice-props-set number)
379 (make-simultaneous-music (car lst))))
381 "Voice" (number->string number))
382 (voicify-list (cdr lst) (+ number 1))
386 (define (voicify-chord ch)
387 "Split the parts of a chord into different Voices using separator"
388 (let* ((es (ly:get-mus-property ch 'elements)))
391 (ly:set-mus-property! ch 'elements
392 (voicify-list (split-list es music-separator?) 0))
396 (define (voicify-music m)
397 "Recursively split chords that are separated with \\ "
399 (if (not (ly:music? m))
401 (error "not music!"))
404 ((es (ly:get-mus-property m 'elements))
405 (e (ly:get-mus-property m 'element))
408 (ly:set-mus-property! m 'elements (map voicify-music es)))
410 (ly:set-mus-property! m 'element (voicify-music e)))
412 (and (equal? (ly:music-name m) "Simultaneous_music")
413 (reduce (lambda (x y ) (or x y)) (map music-separator? es)))
420 (define-public (empty-music)
421 (ly:export (make-music-by-name 'Music))
425 ; Make a function that checks score element for being of a specific type.
426 (define-public (make-type-checker symbol)
429 ;;(eq? #t (ly:get-grob-property elt symbol))
430 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
434 (define-public (smart-bar-check n)
435 "Make a bar check that checks for a specific bar number.
439 (m (make-music-by-name 'ApplyContext))
443 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
447 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
451 (ly:set-mus-property! m 'procedure checker)
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 ;; warn for bare chords at start.
458 (define (has-request-chord elts)
459 (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly:music-name x)
460 "Request_chord")) elts)
463 (define (ly:music-message music msg)
466 (ip (ly:get-mus-property music 'origin))
469 (if (ly:input-location? ip)
470 (ly:input-message ip msg)
474 (define (check-start-chords music)
475 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
478 ((es (ly:get-mus-property music 'elements))
479 (e (ly:get-mus-property music 'element))
480 (name (ly:music-name music))
484 ((equal? name "Context_specced_music") #t)
485 ((equal? name "Simultaneous_music")
487 (if (has-request-chord es)
488 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
489 (map check-start-chords es)))
491 ((equal? name "Sequential_music")
493 (check-start-chords (car es))))
494 (else (if (ly:music? e) (check-start-chords e )))
500 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
501 ;; switch it on here, so parsing and init isn't checked (too slow!)
503 ;; automatic music transformations.
505 (define (switch-on-debugging m)
506 (set-debug-cell-accesses! 15000)
510 (define-public toplevel-music-functions
511 (list check-start-chords
513 (lambda (x) (music-map glue-mm-rest-texts x))
514 ; switch-on-debugging