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 (map (lambda (x) (ly:set-mus-property! x 'origin location))
232 (list start stop skip ch ch2 seq))
233 (ly:set-mus-property! start 'span-direction START)
234 (ly:set-mus-property! stop 'span-direction STOP)
235 (ly:set-mus-property! skip 'duration duration)
236 (ly:set-mus-property! seq 'elements
239 (make-event-chord (list start))
240 (make-event-chord (list skip))
241 (make-event-chord (list stop))
248 (define-public (glue-mm-rest-texts music)
249 "Check if we have R1*4-\markup { .. }, and if applicable convert to
250 a property set for MultiMeasureRestNumber."
252 (define (script-to-mmrest-text script-music)
253 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
257 (text (ly:get-mus-property script-music 'text))
258 (dir (ly:get-mus-property script-music 'direction))
259 (p (make-music-by-name 'MultiMeasureTextEvent))
263 (ly:set-mus-property! p 'direction dir))
264 (ly:set-mus-property! p 'text text)
268 (if (eq? (ly:get-mus-property music 'name) 'MultiMeasureRestMusicGroup)
271 (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
272 (es (ly:get-mus-property music 'elements))
273 (texts (map script-to-mmrest-text (filter-list text? es)))
274 (others (filter-out-list text? es))
277 (ly:set-mus-property!
279 (cons (make-event-chord texts) others)
286 (define-public (make-property-set sym val)
289 (m (make-music-by-name 'PropertySet))
291 (ly:set-mus-property! m 'symbol sym)
292 (ly:set-mus-property! m 'value val)
296 (define-public (make-time-signature-set num den . rest)
297 " Set properties for time signature NUM/DEN.
298 Rest can contain a list of beat groupings
304 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
305 (beat (ly:make-moment 1 den))
306 (len (ly:make-moment num den))
307 (set2 (make-property-set 'beatLength beat))
308 (set3 (make-property-set 'measureLength len))
309 (set4 (make-property-set 'beatGrouping (if (pair? rest)
312 (basic (list set1 set2 set3 set4))
317 (make-sequential-music basic) "Timing")))
319 (define-public (set-time-signature num den . rest)
320 (ly:export (apply make-time-signature-set `(,num ,den . ,rest)))
323 (define-public (make-penalty-music pen)
325 ((m (make-music-by-name 'BreakEvent)))
326 (ly:set-mus-property! m 'penalty pen)
329 (define-public (make-articulation name)
331 (m (make-music-by-name 'ArticulationEvent))
333 (ly:set-mus-property! m 'articulation-type name)
337 (define-public (make-span-event type spandir)
339 (m (make-music-by-name type))
341 (ly:set-mus-property! m 'span-direction spandir)
345 (define-public (set-mus-properties! m alist)
346 "Set all of ALIST as properties of M."
349 (ly:set-mus-property! m (caar alist) (cdar alist))
350 (set-mus-properties! m (cdr alist)))
353 (define-public (music-separator? m)
355 (let* ((ts (ly:get-mus-property m 'types )))
360 ;;; splitting chords into voices.
362 (define (voicify-list lst number)
363 "Make a list of Musics.
365 voicify-list :: [ [Music ] ] -> number -> [Music]
366 LST is a list music-lists.
370 (cons (context-spec-music
371 (make-sequential-music
373 (make-voice-props-set number)
374 (make-simultaneous-music (car lst))))
376 "Voice" (number->string number))
377 (voicify-list (cdr lst) (+ number 1))
381 (define (voicify-chord ch)
382 "Split the parts of a chord into different Voices using separator"
383 (let* ((es (ly:get-mus-property ch 'elements)))
386 (ly:set-mus-property! ch 'elements
387 (voicify-list (split-list es music-separator?) 0))
391 (define (voicify-music m)
392 "Recursively split chords that are separated with \\ "
394 (if (not (ly:music? m))
396 (error "not music!"))
399 ((es (ly:get-mus-property m 'elements))
400 (e (ly:get-mus-property m 'element))
403 (ly:set-mus-property! m 'elements (map voicify-music es)))
405 (ly:set-mus-property! m 'element (voicify-music e)))
407 (and (equal? (ly:music-name m) "Simultaneous_music")
408 (reduce (lambda (x y ) (or x y)) (map music-separator? es)))
415 (define-public (empty-music)
416 (ly:export (make-music-by-name 'Music))
420 ; Make a function that checks score element for being of a specific type.
421 (define-public (make-type-checker symbol)
424 ;;(eq? #t (ly:get-grob-property elt symbol))
425 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
429 (define-public (smart-bar-check n)
430 "Make a bar check that checks for a specific bar number.
434 (m (make-music-by-name 'ApplyContext))
438 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
442 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
446 (ly:set-mus-property! m 'procedure checker)
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; warn for bare chords at start.
453 (define (has-request-chord elts)
454 (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly:music-name x)
455 "Request_chord")) elts)
458 (define (ly:music-message music msg)
461 (ip (ly:get-mus-property music 'origin))
464 (if (ly:input-location? ip)
465 (ly:input-message ip msg)
469 (define (check-start-chords music)
470 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
473 ((es (ly:get-mus-property music 'elements))
474 (e (ly:get-mus-property music 'element))
475 (name (ly:music-name music))
479 ((equal? name "Context_specced_music") #t)
480 ((equal? name "Simultaneous_music")
482 (if (has-request-chord es)
483 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
484 (map check-start-chords es)))
486 ((equal? name "Sequential_music")
488 (check-start-chords (car es))))
489 (else (if (ly:music? e) (check-start-chords e )))
495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496 ;; switch it on here, so parsing and init isn't checked (too slow!)
498 ;; automatic music transformations.
500 (define (switch-on-debugging m)
501 (set-debug-cell-accesses! 15000)
505 (define-public toplevel-music-functions
506 (list check-start-chords
508 (lambda (x) (music-map glue-mm-rest-texts x))
509 ; switch-on-debugging