1 ;;; define-music-display-methods.scm -- data for displaying music
2 ;;; expressions using LilyPond notation.
4 ;;; Copyright (C) 2005--2009 Nicolas Sceaux <nicolas.sceaux@free.fr>
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;; Display method implementation
12 (define-module (scm display-lily))
17 (define (scheme-expr->lily-string scm-arg)
18 (cond ((or (number? scm-arg)
20 (format #f "~s" scm-arg))
21 ((or (symbol? scm-arg)
23 (format #f "'~s" scm-arg))
26 (or (procedure-name scm-arg)
27 (with-output-to-string
29 (pretty-print (procedure-source scm-arg)))))))
32 (with-output-to-string
34 (display-scheme-music scm-arg)))))))
39 (define-public (markup->lily-string markup-expr)
40 "Return a string describing, in LilyPond syntax, the given markup expression."
41 (define (proc->command proc)
42 (let ((cmd-markup (symbol->string (procedure-name proc))))
43 (substring cmd-markup 0 (- (string-length cmd-markup)
44 (string-length "-markup")))))
45 (define (arg->string arg)
48 ((markup? arg) ;; a markup
49 (markup->lily-string-aux arg))
50 ((and (pair? arg) (every markup? arg)) ;; a markup list
51 (format #f "{~{ ~a~}}" (map-in-order markup->lily-string-aux arg)))
52 (else ;; a scheme argument
53 (format #f "#~a" (scheme-expr->lily-string arg)))))
54 (define (markup->lily-string-aux expr)
57 (let ((cmd (car expr))
59 (if (eqv? cmd simple-markup) ;; a simple markup
60 (format #f "~s" (car args))
61 (format #f "\\~a~{ ~a~}"
63 (map-in-order arg->string args))))))
64 (cond ((string? markup-expr)
65 (format #f "~s" markup-expr))
66 ((eqv? (car markup-expr) simple-markup)
67 (format #f "~s" (second markup-expr)))
69 (format #f "\\markup ~a"
70 (markup->lily-string-aux markup-expr)))))
76 ;; It is a pity that there is no rassoc in Scheme.
77 (define* (rassoc item alist #:optional (test equal?))
78 (do ((alist alist (cdr alist))
80 ((or result (null? alist)) result)
81 (if (and (car alist) (test item (cdar alist)))
82 (set! result (car alist)))))
84 (define-public (note-name->lily-string ly-pitch parser)
85 ;; here we define a custom pitch= function, since we do not want to
86 ;; test whether octaves are also equal. (otherwise, we would be using equal?)
87 (define (pitch= pitch1 pitch2)
88 (and (= (ly:pitch-notename pitch1) (ly:pitch-notename pitch2))
89 (= (ly:pitch-alteration pitch1) (ly:pitch-alteration pitch2))))
90 (let ((result (rassoc ly-pitch (ly:parser-lookup parser 'pitchnames) pitch=)))
95 (define-public (octave->lily-string pitch)
96 (let ((octave (ly:pitch-octave pitch)))
98 (make-string (1+ octave) #\'))
100 (make-string (1- (* -1 octave)) #\,))
106 (define*-public (duration->lily-string ly-duration #:key (prev-duration (*previous-duration*))
107 (force-duration (*force-duration*))
108 (time-factor-numerator (*time-factor-numerator*))
109 (time-factor-denominator (*time-factor-denominator*)))
110 (let ((log2 (ly:duration-log ly-duration))
111 (dots (ly:duration-dot-count ly-duration))
112 (num+den (ly:duration-factor ly-duration)))
113 (if (or force-duration (not prev-duration) (not (equal? ly-duration prev-duration)))
114 (string-append (case log2
118 (else (number->string (expt 2 log2))))
119 (make-string dots #\.)
120 (let ((num? (not (or (= 1 (car num+den))
121 (and time-factor-numerator
122 (= (car num+den) time-factor-numerator)))))
123 (den? (not (or (= 1 (cdr num+den))
124 (and time-factor-denominator
125 (= (cdr num+den) time-factor-denominator))))))
127 (format #f "*~a/~a" (car num+den) (cdr num+den)))
129 (format #f "*~a" (car num+den)))
138 (make-music-type-predicate
139 'AbsoluteDynamicEvent
151 'MultiMeasureTextEvent
166 (define* (event-direction->lily-string event #:optional (required #t))
167 (let ((direction (ly:music-property event 'direction)))
168 (cond ((or (not direction) (null? direction) (= CENTER direction))
169 (if required "-" ""))
170 ((= UP direction) "^")
171 ((= DOWN direction) "_")
174 (define-macro (define-post-event-display-method type vars direction-required str)
175 `(define-display-method ,type ,vars
177 (event-direction->lily-string ,(car vars) ,direction-required)
180 (define-macro (define-span-event-display-method type vars direction-required str-start str-stop)
181 `(define-display-method ,type ,vars
183 (event-direction->lily-string ,(car vars) ,direction-required)
184 (if (= START (ly:music-property ,(car vars) 'span-direction))
188 (define-display-method HyphenEvent (event parser)
190 (define-display-method ExtenderEvent (event parser)
192 (define-display-method TieEvent (event parser)
194 (define-display-method BeamForbidEvent (event parser)
196 (define-display-method StringNumberEvent (event parser)
197 (format #f "\\~a" (ly:music-property event 'string-number)))
200 (define-display-method TremoloEvent (event parser)
201 (let ((tremolo-type (ly:music-property event 'tremolo-type)))
202 (format #f ":~a" (if (= 0 tremolo-type)
206 (define-post-event-display-method ArticulationEvent (event parser) #t
207 (let ((articulation (ly:music-property event 'articulation-type)))
208 (case (string->symbol articulation)
212 ((staccatissimo) "|")
216 (else (format #f "\\~a" articulation)))))
218 (define-post-event-display-method FingeringEvent (event parser) #t
219 (ly:music-property event 'digit))
221 (define-post-event-display-method TextScriptEvent (event parser) #t
222 (markup->lily-string (ly:music-property event 'text)))
224 (define-post-event-display-method MultiMeasureTextEvent (event parser) #t
225 (markup->lily-string (ly:music-property event 'text)))
227 (define-post-event-display-method HarmonicEvent (event parser) #f "\\harmonic")
228 (define-post-event-display-method GlissandoEvent (event parser) #t "\\glissando")
229 (define-post-event-display-method ArpeggioEvent (event parser) #t "\\arpeggio")
230 (define-post-event-display-method AbsoluteDynamicEvent (event parser) #f
231 (format #f "\\~a" (ly:music-property event 'text)))
233 (define-span-event-display-method BeamEvent (event parser) #f "[" "]")
234 (define-span-event-display-method SlurEvent (event parser) #f "(" ")")
235 (define-span-event-display-method CrescendoEvent (event parser) #f "\\<" "\\!")
236 (define-span-event-display-method DecrescendoEvent (event parser) #f "\\>" "\\!")
237 (define-span-event-display-method PhrasingSlurEvent (event parser) #f "\\(" "\\)")
238 (define-span-event-display-method SustainEvent (event parser) #f "\\sustainOn" "\\sustainOff")
239 (define-span-event-display-method SostenutoEvent (event parser) #f "\\sostenutoOn" "\\sostenutoOff")
240 (define-span-event-display-method TextSpanEvent (event parser) #f "\\startTextSpan" "\\stopTextSpan")
241 (define-span-event-display-method TrillSpanEvent (event parser) #f "\\startTrillSpan" "\\stopTrillSpan")
242 (define-span-event-display-method StaffSpanEvent (event parser) #f "\\startStaff" "\\stopStaff")
243 (define-span-event-display-method NoteGroupingEvent (event parser) #f "\\startGroup" "\\stopGroup")
244 (define-span-event-display-method UnaCordaEvent (event parser) #f "\\unaCorda" "\\treCorde")
250 (define-display-method GraceMusic (expr parser)
251 (format #f "\\grace ~a"
252 (music->lily-string (ly:music-property expr 'element) parser)))
254 ;; \acciaccatura \appoggiatura \grace
255 ;; TODO: it would be better to compare ?start and ?stop
256 ;; with startAppoggiaturaMusic and stopAppoggiaturaMusic,
257 ;; using a custom music equality predicate.
258 (define-extra-display-method GraceMusic (expr parser)
259 "Display method for appoggiatura."
260 (with-music-match (expr (music
267 ;; we check whether ?start and ?stop look like
268 ;; startAppoggiaturaMusic stopAppoggiaturaMusic
269 (and (with-music-match (?start (music
275 duration (ly:make-duration 0 0 0 1))
278 span-direction START))))))
280 (with-music-match (?stop (music
286 duration (ly:make-duration 0 0 0 1))
289 span-direction STOP))))))
290 (format #f "\\appoggiatura ~a" (music->lily-string ?music parser))))))
293 (define-extra-display-method GraceMusic (expr parser)
294 "Display method for acciaccatura."
295 (with-music-match (expr (music
302 ;; we check whether ?start and ?stop look like
303 ;; startAcciaccaturaMusic stopAcciaccaturaMusic
304 (and (with-music-match (?start (music
310 duration (ly:make-duration 0 0 0 1))
313 span-direction START)))
318 grob-property-path '(stroke-style)
322 (with-music-match (?stop (music
328 grob-property-path '(stroke-style)
334 duration (ly:make-duration 0 0 0 1))
337 span-direction STOP))))))
338 (format #f "\\acciaccatura ~a" (music->lily-string ?music parser))))))
340 (define-extra-display-method GraceMusic (expr parser)
341 "Display method for grace."
342 (with-music-match (expr (music
349 ;; we check whether ?start and ?stop look like
350 ;; startGraceMusic stopGraceMusic
351 (and (null? (ly:music-property ?start 'elements))
352 (null? (ly:music-property ?stop 'elements))
353 (format #f "\\grace ~a" (music->lily-string ?music parser)))))
359 (define-display-method SequentialMusic (seq parser)
360 (let ((force-line-break (and (*force-line-break*)
362 (> (length (ly:music-property seq 'elements))
363 (*max-element-number-before-break*))))
364 (elements (ly:music-property seq 'elements))
365 (chord? (make-music-type-predicate 'EventChord))
366 (cluster? (make-music-type-predicate 'ClusterNoteEvent))
367 (note? (make-music-type-predicate 'NoteEvent)))
368 (format #f "~a~a{~v%~v_~{~a ~}~v%~v_}"
371 (any cluster? (ly:music-property e 'elements))))
375 (if (*explicit-mode*)
376 ;; if the sequence contains EventChord which contains figures ==> figuremode
377 ;; if the sequence contains EventChord which contains lyrics ==> lyricmode
378 ;; if the sequence contains EventChord which contains drum notes ==> drummode
379 (cond ((any (lambda (chord)
380 (any (make-music-type-predicate 'BassFigureEvent)
381 (ly:music-property chord 'elements)))
382 (filter chord? elements))
384 ((any (lambda (chord)
385 (any (make-music-type-predicate 'LyricEvent)
386 (ly:music-property chord 'elements)))
387 (filter chord? elements))
389 ((any (lambda (chord)
392 (not (null? (ly:music-property event 'drum-type)))))
393 (ly:music-property chord 'elements)))
394 (filter chord? elements))
396 (else ;; TODO: other modes?
399 (if force-line-break 1 0)
400 (if force-line-break (+ 2 (*indent*)) 1)
401 (parameterize ((*indent* (+ 2 (*indent*))))
402 (map-in-order (lambda (music)
403 (music->lily-string music parser))
405 (if force-line-break 1 0)
406 (if force-line-break (*indent*) 0))))
408 (define-display-method SimultaneousMusic (sim parser)
409 (parameterize ((*indent* (+ 3 (*indent*))))
410 (format #f "<< ~{~a ~}>>"
411 (map-in-order (lambda (music)
412 (music->lily-string music parser))
413 (ly:music-property sim 'elements)))))
415 (define-extra-display-method SimultaneousMusic (expr parser)
416 "If `sim' is an \afterGrace expression, return \"\\afterGrace ...\".
417 Otherwise, return #f."
418 ;; TODO: do something with afterGraceFraction?
419 (with-music-match (expr (music 'SimultaneousMusic
420 elements (?before-grace
421 (music 'SequentialMusic
422 elements ((music 'SkipMusic)
425 (format #f "\\afterGrace ~a ~a"
426 (music->lily-string ?before-grace parser)
427 (music->lily-string ?grace parser))))
433 (define-display-method EventChord (chord parser)
434 ;; event_chord : simple_element post_events
436 ;; | note_chord_element
438 ;; TODO : tagged post_events
439 ;; post_events : ( post_event | tagged_post_event )*
440 ;; tagged_post_event: '-' \tag embedded_scm post_event
442 (let* ((elements (ly:music-property chord 'elements))
443 (simple-elements (filter (make-music-type-predicate
444 'NoteEvent 'ClusterNoteEvent 'RestEvent
445 'MultiMeasureRestEvent 'SkipEvent 'LyricEvent)
447 (if ((make-music-type-predicate 'StaffSpanEvent 'BreathingEvent) (car elements))
448 ;; first, a special case: StaffSpanEvent (\startStaff, \stopStaff)
449 ;; and BreathingEvent (\breathe)
450 (music->lily-string (car elements) parser)
451 (if (and (not (null? simple-elements))
452 (null? (cdr simple-elements))
453 ;; special case: if this simple_element has a HarmonicEvent in its
454 ;; 'articulations list, it should be interpreted instead as a
455 ;; note_chord_element, since \harmonic only works inside chords,
456 ;; even for single notes, e.g., < c\harmonic >
457 (null? (filter (make-music-type-predicate 'HarmonicEvent)
458 (ly:music-property (car simple-elements) 'articulations))))
459 ;; simple_element : note | figure | rest | mmrest | lyric_element | skip
460 (let* ((simple-element (car simple-elements))
461 (duration (ly:music-property simple-element 'duration))
462 (lily-string (format #f "~a~a~a~{~a ~}"
463 (music->lily-string simple-element parser)
464 (duration->lily-string duration)
465 (if (and ((make-music-type-predicate 'RestEvent) simple-element)
466 (ly:pitch? (ly:music-property simple-element 'pitch)))
469 (map-in-order (lambda (music)
470 (music->lily-string music parser))
471 (filter post-event? elements)))))
472 (*previous-duration* duration)
474 (let ((chord-elements (filter (make-music-type-predicate
475 'NoteEvent 'ClusterNoteEvent 'BassFigureEvent)
477 (post-events (filter post-event? elements)))
478 (if (not (null? chord-elements))
479 ;; note_chord_element : '<' (notepitch | drumpitch)* '>" duration post_events
480 (let ((lily-string (format #f "< ~{~a ~}>~a~{~a ~}"
481 (map-in-order (lambda (music)
482 (music->lily-string music parser))
484 (duration->lily-string (ly:music-property (car chord-elements)
486 (map-in-order (lambda (music)
487 (music->lily-string music parser))
489 (*previous-duration* (ly:music-property (car chord-elements) 'duration))
492 (format #f "~{~a ~}" (map-in-order (lambda (music)
493 (music->lily-string music parser))
496 (define-display-method MultiMeasureRestMusic (mmrest parser)
497 (let* ((dur (ly:music-property mmrest 'duration))
498 (ly (format #f "R~a~{~a ~}"
499 (duration->lily-string dur)
500 (map-in-order (lambda (music)
501 (music->lily-string music parser))
502 (ly:music-property mmrest 'articulations)))))
503 (*previous-duration* dur)
506 (define-display-method SkipMusic (skip parser)
507 (format #f "\\skip ~a" (duration->lily-string (ly:music-property skip 'duration) #:force-duration #t)))
510 ;;; Notes, rests, skips...
513 (define (simple-note->lily-string event parser)
514 (format #f "~a~a~a~a~{~a~}" ; pitchname octave !? octave-check articulations
515 (note-name->lily-string (ly:music-property event 'pitch) parser)
516 (octave->lily-string (ly:music-property event 'pitch))
517 (let ((forced (ly:music-property event 'force-accidental))
518 (cautionary (ly:music-property event 'cautionary)))
519 (cond ((and (not (null? forced))
521 (not (null? cautionary))
524 ((and (not (null? forced)) forced) "!")
526 (let ((octave-check (ly:music-property event 'absolute-octave)))
527 (if (not (null? octave-check))
528 (format #f "=~a" (cond ((>= octave-check 0)
529 (make-string (1+ octave-check) #\'))
531 (make-string (1- (* -1 octave-check)) #\,))
534 (map-in-order (lambda (event)
535 (music->lily-string event parser))
536 (ly:music-property event 'articulations))))
538 (define-display-method NoteEvent (note parser)
539 (cond ((not (null? (ly:music-property note 'pitch))) ;; note
540 (simple-note->lily-string note parser))
541 ((not (null? (ly:music-property note 'drum-type))) ;; drum
542 (format #f "~a" (ly:music-property note 'drum-type)))
546 (define-display-method ClusterNoteEvent (note parser)
547 (simple-note->lily-string note parser))
549 (define-display-method RestEvent (rest parser)
550 (if (not (null? (ly:music-property rest 'pitch)))
551 (simple-note->lily-string rest parser)
554 (define-display-method MultiMeasureRestEvent (rest parser)
557 (define-display-method SkipEvent (rest parser)
560 (define-display-method MarkEvent (mark parser)
561 (let ((label (ly:music-property mark 'label)))
564 (format #f "\\mark ~a" (markup->lily-string label)))))
566 (define-display-method KeyChangeEvent (key parser)
567 (let ((pitch-alist (ly:music-property key 'pitch-alist))
568 (tonic (ly:music-property key 'tonic)))
569 (if (or (null? pitch-alist)
572 (let ((c-pitch-alist (ly:transpose-key-alist pitch-alist
573 (ly:pitch-diff (ly:make-pitch 0 0 0) tonic))))
574 (format #f "\\key ~a \\~a~a"
575 (note-name->lily-string (ly:music-property key 'tonic) parser)
578 (equal? (ly:parser-lookup parser mode) c-pitch-alist))
579 (symbol->string mode)
581 '(major minor ionian locrian aeolian mixolydian lydian phrygian dorian))
582 (new-line->lily-string))))))
584 (define-display-method RelativeOctaveCheck (octave parser)
585 (let ((pitch (ly:music-property octave 'pitch)))
586 (format #f "\\octaveCheck ~a~a"
587 (note-name->lily-string pitch parser)
588 (octave->lily-string pitch))))
590 (define-display-method VoiceSeparator (sep parser)
593 (define-display-method LigatureEvent (ligature parser)
594 (if (= START (ly:music-property ligature 'span-direction))
598 (define-display-method BarCheck (check parser)
599 (format #f "|~a" (new-line->lily-string)))
601 (define-display-method PesOrFlexaEvent (expr parser)
604 (define-display-method BassFigureEvent (figure parser)
605 (let ((alteration (ly:music-property figure 'alteration))
606 (fig (ly:music-property figure 'figure))
607 (bracket-start (ly:music-property figure 'bracket-start))
608 (bracket-stop (ly:music-property figure 'bracket-stop)))
610 (format #f "~a~a~a~a"
611 (if (null? bracket-start) "" "[")
612 (cond ((null? fig) "_")
613 ((markup? fig) (second fig)) ;; fig: (<number-markup> "number")
615 (if (null? alteration)
618 ((= alteration DOUBLE-FLAT) "--")
619 ((= alteration FLAT) "-")
620 ((= alteration NATURAL) "!")
621 ((= alteration SHARP) "+")
622 ((= alteration DOUBLE-SHARP) "++")
624 (if (null? bracket-stop) "" "]"))))
626 (define-display-method LyricEvent (lyric parser)
627 (let ((text (ly:music-property lyric 'text)))
628 (if (or (string? text)
629 (eqv? (first text) simple-markup))
630 ;; a string or a simple markup
631 (let ((string (if (string? text)
634 (if (string-match "(\"| |[0-9])" string)
635 ;; TODO check exactly in which cases double quotes should be used
636 (format #f "~s" string)
638 (markup->lily-string text))))
640 (define-display-method BreathingEvent (event parser)
647 (define-display-method AutoChangeMusic (m parser)
648 (format #f "\\autochange ~a"
649 (music->lily-string (ly:music-property m 'element) parser)))
651 (define-display-method ContextChange (m parser)
652 (format #f "\\change ~a = \"~a\""
653 (ly:music-property m 'change-to-type)
654 (ly:music-property m 'change-to-id)))
658 (define-display-method TimeScaledMusic (times parser)
659 (let* ((num (ly:music-property times 'numerator))
660 (den (ly:music-property times 'denominator))
661 (nd-gcd (gcd num den)))
662 (parameterize ((*force-line-break* #f)
663 (*time-factor-numerator* (/ num nd-gcd))
664 (*time-factor-denominator* (/ den nd-gcd)))
665 (format #f "\\times ~a/~a ~a"
668 (music->lily-string (ly:music-property times 'element) parser)))))
670 (define-display-method RelativeOctaveMusic (m parser)
671 (music->lily-string (ly:music-property m 'element) parser))
673 (define-display-method TransposedMusic (m parser)
674 (music->lily-string (ly:music-property m 'element) parser))
680 (define (repeat->lily-string expr repeat-type parser)
681 (format #f "\\repeat ~a ~a ~a ~a"
683 (ly:music-property expr 'repeat-count)
684 (music->lily-string (ly:music-property expr 'element) parser)
685 (let ((alternatives (ly:music-property expr 'elements)))
686 (if (null? alternatives)
688 (format #f "\\alternative { ~{~a ~}}"
689 (map-in-order (lambda (music)
690 (music->lily-string music parser))
693 (define-display-method VoltaRepeatedMusic (expr parser)
694 (repeat->lily-string expr "volta" parser))
696 (define-display-method UnfoldedRepeatedMusic (expr parser)
697 (repeat->lily-string expr "unfold" parser))
699 (define-display-method PercentRepeatedMusic (expr parser)
700 (repeat->lily-string expr "percent" parser))
702 (define-display-method TremoloRepeatedMusic (expr parser)
703 (let* ((count (ly:music-property expr 'repeat-count))
704 (dots (if (= 0 (modulo count 3)) 0 1))
705 (shift (- (log2 (if (= 0 dots)
708 (element (ly:music-property expr 'element))
710 (if (eqv? (ly:music-property element 'name) 'SequentialMusic)
712 (set! shift (1- shift))
713 (set! den-mult (length (ly:music-property element 'elements)))))
714 (music-map (lambda (m)
715 (let ((duration (ly:music-property m 'duration)))
716 (if (ly:duration? duration)
717 (let* ((dlog (ly:duration-log duration))
718 (ddots (ly:duration-dot-count duration))
719 (dfactor (ly:duration-factor duration))
721 (dden (cdr dfactor)))
722 (set! (ly:music-property m 'duration)
723 (ly:make-duration (- dlog shift)
724 ddots ;;(- ddots dots) ; ????
726 (/ dden den-mult))))))
729 (format #f "\\repeat tremolo ~a ~a"
731 (music->lily-string element parser))))
737 (define-display-method ContextSpeccedMusic (expr parser)
738 (let ((id (ly:music-property expr 'context-id))
739 (create-new (ly:music-property expr 'create-new))
740 (music (ly:music-property expr 'element))
741 (operations (ly:music-property expr 'property-operations))
742 (ctype (ly:music-property expr 'context-type)))
743 (format #f "~a ~a~a~a ~a"
744 (if (and (not (null? create-new)) create-new)
750 (format #f " = ~s" id))
751 (if (null? operations)
753 (format #f " \\with {~{~a~}~%~v_}"
754 (parameterize ((*indent* (+ (*indent*) 2)))
756 (format #f "~%~v_\\~a ~s"
760 (reverse operations)))
762 (parameterize ((*current-context* ctype))
763 (music->lily-string music parser)))))
765 ;; special cases: \figures \lyrics \drums
766 (define-extra-display-method ContextSpeccedMusic (expr parser)
767 (with-music-match (expr (music 'ContextSpeccedMusic
769 property-operations ?op
770 context-type ?context-type
773 (parameterize ((*explicit-mode* #f))
776 (format #f "\\figures ~a" (music->lily-string ?sequence parser)))
778 (format #f "\\lyrics ~a" (music->lily-string ?sequence parser)))
780 (format #f "\\drums ~a" (music->lily-string ?sequence parser)))
785 ;;; Context properties
787 (define-extra-display-method ContextSpeccedMusic (expr parser)
788 (let ((element (ly:music-property expr 'element))
789 (property-tuning? (make-music-type-predicate 'PropertySet
793 (sequence? (make-music-type-predicate 'SequentialMusic)))
794 (if (and (ly:music? element)
795 (or (property-tuning? element)
796 (and (sequence? element)
797 (every property-tuning? (ly:music-property element 'elements)))))
798 (parameterize ((*current-context* (ly:music-property expr 'context-type)))
799 (music->lily-string element parser))
802 (define (property-value->lily-string arg parser)
803 (cond ((ly:music? arg)
804 (music->lily-string arg parser))
806 (format #f "#~s" arg))
808 (markup->lily-string arg))
810 (format #f "#~a" (scheme-expr->lily-string arg)))))
812 (define-display-method PropertySet (expr parser)
813 (let ((property (ly:music-property expr 'symbol))
814 (value (ly:music-property expr 'value))
815 (once (ly:music-property expr 'once)))
816 (format #f "~a\\set ~a~a = ~a~a"
817 (if (and (not (null? once)))
820 (if (eqv? (*current-context*) 'Bottom)
822 (format #f "~a . " (*current-context*)))
824 (property-value->lily-string value parser)
825 (new-line->lily-string))))
827 (define-display-method PropertyUnset (expr parser)
828 (format #f "\\unset ~a~a~a"
829 (if (eqv? (*current-context*) 'Bottom)
831 (format #f "~a . " (*current-context*)))
832 (ly:music-property expr 'symbol)
833 (new-line->lily-string)))
835 ;;; Layout properties
837 (define-display-method OverrideProperty (expr parser)
838 (let* ((symbol (ly:music-property expr 'symbol))
839 (property-path (ly:music-property expr 'grob-property-path))
840 (properties (if (pair? property-path)
842 (list (ly:music-property expr 'grob-property))))
843 (value (ly:music-property expr 'grob-value))
844 (once (ly:music-property expr 'once)))
846 (format #f "~a\\override ~a~a #'~a = ~a~a"
851 (if (eqv? (*current-context*) 'Bottom)
853 (format #f "~a . " (*current-context*)))
855 (if (null? (cdr properties))
858 (property-value->lily-string value parser)
859 (new-line->lily-string))))
861 (define-display-method RevertProperty (expr parser)
862 (let ((symbol (ly:music-property expr 'symbol))
863 (properties (ly:music-property expr 'grob-property-path)))
864 (format #f "\\revert ~a~a #'~a~a"
865 (if (eqv? (*current-context*) 'Bottom)
867 (format #f "~a . " (*current-context*)))
869 (if (null? (cdr properties))
872 (new-line->lily-string))))
874 ;;; \melisma and \melismaEnd
875 (define-extra-display-method ContextSpeccedMusic (expr parser)
876 "If expr is a melisma, return \"\\melisma\", otherwise, return #f."
877 (with-music-match (expr (music 'ContextSpeccedMusic
878 element (music 'PropertySet
880 symbol 'melismaBusy)))
883 (define-extra-display-method ContextSpeccedMusic (expr parser)
884 "If expr is a melisma end, return \"\\melismaEnd\", otherwise, return #f."
885 (with-music-match (expr (music 'ContextSpeccedMusic
886 element (music 'PropertyUnset
887 symbol 'melismaBusy)))
891 ;;; Check for all three different syntaxes of tempo:
892 ;;; \tempo string duration=note, \tempo duration=note and \tempo string
893 (define-extra-display-method ContextSpeccedMusic (expr parser)
894 "If expr is a tempo, return \"\\tempo x = nnn\", otherwise return #f."
895 (or (with-music-match (expr (music 'ContextSpeccedMusic
896 element (music 'SequentialMusic
897 elements ((music 'PropertySet
901 symbol 'tempoWholesPerMinute)
904 symbol 'tempoUnitDuration)
907 symbol 'tempoUnitCount)))))
908 (format #f "\\tempo ~a ~a = ~a"
909 (scheme-expr->lily-string ?unit-text)
910 (duration->lily-string ?unit-duration #:force-duration #t)
912 (with-music-match (expr (music 'ContextSpeccedMusic
913 element (music 'SequentialMusic
914 elements ((music 'PropertyUnset
917 symbol 'tempoWholesPerMinute)
920 symbol 'tempoUnitDuration)
923 symbol 'tempoUnitCount)))))
924 (format #f "\\tempo ~a = ~a"
925 (duration->lily-string ?unit-duration #:force-duration #t)
927 (with-music-match (expr (music 'ContextSpeccedMusic
928 element (music 'SequentialMusic
929 elements ((music 'PropertySet
931 symbol 'tempoText)))))
932 (format #f "\\tempo ~a" (scheme-expr->lily-string ?tempo-text)))))
935 (define clef-name-alist #f)
936 (define-public (memoize-clef-names clefs)
937 "Initialize `clef-name-alist', if not already set."
938 (if (not clef-name-alist)
939 (set! clef-name-alist
940 (map (lambda (name+vals)
941 (cons (cdr name+vals)
945 (define-extra-display-method ContextSpeccedMusic (expr parser)
946 "If `expr' is a clef change, return \"\\clef ...\"
947 Otherwise, return #f."
948 (with-music-match (expr (music 'ContextSpeccedMusic
950 element (music 'SequentialMusic
951 elements ((music 'PropertySet
955 symbol 'middleCClefPosition)
958 symbol 'clefPosition)
960 value ?clef-octavation
961 symbol 'clefOctavation)
963 procedure ly:set-middle-C!)))))
964 (let ((clef-name (assoc-get (list ?clef-glyph ?clef-position 0)
967 (format #f "\\clef \"~a~{~a~a~}\"~a"
969 (cond ((= 0 ?clef-octavation)
971 ((> ?clef-octavation 0)
972 (list "^" (1+ ?clef-octavation)))
974 (list "_" (- 1 ?clef-octavation))))
975 (new-line->lily-string))
979 (define-extra-display-method ContextSpeccedMusic (expr parser)
980 "If `expr' is a time signature set, return \"\\time ...\".
981 Otherwise, return #f. Note: default grouping is not available."
993 (with-music-match ((cadr ?elts)
997 (with-music-match ((caddr ?elts)
999 symbol 'measureLength))
1001 (with-music-match ((car ?elts)
1004 symbol 'timeSignatureFraction))
1005 (if (eq? (length ?elts) 3)
1008 (car ?num+den) (cdr ?num+den) (new-line->lily-string))
1010 #f "#(set-time-signature ~a ~a '(<grouping-specifier>))~a"
1011 (car ?num+den) (cdr ?num+den) (new-line->lily-string)))))))
1014 (define-extra-display-method ContextSpeccedMusic (expr parser)
1015 "If `expr' is a bar, return \"\\bar ...\".
1016 Otherwise, return #f."
1017 (with-music-match (expr (music 'ContextSpeccedMusic
1018 context-type 'Timing
1019 element (music 'PropertySet
1022 (format #f "\\bar \"~a\"~a" ?bar-type (new-line->lily-string))))
1025 (define (duration->moment ly-duration)
1026 (let ((log2 (ly:duration-log ly-duration))
1027 (dots (ly:duration-dot-count ly-duration))
1028 (num+den (ly:duration-factor ly-duration)))
1029 (let* ((m (expt 2 (- log2)))
1030 (factor (/ (car num+den) (cdr num+den))))
1031 (/ (do ((i 0 (1+ i))
1032 (delta (/ m 2) (/ delta 2)))
1034 (set! m (+ m delta)))
1037 (define moment-duration-alist (map (lambda (duration)
1038 (cons (duration->moment duration)
1040 (append-map (lambda (log2)
1042 (ly:make-duration log2 dots 1 1))
1046 (define (moment->duration moment)
1047 (assoc-get (- moment) moment-duration-alist))
1049 (define-extra-display-method ContextSpeccedMusic (expr parser)
1050 "If `expr' is a partial measure, return \"\\partial ...\".
1051 Otherwise, return #f."
1052 (with-music-match (expr (music
1053 'ContextSpeccedMusic
1055 'ContextSpeccedMusic
1056 context-type 'Timing
1060 symbol 'measurePosition))))
1061 (let ((duration (moment->duration (/ (ly:moment-main-numerator ?moment)
1062 (ly:moment-main-denominator ?moment)))))
1063 (and duration (format #f "\\partial ~a" (duration->lily-string duration
1064 #:force-duration #t))))))
1069 (define-display-method ApplyOutputEvent (applyoutput parser)
1070 (let ((proc (ly:music-property applyoutput 'procedure))
1071 (ctx (ly:music-property applyoutput 'context-type)))
1072 (format #f "\\applyOutput #'~a #~a"
1074 (or (procedure-name proc)
1075 (with-output-to-string
1077 (pretty-print (procedure-source proc))))))))
1079 (define-display-method ApplyContext (applycontext parser)
1080 (let ((proc (ly:music-property applycontext 'procedure)))
1081 (format #f "\\applyContext #~a"
1082 (or (procedure-name proc)
1083 (with-output-to-string
1085 (pretty-print (procedure-source proc))))))))
1088 (define-display-method PartCombineMusic (expr parser)
1089 (format #f "\\partcombine ~{~a ~}"
1090 (map-in-order (lambda (music)
1091 (music->lily-string music parser))
1092 (ly:music-property expr 'elements))))
1094 (define-extra-display-method PartCombineMusic (expr parser)
1095 (with-music-match (expr (music 'PartCombineMusic
1096 elements ((music 'UnrelativableMusic
1097 element (music 'ContextSpeccedMusic
1100 element ?sequence1))
1101 (music 'UnrelativableMusic
1102 element (music 'ContextSpeccedMusic
1105 element ?sequence2)))))
1106 (format #f "\\partcombine ~a~a~a"
1107 (music->lily-string ?sequence1 parser)
1108 (new-line->lily-string)
1109 (music->lily-string ?sequence2 parser))))
1111 (define-display-method UnrelativableMusic (expr parser)
1112 (music->lily-string (ly:music-property expr 'element) parser))
1115 (define-display-method QuoteMusic (expr parser)
1116 (or (with-music-match (expr (music
1118 quoted-voice-direction ?quoted-voice-direction
1119 quoted-music-name ?quoted-music-name
1120 quoted-context-id "cue"
1121 quoted-context-type 'Voice
1123 (format #f "\\cueDuring #~s #~a ~a"
1125 ?quoted-voice-direction
1126 (music->lily-string ?music parser)))
1127 (format #f "\\quoteDuring #~s ~a"
1128 (ly:music-property expr 'quoted-music-name)
1129 (music->lily-string (ly:music-property expr 'element) parser))))
1134 (define-display-method LineBreakEvent (expr parser)
1135 (if (null? (ly:music-property expr 'break-permission))
1139 (define-display-method PageBreakEvent (expr parser)
1140 (if (null? (ly:music-property expr 'break-permission))
1144 (define-display-method PageTurnEvent (expr parser)
1145 (if (null? (ly:music-property expr 'break-permission))
1149 (define-extra-display-method EventChord (expr parser)
1150 (with-music-match (expr (music 'EventChord
1151 elements ((music 'LineBreakEvent
1152 break-permission 'force)
1153 (music 'PageBreakEvent
1154 break-permission 'force))))
1157 (define-extra-display-method EventChord (expr parser)
1158 (with-music-match (expr (music 'EventChord
1159 elements ((music 'LineBreakEvent
1160 break-permission 'force)
1161 (music 'PageBreakEvent
1162 break-permission 'force)
1163 (music 'PageTurnEvent
1164 break-permission 'force))))
1172 (define-display-method LyricCombineMusic (expr parser)
1173 (format #f "\\lyricsto ~s ~a"
1174 (ly:music-property expr 'associated-context)
1175 (parameterize ((*explicit-mode* #f))
1176 (music->lily-string (ly:music-property expr 'element) parser))))
1179 (define-extra-display-method SimultaneousMusic (expr parser)
1180 (with-music-match (expr (music 'SimultaneousMusic
1181 elements ((music 'ContextSpeccedMusic
1184 element ?note-sequence)
1185 (music 'ContextSpeccedMusic
1186 context-type 'Lyrics
1188 element (music 'LyricCombineMusic
1189 associated-context ?associated-id
1190 element ?lyric-sequence)))))
1191 (if (string=? ?id ?associated-id)
1192 (format #f "~a~a \\addlyrics ~a"
1193 (music->lily-string ?note-sequence parser)
1194 (new-line->lily-string)
1195 (parameterize ((*explicit-mode* #f))
1196 (music->lily-string ?lyric-sequence parser)))