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-one-music music)
33 (define-public (display-music arg)
34 (music-map display-one-music arg))
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (define (shift-one-duration-log music shift dot)
39 " add SHIFT to ly:duration-log and optionally
40 a dot to any note encountered. This scales the music up by a factor
41 2^shift * (2 - (1/2)^dot)"
45 (d (ly:get-mus-property music 'duration))
49 (cp (ly:duration-factor d))
50 (nd (ly:make-duration (+ shift (ly:duration-log d))
51 (+ dot (ly:duration-dot-count d))
56 (ly:set-mus-property! music 'duration nd)
62 (define-public (shift-duration-log music shift dot)
63 (music-map (lambda (x) (shift-one-duration-log x shift dot))
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 (define-public (unfold-repeats music)
72 This function replaces all repeats with unfold repeats. It was
73 written by Rune Zedeler. "
74 (let* ((es (ly:get-mus-property music 'elements))
75 (e (ly:get-mus-property music 'element))
76 (n (ly:music-name music)))
78 (if (equal? n "Repeated_music")
81 (ly:get-mus-property music 'iterator-ctor)
82 Chord_tremolo_iterator::constructor)
83 (shift-duration-log music (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
86 music 'length Repeated_music::unfolded_music_length)
88 music 'start-moment-function Repeated_music::first_start)
90 music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
95 (map unfold-repeats es)))
105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 (define (pitchify-scripts music)
109 "Copy the pitch fields of the Note_requests into Text_script_requests, to aid
111 (define (find-note musics)
112 (filter-list (lambda (m) (equal? (ly:music-name m) "Note_req")) musics)
114 (define (find-scripts musics)
115 (filter-list (lambda (m) (equal? (ly:music-name m) "Text_script_req")) musics))
118 (e (ly:get-mus-property music 'element))
119 (es (ly:get-mus-property music 'elements))
120 (notes (find-note es))
121 (pitch (if (pair? notes) (ly:get-mus-property (car notes) 'pitch) #f))
125 (map (lambda (x) (ly:set-mus-property! x 'pitch pitch)) (find-scripts es))
129 (ly:set-mus-property!
131 (map pitchify-scripts es)))
134 (ly:set-mus-property!
136 (pitchify-scripts e)))
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; property setting music objs.
143 (define-public (make-grob-property-set grob gprop val)
144 "Make a M-exp that sets GPROP to VAL in GROBS. Does a pop first, i.e.
145 this is not an override
148 (let* ((m (make-music-by-name 'OverrideProperty)))
149 (ly:set-mus-property! m 'symbol grob)
150 (ly:set-mus-property! m 'grob-property gprop)
151 (ly:set-mus-property! m 'grob-value val)
152 (ly:set-mus-property! m 'pop-first #t)
159 (define-public (make-grob-property-revert grob gprop)
160 "Revert the grob property GPROP for GROB."
161 (let* ((m (make-music-by-name 'OverrideProperty)))
162 (ly:set-mus-property! m 'symbol grob)
163 (ly:set-mus-property! m 'grob-property gprop)
169 (define-public (make-voice-props-set n)
170 (make-sequential-music
172 (map (lambda (x) (make-grob-property-set x 'direction
174 '(Tie Slur Stem Dots))
176 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
177 (make-grob-property-set 'MultiMeasureRest 'staff-position
185 (define-public (make-voice-props-revert)
186 (make-sequential-music
188 (make-grob-property-revert 'Tie 'direction)
189 (make-grob-property-revert 'Dots 'direction)
190 (make-grob-property-revert 'Stem 'direction)
191 (make-grob-property-revert 'Slur 'direction)
192 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
196 (define-public (context-spec-music m context . rest)
197 "Add \context CONTEXT = foo to M. "
199 (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
200 (ly:set-mus-property! cm 'element m)
201 (ly:set-mus-property! cm 'context-type context)
202 (if (and (pair? rest) (string? (car rest)))
203 (ly:set-mus-property! cm 'context-id (car rest))
208 (define-public (make-sequential-music elts)
209 (let* ((m (make-music-by-name 'SequentialMusic)))
210 (ly:set-mus-property! m 'elements elts)
214 (define-public (make-simultaneous-music elts)
215 (let* ((m (make-music-by-name 'SimultaneousMusic)))
216 (ly:set-mus-property! m 'elements elts)
220 (define-public (make-event-chord elts)
221 (let* ((m (make-music-by-name 'EventChord)))
222 (ly:set-mus-property! m 'elements elts)
227 (define-public (make-multi-measure-rest duration location)
230 (start (make-music-by-name 'MultiMeasureRestEvent))
231 (stop (make-music-by-name 'MultiMeasureRestEvent))
232 (skip ( make-music-by-name 'SkipEvent))
233 (ch (make-music-by-name 'BarCheck))
234 (ch2 (make-music-by-name 'BarCheck))
237 (ly:set-mus-property! start 'span-direction START)
238 (ly:set-mus-property! stop 'span-direction STOP)
239 (ly:set-mus-property! skip 'duration duration)
240 (map (lambda (x) (ly:set-mus-property! x 'origin location))
241 (list start stop skip ch ch2))
242 (make-sequential-music
245 (make-event-chord (list start))
246 (make-event-chord (list skip))
247 (make-event-chord (list stop))
253 (define-public (make-property-set sym val)
256 (m (make-music-by-name 'PropertySet))
258 (ly:set-mus-property! m 'symbol sym)
259 (ly:set-mus-property! m 'value val)
263 (define-public (make-time-signature-set num den . rest)
264 " Set properties for time signature NUM/DEN.
265 Rest can contain a list of beat groupings
271 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
272 (beat (ly:make-moment 1 den))
273 (len (ly:make-moment num den))
274 (set2 (make-property-set 'beatLength beat))
275 (set3 (make-property-set 'measureLength len))
276 (set4 (make-property-set 'beatGrouping (if (pair? rest)
279 (basic (list set1 set2 set3 set4))
284 (make-sequential-music basic) "Timing")))
286 (define-public (set-time-signature num den . rest)
287 (ly:export (apply make-time-signature-set `(,num ,den . ,rest)))
290 (define-public (make-penalty-music pen)
292 ((m (make-music-by-name 'BreakEvent)))
293 (ly:set-mus-property! m 'penalty pen)
296 (define-public (make-articulation name)
298 (m (make-music-by-name 'ArticulationEvent))
300 (ly:set-mus-property! m 'articulation-type name)
304 (define-public (make-span-event type spandir)
306 (m (make-music-by-name type))
308 (ly:set-mus-property! m 'span-direction spandir)
312 (define-public (set-mus-properties! m alist)
313 "Set all of ALIST as properties of M."
316 (ly:set-mus-property! m (caar alist) (cdar alist))
317 (set-mus-properties! m (cdr alist)))
320 (define-public (music-separator? m)
322 (let* ((ts (ly:get-mus-property m 'types )))
326 (define (split-one sep? l acc)
327 "Split off the first parts before separator and return both parts.
334 (split-one sep? (cdr l) (cons (car l) acc))
338 (define-public (split-list l sep?)
341 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
348 (let* ((c (split-one sep? l '())))
349 (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
354 ;;; splitting chords into voices.
356 (define (voicify-list lst number)
357 "Make a list of Musics.
359 voicify-list :: [ [Music ] ] -> number -> [Music]
360 LST is a list music-lists.
364 (cons (context-spec-music
365 (make-sequential-music
367 (make-voice-props-set number)
368 (make-simultaneous-music (car lst))))
370 "Voice" (number->string number))
371 (voicify-list (cdr lst) (+ number 1))
375 (define (voicify-chord ch)
376 "Split the parts of a chord into different Voices using separator"
377 (let* ((es (ly:get-mus-property ch 'elements)))
380 (ly:set-mus-property! ch 'elements
381 (voicify-list (split-list es music-separator?) 0))
385 (define (voicify-music m)
386 "Recursively split chords that are separated with \\ "
388 (if (not (ly:music? m))
390 (error "not music!"))
393 ((es (ly:get-mus-property m 'elements))
394 (e (ly:get-mus-property m 'element))
397 (ly:set-mus-property! m 'elements (map voicify-music es)))
399 (ly:set-mus-property! m 'element (voicify-music e)))
401 (and (equal? (ly:music-name m) "Simultaneous_music")
402 (reduce (lambda (x y ) (or x y)) (map music-separator? es)))
409 (define-public (empty-music)
410 (ly:export (make-music-by-name 'Music))
414 ; Make a function that checks score element for being of a specific type.
415 (define-public (make-type-checker symbol)
418 ;;(eq? #t (ly:get-grob-property elt symbol))
419 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
423 (define-public (smart-bar-check n)
424 "Make a bar check that checks for a specific bar number.
428 (m (make-music-by-name 'ApplyContext))
432 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
436 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
440 (ly:set-mus-property! m 'procedure checker)
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
445 ;; warn for bare chords at start.
447 (define (has-request-chord elts)
448 (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly:music-name x)
449 "Request_chord")) elts)
452 (define (ly:music-message music msg)
454 (ip (ly:get-mus-property music 'origin))
457 (if (ly:input-location? ip)
458 (ly:input-message ip msg)
462 (define (check-start-chords music)
463 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
466 ((es (ly:get-mus-property music 'elements))
467 (e (ly:get-mus-property music 'element))
468 (name (ly:music-name music))
472 ((equal? name "Context_specced_music") #t)
473 ((equal? name "Simultaneous_music")
475 (if (has-request-chord es)
476 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
477 (map check-start-chords es)))
479 ((equal? name "Sequential_music")
481 (check-start-chords (car es))))
482 (else (if (ly:music? e) (check-start-chords e )))
489 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
490 ;; switch it on here, so parsing and init isn't checked (too slow!)
492 ;; automatic music transformations.
494 (define (switch-on-debugging m)
495 (set-debug-cell-accesses! 15000)
499 (define-public toplevel-music-functions
500 (list check-start-chords
503 ; switch-on-debugging