]> git.donarmstrong.com Git - lilypond.git/blob - scm/part-combiner.scm
* scm/part-combiner.scm (make-part-combine-music): don't do
[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  Han-Wen Nienhuys <hanwen@cs.uu.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:music-property  x 'name) 'NoteEvent))
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* ((s1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f))
88            (s2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f))
89            (min (cond ((and s1 s2) (moment-min (when s1) (when s2)))
90                       (s1 (when s1))
91                       (s2 (when s2))
92                       (else #f)))
93            (inc1 (if (and s1 (equal? min (when s1))) 1 0))
94            (inc2 (if (and s2 (equal? min (when s2))) 1 0))
95            (ss-object (if min
96                           (make <Split-state>
97                             #:when min
98                             #:voice-states (cons s1 s2)
99                             #:synced (= inc1 inc2))
100                           #f)))
101       (if s1
102           (set! (split-index s1) ss-idx))
103       (if s2
104           (set! (split-index s2) 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
114 (define (analyse-spanner-states voice-state-vec)
115
116   (define (helper index active)
117     "Analyse EVS at INDEX, given state ACTIVE."
118     
119     (define (analyse-tie-start active ev)
120       (if (equal? (ly:music-property ev 'name) 'TieEvent)
121           (acons 'tie index active)
122           active))
123     
124     (define (analyse-tie-end active ev)
125       (if (equal? (ly:music-property ev 'name) 'NoteEvent)
126           (assoc-remove! active 'tie)
127           active))
128
129     (define (analyse-absdyn-end active ev)
130       (if (equal? (ly:music-property ev 'name) 'AbsoluteDynamicEvent)
131           (assoc-remove! (assoc-remove! active 'cresc) 'decr)
132           active))
133     
134     (define (active<? a b)
135       (cond ((symbol<? (car a) (car b)) #t)
136             ((symbol<? (car b) (car b)) #f)
137             (else (< (cdr a) (cdr b)))))
138     
139     (define (analyse-span-event active ev)
140       (let* ((name (ly:music-property ev 'name))
141              (key (cond ((equal? name 'SlurEvent) 'slur)
142                         ((equal? name 'PhrasingSlurEvent) 'tie)
143                         ((equal? name 'BeamEvent) 'beam)
144                         ((equal? name 'CrescendoEvent) 'cresc)
145                         ((equal? name 'DecrescendoEvent) 'decr)
146                         (else #f)))
147              (sp (ly:music-property ev 'span-direction)))
148         (if (and (symbol? key) (ly:dir? sp))
149             (if (= sp STOP)
150                 (assoc-remove! active key)
151                 (acons key
152                        (split-index (vector-ref voice-state-vec index))
153                        active))
154             active)))
155
156     (define (analyse-events active evs)
157       "Run all analyzers on ACTIVE and EVS"
158       (define (run-analyzer analyzer active evs)
159         (if (pair? evs)
160             (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
161             active))
162       (define (run-analyzers analyzers active evs)
163         (if (pair? analyzers)
164             (run-analyzers (cdr analyzers)
165                            (run-analyzer (car analyzers) active evs)
166                            evs)
167             active))
168       (sort ;; todo: use fold or somesuch.
169        (run-analyzers (list analyse-absdyn-end analyse-span-event
170                             ;; note: tie-start/span comes after tie-end/absdyn.
171                             analyse-tie-end analyse-tie-start)
172                       active evs)
173        active<?))
174
175     ;; must copy, since we use assoc-remove!
176     (if (< index (vector-length voice-state-vec))
177         (begin
178           (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
179           (set! (span-state (vector-ref voice-state-vec index))
180                 (list-copy active))
181           (helper (1+ index) active))))
182   
183   (helper 0 '()))
184
185
186         
187 (define noticed '())
188 (define part-combine-listener '())
189 (define-public (set-part-combine-listener x)
190   (set! part-combine-listener x))
191
192 (define-public (notice-the-events-for-pc context lst)
193   (set! noticed (acons (ly:context-id context) lst noticed)))
194
195 (define-public (make-part-combine-music music-list)
196   (let ((m (make-music 'PartCombineMusic))
197         (m1 (make-non-relative-music (context-spec-music (car music-list) 'Voice "one")))
198         (m2  (make-non-relative-music  (context-spec-music (cadr music-list) 'Voice "two"))))
199     (set! (ly:music-property m 'elements) (list m1 m2))
200     (ly:run-translator m2 part-combine-listener)
201     (ly:run-translator m1 part-combine-listener)
202     (set! (ly:music-property m 'split-list)
203           (determine-split-list (reverse! (cdr (assoc "one" noticed)) '())
204                                 (reverse! (cdr (assoc "two" noticed)) '())))
205     (set! noticed '())
206     m))
207
208 (define-public (determine-split-list evl1 evl2)
209   "EVL1 and EVL2 should be ascending"
210   (let* ((pc-debug #f)
211          (chord-threshold 8)
212          (voice-state-vec1 (make-voice-states evl1))
213          (voice-state-vec2 (make-voice-states evl2))
214          (result (make-split-state voice-state-vec1 voice-state-vec2)))
215     
216     (define (analyse-time-step ri)
217       (define (put x . index)
218         "Put the result to X, starting from INDEX backwards.
219
220 Only set if not set previously.
221 "
222         (let ((i (if (pair? index) (car index) ri)))
223           (if (and (<= 0 i)
224                    (not (symbol? (configuration (vector-ref result i)))))
225               (begin
226                 (set! (configuration (vector-ref result i)) x)
227                 (put x (1- i))))))
228       
229       (define (copy-state-from state-vec vs)
230         (define (copy-one-state key-idx)
231           (let* ((idx (cdr key-idx))
232                  (prev-ss (vector-ref result idx))
233                  (prev (configuration prev-ss)))
234             (if (symbol? prev)
235                 (put prev))))
236         (map copy-one-state (span-state vs)))
237
238       (define (analyse-notes now-state) 
239         (let* ((vs1 (car (voice-states now-state)))
240                (vs2 (cdr (voice-states now-state)))
241                (notes1 (note-events vs1))
242                (durs1    (sort (map (lambda (x) (ly:music-property x 'duration))
243                                     notes1)
244                                ly:duration<?))
245                (pitches1 (sort (map (lambda (x) (ly:music-property x 'pitch))
246                                     notes1)
247                                ly:pitch<?))
248                (notes2   (note-events vs2))
249                (durs2    (sort (map (lambda (x) (ly:music-property x 'duration))
250                                     notes2)
251                                ly:duration<?))
252                (pitches2 (sort (map (lambda (x) (ly:music-property x 'pitch))
253                                     notes2)
254                                ly:pitch<?)))
255           (cond ((> (length notes1) 1) (put 'apart))
256                 ((> (length notes2) 1) (put 'apart))
257                 ((not (= (length notes1) (length notes2)))
258                  (put 'apart))
259                 ((and (= (length durs1) 1)
260                       (= (length durs2) 1)
261                       (not (equal? (car durs1) (car durs2))))
262                  (put 'apart))
263                 (else
264                  (if (and (= (length pitches1) (length pitches2)))
265                      (if (and (pair? pitches1)
266                               (pair? pitches2)
267                               (< chord-threshold (ly:pitch-steps
268                                                   (ly:pitch-diff (car pitches1) (car pitches2)))))
269                          (put 'apart)
270                          ;; copy previous split state from spanner state
271                          (begin
272                            (if (previous-voice-state vs1)
273                                (copy-state-from voice-state-vec1
274                                                 (previous-voice-state vs1)))
275                            (if (previous-voice-state vs2)
276                                (copy-state-from voice-state-vec2
277                                                 (previous-voice-state vs2)))
278                            (if (and (null? (span-state vs1)) (null? (span-state vs2)))
279                                (put 'chords)))))))))
280       
281       (if (< ri (vector-length result))
282           (let* ((now-state (vector-ref result ri))
283                  (vs1 (car (voice-states now-state)))
284                  (vs2 (cdr (voice-states now-state))))
285             (cond ((not vs1) (put 'apart))
286                   ((not vs2) (put 'apart))
287                   (else
288                    (let ((active1 (previous-span-state vs1))
289                          (active2 (previous-span-state vs2))
290                          (new-active1 (span-state vs1))
291                          (new-active2 (span-state vs2)))
292                      (if pc-debug
293                          (display (list (when now-state) ri
294                                         active1 "->" new-active1
295                                         active2 "->" new-active2
296                                         "\n")))
297                      (if (and (synced? now-state)
298                               (equal? active1 active2)
299                               (equal? new-active1 new-active2))
300                     (analyse-notes now-state)
301                     ;; active states different:
302                     (put 'apart)))
303                    ;; go to the next one, if it exists.
304                    (analyse-time-step (1+ ri)))))))
305     
306     (define (analyse-a2 ri)
307       (if (< ri (vector-length result))
308           (let* ((now-state (vector-ref result ri))
309                  (vs1 (car (voice-states now-state)))
310                  (vs2 (cdr (voice-states now-state))))
311             (if (and (equal? (configuration now-state) 'chords)
312                      vs1 vs2)
313                 (let ((notes1 (note-events vs1)) 
314                       (notes2 (note-events vs2)))
315                   (cond ((and (= 1 (length notes1))
316                               (= 1 (length notes2))
317                               (equal? (ly:music-property (car notes1) 'pitch)
318                                       (ly:music-property (car notes2) 'pitch)))
319                          (set! (configuration now-state) 'unisono))
320                         ((and (= 0 (length notes1))
321                               (= 0 (length notes2)))
322                          (set! (configuration now-state) 'unisilence)))))
323             (analyse-a2 (1+ ri)))))
324     
325     (define (analyse-solo12 ri)
326       
327       (define (previous-config vs)
328         (let* ((pvs (previous-voice-state vs))
329                (spi (if pvs (split-index pvs) #f))
330                (prev-split (if spi (vector-ref result spi) #f)))
331           (if prev-split
332               (configuration prev-split)
333               'apart)))
334       
335       (define (put-range x a b)
336         ;; (display (list "put range "  x a b "\n"))
337         (do ((i a (1+ i)))
338             ((> i b) b)
339           (set! (configuration (vector-ref result i)) x)))
340       
341       (define (put x)
342         ;; (display (list "putting "  x "\n"))
343         (set! (configuration (vector-ref result ri)) x))
344       
345       (define (current-voice-state now-state voice-num)
346         (define vs ((if (= 1 voice-num) car cdr)
347                     (voice-states now-state)))
348         (if (or (not vs) (equal? (when now-state) (when vs)))
349             vs
350             (previous-voice-state vs)))
351       
352       (define (try-solo type start-idx current-idx)
353         "Find a maximum stretch that can be marked as solo. Only set
354 the mark when there are no spanners active."
355         (if (< current-idx (vector-length result))
356             (let* ((now-state (vector-ref result current-idx))
357                    (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
358                    (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
359                    (silent-notes (if silent-state (note-events silent-state) '()))
360                    (solo-notes (if solo-state (note-events solo-state) '()))
361                    (soln (length solo-notes))
362                    (siln (length silent-notes)))
363               ;; (display (list "trying " type " at "  (when now-state) solo-state silent-state  "\n"))
364               (cond ((not (equal? (configuration now-state) 'apart))
365                      current-idx)
366                     ((> siln 0) start-idx)
367                     ((and (null? (span-state solo-state)))
368                      ;;
369                      ;; This includes rests. This isn't a problem: long rests
370                      ;; will be shared with the silent voice, and be marked
371                      ;; as unisilence. Therefore, long rests won't 
372                      ;;  accidentally be part of a solo.
373                      ;;
374                      (put-range type start-idx current-idx)
375                      (try-solo type (1+ current-idx) (1+  current-idx)))
376                     (else
377                      (try-solo type start-idx (1+ current-idx)))))
378             start-idx)) ; try-solo
379       
380       (define (analyse-moment ri)
381         "Analyse 'apart starting at RI. Return next index. "
382         (let* ((now-state (vector-ref result ri))
383                (vs1 (current-voice-state now-state 1))
384                (vs2 (current-voice-state now-state 2))
385                ;; (vs1 (car (voice-states now-state)))
386                ;; (vs2 (cdr (voice-states now-state)))
387                (notes1 (if vs1 (note-events vs1) '()))
388                (notes2 (if vs2 (note-events vs2) '()))
389                (n1 (length notes1))
390                (n2 (length notes2)))
391           ;; (display (list "analyzing step " ri "  moment " (when now-state) vs1 vs2  "\n"))
392           (max                          ; we should always increase.
393            (cond ((and (= n1 0) (= n2 0))
394                   (put 'apart-silence)
395                   (1+ ri))
396                  ((and (= n2 0)
397                        (equal? (when vs1) (when now-state))
398                        (null? (previous-span-state vs1)))
399                   (try-solo 'solo1 ri ri))
400                  ((and (= n1 0)
401                        (equal? (when vs2) (when now-state))
402                        (null? (previous-span-state vs2)))
403                   (try-solo 'solo2 ri ri))
404                  (else (1+ ri)))
405            (1+ ri)))) ; analyse-moment
406       
407       (if (< ri (vector-length result))
408           (if (equal? (configuration (vector-ref result ri)) 'apart)
409               (analyse-solo12 (analyse-moment ri))
410               (analyse-solo12 (1+ ri))))) ; analyse-solo12
411
412     (analyse-spanner-states voice-state-vec1)
413     (analyse-spanner-states voice-state-vec2)
414     (if #f
415         (begin
416           (display voice-state-vec1)
417           (display "***\n")
418           (display voice-state-vec2)
419           (display "***\n")
420           (display result)
421           (display "***\n")))
422     (analyse-time-step 0)
423     ;; (display result)
424     (analyse-a2 0)
425     ;; (display result)
426     (analyse-solo12 0)
427     ;; (display result)
428     (set! result (map
429                   (lambda (x) (cons (when x) (configuration x)))
430                   (vector->list result)))
431     ;; (if pc-debug (display result))
432     result))
433
434 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
435 ;; autochange - fairly related to part combining.
436
437 (define-public (make-autochange-music music)
438   (define (generate-split-list change-moment event-list acc)
439     (if (null? event-list)
440         acc
441         (let* ((now-tun (caar event-list))
442                (evs (map car (cdar event-list)))
443                (now (car now-tun))
444                (notes (filter (lambda (x)
445                                 (equal? (ly:music-property  x 'name) 'NoteEvent))
446                               evs))
447                (pitch (if (pair? notes)
448                           (ly:music-property (car notes) 'pitch)
449                           #f)))
450           ;; tail recursive.
451           (if (and pitch (not (= (ly:pitch-steps pitch) 0)))
452               (generate-split-list #f
453                                    (cdr event-list)
454                                    (cons (cons
455
456                                           (if change-moment
457                                               change-moment
458                                               now)
459                                           (sign (ly:pitch-steps pitch))) acc))
460               (generate-split-list
461                (if pitch #f now)
462                (cdr event-list) acc)))))
463   
464   (set! noticed '())
465   (let* ((m (make-music 'AutoChangeMusic))
466          (context (ly:run-translator (make-non-relative-music music) part-combine-listener))
467          (evs (last-pair noticed))
468          (split (reverse! (generate-split-list
469                            #f
470                            (if (pair? evs)
471                                (reverse! (cdar evs) '()) '())
472                            '())
473                           '())))
474     (set! (ly:music-property m 'element) music)
475     (set! (ly:music-property m 'split-list) split)
476     (set! noticed '())
477     m))
478
479
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481
482 (define-public (add-quotable name mus)
483   (set! noticed '())
484   (let* ((tab (eval 'musicQuotes (current-module) ))
485          (context (ly:run-translator (context-spec-music mus 'Voice)
486                                      part-combine-listener))
487          (evs (last-pair noticed)))
488     (if (pair? evs)
489         (hash-set! tab name
490                    (list->vector (reverse! (car evs) '()))))))