]> git.donarmstrong.com Git - lilypond.git/blob - scm/part-combiner.scm
Issue 4233: Improve partcombine multi-measure rest handling.
[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 (any-mmrest-events (vs <Voice-state>))
54   (define (f? x)
55     (ly:in-event-class? x 'multi-measure-rest-event))
56   (any f? (events vs)))
57
58 (define-method (previous-voice-state (vs <Voice-state>))
59   (let ((i (slot-ref vs 'vector-index))
60         (v (slot-ref vs 'state-vector)))
61     (if (< 0 i)
62         (vector-ref v (1- i))
63         #f)))
64
65 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66
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
74   ;;
75   (is #:init-keyword #:voice-states #:accessor voice-states)
76   (synced  #:init-keyword #:synced #:init-value  #f #:getter synced?))
77
78
79 (define-method (write (x <Split-state> ) f)
80   (display (moment x) f)
81   (display " = " f)
82   (display (configuration x) f)
83   (if (synced? x)
84       (display " synced "))
85   (display "\n" f))
86
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))
92          (vs1 (car vss))
93          (vs2 (cdr vss)))
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)))
98     (cons vs1 vs2)))
99
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101
102
103 (define (previous-span-state vs)
104   (let ((p (previous-voice-state vs)))
105     (if p (span-state p) '())))
106
107 (define (make-voice-states evl)
108   (let ((vec (list->vector (map (lambda (v)
109                                   (make <Voice-state>
110                                     #:moment (caar v)
111                                     #:tuning (cdar v)
112                                     #:events (map car (cdr v))))
113                                 evl))))
114     (do ((i 0 (1+ i)))
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))))
118
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
122 Voice-state objects
123 "
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))
130                       (else #f)))
131            (inc1 (if (and state1 (equal? min (moment state1))) 1 0))
132            (inc2 (if (and state2 (equal? min (moment state2))) 1 0))
133            (ss-object (if min
134                           (make <Split-state>
135                             #:moment min
136                             #:voice-states (cons state1 state2)
137                             #:synced (= inc1 inc2))
138                           #f)))
139       (if state1
140           (set! (split-index state1) ss-idx))
141       (if state2
142           (set! (split-index state2) ss-idx))
143       (if min
144           (helper (1+ ss-idx)
145                   (cons ss-object ss-list)
146                   (+ idx1 inc1)
147                   (+ idx2 inc2))
148           ss-list)))
149   (list->vector (reverse! (helper 0 '() 0  0) '())))
150
151 (define (analyse-spanner-states voice-state-vec)
152
153   (define (helper index active)
154     "Analyse EVS at INDEX, given state ACTIVE."
155
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))
159                  active)
160           active))
161
162     (define (analyse-tie-end active ev)
163       (if (ly:in-event-class? ev 'note-event)
164           (assoc-remove! active 'tie)
165           active))
166
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)
172           active))
173
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)))))
178
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)
186                         (else #f)))
187              (sp (ly:event-property ev 'span-direction)))
188         (if (and (symbol? key) (ly:dir? sp))
189             (if (= sp STOP)
190                 (assoc-remove! active key)
191                 (acons key
192                        (split-index (vector-ref voice-state-vec index))
193                        active))
194             active)))
195
196     (define (analyse-events active evs)
197       "Run all analyzers on ACTIVE and EVS"
198       (define (run-analyzer analyzer active evs)
199         (if (pair? evs)
200             (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
201             active))
202       (define (run-analyzers analyzers active evs)
203         (if (pair? analyzers)
204             (run-analyzers (cdr analyzers)
205                            (run-analyzer (car analyzers) active evs)
206                            evs)
207             active))
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)
212                       active evs)
213        active<?))
214
215     ;; must copy, since we use assoc-remove!
216     (if (< index (vector-length voice-state-vec))
217         (begin
218           (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
219           (set! (span-state (vector-ref voice-state-vec index))
220                 (list-copy active))
221           (helper (1+ index) active))))
222
223   (helper 0 '()))
224
225 (define recording-group-functions
226   ;;Selected parts from @var{toplevel-music-functions} not requiring @code{parser}.
227   (list
228    (lambda (music) (expand-repeat-chords! '(rhythmic-event) music))
229    expand-repeat-notes!))
230
231
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."
237   (let*
238       ((context-list '())
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
244         (ly:make-listener
245          (lambda (sev)
246            (let*
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)))
250                 (acc '())
251                 (accumulate-event-listener
252                  (ly:make-listener (lambda (ev)
253                                      (set! acc (cons (cons ev #t) acc)))))
254                 (save-acc-listener
255                  (ly:make-listener (lambda (tev)
256                                      (if (pair? acc)
257                                          (let ((this-moment
258                                                 (cons (cons now-mom
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
263                                                       (reverse acc))))
264                                            (set-cdr! this-moment-list
265                                                      (cons this-moment (cdr this-moment-list)))
266                                            (set! acc '())))))))
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))
277      global)
278     context-list))
279
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)))
287
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) '())
294                                     chord-range)
295               '()))
296     m))
297
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."
300   (let* ((pc-debug #f)
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)))
306
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)
312
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)
318         (if (not vs)
319             '()
320             (filter-map get-forced-event (events vs))))
321       ;; end part-combine-events
322
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)))
328             state
329             (if (cdr evt)
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
335
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)
341                            (append
342                             (part-combine-events (car (voice-states now-state)))
343                             (part-combine-events (cdr (voice-states now-state))))
344                            '()))
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)
349                                   (cdr state)
350                                   (car state))))
351             (set! (forced-configuration (vector-ref result result-idx))
352                   force-state)
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
357
358
359     (define (analyse-time-step result-idx)
360       (define (put x . index)
361         "Put the result to X, starting from INDEX backwards.
362
363 Only set if not set previously.
364 "
365         (let ((i (if (pair? index) (car index) result-idx)))
366           (if (and (<= 0 i)
367                    (not (symbol? (configuration (vector-ref result i)))))
368               (begin
369                 (set! (configuration (vector-ref result i)) x)
370                 (put x (1- i))))))
371
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)))
377             (if (symbol? prev)
378                 (put prev))))
379         (for-each copy-one-state (span-state vs)))
380
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))
386                                  notes1)
387                             ly:duration<?))
388                (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
389                                     notes1)
390                                ly:pitch<?))
391                (notes2 (note-events vs2))
392                (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
393                                  notes2)
394                             ly:duration<?))
395                (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
396                                     notes2)
397                                ly: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)
402                       (= (length durs2) 1)
403                       (not (equal? (car durs1) (car durs2))))
404                  (put 'apart))
405                 (else
406                  (if (and (= (length pitches1) (length pitches2)))
407                      (if (and (pair? pitches1)
408                               (pair? pitches2)
409                               ; Is the interval outside of chord-range?
410                               (let ((diff (ly:pitch-steps
411                                            (ly:pitch-diff (car pitches1)
412                                                           (car pitches2)))))
413                                 (or (< diff chord-min-diff)
414                                     (> diff chord-max-diff)
415                                     )))
416                          (put 'apart)
417                          ;; copy previous split state from spanner state
418                          (begin
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)))))))))
427
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))))
432
433             (cond ((not vs1) (put 'apart))
434                   ((not vs2) (put 'apart))
435                   (else
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)))
440                      (if #f ; debug
441                          (display (list (moment now-state) result-idx
442                                         active1 "->" new-active1
443                                         active2 "->" new-active2
444                                         "\n")))
445                      (if (and (synced? now-state)
446                               (equal? active1 active2)
447                               (equal? new-active1 new-active2))
448                          (analyse-notes now-state)
449
450                          ;; active states different:
451                          (put 'apart)))
452
453                    ;; go to the next one, if it exists.
454                    (analyse-time-step (1+ result-idx)))))))
455
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))))
461
462             (define (analyse-synced-silence)
463               (let ((rests1 (if vs1 (rest-and-skip-events vs1) '()))
464                     (rests2 (if vs2 (rest-and-skip-events vs2) '())))
465                 (cond
466
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))
472
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))
481
482                  ;; rests of different durations or mixed with
483                  ;; skips or multi-measure rests
484                  (else
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))
488
489                  )))
490
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)))
494                 (cond
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.
498                  ((and any-mmrests1
499                        (equal? (moment vs1) (moment now-state))
500                        (or (not vs2) any-mmrests2))
501                   (set! (configuration now-state) 'silence1))
502
503                  ;; as above with parts swapped
504                  ((and any-mmrests2
505                        (equal? (moment vs2) (moment now-state))
506                        (or (not vs1) any-mmrests1))
507                   (set! (configuration now-state) 'silence2))
508                  )))
509
510             (if (or vs1 vs2)
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))
521
522                         ((synced? now-state)
523                          (if (and (= 0 (length notes1))
524                                   (= 0 (length notes2)))
525                              (analyse-synced-silence)))
526
527                         (else ;; not synchronized
528                          (let* ((vss
529                                  (current-or-previous-voice-states now-state))
530                                 (vs1 (car vss))
531                                 (vs2 (cdr vss)))
532                            (if (and
533                                 (or (not vs1) (= 0 (length (note-events vs1))))
534                                 (or (not vs2) (= 0 (length (note-events vs2)))))
535                                (analyse-unsynced-silence vs1 vs2))))
536                         )))
537             (analyse-a2 (1+ result-idx)))))
538
539     (define (analyse-solo12 result-idx)
540
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)))
545           (if prev-split
546               (configuration prev-split)
547               'apart)))
548
549       (define (put-range x a b)
550         ;; (display (list "put range "  x a b "\n"))
551         (do ((i a (1+ i)))
552             ((> i b) b)
553           (set! (configuration (vector-ref result i)) x)))
554
555       (define (put x)
556         ;; (display (list "putting "  x "\n"))
557         (set! (configuration (vector-ref result result-idx)) x))
558
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)))
563             vs
564             (previous-voice-state vs)))
565
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.
569
570       return next idx to analyse.
571 "
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))
580                      current-idx)
581                     ((> (length silent-notes) 0) start-idx)
582                     ((not solo-state)
583                      (put-range type start-idx current-idx)
584                      current-idx)
585                     ((and
586                       (null? (span-state solo-state)))
587
588                      ;;
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.
593                      ;;
594                      (put-range type start-idx current-idx)
595                      (try-solo type (1+ current-idx) (1+  current-idx)))
596                     (else
597                      (try-solo type start-idx (1+ current-idx)))))
598             ;; try-solo
599             start-idx))
600
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))
610                                #f))
611                (prev-config (if prev-state
612                                 (configuration prev-state)
613                                 'apart-silence)))
614           (cond
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
620             (put 'silence1))
621
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
627             (put 'silence2))
628
629            ((synced? now-state)
630             (put 'apart-silence))
631
632            ;; remain in the silence1/2 states until resync
633            ((equal? prev-config 'silence1)
634             (put 'silence1))
635
636            ((equal? prev-config 'silence2)
637             (put 'silence2))
638
639            (else
640             (put 'apart-silence)))
641
642           (1+ result-idx)))
643
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) '()))
653                (n1 (length notes1))
654                (n2 (length notes2)))
655           ;; (display (list "analyzing step " result-idx "  moment " (moment now-state) vs1 vs2  "\n"))
656           (max
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
661                   ;; 'apart-silence.
662                   (analyse-apart-silence result-idx))
663                  ((and (= n2 0)
664                        (equal? (moment vs1) (moment now-state))
665                        (null? (previous-span-state vs1)))
666                   (try-solo 'solo1 result-idx result-idx))
667                  ((and (= n1 0)
668                        (equal? (moment vs2) (moment now-state))
669                        (null? (previous-span-state vs2)))
670                   (try-solo 'solo2 result-idx result-idx))
671
672                  (else (1+ result-idx)))
673            ;; analyse-moment
674            (1+ result-idx))))
675
676       (if (< result-idx (vector-length result))
677           (let ((conf (configuration (vector-ref result result-idx))))
678             (cond
679              ((equal? conf 'apart)
680               (analyse-solo12 (analyse-apart result-idx)))
681              ((equal? conf 'apart-silence)
682               (analyse-solo12 (analyse-apart-silence result-idx)))
683              (else
684               (analyse-solo12 (1+ result-idx))))))) ; analyse-solo12
685
686     (analyse-spanner-states voice-state-vec1)
687     (analyse-spanner-states voice-state-vec2)
688     (if #f
689         (begin
690           (display voice-state-vec1)
691           (display "***\n")
692           (display voice-state-vec2)
693           (display "***\n")
694           (display result)
695           (display "***\n")))
696
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)
706     ;; (display result)
707     ;; Check for unisono or unisilence moments
708     (analyse-a2 0)
709     ;;(display result)
710     (analyse-solo12 0)
711     ;; (display result)
712     (set! result (map
713                   ;; forced-configuration overrides, if it is set
714                   (lambda (x) (cons (moment x) (or (forced-configuration x) (configuration x))))
715                   (vector->list result)))
716     (if #f ;; pc-debug
717         (display result))
718     result))
719
720
721 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
722
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) '())))
733
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))))))
745
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)))))