1 ;;;; music-functions.scm -- implement Scheme output routines for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 (use-modules (ice-9 optargs))
10 ;;; ly:music-property with setter
11 ;;; (ly:music-property my-music 'elements)
12 ;;; ==> the 'elements property
13 ;;; (set! (ly:music-property my-music 'elements) value)
14 ;;; ==> set the 'elements property and return it
15 (define-public ly:music-property
16 (make-procedure-with-setter ly:music-property
17 ly:music-set-property!))
19 (define-public ly:grob-property
20 (make-procedure-with-setter ly:grob-property
21 ly:grob-set-property!))
23 (define-public (music-map function music)
24 "Apply @var{function} to @var{music} and all of the music it contains. "
25 (let ((es (ly:music-property music 'elements))
26 (e (ly:music-property music 'element)))
27 (set! (ly:music-property music 'elements)
28 (map (lambda (y) (music-map function y)) es))
30 (set! (ly:music-property music 'element)
31 (music-map function e)))
34 (define-public (music-filter pred? music)
35 "Filter out music expressions that do not satisfy PRED."
37 (define (inner-music-filter pred? music)
39 (let* ((es (ly:music-property music 'elements))
40 (e (ly:music-property music 'element))
41 (as (ly:music-property music 'articulations))
42 (filtered-as (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) as)))
43 (filtered-e (if (ly:music? e)
44 (inner-music-filter pred? e)
46 (filtered-es (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) es))))
47 (set! (ly:music-property music 'element) filtered-e)
48 (set! (ly:music-property music 'elements) filtered-es)
49 (set! (ly:music-property music 'articulations) filtered-as)
50 ;; if filtering emptied the expression, we remove it completely.
52 (and (eq? filtered-es '()) (not (ly:music? e))
53 (or (not (eq? es '()))
58 (set! music (inner-music-filter pred? music))
61 (make-music 'Music))) ;must return music.
63 (define-public (remove-tag tag)
67 (let* ((tags (ly:music-property m 'tags))
68 (res (memq tag tags)))
72 (define-public (display-music music)
73 "Display music, not done with music-map for clarity of presentation."
76 (let ((es (ly:music-property music 'elements))
77 (e (ly:music-property music 'element)))
78 (display (ly:mutable-music-properties music))
80 (begin (display "\nElements: {\n")
81 (map display-music es)
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 (define (shift-one-duration-log music shift dot)
94 " add SHIFT to ly:duration-log and optionally
95 a dot to any note encountered. This scales the music up by a factor
96 2^shift * (2 - (1/2)^dot)"
97 (let ((d (ly:music-property music 'duration)))
99 (let* ((cp (ly:duration-factor d))
100 (nd (ly:make-duration (+ shift (ly:duration-log d))
101 (+ dot (ly:duration-dot-count d))
104 (set! (ly:music-property music 'duration) nd)))
109 (define-public (shift-duration-log music shift dot)
110 (music-map (lambda (x) (shift-one-duration-log x shift dot))
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 (define-public (note-to-cluster music)
118 "Replace NoteEvents by ClusterNoteEvents."
119 (if (eq? (ly:music-property music 'name) 'NoteEvent)
120 (make-music 'ClusterNoteEvent
121 'pitch (ly:music-property music 'pitch)
122 'duration (ly:music-property music 'duration))
125 (define-public (notes-to-clusters music)
126 (music-map note-to-cluster music))
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 (define-public (unfold-repeats music)
133 This function replaces all repeats with unfold repeats. It was
134 written by Rune Zedeler. "
136 (let ((es (ly:music-property music 'elements))
137 (e (ly:music-property music 'element))
138 (n (ly:music-name music)))
139 (if (equal? n "Repeated_music")
141 ((seq-arg? (memq 'sequential-music
142 (ly:music-property e 'types))))
144 (if (equal? (ly:music-property music 'iterator-ctor)
145 Chord_tremolo_iterator::constructor)
147 (shift-duration-log music (+ (if seq-arg? 1 0)
148 (ly:intlog2 (ly:music-property music 'repeat-count))) 0)
150 (ly:music-compress e (ly:make-moment (length (ly:music-property e 'elements)) 1)))
153 (set! (ly:music-property music 'length)
154 Repeated_music::unfolded_music_length)
155 (set! (ly:music-property music 'start-moment-function)
156 Repeated_music::first_start)
157 (set! (ly:music-property music 'iterator-ctor)
158 Unfolded_repeat_iterator::constructor)))
161 (set! (ly:music-property music 'elements)
162 (map unfold-repeats es)))
164 (set! (ly:music-property music 'element)
169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
170 ;; property setting music objs.
172 (define-public (make-grob-property-set grob gprop val)
173 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
174 i.e. this is not an override"
175 (make-music 'OverrideProperty
181 (define-public (make-grob-property-override grob gprop val)
182 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
183 i.e. this is not an override"
184 (make-music 'OverrideProperty
189 (define-public (make-grob-property-revert grob gprop)
190 "Revert the grob property GPROP for GROB."
191 (make-music 'OverrideProperty
193 'grob-property gprop))
195 (define direction-polyphonic-grobs
196 '(Stem Tie Rest Slur Script TextScript Dots DotColumn
199 (define-public (make-voice-props-set n)
200 (make-sequential-music
202 (map (lambda (x) (make-grob-property-set x 'direction
204 direction-polyphonic-grobs)
206 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
207 (make-grob-property-set 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))
212 (define-public (make-voice-props-revert)
213 (make-sequential-music
215 (map (lambda (x) (make-grob-property-revert x 'direction))
216 direction-polyphonic-grobs)
217 (list (make-grob-property-revert 'NoteColumn 'horizontal-shift))
218 (list (make-grob-property-revert 'MultiMeasureRest 'staff-position)))))
221 (define*-public (context-spec-music m context #:optional id)
222 "Add \\context CONTEXT = ID to M. "
223 (let ((cm (make-music 'ContextSpeccedMusic
225 'context-type context)))
227 (set! (ly:music-property cm 'context-id) id))
231 (define-public (descend-to-context m context)
232 "Like context-spec-music, but only descending. "
233 (let ((cm (context-spec-music m context)))
234 (ly:music-set-property! cm 'descend-only #t)
237 (define-public (make-non-relative-music mus)
238 (make-music 'UnrelativableMusic
242 (define-public (make-apply-context func)
243 (make-music 'ApplyContext
246 (define-public (make-sequential-music elts)
247 (make-music 'SequentialMusic
250 (define-public (make-simultaneous-music elts)
251 (make-music 'SimultaneousMusic
254 (define-public (make-event-chord elts)
255 (make-music 'EventChord
258 (define-public (make-skip-music dur)
259 (make-music 'SkipMusic
261 (define-public (make-grace-music music)
262 (make-music 'GraceMusic
268 (define-public (make-multi-measure-rest duration location)
269 (make-music 'MultiMeasureRestMusicGroup
271 'elements (list (make-music 'BarCheck
273 (make-event-chord (list (make-music 'MultiMeasureRestEvent
275 'duration duration)))
276 (make-music 'BarCheck
279 (define-public (glue-mm-rest-texts music)
280 "Check if we have R1*4-\\markup { .. }, and if applicable convert to
281 a property set for MultiMeasureRestNumber."
282 (define (script-to-mmrest-text script-music)
283 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
284 (let ((dir (ly:music-property script-music 'direction))
285 (p (make-music 'MultiMeasureTextEvent
286 'text (ly:music-property script-music 'text))))
288 (set! (ly:music-property p 'direction) dir))
290 (if (eq? (ly:music-property music 'name) 'MultiMeasureRestMusicGroup)
291 (let* ((text? (lambda (x) (memq 'script-event (ly:music-property x 'types))))
292 (es (ly:music-property music 'elements))
293 (texts (map script-to-mmrest-text (filter text? es)))
294 (others (remove text? es)))
296 (set! (ly:music-property music 'elements)
297 (cons (make-event-chord texts) others)))))
301 (define-public (make-property-set sym val)
302 (make-music 'PropertySet
306 (define-public (make-ottava-set octavation)
307 (let ((m (make-music 'ApplyContext)))
308 (define (ottava-modify context)
309 "Either reset middleCPosition to the stored original, or remember
310 old middleCPosition, add OCTAVATION to middleCPosition, and set
311 OTTAVATION to `8va', or whatever appropriate."
312 (if (number? (ly:context-property context 'middleCPosition))
314 (let ((where (ly:context-property-where-defined context 'middleCPosition))
315 (oc0 (ly:context-property context 'originalCentralCPosition)))
316 (ly:context-set-property! context 'middleCPosition oc0)
317 (ly:context-unset-property where 'originalCentralCPosition)
318 (ly:context-unset-property where 'ottavation))
319 (let* ((where (ly:context-property-where-defined context 'middleCPosition))
320 (c0 (ly:context-property context 'middleCPosition))
321 (new-c0 (+ c0 (* -7 octavation)))
322 (string (cdr (assoc octavation '((2 . "15ma")
326 (-2 . "15ma bassa"))))))
327 (ly:context-set-property! context 'middleCPosition new-c0)
328 (ly:context-set-property! context 'originalCentralCPosition c0)
329 (ly:context-set-property! context 'ottavation string)))))
330 (set! (ly:music-property m 'procedure) ottava-modify)
331 (context-spec-music m 'Staff)))
333 (define-public (set-octavation ottavation)
334 (ly:export (make-ottava-set ottavation)))
336 (define-public (make-time-signature-set num den . rest)
337 " Set properties for time signature NUM/DEN.
338 Rest can contain a list of beat groupings "
339 (let* ((set1 (make-property-set 'timeSignatureFraction (cons num den)))
340 (beat (ly:make-moment 1 den))
341 (len (ly:make-moment num den))
342 (set2 (make-property-set 'beatLength beat))
343 (set3 (make-property-set 'measureLength len))
344 (set4 (make-property-set 'beatGrouping (if (pair? rest)
347 (basic (list set1 set2 set3 set4)))
349 (context-spec-music (make-sequential-music basic) 'Timing) 'Score)))
351 (define-public (make-mark-set label)
352 "make the music for the \\mark command."
353 (let* ((set (if (integer? label)
354 (context-spec-music (make-property-set 'rehearsalMark label)
357 (ev (make-music 'MarkEvent))
358 (ch (make-event-chord (list ev))))
360 (make-sequential-music (list set ch))
362 (set! (ly:music-property ev 'label) label)
365 (define-public (set-time-signature num den . rest)
366 (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
368 (define-public (make-penalty-music pen)
369 (make-music 'BreakEvent
372 (define-public (make-articulation name)
373 (make-music 'ArticulationEvent
374 'articulation-type name))
376 (define-public (make-lyric-event string duration)
377 (make-music 'LyricEvent
381 (define-public (make-span-event type spandir)
383 'span-direction spandir))
385 (define-public (set-mus-properties! m alist)
386 "Set all of ALIST as properties of M."
389 (set! (ly:music-property m (caar alist)) (cdar alist))
390 (set-mus-properties! m (cdr alist)))))
392 (define-public (music-separator? m)
394 (let ((ts (ly:music-property m 'types)))
395 (memq 'separator ts)))
398 ;;; splitting chords into voices.
399 (define (voicify-list lst number)
400 "Make a list of Musics.
402 voicify-list :: [ [Music ] ] -> number -> [Music]
403 LST is a list music-lists.
405 NUMBER is 0-base, i.e. Voice=1 (upstems) has number 0.
409 (cons (context-spec-music
410 (make-sequential-music
411 (list (make-voice-props-set number)
412 (make-simultaneous-music (car lst))))
413 'Voice (number->string (1+ number)))
414 (voicify-list (cdr lst) (1+ number)))))
416 (define (voicify-chord ch)
417 "Split the parts of a chord into different Voices using separator"
418 (let ((es (ly:music-property ch 'elements)))
419 (set! (ly:music-property ch 'elements)
420 (voicify-list (split-list es music-separator?) 0))
423 (define-public (voicify-music m)
424 "Recursively split chords that are separated with \\ "
425 (if (not (ly:music? m))
427 (error "not music!")))
428 (let ((es (ly:music-property m 'elements))
429 (e (ly:music-property m 'element)))
431 (set! (ly:music-property m 'elements) (map voicify-music es)))
433 (set! (ly:music-property m 'element) (voicify-music e)))
434 (if (and (equal? (ly:music-name m) "Simultaneous_music")
435 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
436 (set! m (context-spec-music (voicify-chord m) 'Staff)))
439 (define-public (empty-music)
440 (ly:export (make-music 'Music)))
443 ; Make a function that checks score element for being of a specific type.
444 (define-public (make-type-checker symbol)
447 ;;(eq? #t (ly:grob-property elt symbol))
448 (not (eq? #f (memq symbol (ly:grob-property elt 'interfaces))))))
450 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
452 (set! (ly:grob-property grob sym) val)))
455 (define-public ((set-output-property grob-name symbol val) grob grob-c context)
458 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
461 (let ((meta (ly:grob-property grob 'meta)))
462 (if (equal? (cdr (assoc 'name meta)) grob-name)
463 (set! (ly:grob-property grob symbol) val))))
467 (define-public (smart-bar-check n)
468 "Make a bar check that checks for a specific bar number.
470 (let ((m (make-music 'ApplyContext)))
472 (let* ((bn (ly:context-property tr 'currentBarNumber)))
476 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
478 (set! (ly:music-property m 'procedure) checker)
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482 ;; warn for bare chords at start.
484 (define (has-request-chord elts)
485 (reduce (lambda (x y) (or x y)) #f
487 (equal? (ly:music-name x) "Request_chord"))
490 (define (ly:music-message music msg)
491 (let ((ip (ly:music-property music 'origin)))
492 (if (ly:input-location? ip)
493 (ly:input-message ip msg)
496 (define (check-start-chords music)
497 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords),
498 without context specification. Called from parser."
499 (let ((es (ly:music-property music 'elements))
500 (e (ly:music-property music 'element))
501 (name (ly:music-name music)))
502 (cond ((equal? name "Context_specced_music") #t)
503 ((equal? name "Simultaneous_music")
504 (if (has-request-chord es)
505 (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
506 (map check-start-chords es)))
507 ((equal? name "Sequential_music")
509 (check-start-chords (car es))))
510 (else (if (ly:music? e) (check-start-chords e)))))
515 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 ;; setting stuff for grace context.
520 (define (vector-extend v x)
521 "Make a new vector consisting of V, with X added to the end."
523 ((n (vector-length v))
524 (nv (make-vector (+ n 1) '())))
525 (vector-move-left! v 0 n nv 0)
529 (define (vector-map f v)
530 "Map F over V. This function returns nothing."
531 (do ((n (vector-length v))
534 (f (vector-ref v i))))
536 (define (vector-reverse-map f v)
537 "Map F over V, N to 0 order. This function returns nothing."
538 (do ((i (- (vector-length v) 1) (- i 1)))
540 (f (vector-ref v i))))
542 ;; TODO: make a remove-grace-property too.
543 (define-public (add-grace-property context-name grob sym val)
544 "Set SYM=VAL for GROB in CONTEXT-NAME. "
545 (define (set-prop context)
546 (let* ((where (ly:context-property-where-defined context 'graceSettings))
547 (current (ly:context-property where 'graceSettings))
548 (new-settings (vector-extend current (list context-name grob sym val))))
549 (ly:context-set-property! where 'graceSettings new-settings)))
550 (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
553 (define-public (set-start-grace-properties context)
554 (define (execute-1 x)
555 (let ((tr (ly:context-find context (car x))))
557 (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x)))))
559 (let ((props (ly:context-property context 'graceSettings)))
561 (vector-map execute-1 props))))
563 (define-public (set-stop-grace-properties context)
564 (define (execute-1 x)
565 (let ((tr (ly:context-find context (car x))))
567 (ly:context-pushpop-property tr (cadr x) (caddr x)))))
569 (let ((props (ly:context-property context 'graceSettings)))
571 (vector-reverse-map execute-1 props))))
573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
574 ;; switch it on here, so parsing and init isn't checked (too slow!)
576 ;; automatic music transformations.
578 (define (switch-on-debugging m)
579 (if (defined? 'set-debug-cell-accesses!)
580 (set-debug-cell-accesses! 15000))
583 (define-public toplevel-music-functions
585 ;; check-start-chords ; ; no longer needed with chord syntax.
587 (lambda (x) (music-map glue-mm-rest-texts x))
588 ;; switch-on-debugging
594 (define (apply-durations lyric-music durations)
595 (define (apply-duration music)
596 (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
597 (ly:duration? (ly:music-property music 'duration)))
599 (set! (ly:music-property music 'duration) (car durations))
600 (set! durations (cdr durations)))))
602 (music-map apply-duration lyric-music))
605 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
608 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
609 "Usage: see input/regression/balloon.ly "
610 (let* ((meta (ly:grob-property grob 'meta))
611 (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
612 (cb (ly:grob-property grob 'print-function)))
613 (if (equal? nm object-name)
615 (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
616 (set! (ly:grob-property grob 'balloon-original-callback) cb)
617 (set! (ly:grob-property grob 'balloon-text) text)
618 (set! (ly:grob-property grob 'balloon-text-offset) off)
619 (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
624 (define-public (set-accidentals-properties extra-natural
628 (make-sequential-music
629 (append (if (boolean? extra-natural)
630 (list (make-property-set 'extraNatural extra-natural))
632 (list (make-property-set 'autoAccidentals auto-accs)
633 (make-property-set 'autoCautionaries auto-cauts))))
636 (define-public (set-accidental-style style . rest)
637 "Set accidental style to STYLE. Optionally takes a context argument,
638 e.g. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
639 use PianoStaff as a context. "
640 (let ((context (if (pair? rest)
642 (pcontext (if (pair? rest)
643 (car rest) 'PianoStaff)))
646 ;; accidentals as they were common in the 18th century.
647 ((equal? style 'default)
648 (set-accidentals-properties #t '(Staff (same-octave . 0))
650 ;; accidentals from one voice do NOT get cancelled in other voices
651 ((equal? style 'voice)
652 (set-accidentals-properties #t '(Voice (same-octave . 0))
654 ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
655 ;; This includes all the default accidentals, but accidentals also needs cancelling
656 ;; in other octaves and in the next measure.
657 ((equal? style 'modern)
658 (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
660 ;; the accidentals that Stone adds to the old standard as cautionaries
661 ((equal? style 'modern-cautionary)
662 (set-accidentals-properties #f '(Staff (same-octave . 0))
663 '(Staff (any-octave . 0) (same-octave . 1))
665 ;; Multivoice accidentals to be read both by musicians playing one voice
666 ;; and musicians playing all voices.
667 ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
668 ((equal? style 'modern-voice)
669 (set-accidentals-properties #f
670 '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
671 Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
674 ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
676 ((equal? style 'modern-voice-cautionary)
677 (set-accidentals-properties #f
678 '(Voice (same-octave . 0) )
679 '(Voice (any-octave . 0) (same-octave . 1)
680 Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
682 ;; stone's suggestions for accidentals on grand staff.
683 ;; Accidentals are cancelled across the staves in the same grand staff as well
684 ((equal? style 'piano)
685 (set-accidentals-properties #f
686 '( Staff (same-octave . 0) (any-octave . 0) (same-octave . 1)
687 PianoStaff (any-octave . 0) (same-octave . 1))
690 ((equal? style 'piano-cautionary)
691 (set-accidentals-properties #f
692 '(Staff (same-octave . 0))
693 '(Staff (any-octave . 0) (same-octave . 1)
694 PianoStaff (any-octave . 0) (same-octave . 1))
696 ;; do not set localKeySignature when a note alterated differently from
697 ;; localKeySignature is found.
698 ;; Causes accidentals to be printed at every note instead of
699 ;; remembered for the duration of a measure.
700 ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
701 ((equal? style 'forget)
702 (set-accidentals-properties '()
703 '(Staff (same-octave . -1))
705 ;; Do not reset the key at the start of a measure. Accidentals will be
706 ;; printed only once and are in effect until overridden, possibly many
708 ((equal? style 'no-reset)
709 (set-accidentals-properties '()
710 '(Staff (same-octave . #t))
714 (ly:warn (string-append "Unknown accidental style: " (symbol->string style)))
715 (make-sequential-music '()))))))