]> git.donarmstrong.com Git - lilypond.git/blob - scm/part-combiner.scm
a444313f6617f1d385580e8cd76905b1041b8eb7
[lilypond.git] / scm / part-combiner.scm
1 ;;;; part-combiner.scm -- Part combining, staff changes.
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2004--2006     Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7 ;; todo: figure out how to make module,
8 ;; without breaking nested ly scopes
9
10 (define-class <Voice-state> ()
11   (event-list #:init-value '() #:accessor events #:init-keyword #:events)
12   (when-moment #:accessor when #:init-keyword #:when)
13   (tuning #:accessor tuning #:init-keyword #:tuning)
14   (split-index #:accessor split-index)
15   (vector-index)
16   (state-vector)
17   ;;;
18   ;; spanner-state is an alist
19   ;; of (SYMBOL . RESULT-INDEX), which indicates where
20   ;; said spanner was started.
21   (spanner-state #:init-value '() #:accessor span-state))
22   
23 (define-method (write (x <Voice-state> ) file)
24   (display (when x) file)
25   (display " evs = " file)
26   (display (events x) file)
27   (display " active = " file)
28   (display (span-state x) file)
29   (display "\n" file))
30
31 (define-method (note-events (vs <Voice-state>))
32   (define (f? x)
33     (equal? (ly:event-property  x 'class) 'note-event))
34   (filter f? (events vs)))
35
36 (define-method (previous-voice-state (vs <Voice-state>))
37   (let ((i (slot-ref vs 'vector-index))
38         (v (slot-ref vs 'state-vector)))
39     (if (< 0 i)
40         (vector-ref v (1- i))
41         #f)))
42
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44
45 (define-class <Split-state> ()
46   (configuration #:init-value '() #:accessor configuration)
47   (when-moment #:accessor when #:init-keyword #:when)
48   ;; voice-states are states starting with the Split-state or later
49   ;;
50   (is #:init-keyword #:voice-states #:accessor voice-states)
51   (synced  #:init-keyword #:synced #:init-value  #f #:getter synced?))
52                              
53
54 (define-method (write (x <Split-state> ) f)
55   (display (when x) f)
56   (display " = " f)
57   (display (configuration x) f)
58   (if (synced? x)
59       (display " synced "))
60   (display "\n" f))
61
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63
64
65 (define (previous-span-state vs)
66   (let ((p (previous-voice-state vs)))
67     (if p (span-state p) '())))
68
69 (define (make-voice-states evl)
70   (let ((vec (list->vector (map (lambda (v)
71                                   (make <Voice-state>
72                                     #:when (caar v)
73                                     #:tuning (cdar v)
74                                     #:events (map car (cdr v))))
75                                 evl))))
76     (do ((i 0 (1+ i)))
77         ((= i (vector-length vec)) vec)
78       (slot-set! (vector-ref vec i) 'vector-index i)
79       (slot-set! (vector-ref vec i) 'state-vector vec))))
80
81 (define (make-split-state vs1 vs2)
82   "Merge lists VS1 and VS2, containing Voice-state objects into vector
83 of Split-state objects, crosslinking the Split-state vector and
84 Voice-state objects
85 "  
86   (define (helper ss-idx ss-list idx1 idx2)
87     (let* ((state1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f))
88            (state2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f))
89            (min (cond ((and state1 state2) (moment-min (when state1) (when state2)))
90                       (state1 (when state1))
91                       (state2 (when state2))
92                       (else #f)))
93            (inc1 (if (and state1 (equal? min (when state1))) 1 0))
94            (inc2 (if (and state2 (equal? min (when state2))) 1 0))
95            (ss-object (if min
96                           (make <Split-state>
97                             #:when min
98                             #:voice-states (cons state1 state2)
99                             #:synced (= inc1 inc2))
100                           #f)))
101       (if state1
102           (set! (split-index state1) ss-idx))
103       (if state2
104           (set! (split-index state2) ss-idx))
105       (if min
106           (helper (1+ ss-idx)
107                   (cons ss-object ss-list)
108                   (+ idx1 inc1)
109                   (+ idx2 inc2))
110           ss-list)))
111   (list->vector (reverse! (helper 0 '() 0  0) '())))
112
113 (define (analyse-spanner-states voice-state-vec)
114
115   (define (helper index active)
116     "Analyse EVS at INDEX, given state ACTIVE."
117     
118     (define (analyse-tie-start active ev)
119       (if (equal? (ly:event-property ev 'class) 'tie-event)
120           (acons 'tie (split-index (vector-ref voice-state-vec index))
121                  active)
122           active))
123     
124     (define (analyse-tie-end active ev)
125       (if (equal? (ly:event-property ev 'class) 'note-event)
126           (assoc-remove! active 'tie)
127           active))
128
129     (define (analyse-absdyn-end active ev)
130       (if (or (equal? (ly:event-property ev 'class) 'absolute-dynamic-event)
131               (and (equal? (ly:event-property ev 'class) 'crescendo-event)
132                    (equal? STOP (ly:event-property ev 'span-direction))))
133           (assoc-remove! (assoc-remove! active 'cresc) 'decr)
134           active))
135     
136     (define (active<? a b)
137       (cond ((symbol<? (car a) (car b)) #t)
138             ((symbol<? (car b) (car b)) #f)
139             (else (< (cdr a) (cdr b)))))
140     
141     (define (analyse-span-event active ev)
142       (let* ((name (ly:event-property ev 'class))
143              (key (cond ((equal? name 'slur-event) 'slur)
144                         ((equal? name 'phrasing-slur-event) 'tie)
145                         ((equal? name 'beam-event) 'beam)
146                         ((equal? name 'crescendo-event) 'cresc)
147                         ((equal? name 'decrescendo-event) 'decr)
148                         (else #f)))
149              (sp (ly:event-property ev 'span-direction)))
150         (if (and (symbol? key) (ly:dir? sp))
151             (if (= sp STOP)
152                 (assoc-remove! active key)
153                 (acons key
154                        (split-index (vector-ref voice-state-vec index))
155                        active))
156             active)))
157
158     (define (analyse-events active evs)
159       "Run all analyzers on ACTIVE and EVS"
160       (define (run-analyzer analyzer active evs)
161         (if (pair? evs)
162             (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
163             active))
164       (define (run-analyzers analyzers active evs)
165         (if (pair? analyzers)
166             (run-analyzers (cdr analyzers)
167                            (run-analyzer (car analyzers) active evs)
168                            evs)
169             active))
170       (sort ;; todo: use fold or somesuch.
171        (run-analyzers (list analyse-absdyn-end analyse-span-event
172                             ;; note: tie-start/span comes after tie-end/absdyn.
173                             analyse-tie-end analyse-tie-start)
174                       active evs)
175        active<?))
176
177     ;; must copy, since we use assoc-remove!
178     (if (< index (vector-length voice-state-vec))
179         (begin
180           (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
181           (set! (span-state (vector-ref voice-state-vec index))
182                 (list-copy active))
183           (helper (1+ index) active))))
184   
185   (helper 0 '()))
186
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 (define-public (recording-group-emulate music odef) 
189   "Interprets music according to odef, but stores all events in a chronological list, similar to the Recording_group_engraver in 2.8 and earlier"
190   (let*
191       ((context-list '())
192        (now-mom (ly:make-moment 0 0))
193        (global (ly:make-global-context odef))
194        (mom-listener (ly:make-listener 
195                       (lambda (tev)
196                         (set! now-mom (ly:event-property tev 'moment)))))
197        (new-context-listener
198         (ly:make-listener
199          (lambda (sev)
200              (let*
201                  ((child (ly:event-property sev 'context))
202                   (this-moment-list
203                    (cons (ly:context-id child) '()))
204                   (dummy
205                    (set! context-list (cons this-moment-list context-list)))
206                   (acc '())
207                   (accumulate-event-listener
208                    (ly:make-listener (lambda (ev)
209                                        (set! acc (cons (cons ev #t) acc)))))
210                   (save-acc-listener (ly:make-listener (lambda (tev)
211                                                          (if (pair? acc)
212                                                              (let ((this-moment (cons (cons now-mom (ly:context-property child 'instrumentTransposition))
213                                                                                       acc)))
214                                                                (set-cdr! this-moment-list (cons this-moment (cdr this-moment-list)))
215                                                                (set! acc '())))))))
216                (ly:add-listener accumulate-event-listener (ly:context-event-source child) 'music-event)
217                (ly:add-listener save-acc-listener (ly:context-event-source global) 'OneTimeStep))))))
218     (ly:add-listener new-context-listener (ly:context-events-below global) 'AnnounceNewContext)
219     (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare)
220     (ly:interpret-music-expression (make-non-relative-music music) global)
221     context-list))
222
223 (define noticed '())
224 ;; todo: junk this, extract $defaultlayout from parser instead
225 (define part-combine-listener '())
226
227 ; UGH - should pass noticed setter to part-combine-listener
228 (define-safe-public (set-part-combine-listener x)
229   (set! part-combine-listener x))
230
231 (define-public (notice-the-events-for-pc context lst)
232   "add CONTEXT-ID, EVENT list to NOTICED variable."
233   
234   (set! noticed (acons (ly:context-id context) lst noticed)))
235
236 (define-public (make-part-combine-music music-list)
237   (let* ((m (make-music 'PartCombineMusic))
238          (m1 (make-non-relative-music (context-spec-music (first music-list) 'Voice "one")))
239          (m2  (make-non-relative-music  (context-spec-music (second music-list) 'Voice "two")))
240          (evs2 (recording-group-emulate m2 part-combine-listener))
241          (evs1 (recording-group-emulate m1 part-combine-listener)))
242     
243     (set! (ly:music-property m 'elements) (list m1 m2))
244     (set! (ly:music-property m 'split-list)
245           (determine-split-list (reverse! (cdr (assoc "one" evs1)) '())
246                                 (reverse! (cdr (assoc "two" evs2)) '())))
247     m))
248
249 (define-public (determine-split-list evl1 evl2)
250   "EVL1 and EVL2 should be ascending"
251   (let* ((pc-debug #f)
252          (chord-threshold 8)
253          (voice-state-vec1 (make-voice-states evl1))
254          (voice-state-vec2 (make-voice-states evl2))
255          (result (make-split-state voice-state-vec1 voice-state-vec2)))
256     
257     (define (analyse-time-step result-idx)
258       (define (put x . index)
259         "Put the result to X, starting from INDEX backwards.
260
261 Only set if not set previously.
262 "
263         (let ((i (if (pair? index) (car index) result-idx)))
264           (if (and (<= 0 i)
265                    (not (symbol? (configuration (vector-ref result i)))))
266               (begin
267                 (set! (configuration (vector-ref result i)) x)
268                 (put x (1- i))))))
269       
270       (define (copy-state-from state-vec vs)
271         (define (copy-one-state key-idx)
272           (let* ((idx (cdr key-idx))
273                  (prev-ss (vector-ref result idx))
274                  (prev (configuration prev-ss)))
275             (if (symbol? prev)
276                 (put prev))))
277         (map copy-one-state (span-state vs)))
278
279       (define (analyse-notes now-state)
280         (let* ((vs1 (car (voice-states now-state)))
281                (vs2 (cdr (voice-states now-state)))
282                (notes1 (note-events vs1))
283                (durs1 (sort (map (lambda (x) (ly:event-property x 'duration))
284                                  notes1)
285                             ly:duration<?))
286                (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
287                                     notes1)
288                                ly:pitch<?))
289                (notes2 (note-events vs2))
290                (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
291                                  notes2)
292                             ly:duration<?))
293                (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
294                                     notes2)
295                                ly:pitch<?)))
296           (cond ((> (length notes1) 1) (put 'apart))
297                 ((> (length notes2) 1) (put 'apart))
298                 ((= 1 (+ (length notes2) (length notes1))) (put 'apart))
299                 ((and (= (length durs1) 1)
300                       (= (length durs2) 1)
301                       (not (equal? (car durs1) (car durs2))))
302                  (put 'apart))
303                 (else
304                  (if (and (= (length pitches1) (length pitches2)))
305                      (if (and (pair? pitches1)
306                               (pair? pitches2)
307                               (or
308                                (< chord-threshold (ly:pitch-steps
309                                                    (ly:pitch-diff (car pitches1)
310                                                                   (car pitches2))))
311
312                                ;; voice crossings:
313                                (> 0 (ly:pitch-steps (ly:pitch-diff (car pitches1)
314                                                                    (car pitches2))))
315                                ))
316                          (put 'apart)
317                          ;; copy previous split state from spanner state
318                          (begin
319                            (if (previous-voice-state vs1)
320                                (copy-state-from voice-state-vec1
321                                                 (previous-voice-state vs1)))
322                            (if (previous-voice-state vs2)
323                                (copy-state-from voice-state-vec2
324                                                 (previous-voice-state vs2)))
325                            (if (and (null? (span-state vs1)) (null? (span-state vs2)))
326                                (put 'chords)))))))))
327       
328       (if (< result-idx (vector-length result))
329           (let* ((now-state (vector-ref result result-idx))
330                  (vs1 (car (voice-states now-state)))
331                  (vs2 (cdr (voice-states now-state))))
332             
333             (cond ((not vs1) (put 'apart))
334                   ((not vs2) (put 'apart))
335                   (else
336                    (let ((active1 (previous-span-state vs1))
337                          (active2 (previous-span-state vs2))
338                          (new-active1 (span-state vs1))
339                          (new-active2 (span-state vs2)))
340                      (if #f ; debug
341                          (display (list (when now-state) result-idx
342                                         active1 "->" new-active1
343                                         active2 "->" new-active2
344                                         "\n")))
345                      (if (and (synced? now-state)
346                               (equal? active1 active2)
347                               (equal? new-active1 new-active2))
348                          (analyse-notes now-state)
349                          
350                          ;; active states different:
351                          (put 'apart)))
352                    
353                    ;; go to the next one, if it exists.
354                    (analyse-time-step (1+ result-idx)))))))
355     
356     (define (analyse-a2 result-idx)
357       (if (< result-idx (vector-length result))
358           (let* ((now-state (vector-ref result result-idx))
359                  (vs1 (car (voice-states now-state)))
360                  (vs2 (cdr (voice-states now-state))))
361             (if (and (equal? (configuration now-state) 'chords)
362                      vs1 vs2)
363                 (let ((notes1 (note-events vs1)) 
364                       (notes2 (note-events vs2)))
365                   (cond ((and (= 1 (length notes1))
366                               (= 1 (length notes2))
367                               (equal? (ly:event-property (car notes1) 'pitch)
368                                       (ly:event-property (car notes2) 'pitch)))
369                          (set! (configuration now-state) 'unisono))
370                         ((and (= 0 (length notes1))
371                               (= 0 (length notes2)))
372                          (set! (configuration now-state) 'unisilence)))))
373             (analyse-a2 (1+ result-idx)))))
374     
375     (define (analyse-solo12 result-idx)
376       
377       (define (previous-config vs)
378         (let* ((pvs (previous-voice-state vs))
379                (spi (if pvs (split-index pvs) #f))
380                (prev-split (if spi (vector-ref result spi) #f)))
381           (if prev-split
382               (configuration prev-split)
383               'apart)))
384       
385       (define (put-range x a b)
386         ;; (display (list "put range "  x a b "\n"))
387         (do ((i a (1+ i)))
388             ((> i b) b)
389           (set! (configuration (vector-ref result i)) x)))
390       
391       (define (put x)
392         ;; (display (list "putting "  x "\n"))
393         (set! (configuration (vector-ref result result-idx)) x))
394       
395       (define (current-voice-state now-state voice-num)
396         (define vs ((if (= 1 voice-num) car cdr)
397                     (voice-states now-state)))
398         (if (or (not vs) (equal? (when now-state) (when vs)))
399             vs
400             (previous-voice-state vs)))
401       
402       (define (try-solo type start-idx current-idx)
403         "Find a maximum stretch that can be marked as solo. Only set
404 the mark when there are no spanners active.
405
406       return next idx to analyse.
407 "
408         (if (< current-idx (vector-length result))
409             (let* ((now-state (vector-ref result current-idx))
410                    (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
411                    (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
412                    (silent-notes (if silent-state (note-events silent-state) '()))
413                    (solo-notes (if solo-state (note-events solo-state) '())))
414               ;; (display (list "trying " type " at "  (when now-state) solo-state silent-state  "\n"))
415               (cond ((not (equal? (configuration now-state) 'apart))
416                      current-idx)
417                     ((> (length silent-notes) 0) start-idx)
418                     ((not solo-state)
419                      (put-range type start-idx current-idx)
420                      current-idx)
421                     ((and
422                       (null? (span-state solo-state)))
423
424                      ;;
425                      ;; This includes rests. This isn't a problem: long rests
426                      ;; will be shared with the silent voice, and be marked
427                      ;; as unisilence. Therefore, long rests won't 
428                      ;;  accidentally be part of a solo.
429                      ;;
430                      (put-range type start-idx current-idx)
431                      (try-solo type (1+ current-idx) (1+  current-idx)))
432                     (else
433                      (try-solo type start-idx (1+ current-idx)))))
434             ;; try-solo
435             start-idx))
436       
437       (define (analyse-moment result-idx)
438         "Analyse 'apart starting at RESULT-IDX. Return next index. "
439         (let* ((now-state (vector-ref result result-idx))
440                (vs1 (current-voice-state now-state 1))
441                (vs2 (current-voice-state now-state 2))
442                ;; (vs1 (car (voice-states now-state)))
443                ;; (vs2 (cdr (voice-states now-state)))
444                (notes1 (if vs1 (note-events vs1) '()))
445                (notes2 (if vs2 (note-events vs2) '()))
446                (n1 (length notes1))
447                (n2 (length notes2)))
448           ;; (display (list "analyzing step " result-idx "  moment " (when now-state) vs1 vs2  "\n"))
449           (max
450            ;; we should always increase.
451            (cond ((and (= n1 0) (= n2 0))
452                   (put 'apart-silence)
453                   (1+ result-idx))
454                  ((and (= n2 0)
455                        (equal? (when vs1) (when now-state))
456                        (null? (previous-span-state vs1)))
457                   (try-solo 'solo1 result-idx result-idx))
458                  ((and (= n1 0)
459                        (equal? (when vs2) (when now-state))
460                        (null? (previous-span-state vs2)))
461                   (try-solo 'solo2 result-idx result-idx))
462                  
463                  (else (1+ result-idx)))
464            ;; analyse-moment
465            (1+ result-idx))))
466       
467       (if (< result-idx (vector-length result))
468           (if (equal? (configuration (vector-ref result result-idx)) 'apart)
469               (analyse-solo12 (analyse-moment result-idx))
470               (analyse-solo12 (1+ result-idx))))) ; analyse-solo12
471
472     (analyse-spanner-states voice-state-vec1)
473     (analyse-spanner-states voice-state-vec2)
474     (if #f
475         (begin
476           (display voice-state-vec1)
477           (display "***\n")
478           (display voice-state-vec2)
479           (display "***\n")
480           (display result)
481           (display "***\n")))
482     (analyse-time-step 0)
483     ;; (display result)
484     (analyse-a2 0)
485     ;;(display result)
486     (analyse-solo12 0)
487     ;; (display result)
488     (set! result (map
489                   (lambda (x) (cons (when x) (configuration x)))
490                   (vector->list result)))
491     (if #f ;; pc-debug
492          (display result))
493     result))
494
495
496 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
497
498 (define-public (add-quotable name mus)
499   (set! noticed '())
500   (let* ((tab (eval 'musicQuotes (current-module)))
501          (context-list (recording-group-emulate (context-spec-music mus 'Voice)
502                                               part-combine-listener)))
503     (if (pair? context-list)
504         (hash-set! tab name
505                    ;; cdr : skip name string
506                    (list->vector (reverse! (cdar context-list)
507                                            '()))))))