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