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. "
135 (let ((es (ly:music-property music 'elements))
136 (e (ly:music-property music 'element))
137 (n (ly:music-name music)))
138 (if (equal? n "Repeated_music")
140 (if (equal? (ly:music-property music 'iterator-ctor)
141 Chord_tremolo_iterator::constructor)
142 (shift-duration-log music (ly:intlog2 (ly:music-property music 'repeat-count)) 0))
143 (set! (ly:music-property music 'length)
144 Repeated_music::unfolded_music_length)
145 (set! (ly:music-property music 'start-moment-function)
146 Repeated_music::first_start)
147 (set! (ly:music-property music 'iterator-ctor)
148 Unfolded_repeat_iterator::constructor)))
150 (set! (ly:music-property music 'elements)
151 (map unfold-repeats es)))
153 (set! (ly:music-property music 'element)
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; property setting music objs.
161 (define-public (make-grob-property-set grob gprop val)
162 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
163 i.e. this is not an override"
164 (make-music 'OverrideProperty
170 (define-public (make-grob-property-override grob gprop val)
171 "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
172 i.e. this is not an override"
173 (make-music 'OverrideProperty
178 (define-public (make-grob-property-revert grob gprop)
179 "Revert the grob property GPROP for GROB."
180 (make-music 'OverrideProperty
182 'grob-property gprop))
184 (define direction-polyphonic-grobs
185 '(Tie Rest Slur Script TextScript Stem Dots DotColumn))
187 (define-public (make-voice-props-set n)
188 (make-sequential-music
190 (map (lambda (x) (make-grob-property-set x 'direction
192 direction-polyphonic-grobs)
193 (list (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
194 (make-grob-property-set 'MultiMeasureRest 'staff-position
195 (if (odd? n) -4 4))))))
197 (define-public (make-voice-props-revert)
198 (make-sequential-music
200 (map (lambda (x) (make-grob-property-revert x 'direction))
201 direction-polyphonic-grobs)
202 (list (make-grob-property-revert 'NoteColumn 'horizontal-shift)))))
205 (define*-public (context-spec-music m context #:optional id)
206 "Add \\context CONTEXT = ID to M. "
207 (let ((cm (make-music 'ContextSpeccedMusic
209 'context-type context)))
211 (set! (ly:music-property cm 'context-id) id))
214 (define-public (make-apply-context func)
215 (make-music 'ApplyContext
218 (define-public (make-sequential-music elts)
219 (make-music 'SequentialMusic
222 (define-public (make-simultaneous-music elts)
223 (make-music 'SimultaneousMusic
226 (define-public (make-event-chord elts)
227 (make-music 'EventChord
230 (define-public (make-skip-music dur)
231 (make-music 'SkipMusic
237 (define-public (make-multi-measure-rest duration location)
238 (make-music 'MultiMeasureRestMusicGroup
240 'elements (list (make-music 'BarCheck
242 (make-event-chord (list (make-music 'MultiMeasureRestEvent
244 'duration duration)))
245 (make-music 'BarCheck
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."
251 (define (script-to-mmrest-text script-music)
252 "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
253 (let ((dir (ly:music-property script-music 'direction))
254 (p (make-music 'MultiMeasureTextEvent
255 'text (ly:music-property script-music 'text))))
257 (set! (ly:music-property p 'direction) dir))
259 (if (eq? (ly:music-property music 'name) 'MultiMeasureRestMusicGroup)
260 (let* ((text? (lambda (x) (memq 'script-event (ly:music-property x 'types))))
261 (es (ly:music-property music 'elements))
262 (texts (map script-to-mmrest-text (filter text? es)))
263 (others (remove text? es)))
265 (set! (ly:music-property music 'elements)
266 (cons (make-event-chord texts) others)))))
270 (define-public (make-property-set sym val)
271 (make-music 'PropertySet
275 (define-public (make-ottava-set octavation)
276 (let ((m (make-music 'ApplyContext)))
277 (define (ottava-modify context)
278 "Either reset middleCPosition to the stored original, or remember
279 old middleCPosition, add OCTAVATION to middleCPosition, and set
280 OTTAVATION to `8va', or whatever appropriate."
281 (if (number? (ly:context-property context 'middleCPosition))
283 (let ((where (ly:context-property-where-defined context 'middleCPosition))
284 (oc0 (ly:context-property context 'originalCentralCPosition)))
285 (ly:context-set-property! context 'middleCPosition oc0)
286 (ly:unset-context-property where 'originalCentralCPosition)
287 (ly:unset-context-property where 'ottavation))
288 (let* ((where (ly:context-property-where-defined context 'middleCPosition))
289 (c0 (ly:context-property context 'middleCPosition))
290 (new-c0 (+ c0 (* -7 octavation)))
291 (string (cdr (assoc octavation '((2 . "15ma")
295 (-2 . "15ma bassa"))))))
296 (ly:context-set-property! context 'middleCPosition new-c0)
297 (ly:context-set-property! context 'originalCentralCPosition c0)
298 (ly:context-set-property! context 'ottavation string)))))
299 (set! (ly:music-property m 'procedure) ottava-modify)
300 (context-spec-music m 'Staff)))
302 (define-public (set-octavation ottavation)
303 (ly:export (make-ottava-set ottavation)))
305 (define-public (make-time-signature-set num den . rest)
306 " Set properties for time signature NUM/DEN.
307 Rest can contain a list of beat groupings
310 (let* ((set1 (make-property-set 'timeSignatureFraction (cons num den)))
311 (beat (ly:make-moment 1 den))
312 (len (ly:make-moment num den))
313 (set2 (make-property-set 'beatLength beat))
314 (set3 (make-property-set 'measureLength len))
315 (set4 (make-property-set 'beatGrouping (if (pair? rest)
318 (basic (list set1 set2 set3 set4)))
320 (context-spec-music (make-sequential-music basic) 'Timing) 'Score)))
322 (define-public (make-mark-set label)
323 "make the music for the \\mark command."
324 (let* ((set (if (integer? label)
325 (context-spec-music (make-property-set 'rehearsalMark label)
328 (ev (make-music 'MarkEvent))
329 (ch (make-event-chord (list ev))))
331 (make-sequential-music (list set ch))
333 (set! (ly:music-property ev 'label) label)
336 (define-public (set-time-signature num den . rest)
337 (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
339 (define-public (make-penalty-music pen)
340 (make-music 'BreakEvent
343 (define-public (make-articulation name)
344 (make-music 'ArticulationEvent
345 'articulation-type name))
347 (define-public (make-lyric-event string duration)
348 (make-music 'LyricEvent
352 (define-public (make-span-event type spandir)
354 'span-direction spandir))
356 (define-public (set-mus-properties! m alist)
357 "Set all of ALIST as properties of M."
360 (set! (ly:music-property m (caar alist)) (cdar alist))
361 (set-mus-properties! m (cdr alist)))))
363 (define-public (music-separator? m)
365 (let ((ts (ly:music-property m 'types)))
366 (memq 'separator ts)))
369 ;;; splitting chords into voices.
370 (define (voicify-list lst number)
371 "Make a list of Musics.
373 voicify-list :: [ [Music ] ] -> number -> [Music]
374 LST is a list music-lists.
376 NUMBER is 0-base, i.e. Voice=1 (upstems) has number 0.
380 (cons (context-spec-music
381 (make-sequential-music
382 (list (make-voice-props-set number)
383 (make-simultaneous-music (car lst))))
384 'Voice (number->string (1+ number)))
385 (voicify-list (cdr lst) (1+ number)))))
387 (define (voicify-chord ch)
388 "Split the parts of a chord into different Voices using separator"
389 (let ((es (ly:music-property ch 'elements)))
390 (set! (ly:music-property ch 'elements)
391 (voicify-list (split-list es music-separator?) 0))
394 (define-public (voicify-music m)
395 "Recursively split chords that are separated with \\ "
396 (if (not (ly:music? m))
398 (error "not music!")))
399 (let ((es (ly:music-property m 'elements))
400 (e (ly:music-property m 'element)))
402 (set! (ly:music-property m 'elements) (map voicify-music es)))
404 (set! (ly:music-property m 'element) (voicify-music e)))
405 (if (and (equal? (ly:music-name m) "Simultaneous_music")
406 (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
407 (set! m (context-spec-music (voicify-chord m) 'Staff)))
410 (define-public (empty-music)
411 (ly:export (make-music '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:grob-property elt symbol))
419 (not (eq? #f (memq symbol (ly:grob-property elt 'interfaces))))))
421 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
423 (set! (ly:grob-property grob sym) val)))
426 (define-public ((set-output-property grob-name symbol val) grob grob-c context)
429 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
432 (let ((meta (ly:grob-property grob 'meta)))
433 (if (equal? (cdr (assoc 'name meta)) grob-name)
434 (set! (ly:grob-property grob symbol) val))))
438 (define-public (smart-bar-check n)
439 "Make a bar check that checks for a specific bar number.
441 (let ((m (make-music 'ApplyContext)))
443 (let* ((bn (ly:context-property tr 'currentBarNumber)))
447 (format "Bar check failed, we should have reached ~a, instead at ~a\n"
449 (set! (ly:music-property m 'procedure) checker)
452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
453 ;; warn for bare chords at start.
455 (define (has-request-chord elts)
456 (reduce (lambda (x y) (or x y)) #f
458 (equal? (ly:music-name x) "Request_chord"))
461 (define (ly:music-message music msg)
462 (let ((ip (ly:music-property music 'origin)))
463 (if (ly:input-location? ip)
464 (ly:input-message ip msg)
467 (define (check-start-chords music)
468 "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords),
469 without context specification. Called from parser."
470 (let ((es (ly:music-property music 'elements))
471 (e (ly:music-property music 'element))
472 (name (ly:music-name music)))
473 (cond ((equal? name "Context_specced_music") #t)
474 ((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)))
478 ((equal? name "Sequential_music")
480 (check-start-chords (car es))))
481 (else (if (ly:music? e) (check-start-chords e)))))
486 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;; setting stuff for grace context.
491 (define (vector-extend v x)
492 "Make a new vector consisting of V, with X added to the end."
494 ((n (vector-length v))
495 (nv (make-vector (+ n 1) '())))
496 (vector-move-left! v 0 n nv 0)
500 (define (vector-map f v)
501 "Map F over V. This function returns nothing."
502 (do ((n (vector-length v))
505 (f (vector-ref v i))))
507 (define (vector-reverse-map f v)
508 "Map F over V, N to 0 order. This function returns nothing."
509 (do ((i (- (vector-length v) 1) (- i 1)))
511 (f (vector-ref v i))))
513 ;; TODO: make a remove-grace-property too.
514 (define-public (add-grace-property context-name grob sym val)
515 "Set SYM=VAL for GROB in CONTEXT-NAME. "
516 (define (set-prop context)
517 (let* ((where (ly:context-property-where-defined context 'graceSettings))
518 (current (ly:context-property where 'graceSettings))
519 (new-settings (vector-extend current (list context-name grob sym val))))
520 (ly:context-set-property! where 'graceSettings new-settings)))
521 (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
524 (define-public (set-start-grace-properties context)
525 (define (execute-1 x)
526 (let ((tr (ly:context-find context (car x))))
528 (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x)))))
530 (let ((props (ly:context-property context 'graceSettings)))
532 (vector-map execute-1 props))))
534 (define-public (set-stop-grace-properties context)
535 (define (execute-1 x)
536 (let ((tr (ly:context-find context (car x))))
538 (ly:context-pushpop-property tr (cadr x) (caddr x)))))
540 (let ((props (ly:context-property context 'graceSettings)))
542 (vector-reverse-map execute-1 props))))
544 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
545 ;; switch it on here, so parsing and init isn't checked (too slow!)
547 ;; automatic music transformations.
549 (define (switch-on-debugging m)
550 (set-debug-cell-accesses! 15000)
553 (define-public toplevel-music-functions
555 ;; check-start-chords ; ; no longer needed with chord syntax.
557 (lambda (x) (music-map glue-mm-rest-texts x))
558 ;; switch-on-debugging
564 (define (apply-durations lyric-music durations)
565 (define (apply-duration music)
566 (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
567 (ly:duration? (ly:music-property music 'duration)))
569 (set! (ly:music-property music 'duration) (car durations))
570 (set! durations (cdr durations)))))
572 (music-map apply-duration lyric-music))
575 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
578 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
579 "Usage: see input/regression/balloon.ly "
580 (let* ((meta (ly:grob-property grob 'meta))
581 (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
582 (cb (ly:grob-property grob 'print-function)))
583 (if (equal? nm object-name)
585 (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
586 (set! (ly:grob-property grob 'balloon-original-callback) cb)
587 (set! (ly:grob-property grob 'balloon-text) text)
588 (set! (ly:grob-property grob 'balloon-text-offset) off)
589 (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
591 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
594 (define-public (set-accidentals-properties extra-natural
598 (make-sequential-music
599 (append (if (boolean? extra-natural)
600 (list (make-property-set 'extraNatural extra-natural))
602 (list (make-property-set 'autoAccidentals auto-accs)
603 (make-property-set 'autoCautionaries auto-cauts))))
606 (define-public (set-accidental-style style . rest)
607 "Set accidental style to STYLE. Optionally takes a context argument,
608 eg. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
609 use PianoStaff as a context. "
610 (let ((context (if (pair? rest)
612 (pcontext (if (pair? rest)
613 (car rest) 'PianoStaff)))
616 ;; accidentals as they were common in the 18th century.
617 ((equal? style 'default)
618 (set-accidentals-properties #t '(Staff (same-octave . 0))
620 ;; accidentals from one voice do NOT get cancelled in other voices
621 ((equal? style 'voice)
622 (set-accidentals-properties #t '(Voice (same-octave . 0))
624 ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
625 ;; This includes all the default accidentals, but accidentals also needs cancelling
626 ;; in other octaves and in the next measure.
627 ((equal? style 'modern)
628 (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
630 ;; the accidentals that Stone adds to the old standard as cautionaries
631 ((equal? style 'modern-cautionary)
632 (set-accidentals-properties #f '(Staff (same-octave . 0))
633 '(Staff (any-octave . 0) (same-octave . 1))
635 ;; Multivoice accidentals to be read both by musicians playing one voice
636 ;; and musicians playing all voices.
637 ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
638 ((equal? style 'modern-voice)
639 (set-accidentals-properties #f
640 '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
641 Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
644 ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
646 ((equal? style 'modern-voice-cautionary)
647 (set-accidentals-properties #f
648 '(Voice (same-octave . 0) )
649 '(Voice (any-octave . 0) (same-octave . 1)
650 Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
652 ;; stone's suggestions for accidentals on grand staff.
653 ;; Accidentals are cancelled across the staves in the same grand staff as well
654 ((equal? style 'piano)
655 (set-accidentals-properties #f
656 '( Staff (same-octave . 0) (any-octave . 0) (same-octave . 1)
657 PianoStaff (any-octave . 0) (same-octave . 1))
660 ((equal? style 'piano-cautionary)
661 (set-accidentals-properties #f
662 '(Staff (same-octave . 0))
663 '(Staff (any-octave . 0) (same-octave . 1)
664 PianoStaff (any-octave . 0) (same-octave . 1))
666 ;; do not set localKeySignature when a note alterated differently from
667 ;; localKeySignature is found.
668 ;; Causes accidentals to be printed at every note instead of
669 ;; remembered for the duration of a measure.
670 ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
671 ((equal? style 'forget)
672 (set-accidentals-properties '()
673 '(Staff (same-octave . -1))
675 ;; Do not reset the key at the start of a measure. Accidentals will be
676 ;; printed only once and are in effect until overridden, possibly many
678 ((equal? style 'no-reset)
679 (set-accidentals-properties '()
680 '(Staff (same-octave . #t))
684 (ly:warn (string-append "Unknown accidental style: " (symbol->string style)))
685 (make-sequential-music '()))))))