1 ;;; define-music-display-methods.scm -- data for displaying music
2 ;;; expressions using LilyPond notation.
4 ;;; Copyright (C) 2005--2011 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
42 (define (proc->command proc)
43 (let ((cmd-markup (symbol->string (procedure-name proc))))
44 (substring cmd-markup 0 (- (string-length cmd-markup)
45 (string-length "-markup")))))
46 (define (arg->string arg)
49 ((markup? arg) ;; a markup
50 (markup->lily-string-aux arg))
51 ((and (pair? arg) (every markup? arg)) ;; a markup list
52 (format #f "{~{ ~a~}}" (map-in-order markup->lily-string-aux arg)))
53 (else ;; a scheme argument
54 (format #f "#~a" (scheme-expr->lily-string arg)))))
55 (define (markup->lily-string-aux expr)
58 (let ((cmd (car expr))
60 (if (eqv? cmd simple-markup) ;; a simple markup
61 (format #f "~s" (car args))
62 (format #f "\\~a~{ ~a~}"
64 (map-in-order arg->string args))))))
65 (cond ((string? markup-expr)
66 (format #f "~s" markup-expr))
67 ((eqv? (car markup-expr) simple-markup)
68 (format #f "~s" (second markup-expr)))
70 (format #f "\\markup ~a"
71 (markup->lily-string-aux markup-expr)))))
77 ;; It is a pity that there is no rassoc in Scheme.
78 (define* (rassoc item alist #:optional (test equal?))
79 (do ((alist alist (cdr alist))
81 ((or result (null? alist)) result)
82 (if (and (car alist) (test item (cdar alist)))
83 (set! result (car alist)))))
85 (define-public (note-name->lily-string ly-pitch parser)
86 ;; here we define a custom pitch= function, since we do not want to
87 ;; test whether octaves are also equal. (otherwise, we would be using equal?)
88 (define (pitch= pitch1 pitch2)
89 (and (= (ly:pitch-notename pitch1) (ly:pitch-notename pitch2))
90 (= (ly:pitch-alteration pitch1) (ly:pitch-alteration pitch2))))
91 (let ((result (rassoc ly-pitch (ly:parser-lookup parser 'pitchnames) pitch=)))
96 (define-public (octave->lily-string pitch)
97 (let ((octave (ly:pitch-octave pitch)))
99 (make-string (1+ octave) #\'))
101 (make-string (1- (* -1 octave)) #\,))
107 (define*-public (duration->lily-string ly-duration #:key (prev-duration (*previous-duration*))
108 (force-duration (*force-duration*))
109 (time-factor-numerator (*time-factor-numerator*))
110 (time-factor-denominator (*time-factor-denominator*)))
111 (let ((log2 (ly:duration-log ly-duration))
112 (dots (ly:duration-dot-count ly-duration))
113 (num+den (ly:duration-factor ly-duration)))
114 (if (or force-duration (not prev-duration) (not (equal? ly-duration prev-duration)))
115 (string-append (case log2
119 (else (number->string (expt 2 log2))))
120 (make-string dots #\.)
121 (let ((num? (not (or (= 1 (car num+den))
122 (and time-factor-numerator
123 (= (car num+den) time-factor-numerator)))))
124 (den? (not (or (= 1 (cdr num+den))
125 (and time-factor-denominator
126 (= (cdr num+den) time-factor-denominator))))))
128 (format #f "*~a/~a" (car num+den) (cdr num+den)))
130 (format #f "*~a" (car num+den)))
139 (make-music-type-predicate
140 'AbsoluteDynamicEvent
154 'MultiMeasureTextEvent
170 (define* (event-direction->lily-string event #:optional (required #t))
171 (let ((direction (ly:music-property event 'direction)))
172 (cond ((or (not direction) (null? direction) (= CENTER direction))
173 (if required "-" ""))
174 ((= UP direction) "^")
175 ((= DOWN direction) "_")
178 (define-macro (define-post-event-display-method type vars direction-required str)
179 `(define-display-method ,type ,vars
181 (event-direction->lily-string ,(car vars) ,direction-required)
184 (define-macro (define-span-event-display-method type vars direction-required str-start str-stop)
185 `(define-display-method ,type ,vars
187 (event-direction->lily-string ,(car vars) ,direction-required)
188 (if (= START (ly:music-property ,(car vars) 'span-direction))
192 (define-display-method HyphenEvent (event parser)
194 (define-display-method ExtenderEvent (event parser)
196 (define-display-method TieEvent (event parser)
198 (define-display-method BeamForbidEvent (event parser)
200 (define-display-method StringNumberEvent (event parser)
201 (format #f "\\~a" (ly:music-property event 'string-number)))
204 (define-display-method TremoloEvent (event parser)
205 (let ((tremolo-type (ly:music-property event 'tremolo-type)))
206 (format #f ":~a" (if (= 0 tremolo-type)
210 (define-post-event-display-method ArticulationEvent (event parser) #t
211 (let ((articulation (ly:music-property event 'articulation-type)))
212 (case (string->symbol articulation)
216 ((staccatissimo) "|")
220 (else (format #f "\\~a" articulation)))))
222 (define-post-event-display-method FingeringEvent (event parser) #t
223 (ly:music-property event 'digit))
225 (define-post-event-display-method TextScriptEvent (event parser) #t
226 (markup->lily-string (ly:music-property event 'text)))
228 (define-post-event-display-method MultiMeasureTextEvent (event parser) #t
229 (markup->lily-string (ly:music-property event 'text)))
231 (define-post-event-display-method BendAfterEvent (event parser) #t
232 (format #f "\\bendAfter #~a" (ly:music-property event 'delta-step)))
234 (define-post-event-display-method HarmonicEvent (event parser) #f "\\harmonic")
235 (define-post-event-display-method GlissandoEvent (event parser) #t "\\glissando")
236 (define-post-event-display-method ArpeggioEvent (event parser) #t "\\arpeggio")
237 (define-post-event-display-method AbsoluteDynamicEvent (event parser) #f
238 (format #f "\\~a" (ly:music-property event 'text)))
240 (define-post-event-display-method StrokeFingerEvent (event parser) #t
241 (format #f "\\rightHandFinger #~a" (ly:music-property event 'digit)))
243 (define-span-event-display-method BeamEvent (event parser) #f "[" "]")
244 (define-span-event-display-method SlurEvent (event parser) #f "(" ")")
245 (define-span-event-display-method CrescendoEvent (event parser) #f "\\<" "\\!")
246 (define-span-event-display-method DecrescendoEvent (event parser) #f "\\>" "\\!")
247 (define-span-event-display-method EpisemaEvent (event parser) #f "\\episemInitium" "\\episemFinis")
248 (define-span-event-display-method PhrasingSlurEvent (event parser) #f "\\(" "\\)")
249 (define-span-event-display-method SustainEvent (event parser) #f "\\sustainOn" "\\sustainOff")
250 (define-span-event-display-method SostenutoEvent (event parser) #f "\\sostenutoOn" "\\sostenutoOff")
251 (define-span-event-display-method TextSpanEvent (event parser) #f "\\startTextSpan" "\\stopTextSpan")
252 (define-span-event-display-method TrillSpanEvent (event parser) #f "\\startTrillSpan" "\\stopTrillSpan")
253 (define-span-event-display-method StaffSpanEvent (event parser) #f "\\startStaff" "\\stopStaff")
254 (define-span-event-display-method NoteGroupingEvent (event parser) #f "\\startGroup" "\\stopGroup")
255 (define-span-event-display-method UnaCordaEvent (event parser) #f "\\unaCorda" "\\treCorde")
261 (define-display-method GraceMusic (expr parser)
262 (format #f "\\grace ~a"
263 (music->lily-string (ly:music-property expr 'element) parser)))
265 ;; \acciaccatura \appoggiatura \grace
266 ;; TODO: it would be better to compare ?start and ?stop
267 ;; with startAppoggiaturaMusic and stopAppoggiaturaMusic,
268 ;; using a custom music equality predicate.
269 (define-extra-display-method GraceMusic (expr parser)
270 "Display method for appoggiatura."
271 (with-music-match (expr (music
278 ;; we check whether ?start and ?stop look like
279 ;; startAppoggiaturaMusic stopAppoggiaturaMusic
280 (and (with-music-match (?start (music
286 duration (ly:make-duration 0 0 0 1))
289 span-direction START))))))
291 (with-music-match (?stop (music
297 duration (ly:make-duration 0 0 0 1))
300 span-direction STOP))))))
301 (format #f "\\appoggiatura ~a" (music->lily-string ?music parser))))))
304 (define-extra-display-method GraceMusic (expr parser)
305 "Display method for acciaccatura."
306 (with-music-match (expr (music
313 ;; we check whether ?start and ?stop look like
314 ;; startAcciaccaturaMusic stopAcciaccaturaMusic
315 (and (with-music-match (?start (music
321 duration (ly:make-duration 0 0 0 1))
324 span-direction START)))
329 grob-property-path '(stroke-style)
333 (with-music-match (?stop (music
339 grob-property-path '(stroke-style)
345 duration (ly:make-duration 0 0 0 1))
348 span-direction STOP))))))
349 (format #f "\\acciaccatura ~a" (music->lily-string ?music parser))))))
351 (define-extra-display-method GraceMusic (expr parser)
352 "Display method for grace."
353 (with-music-match (expr (music
360 ;; we check whether ?start and ?stop look like
361 ;; startGraceMusic stopGraceMusic
362 (and (null? (ly:music-property ?start 'elements))
363 (null? (ly:music-property ?stop 'elements))
364 (format #f "\\grace ~a" (music->lily-string ?music parser)))))
370 (define-display-method SequentialMusic (seq parser)
371 (let ((force-line-break (and (*force-line-break*)
373 (> (length (ly:music-property seq 'elements))
374 (*max-element-number-before-break*))))
375 (elements (ly:music-property seq 'elements))
376 (chord? (make-music-type-predicate 'EventChord))
377 (cluster? (make-music-type-predicate 'ClusterNoteEvent))
378 (note? (make-music-type-predicate 'NoteEvent)))
379 (format #f "~a~a{~v%~v_~{~a ~}~v%~v_}"
382 (any cluster? (ly:music-property e 'elements))))
386 (if (*explicit-mode*)
387 ;; if the sequence contains EventChord which contains figures ==> figuremode
388 ;; if the sequence contains EventChord which contains lyrics ==> lyricmode
389 ;; if the sequence contains EventChord which contains drum notes ==> drummode
390 (cond ((any (lambda (chord)
391 (any (make-music-type-predicate 'BassFigureEvent)
392 (ly:music-property chord 'elements)))
393 (filter chord? elements))
395 ((any (lambda (chord)
396 (any (make-music-type-predicate 'LyricEvent)
397 (ly:music-property chord 'elements)))
398 (filter chord? elements))
400 ((any (lambda (chord)
403 (not (null? (ly:music-property event 'drum-type)))))
404 (ly:music-property chord 'elements)))
405 (filter chord? elements))
407 (else ;; TODO: other modes?
410 (if force-line-break 1 0)
411 (if force-line-break (+ 2 (*indent*)) 1)
412 (parameterize ((*indent* (+ 2 (*indent*))))
413 (map-in-order (lambda (music)
414 (music->lily-string music parser))
416 (if force-line-break 1 0)
417 (if force-line-break (*indent*) 0))))
419 (define-display-method SimultaneousMusic (sim parser)
420 (parameterize ((*indent* (+ 3 (*indent*))))
421 (format #f "<< ~{~a ~}>>"
422 (map-in-order (lambda (music)
423 (music->lily-string music parser))
424 (ly:music-property sim 'elements)))))
426 (define-extra-display-method SimultaneousMusic (expr parser)
427 "If `sim' is an \afterGrace expression, return \"\\afterGrace ...\".
428 Otherwise, return #f."
429 ;; TODO: do something with afterGraceFraction?
430 (with-music-match (expr (music 'SimultaneousMusic
431 elements (?before-grace
432 (music 'SequentialMusic
433 elements ((music 'SkipMusic)
436 (format #f "\\afterGrace ~a ~a"
437 (music->lily-string ?before-grace parser)
438 (music->lily-string ?grace parser))))
444 (define-display-method EventChord (chord parser)
445 ;; event_chord : simple_element post_events
447 ;; | note_chord_element
449 ;; TODO : tagged post_events
450 ;; post_events : ( post_event | tagged_post_event )*
451 ;; tagged_post_event: '-' \tag embedded_scm post_event
453 (let* ((elements (ly:music-property chord 'elements))
454 (simple-elements (filter (make-music-type-predicate
455 'NoteEvent 'ClusterNoteEvent 'RestEvent
456 'MultiMeasureRestEvent 'SkipEvent 'LyricEvent)
458 (if ((make-music-type-predicate 'StaffSpanEvent 'BreathingEvent) (car elements))
459 ;; first, a special case: StaffSpanEvent (\startStaff, \stopStaff)
460 ;; and BreathingEvent (\breathe)
461 (music->lily-string (car elements) parser)
462 (if (and (not (null? simple-elements))
463 (null? (cdr simple-elements))
464 ;; special case: if this simple_element has any post_events in
465 ;; its 'articulations list, it should be interpreted instead
466 ;; as a note_chord_element to prevent spurious output, e.g.,
467 ;; \displayLilyMusic < c-1\4 >8 -> c-1\48
468 (null? (filter post-event?
469 (ly:music-property (car simple-elements) 'articulations))))
470 ;; simple_element : note | figure | rest | mmrest | lyric_element | skip
471 (let* ((simple-element (car simple-elements))
472 (duration (ly:music-property simple-element 'duration))
473 (lily-string (format #f "~a~a~a~{~a ~}"
474 (music->lily-string simple-element parser)
475 (duration->lily-string duration)
476 (if (and ((make-music-type-predicate 'RestEvent) simple-element)
477 (ly:pitch? (ly:music-property simple-element 'pitch)))
480 (map-in-order (lambda (music)
481 (music->lily-string music parser))
482 (filter post-event? elements)))))
483 (*previous-duration* duration)
485 (let ((chord-elements (filter (make-music-type-predicate
486 'NoteEvent 'ClusterNoteEvent 'BassFigureEvent)
488 (post-events (filter post-event? elements)))
489 (if (not (null? chord-elements))
490 ;; note_chord_element : '<' (notepitch | drumpitch)* '>" duration post_events
491 (let ((lily-string (format #f "< ~{~a ~}>~a~{~a ~}"
492 (map-in-order (lambda (music)
493 (music->lily-string music parser))
495 (duration->lily-string (ly:music-property (car chord-elements)
497 (map-in-order (lambda (music)
498 (music->lily-string music parser))
500 (*previous-duration* (ly:music-property (car chord-elements) 'duration))
503 (format #f "~{~a ~}" (map-in-order (lambda (music)
504 (music->lily-string music parser))
507 (define-display-method MultiMeasureRestMusic (mmrest parser)
508 (let* ((dur (ly:music-property mmrest 'duration))
509 (ly (format #f "R~a~{~a ~}"
510 (duration->lily-string dur)
511 (map-in-order (lambda (music)
512 (music->lily-string music parser))
513 (ly:music-property mmrest 'articulations)))))
514 (*previous-duration* dur)
517 (define-display-method SkipMusic (skip parser)
518 (format #f "\\skip ~a" (duration->lily-string (ly:music-property skip 'duration) #:force-duration #t)))
520 (define-display-method OttavaMusic (ottava parser)
521 (format #f "\\ottava #~a" (ly:music-property ottava 'ottava-number)))
524 ;;; Notes, rests, skips...
527 (define (simple-note->lily-string event parser)
528 (format #f "~a~a~a~a~{~a~}" ; pitchname octave !? octave-check articulations
529 (note-name->lily-string (ly:music-property event 'pitch) parser)
530 (octave->lily-string (ly:music-property event 'pitch))
531 (let ((forced (ly:music-property event 'force-accidental))
532 (cautionary (ly:music-property event 'cautionary)))
533 (cond ((and (not (null? forced))
535 (not (null? cautionary))
538 ((and (not (null? forced)) forced) "!")
540 (let ((octave-check (ly:music-property event 'absolute-octave)))
541 (if (not (null? octave-check))
542 (format #f "=~a" (cond ((>= octave-check 0)
543 (make-string (1+ octave-check) #\'))
545 (make-string (1- (* -1 octave-check)) #\,))
548 (map-in-order (lambda (event)
549 (music->lily-string event parser))
550 (ly:music-property event 'articulations))))
552 (define-display-method NoteEvent (note parser)
553 (cond ((not (null? (ly:music-property note 'pitch))) ;; note
554 (simple-note->lily-string note parser))
555 ((not (null? (ly:music-property note 'drum-type))) ;; drum
556 (format #f "~a" (ly:music-property note 'drum-type)))
560 (define-display-method ClusterNoteEvent (note parser)
561 (simple-note->lily-string note parser))
563 (define-display-method RestEvent (rest parser)
564 (if (not (null? (ly:music-property rest 'pitch)))
565 (simple-note->lily-string rest parser)
568 (define-display-method MultiMeasureRestEvent (rest parser)
571 (define-display-method SkipEvent (rest parser)
574 (define-display-method RepeatedChord (chord parser)
575 (music->lily-string (ly:music-property chord 'element) parser))
577 (define-display-method MarkEvent (mark parser)
578 (let ((label (ly:music-property mark 'label)))
581 (format #f "\\mark ~a" (markup->lily-string label)))))
583 (define-display-method KeyChangeEvent (key parser)
584 (let ((pitch-alist (ly:music-property key 'pitch-alist))
585 (tonic (ly:music-property key 'tonic)))
586 (if (or (null? pitch-alist)
589 (let ((c-pitch-alist (ly:transpose-key-alist pitch-alist
590 (ly:pitch-diff (ly:make-pitch 0 0 0) tonic))))
591 (format #f "\\key ~a \\~a~a"
592 (note-name->lily-string (ly:music-property key 'tonic) parser)
595 (equal? (ly:parser-lookup parser mode) c-pitch-alist))
596 (symbol->string mode)
598 '(major minor ionian locrian aeolian mixolydian lydian phrygian dorian))
599 (new-line->lily-string))))))
601 (define-display-method RelativeOctaveCheck (octave parser)
602 (let ((pitch (ly:music-property octave 'pitch)))
603 (format #f "\\octaveCheck ~a~a"
604 (note-name->lily-string pitch parser)
605 (octave->lily-string pitch))))
607 (define-display-method VoiceSeparator (sep parser)
610 (define-display-method LigatureEvent (ligature parser)
611 (if (= START (ly:music-property ligature 'span-direction))
615 (define-display-method BarCheck (check parser)
616 (format #f "|~a" (new-line->lily-string)))
618 (define-display-method PesOrFlexaEvent (expr parser)
621 (define-display-method BassFigureEvent (figure parser)
622 (let ((alteration (ly:music-property figure 'alteration))
623 (fig (ly:music-property figure 'figure))
624 (bracket-start (ly:music-property figure 'bracket-start))
625 (bracket-stop (ly:music-property figure 'bracket-stop)))
627 (format #f "~a~a~a~a"
628 (if (null? bracket-start) "" "[")
629 (cond ((null? fig) "_")
630 ((markup? fig) (second fig)) ;; fig: (<number-markup> "number")
632 (if (null? alteration)
635 ((= alteration DOUBLE-FLAT) "--")
636 ((= alteration FLAT) "-")
637 ((= alteration NATURAL) "!")
638 ((= alteration SHARP) "+")
639 ((= alteration DOUBLE-SHARP) "++")
641 (if (null? bracket-stop) "" "]"))))
643 (define-display-method LyricEvent (lyric parser)
644 (let ((text (ly:music-property lyric 'text)))
645 (if (or (string? text)
646 (eqv? (first text) simple-markup))
647 ;; a string or a simple markup
648 (let ((string (if (string? text)
651 (if (string-match "(\"| |[0-9])" string)
652 ;; TODO check exactly in which cases double quotes should be used
653 (format #f "~s" string)
655 (markup->lily-string text))))
657 (define-display-method BreathingEvent (event parser)
664 (define-display-method AutoChangeMusic (m parser)
665 (format #f "\\autochange ~a"
666 (music->lily-string (ly:music-property m 'element) parser)))
668 (define-display-method ContextChange (m parser)
669 (format #f "\\change ~a = \"~a\""
670 (ly:music-property m 'change-to-type)
671 (ly:music-property m 'change-to-id)))
675 (define-display-method TimeScaledMusic (times parser)
676 (let* ((num (ly:music-property times 'numerator))
677 (den (ly:music-property times 'denominator))
678 (nd-gcd (gcd num den)))
679 (parameterize ((*force-line-break* #f)
680 (*time-factor-numerator* (/ num nd-gcd))
681 (*time-factor-denominator* (/ den nd-gcd)))
682 (format #f "\\times ~a/~a ~a"
685 (music->lily-string (ly:music-property times 'element) parser)))))
687 (define-display-method RelativeOctaveMusic (m parser)
688 (music->lily-string (ly:music-property m 'element) parser))
690 (define-display-method TransposedMusic (m parser)
691 (music->lily-string (ly:music-property m 'element) parser))
697 (define (repeat->lily-string expr repeat-type parser)
698 (format #f "\\repeat ~a ~a ~a ~a"
700 (ly:music-property expr 'repeat-count)
701 (music->lily-string (ly:music-property expr 'element) parser)
702 (let ((alternatives (ly:music-property expr 'elements)))
703 (if (null? alternatives)
705 (format #f "\\alternative { ~{~a ~}}"
706 (map-in-order (lambda (music)
707 (music->lily-string music parser))
710 (define-display-method VoltaRepeatedMusic (expr parser)
711 (repeat->lily-string expr "volta" parser))
713 (define-display-method UnfoldedRepeatedMusic (expr parser)
714 (repeat->lily-string expr "unfold" parser))
716 (define-display-method PercentRepeatedMusic (expr parser)
717 (repeat->lily-string expr "percent" parser))
719 (define-display-method TremoloRepeatedMusic (expr parser)
720 (let* ((count (ly:music-property expr 'repeat-count))
721 (dots (if (= 0 (modulo count 3)) 0 1))
722 (shift (- (log2 (if (= 0 dots)
725 (element (ly:music-property expr 'element))
727 (if (eqv? (ly:music-property element 'name) 'SequentialMusic)
729 (set! shift (1- shift))
730 (set! den-mult (length (ly:music-property element 'elements)))))
731 (music-map (lambda (m)
732 (let ((duration (ly:music-property m 'duration)))
733 (if (ly:duration? duration)
734 (let* ((dlog (ly:duration-log duration))
735 (ddots (ly:duration-dot-count duration))
736 (dfactor (ly:duration-factor duration))
738 (dden (cdr dfactor)))
739 (set! (ly:music-property m 'duration)
740 (ly:make-duration (- dlog shift)
741 ddots ;;(- ddots dots) ; ????
743 (/ dden den-mult))))))
746 (format #f "\\repeat tremolo ~a ~a"
748 (music->lily-string element parser))))
754 (define-display-method ContextSpeccedMusic (expr parser)
755 (let ((id (ly:music-property expr 'context-id))
756 (create-new (ly:music-property expr 'create-new))
757 (music (ly:music-property expr 'element))
758 (operations (ly:music-property expr 'property-operations))
759 (ctype (ly:music-property expr 'context-type)))
760 (format #f "~a ~a~a~a ~a"
761 (if (and (not (null? create-new)) create-new)
767 (format #f " = ~s" id))
768 (if (null? operations)
770 (format #f " \\with {~{~a~}~%~v_}"
771 (parameterize ((*indent* (+ (*indent*) 2)))
773 (format #f "~%~v_\\~a ~s"
779 (parameterize ((*current-context* ctype))
780 (music->lily-string music parser)))))
782 ;; special cases: \figures \lyrics \drums
783 (define-extra-display-method ContextSpeccedMusic (expr parser)
784 (with-music-match (expr (music 'ContextSpeccedMusic
786 property-operations ?op
787 context-type ?context-type
790 (parameterize ((*explicit-mode* #f))
793 (format #f "\\figures ~a" (music->lily-string ?sequence parser)))
795 (format #f "\\lyrics ~a" (music->lily-string ?sequence parser)))
797 (format #f "\\drums ~a" (music->lily-string ?sequence parser)))
802 ;;; Context properties
804 (define-extra-display-method ContextSpeccedMusic (expr parser)
805 (let ((element (ly:music-property expr 'element))
806 (property-tuning? (make-music-type-predicate 'PropertySet
810 (sequence? (make-music-type-predicate 'SequentialMusic)))
811 (if (and (ly:music? element)
812 (or (property-tuning? element)
813 (and (sequence? element)
814 (every property-tuning? (ly:music-property element 'elements)))))
815 (parameterize ((*current-context* (ly:music-property expr 'context-type)))
816 (music->lily-string element parser))
819 (define (property-value->lily-string arg parser)
820 (cond ((ly:music? arg)
821 (music->lily-string arg parser))
823 (format #f "#~s" arg))
825 (markup->lily-string arg))
827 (format #f "#~a" (scheme-expr->lily-string arg)))))
829 (define-display-method PropertySet (expr parser)
830 (let ((property (ly:music-property expr 'symbol))
831 (value (ly:music-property expr 'value))
832 (once (ly:music-property expr 'once)))
833 (format #f "~a\\set ~a~a = ~a~a"
834 (if (and (not (null? once)))
837 (if (eqv? (*current-context*) 'Bottom)
839 (format #f "~a . " (*current-context*)))
841 (property-value->lily-string value parser)
842 (new-line->lily-string))))
844 (define-display-method PropertyUnset (expr parser)
845 (format #f "\\unset ~a~a~a"
846 (if (eqv? (*current-context*) 'Bottom)
848 (format #f "~a . " (*current-context*)))
849 (ly:music-property expr 'symbol)
850 (new-line->lily-string)))
852 ;;; Layout properties
854 (define-display-method OverrideProperty (expr parser)
855 (let* ((symbol (ly:music-property expr 'symbol))
856 (property-path (ly:music-property expr 'grob-property-path))
857 (properties (if (pair? property-path)
859 (list (ly:music-property expr 'grob-property))))
860 (value (ly:music-property expr 'grob-value))
861 (once (ly:music-property expr 'once)))
863 (format #f "~a\\override ~a~a #'~a = ~a~a"
868 (if (eqv? (*current-context*) 'Bottom)
870 (format #f "~a . " (*current-context*)))
872 (if (null? (cdr properties))
875 (property-value->lily-string value parser)
876 (new-line->lily-string))))
878 (define-display-method RevertProperty (expr parser)
879 (let ((symbol (ly:music-property expr 'symbol))
880 (properties (ly:music-property expr 'grob-property-path)))
881 (format #f "\\revert ~a~a #'~a~a"
882 (if (eqv? (*current-context*) 'Bottom)
884 (format #f "~a . " (*current-context*)))
886 (if (null? (cdr properties))
889 (new-line->lily-string))))
891 (define-display-method TimeSignatureMusic (expr parser)
892 (let* ((num (ly:music-property expr 'numerator))
893 (den (ly:music-property expr 'denominator))
894 (structure (ly:music-property expr 'beat-structure)))
895 (if (null? structure)
899 (new-line->lily-string))
901 "#(set-time-signature ~a ~a '~a)~a"
903 (new-line->lily-string)))))
905 ;;; \melisma and \melismaEnd
906 (define-extra-display-method ContextSpeccedMusic (expr parser)
907 "If expr is a melisma, return \"\\melisma\", otherwise, return #f."
908 (with-music-match (expr (music 'ContextSpeccedMusic
909 element (music 'PropertySet
911 symbol 'melismaBusy)))
914 (define-extra-display-method ContextSpeccedMusic (expr parser)
915 "If expr is a melisma end, return \"\\melismaEnd\", otherwise, return #f."
916 (with-music-match (expr (music 'ContextSpeccedMusic
917 element (music 'PropertyUnset
918 symbol 'melismaBusy)))
922 (define-extra-display-method SequentialMusic (expr parser)
923 (with-music-match (expr (music 'SequentialMusic
924 elements ((music 'TempoChangeEvent
927 metronome-count ?count)
928 (music 'ContextSpeccedMusic
929 element (music 'PropertySet
930 symbol 'tempoWholesPerMinute)))))
931 (format #f "\\tempo ~{~a~a~}~a = ~a~a"
933 (list (markup->lily-string ?text) " ")
935 (duration->lily-string ?unit #:force-duration #t)
937 (format #f "~a ~~ ~a" (car ?count) (cdr ?count))
939 (new-line->lily-string))))
941 (define-display-method TempoChangeEvent (expr parser)
942 (let ((text (ly:music-property expr 'text)))
943 (format #f "\\tempo ~a~a"
944 (markup->lily-string text)
945 (new-line->lily-string))))
948 (define clef-name-alist #f)
949 (define-public (memoize-clef-names clefs)
950 "Initialize @code{clef-name-alist}, if not already set."
951 (if (not clef-name-alist)
952 (set! clef-name-alist
953 (map (lambda (name+vals)
954 (cons (cdr name+vals)
958 (define-extra-display-method ContextSpeccedMusic (expr parser)
959 "If @var{expr} is a clef change, return \"\\clef ...\".
960 Otherwise, return @code{#f}."
961 (with-music-match (expr (music 'ContextSpeccedMusic
963 element (music 'SequentialMusic
964 elements ((music 'PropertySet
968 symbol 'middleCClefPosition)
971 symbol 'clefPosition)
973 value ?clef-octavation
974 symbol 'clefOctavation)
976 procedure ly:set-middle-C!)))))
977 (let ((clef-name (assoc-get (list ?clef-glyph ?clef-position 0)
980 (format #f "\\clef \"~a~{~a~a~}\"~a"
982 (cond ((= 0 ?clef-octavation)
984 ((> ?clef-octavation 0)
985 (list "^" (1+ ?clef-octavation)))
987 (list "_" (- 1 ?clef-octavation))))
988 (new-line->lily-string))
992 (define-extra-display-method ContextSpeccedMusic (expr parser)
993 "If `expr' is a bar, return \"\\bar ...\".
994 Otherwise, return #f."
995 (with-music-match (expr (music 'ContextSpeccedMusic
997 element (music 'PropertySet
1000 (format #f "\\bar \"~a\"~a" ?bar-type (new-line->lily-string))))
1003 (define-extra-display-method ContextSpeccedMusic (expr parser)
1004 "If `expr' is a partial measure, return \"\\partial ...\".
1005 Otherwise, return #f."
1006 (with-music-match (expr (music
1007 'ContextSpeccedMusic
1009 'ContextSpeccedMusic
1010 context-type 'Timing
1013 partial-duration ?duration))))
1016 (format #f "\\partial ~a"
1017 (duration->lily-string ?duration #:force-duration #t)))))
1022 (define-display-method ApplyOutputEvent (applyoutput parser)
1023 (let ((proc (ly:music-property applyoutput 'procedure))
1024 (ctx (ly:music-property applyoutput 'context-type)))
1025 (format #f "\\applyOutput #'~a #~a"
1027 (or (procedure-name proc)
1028 (with-output-to-string
1030 (pretty-print (procedure-source proc))))))))
1032 (define-display-method ApplyContext (applycontext parser)
1033 (let ((proc (ly:music-property applycontext 'procedure)))
1034 (format #f "\\applyContext #~a"
1035 (or (procedure-name proc)
1036 (with-output-to-string
1038 (pretty-print (procedure-source proc))))))))
1041 (define-display-method PartCombineMusic (expr parser)
1042 (format #f "\\partcombine ~{~a ~}"
1043 (map-in-order (lambda (music)
1044 (music->lily-string music parser))
1045 (ly:music-property expr 'elements))))
1047 (define-extra-display-method PartCombineMusic (expr parser)
1048 (with-music-match (expr (music 'PartCombineMusic
1049 elements ((music 'UnrelativableMusic
1050 element (music 'ContextSpeccedMusic
1053 element ?sequence1))
1054 (music 'UnrelativableMusic
1055 element (music 'ContextSpeccedMusic
1058 element ?sequence2)))))
1059 (format #f "\\partcombine ~a~a~a"
1060 (music->lily-string ?sequence1 parser)
1061 (new-line->lily-string)
1062 (music->lily-string ?sequence2 parser))))
1064 (define-display-method UnrelativableMusic (expr parser)
1065 (music->lily-string (ly:music-property expr 'element) parser))
1068 (define-display-method QuoteMusic (expr parser)
1069 (or (with-music-match (expr (music
1071 quoted-voice-direction ?quoted-voice-direction
1072 quoted-music-name ?quoted-music-name
1073 quoted-context-id "cue"
1074 quoted-context-type 'Voice
1076 (format #f "\\cueDuring #~s #~a ~a"
1078 ?quoted-voice-direction
1079 (music->lily-string ?music parser)))
1080 (format #f "\\quoteDuring #~s ~a"
1081 (ly:music-property expr 'quoted-music-name)
1082 (music->lily-string (ly:music-property expr 'element) parser))))
1087 (define-display-method LineBreakEvent (expr parser)
1088 (if (null? (ly:music-property expr 'break-permission))
1092 (define-display-method PageBreakEvent (expr parser)
1093 (if (null? (ly:music-property expr 'break-permission))
1097 (define-display-method PageTurnEvent (expr parser)
1098 (if (null? (ly:music-property expr 'break-permission))
1102 (define-extra-display-method EventChord (expr parser)
1103 (with-music-match (expr (music 'EventChord
1104 elements ((music 'LineBreakEvent
1105 break-permission 'force)
1106 (music 'PageBreakEvent
1107 break-permission 'force))))
1110 (define-extra-display-method EventChord (expr parser)
1111 (with-music-match (expr (music 'EventChord
1112 elements ((music 'LineBreakEvent
1113 break-permission 'force)
1114 (music 'PageBreakEvent
1115 break-permission 'force)
1116 (music 'PageTurnEvent
1117 break-permission 'force))))
1125 (define-display-method LyricCombineMusic (expr parser)
1126 (format #f "\\lyricsto ~s ~a"
1127 (ly:music-property expr 'associated-context)
1128 (parameterize ((*explicit-mode* #f))
1129 (music->lily-string (ly:music-property expr 'element) parser))))
1132 (define-extra-display-method SimultaneousMusic (expr parser)
1133 (with-music-match (expr (music 'SimultaneousMusic
1134 elements ((music 'ContextSpeccedMusic
1137 element ?note-sequence)
1138 (music 'ContextSpeccedMusic
1139 context-type 'Lyrics
1141 element (music 'LyricCombineMusic
1142 associated-context ?associated-id
1143 element ?lyric-sequence)))))
1144 (if (string=? ?id ?associated-id)
1145 (format #f "~a~a \\addlyrics ~a"
1146 (music->lily-string ?note-sequence parser)
1147 (new-line->lily-string)
1148 (parameterize ((*explicit-mode* #f))
1149 (music->lily-string ?lyric-sequence parser)))
1152 ;; Silence internal event sent at end of each lyrics block
1153 (define-display-method CompletizeExtenderEvent (expr parser)