]> git.donarmstrong.com Git - lilypond.git/blob - scm/part-combiner.scm
Merge remote-tracking branch 'origin' into translation
[lilypond.git] / scm / part-combiner.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18 ;; todo: figure out how to make module,
19 ;; without breaking nested ly scopes
20
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)
26   (vector-index)
27   (state-vector)
28   ;;;
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))
33
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)
40   (display "\n" file))
41
42 (define-method (note-events (vs <Voice-state>))
43   (define (f? x)
44     (ly:in-event-class? x 'note-event))
45   (filter f? (events vs)))
46
47 (define-method (previous-voice-state (vs <Voice-state>))
48   (let ((i (slot-ref vs 'vector-index))
49         (v (slot-ref vs 'state-vector)))
50     (if (< 0 i)
51         (vector-ref v (1- i))
52         #f)))
53
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55
56 (define-class <Split-state> ()
57   ;; The automatically determined split configuration
58   (configuration #:init-value '() #:accessor configuration)
59   ;; Allow overriding split configuration, takes precedence over configuration
60   (forced-configuration #:init-value #f #:accessor forced-configuration)
61   (when-moment #:accessor moment #:init-keyword #:moment)
62   ;; voice-states are states starting with the Split-state or later
63   ;;
64   (is #:init-keyword #:voice-states #:accessor voice-states)
65   (synced  #:init-keyword #:synced #:init-value  #f #:getter synced?))
66
67
68 (define-method (write (x <Split-state> ) f)
69   (display (moment x) f)
70   (display " = " f)
71   (display (configuration x) f)
72   (if (synced? x)
73       (display " synced "))
74   (display "\n" f))
75
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77
78
79 (define (previous-span-state vs)
80   (let ((p (previous-voice-state vs)))
81     (if p (span-state p) '())))
82
83 (define (make-voice-states evl)
84   (let ((vec (list->vector (map (lambda (v)
85                                   (make <Voice-state>
86                                     #:moment (caar v)
87                                     #:tuning (cdar v)
88                                     #:events (map car (cdr v))))
89                                 evl))))
90     (do ((i 0 (1+ i)))
91         ((= i (vector-length vec)) vec)
92       (slot-set! (vector-ref vec i) 'vector-index i)
93       (slot-set! (vector-ref vec i) 'state-vector vec))))
94
95 (define (make-split-state vs1 vs2)
96   "Merge lists VS1 and VS2, containing Voice-state objects into vector
97 of Split-state objects, crosslinking the Split-state vector and
98 Voice-state objects
99 "
100   (define (helper ss-idx ss-list idx1 idx2)
101     (let* ((state1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f))
102            (state2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f))
103            (min (cond ((and state1 state2) (moment-min (moment state1) (moment state2)))
104                       (state1 (moment state1))
105                       (state2 (moment state2))
106                       (else #f)))
107            (inc1 (if (and state1 (equal? min (moment state1))) 1 0))
108            (inc2 (if (and state2 (equal? min (moment state2))) 1 0))
109            (ss-object (if min
110                           (make <Split-state>
111                             #:moment min
112                             #:voice-states (cons state1 state2)
113                             #:synced (= inc1 inc2))
114                           #f)))
115       (if state1
116           (set! (split-index state1) ss-idx))
117       (if state2
118           (set! (split-index state2) ss-idx))
119       (if min
120           (helper (1+ ss-idx)
121                   (cons ss-object ss-list)
122                   (+ idx1 inc1)
123                   (+ idx2 inc2))
124           ss-list)))
125   (list->vector (reverse! (helper 0 '() 0  0) '())))
126
127 (define (analyse-spanner-states voice-state-vec)
128
129   (define (helper index active)
130     "Analyse EVS at INDEX, given state ACTIVE."
131
132     (define (analyse-tie-start active ev)
133       (if (ly:in-event-class? ev 'tie-event)
134           (acons 'tie (split-index (vector-ref voice-state-vec index))
135                  active)
136           active))
137
138     (define (analyse-tie-end active ev)
139       (if (ly:in-event-class? ev 'note-event)
140           (assoc-remove! active 'tie)
141           active))
142
143     (define (analyse-absdyn-end active ev)
144       (if (or (ly:in-event-class? ev 'absolute-dynamic-event)
145               (and (ly:in-event-class? ev 'span-dynamic-event)
146                    (equal? STOP (ly:event-property ev 'span-direction))))
147           (assoc-remove! (assoc-remove! active 'cresc) 'decr)
148           active))
149
150     (define (active<? a b)
151       (cond ((symbol<? (car a) (car b)) #t)
152             ((symbol<? (car b) (car a)) #f)
153             (else (< (cdr a) (cdr b)))))
154
155     (define (analyse-span-event active ev)
156       (let* ((name (car (ly:event-property ev 'class)))
157              (key (cond ((equal? name 'slur-event) 'slur)
158                         ((equal? name 'phrasing-slur-event) 'tie)
159                         ((equal? name 'beam-event) 'beam)
160                         ((equal? name 'crescendo-event) 'cresc)
161                         ((equal? name 'decrescendo-event) 'decr)
162                         (else #f)))
163              (sp (ly:event-property ev 'span-direction)))
164         (if (and (symbol? key) (ly:dir? sp))
165             (if (= sp STOP)
166                 (assoc-remove! active key)
167                 (acons key
168                        (split-index (vector-ref voice-state-vec index))
169                        active))
170             active)))
171
172     (define (analyse-events active evs)
173       "Run all analyzers on ACTIVE and EVS"
174       (define (run-analyzer analyzer active evs)
175         (if (pair? evs)
176             (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
177             active))
178       (define (run-analyzers analyzers active evs)
179         (if (pair? analyzers)
180             (run-analyzers (cdr analyzers)
181                            (run-analyzer (car analyzers) active evs)
182                            evs)
183             active))
184       (sort ;; todo: use fold or somesuch.
185        (run-analyzers (list analyse-absdyn-end analyse-span-event
186                             ;; note: tie-start/span comes after tie-end/absdyn.
187                             analyse-tie-end analyse-tie-start)
188                       active evs)
189        active<?))
190
191     ;; must copy, since we use assoc-remove!
192     (if (< index (vector-length voice-state-vec))
193         (begin
194           (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
195           (set! (span-state (vector-ref voice-state-vec index))
196                 (list-copy active))
197           (helper (1+ index) active))))
198
199   (helper 0 '()))
200
201 (define recording-group-functions
202   ;;Selected parts from @var{toplevel-music-functions} not requiring @code{parser}.
203   (list
204    (lambda (music) (expand-repeat-chords! '(rhythmic-event) music))
205    expand-repeat-notes!))
206
207
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 (define-public (recording-group-emulate music odef)
210   "Interpret @var{music} according to @var{odef}, but store all events
211 in a chronological list, similar to the @code{Recording_group_engraver} in
212 LilyPond version 2.8 and earlier."
213   (let*
214       ((context-list '())
215        (now-mom (ly:make-moment 0 0))
216        (global (ly:make-global-context odef))
217        (mom-listener (ly:make-listener
218                       (lambda (tev) (set! now-mom (ly:event-property tev 'moment)))))
219        (new-context-listener
220         (ly:make-listener
221          (lambda (sev)
222            (let*
223                ((child (ly:event-property sev 'context))
224                 (this-moment-list (cons (ly:context-id child) '()))
225                 (dummy (set! context-list (cons this-moment-list context-list)))
226                 (acc '())
227                 (accumulate-event-listener
228                  (ly:make-listener (lambda (ev)
229                                      (set! acc (cons (cons ev #t) acc)))))
230                 (save-acc-listener
231                  (ly:make-listener (lambda (tev)
232                                      (if (pair? acc)
233                                          (let ((this-moment
234                                                 (cons (cons now-mom
235                                                             (ly:context-property child 'instrumentTransposition))
236                                                       ;; The accumulate-event-listener above creates
237                                                       ;; the list of events in reverse order, so we
238                                                       ;; have to revert it to the original order again
239                                                       (reverse acc))))
240                                            (set-cdr! this-moment-list
241                                                      (cons this-moment (cdr this-moment-list)))
242                                            (set! acc '())))))))
243              (ly:add-listener accumulate-event-listener
244                               (ly:context-event-source child) 'StreamEvent)
245              (ly:add-listener save-acc-listener
246                               (ly:context-event-source global) 'OneTimeStep))))))
247     (ly:add-listener new-context-listener
248                      (ly:context-events-below global) 'AnnounceNewContext)
249     (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare)
250     (ly:interpret-music-expression
251      (make-non-relative-music
252       (fold (lambda (x m) (x m)) music recording-group-functions))
253      global)
254     context-list))
255
256 (define-public (make-part-combine-music parser music-list direction)
257   (let* ((m (make-music 'PartCombineMusic))
258          (m1 (make-non-relative-music (context-spec-music (first music-list) 'Voice "one")))
259          (m2  (make-non-relative-music  (context-spec-music (second music-list) 'Voice "two")))
260          (listener (ly:parser-lookup parser 'partCombineListener))
261          (evs2 (recording-group-emulate m2 listener))
262          (evs1 (recording-group-emulate m1 listener)))
263
264     (set! (ly:music-property m 'elements) (list m1 m2))
265     (set! (ly:music-property m 'direction) direction)
266     (set! (ly:music-property m 'split-list)
267           (if (and (assoc "one" evs1) (assoc "two" evs2))
268               (determine-split-list (reverse! (assoc-get "one" evs1) '())
269                                     (reverse! (assoc-get "two" evs2) '()))
270               '()))
271     m))
272
273 (define-public (determine-split-list evl1 evl2)
274   "@var{evl1} and @var{evl2} should be ascending."
275   (let* ((pc-debug #f)
276          (chord-threshold 8)
277          (voice-state-vec1 (make-voice-states evl1))
278          (voice-state-vec2 (make-voice-states evl2))
279          (result (make-split-state voice-state-vec1 voice-state-vec2)))
280
281     ;; Go through all moments recursively and check if the events of that
282     ;; moment contain a part-combine-force-event override. If so, store its
283     ;; value in the forced-configuration field, which will override. The
284     ;; previous configuration is used to determine non-terminated settings.
285     (define (analyse-forced-combine result-idx prev-res)
286
287       (define (get-forced-event x)
288         (and (ly:in-event-class? x 'part-combine-force-event)
289              (cons (ly:event-property x 'forced-type)
290                    (ly:event-property x 'once))))
291       (define (part-combine-events vs)
292         (if (not vs)
293             '()
294             (filter-map get-forced-event (events vs))))
295       ;; end part-combine-events
296
297       ;; forced-result: Take the previous config and analyse whether
298       ;; any change happened.... Return new once and permanent config
299       (define (forced-result evt state)
300         ;; sanity check, evt should always be (new-state . once)
301         (if (not (and (pair? evt) (pair? state)))
302             state
303             (if (cdr evt)
304                 ;; Once-event, leave permanent state unchanged
305                 (cons (car evt) (cdr state))
306                 ;; permanent change, leave once state unchanged
307                 (cons (car state) (car evt)))))
308       ;; end forced-combine-result
309
310       ;; body of analyse-forced-combine:
311       (if (< result-idx (vector-length result))
312           (let* ((now-state (vector-ref result result-idx)) ; current result
313                  ;; Extract all part-combine force events
314                  (ev1 (part-combine-events (car (voice-states now-state))))
315                  (ev2 (part-combine-events (cdr (voice-states now-state))))
316                  (evts (append ev1 ev2))
317                  ;; result is (once-state permament-state):
318                  (state (fold forced-result (cons 'automatic prev-res) evts))
319                  ;; Now let once override permanent changes:
320                  (force-state (if (equal? (car state) 'automatic)
321                                   (cdr state)
322                                   (car state))))
323             (set! (forced-configuration (vector-ref result result-idx))
324                   force-state)
325             ;; For the next moment, ignore the once override (car stat)
326             ;; and pass on the permanent override, stored as (cdr state)
327             (analyse-forced-combine (1+ result-idx) (cdr state)))))
328     ;; end analyse-forced-combine
329
330
331     (define (analyse-time-step result-idx)
332       (define (put x . index)
333         "Put the result to X, starting from INDEX backwards.
334
335 Only set if not set previously.
336 "
337         (let ((i (if (pair? index) (car index) result-idx)))
338           (if (and (<= 0 i)
339                    (not (symbol? (configuration (vector-ref result i)))))
340               (begin
341                 (set! (configuration (vector-ref result i)) x)
342                 (put x (1- i))))))
343
344       (define (copy-state-from state-vec vs)
345         (define (copy-one-state key-idx)
346           (let* ((idx (cdr key-idx))
347                  (prev-ss (vector-ref result idx))
348                  (prev (configuration prev-ss)))
349             (if (symbol? prev)
350                 (put prev))))
351         (for-each copy-one-state (span-state vs)))
352
353       (define (analyse-notes now-state)
354         (let* ((vs1 (car (voice-states now-state)))
355                (vs2 (cdr (voice-states now-state)))
356                (notes1 (note-events vs1))
357                (durs1 (sort (map (lambda (x) (ly:event-property x 'duration))
358                                  notes1)
359                             ly:duration<?))
360                (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
361                                     notes1)
362                                ly:pitch<?))
363                (notes2 (note-events vs2))
364                (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
365                                  notes2)
366                             ly:duration<?))
367                (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
368                                     notes2)
369                                ly:pitch<?)))
370           (cond ((> (length notes1) 1) (put 'apart))
371                 ((> (length notes2) 1) (put 'apart))
372                 ((= 1 (+ (length notes2) (length notes1))) (put 'apart))
373                 ((and (= (length durs1) 1)
374                       (= (length durs2) 1)
375                       (not (equal? (car durs1) (car durs2))))
376                  (put 'apart))
377                 (else
378                  (if (and (= (length pitches1) (length pitches2)))
379                      (if (and (pair? pitches1)
380                               (pair? pitches2)
381                               (or
382                                (< chord-threshold (ly:pitch-steps
383                                                    (ly:pitch-diff (car pitches1)
384                                                                   (car pitches2))))
385
386                                ;; voice crossings:
387                                (> 0 (ly:pitch-steps (ly:pitch-diff (car pitches1)
388                                                                    (car pitches2))))
389                                ))
390                          (put 'apart)
391                          ;; copy previous split state from spanner state
392                          (begin
393                            (if (previous-voice-state vs1)
394                                (copy-state-from voice-state-vec1
395                                                 (previous-voice-state vs1)))
396                            (if (previous-voice-state vs2)
397                                (copy-state-from voice-state-vec2
398                                                 (previous-voice-state vs2)))
399                            (if (and (null? (span-state vs1)) (null? (span-state vs2)))
400                                (put 'chords)))))))))
401
402       (if (< result-idx (vector-length result))
403           (let* ((now-state (vector-ref result result-idx))
404                  (vs1 (car (voice-states now-state)))
405                  (vs2 (cdr (voice-states now-state))))
406
407             (cond ((not vs1) (put 'apart))
408                   ((not vs2) (put 'apart))
409                   (else
410                    (let ((active1 (previous-span-state vs1))
411                          (active2 (previous-span-state vs2))
412                          (new-active1 (span-state vs1))
413                          (new-active2 (span-state vs2)))
414                      (if #f ; debug
415                          (display (list (moment now-state) result-idx
416                                         active1 "->" new-active1
417                                         active2 "->" new-active2
418                                         "\n")))
419                      (if (and (synced? now-state)
420                               (equal? active1 active2)
421                               (equal? new-active1 new-active2))
422                          (analyse-notes now-state)
423
424                          ;; active states different:
425                          (put 'apart)))
426
427                    ;; go to the next one, if it exists.
428                    (analyse-time-step (1+ result-idx)))))))
429
430     (define (analyse-a2 result-idx)
431       (if (< result-idx (vector-length result))
432           (let* ((now-state (vector-ref result result-idx))
433                  (vs1 (car (voice-states now-state)))
434                  (vs2 (cdr (voice-states now-state))))
435             (if (and (equal? (configuration now-state) 'chords)
436                      vs1 vs2)
437                 (let ((notes1 (note-events vs1))
438                       (notes2 (note-events vs2)))
439                   (cond ((and (= 1 (length notes1))
440                               (= 1 (length notes2))
441                               (equal? (ly:event-property (car notes1) 'pitch)
442                                       (ly:event-property (car notes2) 'pitch)))
443                          (set! (configuration now-state) 'unisono))
444                         ((and (= 0 (length notes1))
445                               (= 0 (length notes2)))
446                          (set! (configuration now-state) 'unisilence)))))
447             (analyse-a2 (1+ result-idx)))))
448
449     (define (analyse-solo12 result-idx)
450
451       (define (previous-config vs)
452         (let* ((pvs (previous-voice-state vs))
453                (spi (if pvs (split-index pvs) #f))
454                (prev-split (if spi (vector-ref result spi) #f)))
455           (if prev-split
456               (configuration prev-split)
457               'apart)))
458
459       (define (put-range x a b)
460         ;; (display (list "put range "  x a b "\n"))
461         (do ((i a (1+ i)))
462             ((> i b) b)
463           (set! (configuration (vector-ref result i)) x)))
464
465       (define (put x)
466         ;; (display (list "putting "  x "\n"))
467         (set! (configuration (vector-ref result result-idx)) x))
468
469       (define (current-voice-state now-state voice-num)
470         (define vs ((if (= 1 voice-num) car cdr)
471                     (voice-states now-state)))
472         (if (or (not vs) (equal? (moment now-state) (moment vs)))
473             vs
474             (previous-voice-state vs)))
475
476       (define (try-solo type start-idx current-idx)
477         "Find a maximum stretch that can be marked as solo.  Only set
478 the mark when there are no spanners active.
479
480       return next idx to analyse.
481 "
482         (if (< current-idx (vector-length result))
483             (let* ((now-state (vector-ref result current-idx))
484                    (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
485                    (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
486                    (silent-notes (if silent-state (note-events silent-state) '()))
487                    (solo-notes (if solo-state (note-events solo-state) '())))
488               ;; (display (list "trying " type " at "  (moment now-state) solo-state silent-state        "\n"))
489               (cond ((not (equal? (configuration now-state) 'apart))
490                      current-idx)
491                     ((> (length silent-notes) 0) start-idx)
492                     ((not solo-state)
493                      (put-range type start-idx current-idx)
494                      current-idx)
495                     ((and
496                       (null? (span-state solo-state)))
497
498                      ;;
499                      ;; This includes rests. This isn't a problem: long rests
500                      ;; will be shared with the silent voice, and be marked
501                      ;; as unisilence. Therefore, long rests won't
502                      ;;  accidentally be part of a solo.
503                      ;;
504                      (put-range type start-idx current-idx)
505                      (try-solo type (1+ current-idx) (1+  current-idx)))
506                     (else
507                      (try-solo type start-idx (1+ current-idx)))))
508             ;; try-solo
509             start-idx))
510
511       (define (analyse-moment result-idx)
512         "Analyse 'apart starting at RESULT-IDX.  Return next index."
513         (let* ((now-state (vector-ref result result-idx))
514                (vs1 (current-voice-state now-state 1))
515                (vs2 (current-voice-state now-state 2))
516                ;; (vs1 (car (voice-states now-state)))
517                ;; (vs2 (cdr (voice-states now-state)))
518                (notes1 (if vs1 (note-events vs1) '()))
519                (notes2 (if vs2 (note-events vs2) '()))
520                (n1 (length notes1))
521                (n2 (length notes2)))
522           ;; (display (list "analyzing step " result-idx "  moment " (moment now-state) vs1 vs2  "\n"))
523           (max
524            ;; we should always increase.
525            (cond ((and (= n1 0) (= n2 0))
526                   (put 'apart-silence)
527                   (1+ result-idx))
528                  ((and (= n2 0)
529                        (equal? (moment vs1) (moment now-state))
530                        (null? (previous-span-state vs1)))
531                   (try-solo 'solo1 result-idx result-idx))
532                  ((and (= n1 0)
533                        (equal? (moment vs2) (moment now-state))
534                        (null? (previous-span-state vs2)))
535                   (try-solo 'solo2 result-idx result-idx))
536
537                  (else (1+ result-idx)))
538            ;; analyse-moment
539            (1+ result-idx))))
540
541       (if (< result-idx (vector-length result))
542           (if (equal? (configuration (vector-ref result result-idx)) 'apart)
543               (analyse-solo12 (analyse-moment result-idx))
544               (analyse-solo12 (1+ result-idx))))) ; analyse-solo12
545
546     (analyse-spanner-states voice-state-vec1)
547     (analyse-spanner-states voice-state-vec2)
548     (if #f
549         (begin
550           (display voice-state-vec1)
551           (display "***\n")
552           (display voice-state-vec2)
553           (display "***\n")
554           (display result)
555           (display "***\n")))
556
557     ;; Extract all forced combine strategies, i.e. events inserted by
558     ;; \partcombine(Apart|Automatic|SoloI|SoloII|Chords)[Once]
559     ;; They will in the end override the automaically determined ones.
560     ;; Initial state for both voices is no override
561     (analyse-forced-combine 0 #f)
562     ;; Now go through all time steps in a loop and find a combination strategy
563     ;; based only on the events of that one moment (i.e. neglecting longer
564     ;; periods of solo/apart, etc.)
565     (analyse-time-step 0)
566     ;; (display result)
567     ;; Check for unisono or unisilence moments
568     (analyse-a2 0)
569     ;;(display result)
570     (analyse-solo12 0)
571     ;; (display result)
572     (set! result (map
573                   ;; forced-configuration overrides, if it is set
574                   (lambda (x) (cons (moment x) (or (forced-configuration x) (configuration x))))
575                   (vector->list result)))
576     (if #f ;; pc-debug
577         (display result))
578     result))
579
580
581 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
582
583 (define-public (add-quotable parser name mus)
584   (let* ((tab (eval 'musicQuotes (current-module)))
585          (voicename (get-next-unique-voice-name))
586          ;; recording-group-emulate returns an assoc list (reversed!), so
587          ;; hand it a proper unique context name and extract that key:
588          (ctx-spec (context-spec-music mus 'Voice voicename))
589          (listener (ly:parser-lookup parser 'partCombineListener))
590          (context-list (reverse (recording-group-emulate ctx-spec listener)))
591          (raw-voice (assoc voicename context-list))
592          (quote-contents (if (pair? raw-voice) (cdr raw-voice) '())))
593
594     ;; If the context-specced quoted music does not contain anything, try to
595     ;; use the first child, i.e. the next in context-list after voicename
596     ;; That's the case e.g. for \addQuote "x" \relative c \new Voice {...}
597     (if (null? quote-contents)
598         (let find-non-empty ((current-tail (member raw-voice context-list)))
599           ;; if voice has contents, use them, otherwise check next ctx
600           (cond ((null? current-tail) #f)
601                 ((and (pair? (car current-tail))
602                       (pair? (cdar current-tail)))
603                  (set! quote-contents (cdar current-tail)))
604                 (else (find-non-empty (cdr current-tail))))))
605
606     (if (not (null? quote-contents))
607         (hash-set! tab name (list->vector (reverse! quote-contents '())))
608         (ly:music-warning mus (ly:format (_ "quoted music `~a' is empty") name)))))