1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 ;; todo: figure out how to make module,
19 ;; without breaking nested ly scopes
21 (define-class <Voice-state> ()
22 (event-list #:init-value '() #:accessor events #:init-keyword #:events)
23 (when-moment #:accessor moment #:init-keyword #:moment)
24 (tuning #:accessor tuning #:init-keyword #:tuning)
25 (split-index #:accessor split-index)
29 ;; spanner-state is an alist
30 ;; of (SYMBOL . RESULT-INDEX), which indicates where
31 ;; said spanner was started.
32 (spanner-state #:init-value '() #:accessor span-state))
34 (define-method (write (x <Voice-state> ) file)
35 (display (moment x) file)
36 (display " evs = " file)
37 (display (events x) file)
38 (display " active = " file)
39 (display (span-state x) file)
42 (define-method (note-events (vs <Voice-state>))
44 (ly:in-event-class? x 'note-event))
45 (filter f? (events vs)))
47 (define-method (rest-and-skip-events (vs <Voice-state>))
49 (or (ly:in-event-class? x 'rest-event)
50 (ly:in-event-class? x 'skip-event)))
51 (filter f? (events vs)))
53 (define-method (any-mmrest-events (vs <Voice-state>))
55 (ly:in-event-class? x 'multi-measure-rest-event))
58 (define-method (previous-voice-state (vs <Voice-state>))
59 (let ((i (slot-ref vs 'vector-index))
60 (v (slot-ref vs 'state-vector)))
65 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67 (define-class <Split-state> ()
68 ;; The automatically determined split configuration
69 (configuration #:init-value '() #:accessor configuration)
70 ;; Allow overriding split configuration, takes precedence over configuration
71 (forced-configuration #:init-value #f #:accessor forced-configuration)
72 (when-moment #:accessor moment #:init-keyword #:moment)
73 ;; voice-states are states starting with the Split-state or later
75 (is #:init-keyword #:voice-states #:accessor voice-states)
76 (synced #:init-keyword #:synced #:init-value #f #:getter synced?))
79 (define-method (write (x <Split-state> ) f)
80 (display (moment x) f)
82 (display (configuration x) f)
87 (define-method (current-or-previous-voice-states (ss <Split-state>))
88 "Return voice states meeting the following conditions. For a voice
89 in sync, return the current voice state. For a voice out of sync,
90 return the previous voice state."
91 (let* ((vss (voice-states ss))
94 (if (and vs1 (not (equal? (moment vs1) (moment ss))))
95 (set! vs1 (previous-voice-state vs1)))
96 (if (and vs2 (not (equal? (moment vs2) (moment ss))))
97 (set! vs2 (previous-voice-state vs2)))
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 (define (previous-span-state vs)
104 (let ((p (previous-voice-state vs)))
105 (if p (span-state p) '())))
107 (define (make-voice-states evl)
108 (let ((vec (list->vector (map (lambda (v)
112 #:events (map car (cdr v))))
115 ((= i (vector-length vec)) vec)
116 (slot-set! (vector-ref vec i) 'vector-index i)
117 (slot-set! (vector-ref vec i) 'state-vector vec))))
119 (define (make-split-state vs1 vs2)
120 "Merge lists VS1 and VS2, containing Voice-state objects into vector
121 of Split-state objects, crosslinking the Split-state vector and
124 (define (helper ss-idx ss-list idx1 idx2)
125 (let* ((state1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f))
126 (state2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f))
127 (min (cond ((and state1 state2) (moment-min (moment state1) (moment state2)))
128 (state1 (moment state1))
129 (state2 (moment state2))
131 (inc1 (if (and state1 (equal? min (moment state1))) 1 0))
132 (inc2 (if (and state2 (equal? min (moment state2))) 1 0))
136 #:voice-states (cons state1 state2)
137 #:synced (= inc1 inc2))
140 (set! (split-index state1) ss-idx))
142 (set! (split-index state2) ss-idx))
145 (cons ss-object ss-list)
149 (list->vector (reverse! (helper 0 '() 0 0) '())))
151 (define (analyse-spanner-states voice-state-vec)
153 (define (helper index active)
154 "Analyse EVS at INDEX, given state ACTIVE."
156 (define (analyse-tie-start active ev)
157 (if (ly:in-event-class? ev 'tie-event)
158 (acons 'tie (split-index (vector-ref voice-state-vec index))
162 (define (analyse-tie-end active ev)
163 (if (ly:in-event-class? ev 'note-event)
164 (assoc-remove! active 'tie)
167 (define (analyse-absdyn-end active ev)
168 (if (or (ly:in-event-class? ev 'absolute-dynamic-event)
169 (and (ly:in-event-class? ev 'span-dynamic-event)
170 (equal? STOP (ly:event-property ev 'span-direction))))
171 (assoc-remove! (assoc-remove! active 'cresc) 'decr)
174 (define (active<? a b)
175 (cond ((symbol<? (car a) (car b)) #t)
176 ((symbol<? (car b) (car a)) #f)
177 (else (< (cdr a) (cdr b)))))
179 (define (analyse-span-event active ev)
180 (let* ((name (car (ly:event-property ev 'class)))
181 (key (cond ((equal? name 'slur-event) 'slur)
182 ((equal? name 'phrasing-slur-event) 'tie)
183 ((equal? name 'beam-event) 'beam)
184 ((equal? name 'crescendo-event) 'cresc)
185 ((equal? name 'decrescendo-event) 'decr)
187 (sp (ly:event-property ev 'span-direction)))
188 (if (and (symbol? key) (ly:dir? sp))
190 (assoc-remove! active key)
192 (split-index (vector-ref voice-state-vec index))
196 (define (analyse-events active evs)
197 "Run all analyzers on ACTIVE and EVS"
198 (define (run-analyzer analyzer active evs)
200 (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
202 (define (run-analyzers analyzers active evs)
203 (if (pair? analyzers)
204 (run-analyzers (cdr analyzers)
205 (run-analyzer (car analyzers) active evs)
208 (sort ;; todo: use fold or somesuch.
209 (run-analyzers (list analyse-absdyn-end analyse-span-event
210 ;; note: tie-start/span comes after tie-end/absdyn.
211 analyse-tie-end analyse-tie-start)
215 ;; must copy, since we use assoc-remove!
216 (if (< index (vector-length voice-state-vec))
218 (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
219 (set! (span-state (vector-ref voice-state-vec index))
221 (helper (1+ index) active))))
225 (define recording-group-functions
226 ;;Selected parts from @var{toplevel-music-functions} not requiring @code{parser}.
228 (lambda (music) (expand-repeat-chords! '(rhythmic-event) music))
229 expand-repeat-notes!))
232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 (define-public (recording-group-emulate music odef)
234 "Interpret @var{music} according to @var{odef}, but store all events
235 in a chronological list, similar to the @code{Recording_group_engraver} in
236 LilyPond version 2.8 and earlier."
239 (now-mom (ly:make-moment 0 0))
240 (global (ly:make-global-context odef))
241 (mom-listener (ly:make-listener
242 (lambda (tev) (set! now-mom (ly:event-property tev 'moment)))))
243 (new-context-listener
247 ((child (ly:event-property sev 'context))
248 (this-moment-list (cons (ly:context-id child) '()))
249 (dummy (set! context-list (cons this-moment-list context-list)))
251 (accumulate-event-listener
252 (ly:make-listener (lambda (ev)
253 (set! acc (cons (cons ev #t) acc)))))
255 (ly:make-listener (lambda (tev)
259 (ly:context-property child 'instrumentTransposition))
260 ;; The accumulate-event-listener above creates
261 ;; the list of events in reverse order, so we
262 ;; have to revert it to the original order again
264 (set-cdr! this-moment-list
265 (cons this-moment (cdr this-moment-list)))
267 (ly:add-listener accumulate-event-listener
268 (ly:context-event-source child) 'StreamEvent)
269 (ly:add-listener save-acc-listener
270 (ly:context-event-source global) 'OneTimeStep))))))
271 (ly:add-listener new-context-listener
272 (ly:context-events-below global) 'AnnounceNewContext)
273 (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare)
274 (ly:interpret-music-expression
275 (make-non-relative-music
276 (fold (lambda (x m) (x m)) music recording-group-functions))
280 (define-public (make-part-combine-music parser music-list direction chord-range)
281 (let* ((m (make-music 'PartCombineMusic))
282 (m1 (make-non-relative-music (context-spec-music (first music-list) 'Voice "one")))
283 (m2 (make-non-relative-music (context-spec-music (second music-list) 'Voice "two")))
284 (listener (ly:parser-lookup parser 'partCombineListener))
285 (evs2 (recording-group-emulate m2 listener))
286 (evs1 (recording-group-emulate m1 listener)))
288 (set! (ly:music-property m 'elements) (list m1 m2))
289 (set! (ly:music-property m 'direction) direction)
290 (set! (ly:music-property m 'split-list)
291 (if (and (assoc "one" evs1) (assoc "two" evs2))
292 (determine-split-list (reverse! (assoc-get "one" evs1) '())
293 (reverse! (assoc-get "two" evs2) '())
298 (define-public (determine-split-list evl1 evl2 chord-range)
299 "@var{evl1} and @var{evl2} should be ascending. @var{chord-range} is a pair of numbers (min . max) defining the distance in steps between notes that may be combined into a chord or unison."
301 (voice-state-vec1 (make-voice-states evl1))
302 (voice-state-vec2 (make-voice-states evl2))
303 (result (make-split-state voice-state-vec1 voice-state-vec2))
304 (chord-min-diff (car chord-range))
305 (chord-max-diff (cdr chord-range)))
307 ;; Go through all moments recursively and check if the events of that
308 ;; moment contain a part-combine-force-event override. If so, store its
309 ;; value in the forced-configuration field, which will override. The
310 ;; previous configuration is used to determine non-terminated settings.
311 (define (analyse-forced-combine result-idx prev-res)
313 (define (get-forced-event x)
314 (and (ly:in-event-class? x 'part-combine-force-event)
315 (cons (ly:event-property x 'forced-type)
316 (ly:event-property x 'once))))
317 (define (part-combine-events vs)
320 (filter-map get-forced-event (events vs))))
321 ;; end part-combine-events
323 ;; forced-result: Take the previous config and analyse whether
324 ;; any change happened.... Return new once and permanent config
325 (define (forced-result evt state)
326 ;; sanity check, evt should always be (new-state . once)
327 (if (not (and (pair? evt) (pair? state)))
330 ;; Once-event, leave permanent state unchanged
331 (cons (car evt) (cdr state))
332 ;; permanent change, leave once state unchanged
333 (cons (car state) (car evt)))))
334 ;; end forced-combine-result
336 ;; body of analyse-forced-combine:
337 (if (< result-idx (vector-length result))
338 (let* ((now-state (vector-ref result result-idx)) ; current result
339 ;; Extract all part-combine force events
340 (evts (if (synced? now-state)
342 (part-combine-events (car (voice-states now-state)))
343 (part-combine-events (cdr (voice-states now-state))))
345 ;; result is (once-state permament-state):
346 (state (fold forced-result (cons 'automatic prev-res) evts))
347 ;; Now let once override permanent changes:
348 (force-state (if (equal? (car state) 'automatic)
351 (set! (forced-configuration (vector-ref result result-idx))
353 ;; For the next moment, ignore the once override (car stat)
354 ;; and pass on the permanent override, stored as (cdr state)
355 (analyse-forced-combine (1+ result-idx) (cdr state)))))
356 ;; end analyse-forced-combine
359 (define (analyse-time-step result-idx)
360 (define (put x . index)
361 "Put the result to X, starting from INDEX backwards.
363 Only set if not set previously.
365 (let ((i (if (pair? index) (car index) result-idx)))
367 (not (symbol? (configuration (vector-ref result i)))))
369 (set! (configuration (vector-ref result i)) x)
372 (define (copy-state-from state-vec vs)
373 (define (copy-one-state key-idx)
374 (let* ((idx (cdr key-idx))
375 (prev-ss (vector-ref result idx))
376 (prev (configuration prev-ss)))
379 (for-each copy-one-state (span-state vs)))
381 (define (analyse-notes now-state)
382 (let* ((vs1 (car (voice-states now-state)))
383 (vs2 (cdr (voice-states now-state)))
384 (notes1 (note-events vs1))
385 (durs1 (sort (map (lambda (x) (ly:event-property x 'duration))
388 (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
391 (notes2 (note-events vs2))
392 (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
395 (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
398 (cond ((> (length notes1) 1) (put 'apart))
399 ((> (length notes2) 1) (put 'apart))
400 ((= 1 (+ (length notes2) (length notes1))) (put 'apart))
401 ((and (= (length durs1) 1)
403 (not (equal? (car durs1) (car durs2))))
406 (if (and (= (length pitches1) (length pitches2)))
407 (if (and (pair? pitches1)
409 ; Is the interval outside of chord-range?
410 (let ((diff (ly:pitch-steps
411 (ly:pitch-diff (car pitches1)
413 (or (< diff chord-min-diff)
414 (> diff chord-max-diff)
417 ;; copy previous split state from spanner state
419 (if (previous-voice-state vs1)
420 (copy-state-from voice-state-vec1
421 (previous-voice-state vs1)))
422 (if (previous-voice-state vs2)
423 (copy-state-from voice-state-vec2
424 (previous-voice-state vs2)))
425 (if (and (null? (span-state vs1)) (null? (span-state vs2)))
426 (put 'chords)))))))))
428 (if (< result-idx (vector-length result))
429 (let* ((now-state (vector-ref result result-idx))
430 (vs1 (car (voice-states now-state)))
431 (vs2 (cdr (voice-states now-state))))
433 (cond ((not vs1) (put 'apart))
434 ((not vs2) (put 'apart))
436 (let ((active1 (previous-span-state vs1))
437 (active2 (previous-span-state vs2))
438 (new-active1 (span-state vs1))
439 (new-active2 (span-state vs2)))
441 (display (list (moment now-state) result-idx
442 active1 "->" new-active1
443 active2 "->" new-active2
445 (if (and (synced? now-state)
446 (equal? active1 active2)
447 (equal? new-active1 new-active2))
448 (analyse-notes now-state)
450 ;; active states different:
453 ;; go to the next one, if it exists.
454 (analyse-time-step (1+ result-idx)))))))
456 (define (analyse-a2 result-idx)
457 (if (< result-idx (vector-length result))
458 (let* ((now-state (vector-ref result result-idx))
459 (vs1 (car (voice-states now-state)))
460 (vs2 (cdr (voice-states now-state))))
462 (define (analyse-synced-silence)
463 (let ((rests1 (if vs1 (rest-and-skip-events vs1) '()))
464 (rests2 (if vs2 (rest-and-skip-events vs2) '())))
467 ;; multi-measure rests (probably), which the
468 ;; part-combine iterator handles well
469 ((and (= 0 (length rests1))
470 (= 0 (length rests2)))
471 (set! (configuration now-state) 'unisilence))
473 ;; equal rests or equal skips, but not one of each
474 ((and (= 1 (length rests1))
475 (= 1 (length rests2))
476 (equal? (ly:event-property (car rests1) 'class)
477 (ly:event-property (car rests2) 'class))
478 (equal? (ly:event-property (car rests1) 'duration)
479 (ly:event-property (car rests2) 'duration)))
480 (set! (configuration now-state) 'unisilence))
482 ;; rests of different durations or mixed with
483 ;; skips or multi-measure rests
485 ;; TODO For skips, route the rest to the shared
486 ;; voice and the skip to the voice for its part?
487 (set! (configuration now-state) 'apart-silence))
491 (define (analyse-unsynced-silence vs1 vs2)
492 (let ((any-mmrests1 (if vs1 (any-mmrest-events vs1) #f))
493 (any-mmrests2 (if vs2 (any-mmrest-events vs2) #f)))
495 ;; If a multi-measure rest begins now while the other
496 ;; part has an ongoing multi-measure rest (or has
497 ;; ended), start displaying the one that begins now.
499 (equal? (moment vs1) (moment now-state))
500 (or (not vs2) any-mmrests2))
501 (set! (configuration now-state) 'silence1))
503 ;; as above with parts swapped
505 (equal? (moment vs2) (moment now-state))
506 (or (not vs1) any-mmrests1))
507 (set! (configuration now-state) 'silence2))
511 (let ((notes1 (if vs1 (note-events vs1) '()))
512 (notes2 (if vs2 (note-events vs2) '())))
513 ; Todo: What about a2 chords, e.g. string multi-stops?
514 ; Sort and compare notes1 and notes2?
515 (cond ((and (equal? (configuration now-state) 'chords)
516 (= 1 (length notes1))
517 (= 1 (length notes2))
518 (equal? (ly:event-property (car notes1) 'pitch)
519 (ly:event-property (car notes2) 'pitch)))
520 (set! (configuration now-state) 'unisono))
523 (if (and (= 0 (length notes1))
524 (= 0 (length notes2)))
525 (analyse-synced-silence)))
527 (else ;; not synchronized
529 (current-or-previous-voice-states now-state))
533 (or (not vs1) (= 0 (length (note-events vs1))))
534 (or (not vs2) (= 0 (length (note-events vs2)))))
535 (analyse-unsynced-silence vs1 vs2))))
537 (analyse-a2 (1+ result-idx)))))
539 (define (analyse-solo12 result-idx)
541 (define (previous-config vs)
542 (let* ((pvs (previous-voice-state vs))
543 (spi (if pvs (split-index pvs) #f))
544 (prev-split (if spi (vector-ref result spi) #f)))
546 (configuration prev-split)
549 (define (put-range x a b)
550 ;; (display (list "put range " x a b "\n"))
553 (set! (configuration (vector-ref result i)) x)))
556 ;; (display (list "putting " x "\n"))
557 (set! (configuration (vector-ref result result-idx)) x))
559 (define (current-voice-state now-state voice-num)
560 (define vs ((if (= 1 voice-num) car cdr)
561 (voice-states now-state)))
562 (if (or (not vs) (equal? (moment now-state) (moment vs)))
564 (previous-voice-state vs)))
566 (define (try-solo type start-idx current-idx)
567 "Find a maximum stretch that can be marked as solo. Only set
568 the mark when there are no spanners active.
570 return next idx to analyse.
572 (if (< current-idx (vector-length result))
573 (let* ((now-state (vector-ref result current-idx))
574 (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
575 (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
576 (silent-notes (if silent-state (note-events silent-state) '()))
577 (solo-notes (if solo-state (note-events solo-state) '())))
578 ;; (display (list "trying " type " at " (moment now-state) solo-state silent-state "\n"))
579 (cond ((not (equal? (configuration now-state) 'apart))
581 ((> (length silent-notes) 0) start-idx)
583 (put-range type start-idx current-idx)
586 (null? (span-state solo-state)))
589 ;; This includes rests. This isn't a problem: long rests
590 ;; will be shared with the silent voice, and be marked
591 ;; as unisilence. Therefore, long rests won't
592 ;; accidentally be part of a solo.
594 (put-range type start-idx current-idx)
595 (try-solo type (1+ current-idx) (1+ current-idx)))
597 (try-solo type start-idx (1+ current-idx)))))
601 (define (analyse-apart-silence result-idx)
602 "Analyse 'apart-silence starting at RESULT-IDX. Return next index."
603 (let* ((now-state (vector-ref result result-idx))
604 (vs1 (current-voice-state now-state 1))
605 (vs2 (current-voice-state now-state 2))
606 (rests1 (if vs1 (rest-and-skip-events vs1) '()))
607 (rests2 (if vs2 (rest-and-skip-events vs2) '()))
608 (prev-state (if (> result-idx 0)
609 (vector-ref result (- result-idx 1))
611 (prev-config (if prev-state
612 (configuration prev-state)
615 ;; rest with multi-measure rest: choose the rest
616 ((and (synced? now-state)
617 (= 1 (length rests1))
618 (ly:in-event-class? (car rests1) 'rest-event)
619 (= 0 (length rests2))) ; probably mmrest
622 ;; as above with parts swapped
623 ((and (synced? now-state)
624 (= 1 (length rests2))
625 (ly:in-event-class? (car rests2) 'rest-event)
626 (= 0 (length rests1))) ; probably mmrest
630 (put 'apart-silence))
632 ;; remain in the silence1/2 states until resync
633 ((equal? prev-config 'silence1)
636 ((equal? prev-config 'silence2)
640 (put 'apart-silence)))
644 (define (analyse-apart result-idx)
645 "Analyse 'apart starting at RESULT-IDX. Return next index."
646 (let* ((now-state (vector-ref result result-idx))
647 (vs1 (current-voice-state now-state 1))
648 (vs2 (current-voice-state now-state 2))
649 ;; (vs1 (car (voice-states now-state)))
650 ;; (vs2 (cdr (voice-states now-state)))
651 (notes1 (if vs1 (note-events vs1) '()))
652 (notes2 (if vs2 (note-events vs2) '()))
654 (n2 (length notes2)))
655 ;; (display (list "analyzing step " result-idx " moment " (moment now-state) vs1 vs2 "\n"))
657 ;; we should always increase.
658 (cond ((and (= n1 0) (= n2 0))
659 ;; If we hit this, it means that the previous passes
660 ;; have designated as 'apart what is really
662 (analyse-apart-silence result-idx))
664 (equal? (moment vs1) (moment now-state))
665 (null? (previous-span-state vs1)))
666 (try-solo 'solo1 result-idx result-idx))
668 (equal? (moment vs2) (moment now-state))
669 (null? (previous-span-state vs2)))
670 (try-solo 'solo2 result-idx result-idx))
672 (else (1+ result-idx)))
676 (if (< result-idx (vector-length result))
677 (let ((conf (configuration (vector-ref result result-idx))))
679 ((equal? conf 'apart)
680 (analyse-solo12 (analyse-apart result-idx)))
681 ((equal? conf 'apart-silence)
682 (analyse-solo12 (analyse-apart-silence result-idx)))
684 (analyse-solo12 (1+ result-idx))))))) ; analyse-solo12
686 (analyse-spanner-states voice-state-vec1)
687 (analyse-spanner-states voice-state-vec2)
690 (display voice-state-vec1)
692 (display voice-state-vec2)
697 ;; Extract all forced combine strategies, i.e. events inserted by
698 ;; \partcombine(Apart|Automatic|SoloI|SoloII|Chords)[Once]
699 ;; They will in the end override the automaically determined ones.
700 ;; Initial state for both voices is no override
701 (analyse-forced-combine 0 #f)
702 ;; Now go through all time steps in a loop and find a combination strategy
703 ;; based only on the events of that one moment (i.e. neglecting longer
704 ;; periods of solo/apart, etc.)
705 (analyse-time-step 0)
707 ;; Check for unisono or unisilence moments
713 ;; forced-configuration overrides, if it is set
714 (lambda (x) (cons (moment x) (or (forced-configuration x) (configuration x))))
715 (vector->list result)))
721 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
723 (define-public (add-quotable parser name mus)
724 (let* ((tab (eval 'musicQuotes (current-module)))
725 (voicename (get-next-unique-voice-name))
726 ;; recording-group-emulate returns an assoc list (reversed!), so
727 ;; hand it a proper unique context name and extract that key:
728 (ctx-spec (context-spec-music mus 'Voice voicename))
729 (listener (ly:parser-lookup parser 'partCombineListener))
730 (context-list (reverse (recording-group-emulate ctx-spec listener)))
731 (raw-voice (assoc voicename context-list))
732 (quote-contents (if (pair? raw-voice) (cdr raw-voice) '())))
734 ;; If the context-specced quoted music does not contain anything, try to
735 ;; use the first child, i.e. the next in context-list after voicename
736 ;; That's the case e.g. for \addQuote "x" \relative c \new Voice {...}
737 (if (null? quote-contents)
738 (let find-non-empty ((current-tail (member raw-voice context-list)))
739 ;; if voice has contents, use them, otherwise check next ctx
740 (cond ((null? current-tail) #f)
741 ((and (pair? (car current-tail))
742 (pair? (cdar current-tail)))
743 (set! quote-contents (cdar current-tail)))
744 (else (find-non-empty (cdr current-tail))))))
746 (if (not (null? quote-contents))
747 (hash-set! tab name (list->vector (reverse! quote-contents '())))
748 (ly:music-warning mus (ly:format (_ "quoted music `~a' is empty") name)))))