1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; ly:music-property with setter
4 ;;; (ly:music-property my-music 'elements)
5 ;;; ==> the 'elements property
6 ;;; (set! (ly:music-property my-music 'elements) value)
7 ;;; ==> set the 'elements property and return it
8 (define-public ly:music-property
9 (make-procedure-with-setter ly:music-property
10 ly:music-set-property!))
12 (define-public ly:grob-property
13 (make-procedure-with-setter ly:grob-property
14 ly:grob-set-property!))
16 (define-public (music-map function music)
17 "Apply @var{function} to @var{music} and all of the music it contains. "
18 (let ((es (ly:music-property music 'elements))
19 (e (ly:music-property music 'element)))
20 (set! (ly:music-property music 'elements)
21 (map (lambda (y) (music-map function y)) es))
23 (set! (ly:music-property music 'element)
24 (music-map function e)))
27 (define-public (music-filter pred? music)
28 "Filter out music expressions that do not satisfy PRED."
30 (define (inner-music-filter pred? music)
32 (let* ((es (ly:music-property music 'elements))
33 (e (ly:music-property music 'element))
34 (as (ly:music-property music 'articulations))
35 (filtered-as (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) as)))
36 (filtered-e (if (ly:music? e)
37 (inner-music-filter pred? e)
39 (filtered-es (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) es))))
40 (set! (ly:music-property music 'element) filtered-e)
41 (set! (ly:music-property music 'elements) filtered-es)
42 (set! (ly:music-property music 'articulations) filtered-as)
43 ;; if filtering emptied the expression, we remove it completely.
45 (and (eq? filtered-es '()) (not (ly:music? e))
46 (or (not (eq? es '()))
51 (set! music (inner-music-filter pred? music))
54 (make-music 'Music))) ;must return music.
56 (define-public (remove-tag tag)
60 (let* ((tags (ly:music-property m 'tags))
61 (res (memq tag tags)))
65 (define-public (display-music music)
66 "Display music, not done with music-map for clarity of presentation."
69 (let ((es (ly:music-property music 'elements))
70 (e (ly:music-property music 'element)))
71 (display (ly:get-mutable-properties music))
73 (begin (display "\nElements: {\n")
74 (map display-music es)
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 (define (shift-one-duration-log music shift dot)
87 " add SHIFT to ly:duration-log and optionally
88 a dot to any note encountered. This scales the music up by a factor
89 2^shift * (2 - (1/2)^dot)"
90 (let ((d (ly:music-property music 'duration)))
92 (let* ((cp (ly:duration-factor d))
93 (nd (ly:make-duration (+ shift (ly:duration-log d))
94 (+ dot (ly:duration-dot-count d))
97 (set! (ly:music-property music 'duration) nd)))
102 (define-public (shift-duration-log music shift dot)
103 (music-map (lambda (x) (shift-one-duration-log x shift dot))
107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 (define-public (note-to-cluster music)
111 "Replace NoteEvents by ClusterNoteEvents."
112 (if (eq? (ly:music-property music 'name) 'NoteEvent)
113 (make-music 'ClusterNoteEvent
114 'pitch (ly:music-property music 'pitch)
115 'duration (ly:music-property music 'duration))
118 (define-public (notes-to-clusters music)
119 (music-map note-to-cluster music))
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 (define-public (unfold-repeats music)
126 This function replaces all repeats with unfold repeats. It was
127 written by Rune Zedeler. "
128 (let ((es (ly:music-property music 'elements))
129 (e (ly:music-property music 'element))
130 (n (ly:music-name music)))
131 (if (equal? n "Repeated_music")
133 (if (equal? (ly:music-property music 'iterator-ctor)
134 Chord_tremolo_iterator::constructor)
135 (shift-duration-log music (ly:intlog2 (ly:music-property music 'repeat-count)) 0))
136 (set! (ly:music-property music 'length)
137 Repeated_music::unfolded_music_length)
138 (set! (ly:music-property music 'start-moment-function)
139 Repeated_music::first_start)
140 (set! (ly:music-property music 'iterator-ctor)
141 Unfolded_repeat_iterator::constructor)))
143 (set! (ly:music-property music 'elements)
144 (map unfold-repeats es)))
146 (set! (ly:music-property music 'element)
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;; property setting music objs.
154 (define-public (make-grob-property-set grob gprop val)
155 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
156 i.e. this is not an override"
157 (make-music 'OverrideProperty
163 (define-public (make-grob-property-override grob gprop val)
164 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
165 i.e. this is not an override"
166 (make-music 'OverrideProperty
171 (define-public (make-grob-property-revert grob gprop)
172 "Revert the grob property GPROP for GROB."
173 (make-music 'OverrideProperty
175 'grob-property gprop))
177 (define direction-polyphonic-grobs
178 '(Tie Rest Slur Script TextScript Stem Dots DotColumn))
180 (define-public (make-voice-props-set n)
181 (make-sequential-music
183 (map (lambda (x) (make-grob-property-set x 'direction
185 direction-polyphonic-grobs)
186 (list (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
187 (make-grob-property-set 'MultiMeasureRest 'staff-position
188 (if (odd? n) -4 4))))))
190 (define-public (make-voice-props-revert)
191 (make-sequential-music
193 (map (lambda (x) (make-grob-property-revert x 'direction))
194 direction-polyphonic-grobs)
195 (list (make-grob-property-revert 'NoteColumn 'horizontal-shift)))))
198 (define*-public (context-spec-music m context #:optional id)
199 "Add \\context CONTEXT = ID to M. "
200 (let ((cm (make-music 'ContextSpeccedMusic
202 'context-type context)))
204 (set! (ly:music-property cm 'context-id) id))
207 (define-public (make-apply-context func)
208 (make-music 'ApplyContext
211 (define-public (make-sequential-music elts)
212 (make-music 'SequentialMusic
215 (define-public (make-simultaneous-music elts)
216 (make-music 'SimultaneousMusic
219 (define-public (make-event-chord elts)
220 (make-music 'EventChord
223 (define-public (make-skip-music dur)
224 (make-music 'SkipMusic
230 (define-public (make-multi-measure-rest duration location)
231 (make-music 'MultiMeasureRestMusicGroup
233 'elements (list (make-music 'BarCheck
235 (make-event-chord (list (make-music 'MultiMeasureRestEvent
237 'duration duration)))
238 (make-music 'BarCheck
241 (define-public (glue-mm-rest-texts music)
242 "Check if we have R1*4-\\markup { .. }, and if applicable convert to
243 a property set for MultiMeasureRestNumber."
244 (define (script-to-mmrest-text script-music)
245 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
246 (let ((dir (ly:music-property script-music 'direction))
247 (p (make-music 'MultiMeasureTextEvent
248 'text (ly:music-property script-music 'text))))
250 (set! (ly:music-property p 'direction) dir))
252 (if (eq? (ly:music-property music 'name) 'MultiMeasureRestMusicGroup)
253 (let* ((text? (lambda (x) (memq 'script-event (ly:music-property x 'types))))
254 (es (ly:music-property music 'elements))
255 (texts (map script-to-mmrest-text (filter text? es)))
256 (others (remove text? es)))
258 (set! (ly:music-property music 'elements)
259 (cons (make-event-chord texts) others)))))
263 (define-public (make-property-set sym val)
264 (make-music 'PropertySet
268 (define-public (make-ottava-set octavation)
269 (let ((m (make-music 'ApplyContext)))
270 (define (ottava-modify context)
271 "Either reset centralCPosition to the stored original, or remember
272 old centralCPosition, add OCTAVATION to centralCPosition, and set
273 OTTAVATION to `8va', or whatever appropriate."
274 (if (number? (ly:context-property context 'centralCPosition))
276 (let ((where (ly:context-property-where-defined context 'centralCPosition))
277 (oc0 (ly:context-property context 'originalCentralCPosition)))
278 (ly:context-set-property! context 'centralCPosition oc0)
279 (ly:unset-context-property where 'originalCentralCPosition)
280 (ly:unset-context-property where 'ottavation))
281 (let* ((where (ly:context-property-where-defined context 'centralCPosition))
282 (c0 (ly:context-property context 'centralCPosition))
283 (new-c0 (+ c0 (* -7 octavation)))
284 (string (cdr (assoc octavation '((2 . "15ma")
288 (-2 . "15ma bassa"))))))
289 (ly:context-set-property! context 'centralCPosition new-c0)
290 (ly:context-set-property! context 'originalCentralCPosition c0)
291 (ly:context-set-property! context 'ottavation string)))))
292 (set! (ly:music-property m 'procedure) ottava-modify)
293 (context-spec-music m 'Staff)))
295 (define-public (set-octavation ottavation)
296 (ly:export (make-ottava-set ottavation)))
298 (define-public (make-time-signature-set num den . rest)
299 " Set properties for time signature NUM/DEN.
300 Rest can contain a list of beat groupings
303 (let* ((set1 (make-property-set 'timeSignatureFraction (cons num den)))
304 (beat (ly:make-moment 1 den))
305 (len (ly:make-moment num den))
306 (set2 (make-property-set 'beatLength beat))
307 (set3 (make-property-set 'measureLength len))
308 (set4 (make-property-set 'beatGrouping (if (pair? rest)
311 (basic (list set1 set2 set3 set4)))
313 (context-spec-music (make-sequential-music basic) 'Timing) 'Score)))
315 (define-public (make-mark-set label)
316 "make the music for the \\mark command."
317 (let* ((set (if (integer? label)
318 (context-spec-music (make-property-set 'rehearsalMark label)
321 (ev (make-music 'MarkEvent))
322 (ch (make-event-chord (list ev))))
324 (make-sequential-music (list set ch))
326 (set! (ly:music-property ev 'label) label)
329 (define-public (set-time-signature num den . rest)
330 (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
332 (define-public (make-penalty-music pen)
333 (make-music 'BreakEvent
336 (define-public (make-articulation name)
337 (make-music 'ArticulationEvent
338 'articulation-type name))
340 (define-public (make-lyric-event string duration)
341 (make-music 'LyricEvent
345 (define-public (make-span-event type spandir)
347 'span-direction spandir))
349 (define-public (set-mus-properties! m alist)
350 "Set all of ALIST as properties of M."
353 (set! (ly:music-property m (caar alist)) (cdar alist))
354 (set-mus-properties! m (cdr alist)))))
356 (define-public (music-separator? m)
358 (let ((ts (ly:music-property m 'types)))
359 (memq 'separator ts)))
362 ;;; splitting chords into voices.
363 (define (voicify-list lst number)
364 "Make a list of Musics.
366 voicify-list :: [ [Music ] ] -> number -> [Music]
367 LST is a list music-lists.
369 NUMBER is 0-base, i.e. Voice=1 (upstems) has number 0.
373 (cons (context-spec-music
374 (make-sequential-music
375 (list (make-voice-props-set number)
376 (make-simultaneous-music (car lst))))
377 'Voice (number->string (1+ number)))
378 (voicify-list (cdr lst) (1+ number)))))
380 (define (voicify-chord ch)
381 "Split the parts of a chord into different Voices using separator"
382 (let ((es (ly:music-property ch 'elements)))
383 (set! (ly:music-property ch 'elements)
384 (voicify-list (split-list es music-separator?) 0))
387 (define-public (voicify-music m)
388 "Recursively split chords that are separated with \\ "
389 (if (not (ly:music? m))
391 (error "not music!")))
392 (let ((es (ly:music-property m 'elements))
393 (e (ly:music-property m 'element)))
395 (set! (ly:music-property m 'elements) (map voicify-music es)))
397 (set! (ly:music-property m 'element) (voicify-music e)))
398 (if (and (equal? (ly:music-name m) "Simultaneous_music")
399 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
400 (set! m (context-spec-music (voicify-chord m) 'Staff)))
403 (define-public (empty-music)
404 (ly:export (make-music 'Music)))
407 ; Make a function that checks score element for being of a specific type.
408 (define-public (make-type-checker symbol)
411 ;;(eq? #t (ly:grob-property elt symbol))
412 (not (eq? #f (memq symbol (ly:grob-property elt 'interfaces))))))
414 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
416 (set! (ly:grob-property grob sym) val)))
419 (define-public ((set-output-property grob-name symbol val) grob grob-c context)
422 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
425 (let ((meta (ly:grob-property grob 'meta)))
426 (if (equal? (cdr (assoc 'name meta)) grob-name)
427 (set! (ly:grob-property grob symbol) val))))
431 (define-public (smart-bar-check n)
432 "Make a bar check that checks for a specific bar number.
434 (let ((m (make-music 'ApplyContext)))
436 (let* ((bn (ly:context-property tr 'currentBarNumber)))
440 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
442 (set! (ly:music-property m 'procedure) checker)
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 ;; warn for bare chords at start.
448 (define (has-request-chord elts)
449 (reduce (lambda (x y) (or x y)) #f
451 (equal? (ly:music-name x) "Request_chord"))
454 (define (ly:music-message music msg)
455 (let ((ip (ly:music-property music 'origin)))
456 (if (ly:input-location? ip)
457 (ly:input-message ip msg)
460 (define (check-start-chords music)
461 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords),
462 without context specification. Called from parser."
463 (let ((es (ly:music-property music 'elements))
464 (e (ly:music-property music 'element))
465 (name (ly:music-name music)))
466 (cond ((equal? name "Context_specced_music") #t)
467 ((equal? name "Simultaneous_music")
468 (if (has-request-chord es)
469 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
470 (map check-start-chords es)))
471 ((equal? name "Sequential_music")
473 (check-start-chords (car es))))
474 (else (if (ly:music? e) (check-start-chords e)))))
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;; setting stuff for grace context.
484 (define (vector-extend v x)
485 "Make a new vector consisting of V, with X added to the end."
487 ((n (vector-length v))
488 (nv (make-vector (+ n 1) '())))
489 (vector-move-left! v 0 n nv 0)
493 (define (vector-map f v)
494 "Map F over V. This function returns nothing."
495 (do ((n (vector-length v))
498 (f (vector-ref v i))))
500 (define (vector-reverse-map f v)
501 "Map F over V, N to 0 order. This function returns nothing."
502 (do ((i (- (vector-length v) 1) (- i 1)))
504 (f (vector-ref v i))))
506 ;; TODO: make a remove-grace-property too.
507 (define-public (add-grace-property context-name grob sym val)
508 "Set SYM=VAL for GROB in CONTEXT-NAME. "
509 (define (set-prop context)
510 (let* ((where (ly:context-property-where-defined context 'graceSettings))
511 (current (ly:context-property where 'graceSettings))
512 (new-settings (vector-extend current (list context-name grob sym val))))
513 (ly:context-set-property! where 'graceSettings new-settings)))
514 (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
517 (define-public (set-start-grace-properties context)
518 (define (execute-1 x)
519 (let ((tr (ly:translator-find context (car x))))
521 (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x)))))
523 (let ((props (ly:context-property context 'graceSettings)))
525 (vector-map execute-1 props))))
527 (define-public (set-stop-grace-properties context)
528 (define (execute-1 x)
529 (let ((tr (ly:translator-find context (car x))))
531 (ly:context-pushpop-property tr (cadr x) (caddr x)))))
533 (let ((props (ly:context-property context 'graceSettings)))
535 (vector-reverse-map execute-1 props))))
537 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
538 ;; switch it on here, so parsing and init isn't checked (too slow!)
540 ;; automatic music transformations.
542 (define (switch-on-debugging m)
543 (set-debug-cell-accesses! 15000)
546 (define-public toplevel-music-functions
548 ;; check-start-chords ; ; no longer needed with chord syntax.
550 (lambda (x) (music-map glue-mm-rest-texts x))
551 ;; switch-on-debugging
557 (define (apply-durations lyric-music durations)
558 (define (apply-duration music)
559 (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
560 (ly:duration? (ly:music-property music 'duration)))
562 (set! (ly:music-property music 'duration) (car durations))
563 (set! durations (cdr durations)))))
565 (music-map apply-duration lyric-music))
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
571 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
572 "Usage: see input/regression/balloon.ly "
573 (let* ((meta (ly:grob-property grob 'meta))
574 (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
575 (cb (ly:grob-property grob 'print-function)))
576 (if (equal? nm object-name)
578 (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
579 (set! (ly:grob-property grob 'balloon-original-callback) cb)
580 (set! (ly:grob-property grob 'balloon-text) text)
581 (set! (ly:grob-property grob 'balloon-text-offset) off)
582 (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
584 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
587 (define-public (set-accidentals-properties extra-natural
591 (make-sequential-music
592 (append (if (boolean? extra-natural)
593 (list (make-property-set 'extraNatural extra-natural))
595 (list (make-property-set 'autoAccidentals auto-accs)
596 (make-property-set 'autoCautionaries auto-cauts))))
599 (define-public (set-accidental-style style . rest)
600 "Set accidental style to STYLE. Optionally takes a context argument,
601 eg. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
602 use PianoStaff as a context. "
603 (let ((context (if (pair? rest)
605 (pcontext (if (pair? rest)
606 (car rest) 'PianoStaff)))
609 ;; accidentals as they were common in the 18th century.
610 ((equal? style 'default)
611 (set-accidentals-properties #t '(Staff (same-octave . 0))
613 ;; accidentals from one voice do NOT get cancelled in other voices
614 ((equal? style 'voice)
615 (set-accidentals-properties #t '(Voice (same-octave . 0))
617 ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
618 ;; This includes all the default accidentals, but accidentals also needs cancelling
619 ;; in other octaves and in the next measure.
620 ((equal? style 'modern)
621 (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
623 ;; the accidentals that Stone adds to the old standard as cautionaries
624 ((equal? style 'modern-cautionary)
625 (set-accidentals-properties #f '(Staff (same-octave . 0))
626 '(Staff (any-octave . 0) (same-octave . 1))
628 ;; Multivoice accidentals to be read both by musicians playing one voice
629 ;; and musicians playing all voices.
630 ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
631 ((equal? style 'modern-voice)
632 (set-accidentals-properties #f
633 '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
634 Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
637 ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
639 ((equal? style 'modern-voice-cautionary)
640 (set-accidentals-properties #f
641 '(Voice (same-octave . 0) )
642 '(Voice (any-octave . 0) (same-octave . 1)
643 Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
645 ;; stone's suggestions for accidentals on grand staff.
646 ;; Accidentals are cancelled across the staves in the same grand staff as well
647 ((equal? style 'piano)
648 (set-accidentals-properties #f
649 '( Staff (same-octave . 0) (any-octave . 0) (same-octave . 1)
650 PianoStaff (any-octave . 0) (same-octave . 1))
653 ((equal? style 'piano-cautionary)
654 (set-accidentals-properties #f
655 '(Staff (same-octave . 0))
656 '(Staff (any-octave . 0) (same-octave . 1)
657 PianoStaff (any-octave . 0) (same-octave . 1))
659 ;; do not set localKeySignature when a note alterated differently from
660 ;; localKeySignature is found.
661 ;; Causes accidentals to be printed at every note instead of
662 ;; remembered for the duration of a measure.
663 ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
664 ((equal? style 'forget)
665 (set-accidentals-properties '()
666 '(Staff (same-octave . -1))
668 ;; Do not reset the key at the start of a measure. Accidentals will be
669 ;; printed only once and are in effect until overridden, possibly many
671 ((equal? style 'no-reset)
672 (set-accidentals-properties '()
673 '(Staff (same-octave . #t))
677 (ly:warn (string-append "Unknown accidental style: " (symbol->string style)))
678 (make-sequential-music '()))))))