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)
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (define (shift-one-duration-log music shift dot)
52 " add SHIFT to ly:duration-log and optionally
53 a dot to any note encountered. This scales the music up by a factor
54 2^shift * (2 - (1/2)^dot)"
58 (d (ly:get-mus-property music 'duration))
62 (cp (ly:duration-factor d))
63 (nd (ly:make-duration (+ shift (ly:duration-log d))
64 (+ dot (ly:duration-dot-count d))
69 (ly:set-mus-property! music 'duration nd)
75 (define-public (shift-duration-log music shift dot)
76 (music-map (lambda (x) (shift-one-duration-log x shift dot))
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 (define-public (note-to-cluster music)
84 "Replace NoteEvents by ClusterNoteEvents."
85 (if (eq? (ly:get-mus-property music 'name) 'NoteEvent)
86 (let* ((cn (make-music-by-name 'ClusterNoteEvent)))
88 (ly:set-mus-property! cn 'pitch (ly:get-mus-property music 'pitch))
89 (ly:set-mus-property! cn 'duration (ly:get-mus-property music 'duration))
93 (define-public (notes-to-clusters music)
94 (music-map note-to-cluster music))
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 (define-public (unfold-repeats music)
101 This function replaces all repeats with unfold repeats. It was
102 written by Rune Zedeler. "
103 (let* ((es (ly:get-mus-property music 'elements))
104 (e (ly:get-mus-property music 'element))
105 (n (ly:music-name music)))
107 (if (equal? n "Repeated_music")
110 (ly:get-mus-property music 'iterator-ctor)
111 Chord_tremolo_iterator::constructor)
112 (shift-duration-log music (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
114 (ly:set-mus-property!
115 music 'length Repeated_music::unfolded_music_length)
116 (ly:set-mus-property!
117 music 'start-moment-function Repeated_music::first_start)
118 (ly:set-mus-property!
119 music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
122 (ly:set-mus-property!
124 (map unfold-repeats es)))
127 (ly:set-mus-property!
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 ;; property setting music objs.
137 (define-public (make-grob-property-set grob gprop val)
139 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
140 i.e. this is not an override"
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)
145 (ly:set-mus-property! m 'grob-value val)
146 (ly:set-mus-property! m 'pop-first #t)
153 (define-public (make-grob-property-revert grob gprop)
154 "Revert the grob property GPROP for GROB."
155 (let* ((m (make-music-by-name 'OverrideProperty)))
156 (ly:set-mus-property! m 'symbol grob)
157 (ly:set-mus-property! m 'grob-property gprop)
164 (define-public (make-voice-props-set n)
165 (make-sequential-music
167 (map (lambda (x) (make-grob-property-set x 'direction
169 '(Tie Slur Script TextScript Stem Dots))
171 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
172 (make-grob-property-set 'MultiMeasureRest 'staff-position
181 (define-public (make-voice-props-revert)
182 (make-sequential-music
184 (make-grob-property-revert 'Tie 'direction)
185 (make-grob-property-revert 'Dots 'direction)
186 (make-grob-property-revert 'Stem 'direction)
187 (make-grob-property-revert 'Slur 'direction)
188 (make-grob-property-revert 'NoteColumn 'horizontal-shift)
193 (define-public (context-spec-music m context . rest)
194 "Add \context CONTEXT = foo to M. "
196 (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
197 (ly:set-mus-property! cm 'element m)
198 (ly:set-mus-property! cm 'context-type context)
199 (if (and (pair? rest) (string? (car rest)))
200 (ly:set-mus-property! cm 'context-id (car rest))
205 (define-public (make-apply-context func)
208 (m (make-music-by-name 'ApplyContext))
211 (ly:set-mus-property! m 'procedure func)
215 (define-public (make-sequential-music elts)
216 (let* ((m (make-music-by-name 'SequentialMusic)))
217 (ly:set-mus-property! m 'elements elts)
221 (define-public (make-simultaneous-music elts)
222 (let* ((m (make-music-by-name 'SimultaneousMusic)))
223 (ly:set-mus-property! m 'elements elts)
227 (define-public (make-event-chord elts)
228 (let* ((m (make-music-by-name 'EventChord)))
229 (ly:set-mus-property! m 'elements elts)
233 (define-public (make-nonevent-skip dur)
234 (let* ((m (make-music-by-name 'NonEventSkip)))
235 (ly:set-mus-property! m 'duration dur)
242 (define-public (make-multi-measure-rest duration location)
245 (start (make-music-by-name 'MultiMeasureRestEvent))
246 (stop (make-music-by-name 'MultiMeasureRestEvent))
247 (skip ( make-music-by-name 'SkipEvent))
248 (ch (make-music-by-name 'BarCheck))
249 (ch2 (make-music-by-name 'BarCheck))
250 (seq (make-music-by-name 'MultiMeasureRestMusicGroup))
253 (map (lambda (x) (ly:set-mus-property! x 'origin location))
254 (list start stop skip ch ch2 seq))
255 (ly:set-mus-property! start 'span-direction START)
256 (ly:set-mus-property! stop 'span-direction STOP)
257 (ly:set-mus-property! skip 'duration duration)
258 (ly:set-mus-property! seq 'elements
261 (make-event-chord (list start))
262 (make-event-chord (list skip))
263 (make-event-chord (list stop))
270 (define-public (glue-mm-rest-texts music)
271 "Check if we have R1*4-\markup { .. }, and if applicable convert to
272 a property set for MultiMeasureRestNumber."
274 (define (script-to-mmrest-text script-music)
275 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
279 (text (ly:get-mus-property script-music 'text))
280 (dir (ly:get-mus-property script-music 'direction))
281 (p (make-music-by-name 'MultiMeasureTextEvent))
285 (ly:set-mus-property! p 'direction dir))
286 (ly:set-mus-property! p 'text text)
290 (if (eq? (ly:get-mus-property music 'name) 'MultiMeasureRestMusicGroup)
293 (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
294 (es (ly:get-mus-property music 'elements))
295 (texts (map script-to-mmrest-text (filter text? es)))
296 (others (remove text? es))
299 (ly:set-mus-property!
301 (cons (make-event-chord texts) others)
308 (define-public (make-property-set sym val)
311 (m (make-music-by-name 'PropertySet))
313 (ly:set-mus-property! m 'symbol sym)
314 (ly:set-mus-property! m 'value val)
320 (define-public (make-ottava-set octavation)
323 (m (make-music-by-name 'ApplyContext))
327 (define (ottava-modify context)
328 "Either reset centralCPosition to the stored original,
329 or remember old centralCPosition, add OCTAVATION to centralCPosition,
330 and set OTTAVATION to `8va', or whatever appropriate."
331 (if (number? (ly:get-context-property context 'centralCPosition))
335 ((where (ly:context-property-where-defined context 'centralCPosition))
336 (oc0 (ly:get-context-property context 'originalCentralCPosition)))
338 (ly:set-context-property context 'centralCPosition oc0)
339 (ly:unset-context-property where 'originalCentralCPosition)
340 (ly:unset-context-property where 'ottavation))
343 ((where (ly:context-property-where-defined context 'centralCPosition))
344 (c0 (ly:get-context-property context 'centralCPosition))
345 (new-c0 (+ c0 (* -7 octavation)))
347 (assoc octavation '((2 . "15ma")
351 (-2 . "15ma bassa"))))))
353 (ly:set-context-property context 'centralCPosition new-c0)
354 (ly:set-context-property context 'originalCentralCPosition c0)
355 (ly:set-context-property context 'ottavation string)
359 (ly:set-mus-property! m 'procedure ottava-modify)
360 (context-spec-music m "Staff")
363 (define-public (set-octavation ottavation)
364 (ly:export (make-ottava-set ottavation)))
366 (define-public (make-time-signature-set num den . rest)
367 " Set properties for time signature NUM/DEN.
368 Rest can contain a list of beat groupings
374 (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
375 (beat (ly:make-moment 1 den))
376 (len (ly:make-moment num den))
377 (set2 (make-property-set 'beatLength beat))
378 (set3 (make-property-set 'measureLength len))
379 (set4 (make-property-set 'beatGrouping (if (pair? rest)
382 (basic (list set1 set2 set3 set4)))
385 (make-sequential-music basic) "Timing")))
387 (define-public (set-time-signature num den . rest)
388 (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
390 (define-public (make-penalty-music pen)
392 ((m (make-music-by-name 'BreakEvent)))
393 (ly:set-mus-property! m 'penalty pen)
396 (define-public (make-articulation name)
398 (m (make-music-by-name 'ArticulationEvent))
400 (ly:set-mus-property! m 'articulation-type name)
404 (define-public (make-span-event type spandir)
406 (m (make-music-by-name type))
408 (ly:set-mus-property! m 'span-direction spandir)
412 (define-public (set-mus-properties! m alist)
413 "Set all of ALIST as properties of M."
416 (ly:set-mus-property! m (caar alist) (cdar alist))
417 (set-mus-properties! m (cdr alist)))
420 (define-public (music-separator? m)
422 (let* ((ts (ly:get-mus-property m 'types )))
427 ;;; splitting chords into voices.
429 (define (voicify-list lst number)
430 "Make a list of Musics.
432 voicify-list :: [ [Music ] ] -> number -> [Music]
433 LST is a list music-lists.
437 (cons (context-spec-music
438 (make-sequential-music
440 (make-voice-props-set number)
441 (make-simultaneous-music (car lst))))
443 "Voice" (number->string number))
444 (voicify-list (cdr lst) (+ number 1))
448 (define (voicify-chord ch)
449 "Split the parts of a chord into different Voices using separator"
450 (let* ((es (ly:get-mus-property ch 'elements)))
453 (ly:set-mus-property! ch 'elements
454 (voicify-list (split-list es music-separator?) 0))
458 (define (voicify-music m)
459 "Recursively split chords that are separated with \\ "
461 (if (not (ly:music? m))
463 (error "not music!"))
466 ((es (ly:get-mus-property m 'elements))
467 (e (ly:get-mus-property m 'element))
470 (ly:set-mus-property! m 'elements (map voicify-music es)))
472 (ly:set-mus-property! m 'element (voicify-music e)))
474 (and (equal? (ly:music-name m) "Simultaneous_music")
475 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
482 (define-public (empty-music)
483 (ly:export (make-music-by-name 'Music))
487 ; Make a function that checks score element for being of a specific type.
488 (define-public (make-type-checker symbol)
491 ;;(eq? #t (ly:get-grob-property elt symbol))
492 (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
496 (define-public (smart-bar-check n)
497 "Make a bar check that checks for a specific bar number.
501 (m (make-music-by-name 'ApplyContext))
505 (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
509 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
513 (ly:set-mus-property! m 'procedure checker)
517 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
518 ;; warn for bare chords at start.
520 (define (has-request-chord elts)
521 (reduce (lambda (x y) (or x y)) #f (map (lambda (x) (equal? (ly:music-name x)
522 "Request_chord")) elts)
525 (define (ly:music-message music msg)
528 (ip (ly:get-mus-property music 'origin))
531 (if (ly:input-location? ip)
532 (ly:input-message ip msg)
536 (define (check-start-chords music)
537 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called from parser."
540 ((es (ly:get-mus-property music 'elements))
541 (e (ly:get-mus-property music 'element))
542 (name (ly:music-name music))
546 ((equal? name "Context_specced_music") #t)
547 ((equal? name "Simultaneous_music")
549 (if (has-request-chord es)
550 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
551 (map check-start-chords es)))
553 ((equal? name "Sequential_music")
555 (check-start-chords (car es))))
556 (else (if (ly:music? e) (check-start-chords e )))
562 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
563 ;; switch it on here, so parsing and init isn't checked (too slow!)
565 ;; automatic music transformations.
567 (define (switch-on-debugging m)
568 (set-debug-cell-accesses! 15000)
572 (define-public toplevel-music-functions
573 (list check-start-chords
575 (lambda (x) (music-map glue-mm-rest-texts x))
576 ; switch-on-debugging