1 ;;;; part-combiner.scm -- Part combining, staff changes.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 ;; todo: figure out how to make module,
8 ;; without breaking nested ly scopes
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)
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) )
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)
31 (define-method (note-events (vs <Voice-state>))
33 (equal? (ly:music-property x 'name) 'NoteEvent))
34 (filter f? (events vs)))
36 (define-method (previous-voice-state (vs <Voice-state>))
37 (let ((i (slot-ref vs 'vector-index))
38 (v (slot-ref vs 'state-vector)) )
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
50 (is #:init-keyword #:voice-states #:accessor voice-states)
51 (synced #:init-keyword #:synced #:init-value #f #:getter synced?))
54 (define-method (write (x <Split-state> ) f)
57 (display (configuration x) f)
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 (define (previous-span-state vs)
66 (let ((p (previous-voice-state vs)))
67 (if p (span-state p) '())))
69 (define (make-voice-states evl)
70 (let ((vec (list->vector (map (lambda (v)
74 #:events (map car (cdr v))))
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))))
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
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)))
93 (inc1 (if (and s1 (equal? min (when s1))) 1 0))
94 (inc2 (if (and s2 (equal? min (when s2))) 1 0))
98 #:voice-states (cons s1 s2)
99 #:synced (= inc1 inc2))
102 (set! (split-index s1) ss-idx))
104 (set! (split-index s2) ss-idx))
107 (cons ss-object ss-list)
111 (list->vector (reverse! (helper 0 '() 0 0) '())))
114 (define (analyse-spanner-states voice-state-vec)
116 (define (helper index active)
117 "Analyse EVS at INDEX, given state ACTIVE."
119 (define (analyse-tie-start active ev)
120 (if (equal? (ly:music-property ev 'name) 'TieEvent)
121 (acons 'tie (split-index (vector-ref voice-state-vec index))
125 (define (analyse-tie-end active ev)
126 (if (equal? (ly:music-property ev 'name) 'NoteEvent)
127 (assoc-remove! active 'tie)
130 (define (analyse-absdyn-end active ev)
131 (if (equal? (ly:music-property ev 'name) 'AbsoluteDynamicEvent)
132 (assoc-remove! (assoc-remove! active 'cresc) 'decr)
135 (define (active<? a b)
136 (cond ((symbol<? (car a) (car b)) #t)
137 ((symbol<? (car b) (car b)) #f)
138 (else (< (cdr a) (cdr b)))))
140 (define (analyse-span-event active ev)
141 (let* ((name (ly:music-property ev 'name))
142 (key (cond ((equal? name 'SlurEvent) 'slur)
143 ((equal? name 'PhrasingSlurEvent) 'tie)
144 ((equal? name 'BeamEvent) 'beam)
145 ((equal? name 'CrescendoEvent) 'cresc)
146 ((equal? name 'DecrescendoEvent) 'decr)
148 (sp (ly:music-property ev 'span-direction)))
149 (if (and (symbol? key) (ly:dir? sp))
151 (assoc-remove! active key)
153 (split-index (vector-ref voice-state-vec index))
157 (define (analyse-events active evs)
158 "Run all analyzers on ACTIVE and EVS"
159 (define (run-analyzer analyzer active evs)
161 (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
163 (define (run-analyzers analyzers active evs)
164 (if (pair? analyzers)
165 (run-analyzers (cdr analyzers)
166 (run-analyzer (car analyzers) active evs)
169 (sort ;; todo: use fold or somesuch.
170 (run-analyzers (list analyse-absdyn-end analyse-span-event
171 ;; note: tie-start/span comes after tie-end/absdyn.
172 analyse-tie-end analyse-tie-start)
176 ;; must copy, since we use assoc-remove!
177 (if (< index (vector-length voice-state-vec))
179 (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
180 (set! (span-state (vector-ref voice-state-vec index))
182 (helper (1+ index) active))))
189 (define part-combine-listener '())
190 (define-public (set-part-combine-listener x)
191 (set! part-combine-listener x))
193 (define-public (notice-the-events-for-pc context lst)
194 (set! noticed (acons (ly:context-id context) lst noticed)))
196 (define-public (make-part-combine-music music-list)
197 (let ((m (make-music 'PartCombineMusic))
198 (m1 (make-non-relative-music (context-spec-music (car music-list) 'Voice "one")))
199 (m2 (make-non-relative-music (context-spec-music (cadr music-list) 'Voice "two"))))
200 (set! (ly:music-property m 'elements) (list m1 m2))
201 (ly:run-translator m2 part-combine-listener)
202 (ly:run-translator m1 part-combine-listener)
203 (set! (ly:music-property m 'split-list)
204 (determine-split-list (reverse! (cdr (assoc "one" noticed)) '())
205 (reverse! (cdr (assoc "two" noticed)) '())))
209 (define-public (determine-split-list evl1 evl2)
210 "EVL1 and EVL2 should be ascending"
213 (voice-state-vec1 (make-voice-states evl1))
214 (voice-state-vec2 (make-voice-states evl2))
215 (result (make-split-state voice-state-vec1 voice-state-vec2)))
217 (define (analyse-time-step ri)
218 (define (put x . index)
219 "Put the result to X, starting from INDEX backwards.
221 Only set if not set previously.
223 (let ((i (if (pair? index) (car index) ri)))
225 (not (symbol? (configuration (vector-ref result i)))))
227 (set! (configuration (vector-ref result i)) x)
230 (define (copy-state-from state-vec vs)
231 (define (copy-one-state key-idx)
232 (let* ((idx (cdr key-idx))
233 (prev-ss (vector-ref result idx))
234 (prev (configuration prev-ss)))
237 (map copy-one-state (span-state vs)))
239 (define (analyse-notes now-state)
240 (let* ((vs1 (car (voice-states now-state)))
241 (vs2 (cdr (voice-states now-state)))
242 (notes1 (note-events vs1))
243 (durs1 (sort (map (lambda (x) (ly:music-property x 'duration))
246 (pitches1 (sort (map (lambda (x) (ly:music-property x 'pitch))
249 (notes2 (note-events vs2))
250 (durs2 (sort (map (lambda (x) (ly:music-property x 'duration))
253 (pitches2 (sort (map (lambda (x) (ly:music-property x 'pitch))
256 (cond ((> (length notes1) 1) (put 'apart))
257 ((> (length notes2) 1) (put 'apart))
258 ((not (= (length notes1) (length notes2)))
260 ((and (= (length durs1) 1)
262 (not (equal? (car durs1) (car durs2))))
265 (if (and (= (length pitches1) (length pitches2)))
266 (if (and (pair? pitches1)
269 (< chord-threshold (ly:pitch-steps
270 (ly:pitch-diff (car pitches1)
274 (> 0 (ly:pitch-steps (ly:pitch-diff (car pitches1)
278 ;; copy previous split state from spanner state
280 (if (previous-voice-state vs1)
281 (copy-state-from voice-state-vec1
282 (previous-voice-state vs1)))
283 (if (previous-voice-state vs2)
284 (copy-state-from voice-state-vec2
285 (previous-voice-state vs2)))
286 (if (and (null? (span-state vs1)) (null? (span-state vs2)))
287 (put 'chords)))))))))
289 (if (< ri (vector-length result))
290 (let* ((now-state (vector-ref result ri))
291 (vs1 (car (voice-states now-state)))
292 (vs2 (cdr (voice-states now-state))))
295 (cond ((not vs1) (put 'apart))
296 ((not vs2) (put 'apart))
298 (let ((active1 (previous-span-state vs1))
299 (active2 (previous-span-state vs2))
300 (new-active1 (span-state vs1))
301 (new-active2 (span-state vs2)))
303 (display (list (when now-state) ri
304 active1 "->" new-active1
305 active2 "->" new-active2
307 (if (and (synced? now-state)
308 (equal? active1 active2)
309 (equal? new-active1 new-active2))
310 (analyse-notes now-state)
311 ;; active states different:
313 ;; go to the next one, if it exists.
314 (analyse-time-step (1+ ri)))))))
316 (define (analyse-a2 ri)
317 (if (< ri (vector-length result))
318 (let* ((now-state (vector-ref result ri))
319 (vs1 (car (voice-states now-state)))
320 (vs2 (cdr (voice-states now-state))))
321 (if (and (equal? (configuration now-state) 'chords)
323 (let ((notes1 (note-events vs1))
324 (notes2 (note-events vs2)))
325 (cond ((and (= 1 (length notes1))
326 (= 1 (length notes2))
327 (equal? (ly:music-property (car notes1) 'pitch)
328 (ly:music-property (car notes2) 'pitch)))
329 (set! (configuration now-state) 'unisono))
330 ((and (= 0 (length notes1))
331 (= 0 (length notes2)))
332 (set! (configuration now-state) 'unisilence)))))
333 (analyse-a2 (1+ ri)))))
335 (define (analyse-solo12 ri)
337 (define (previous-config vs)
338 (let* ((pvs (previous-voice-state vs))
339 (spi (if pvs (split-index pvs) #f))
340 (prev-split (if spi (vector-ref result spi) #f)))
342 (configuration prev-split)
345 (define (put-range x a b)
346 ;; (display (list "put range " x a b "\n"))
349 (set! (configuration (vector-ref result i)) x)))
352 ;; (display (list "putting " x "\n"))
353 (set! (configuration (vector-ref result ri)) x))
355 (define (current-voice-state now-state voice-num)
356 (define vs ((if (= 1 voice-num) car cdr)
357 (voice-states now-state)))
358 (if (or (not vs) (equal? (when now-state) (when vs)))
360 (previous-voice-state vs)))
362 (define (try-solo type start-idx current-idx)
363 "Find a maximum stretch that can be marked as solo. Only set
364 the mark when there are no spanners active."
365 (if (< current-idx (vector-length result))
366 (let* ((now-state (vector-ref result current-idx))
367 (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
368 (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
369 (silent-notes (if silent-state (note-events silent-state) '()))
370 (solo-notes (if solo-state (note-events solo-state) '()))
371 (soln (length solo-notes))
372 (siln (length silent-notes)))
373 ;; (display (list "trying " type " at " (when now-state) solo-state silent-state "\n"))
374 (cond ((not (equal? (configuration now-state) 'apart))
376 ((> siln 0) start-idx)
378 (put-range type start-idx current-idx)
381 (null? (span-state solo-state)))
383 ;; This includes rests. This isn't a problem: long rests
384 ;; will be shared with the silent voice, and be marked
385 ;; as unisilence. Therefore, long rests won't
386 ;; accidentally be part of a solo.
388 (put-range type start-idx current-idx)
389 (try-solo type (1+ current-idx) (1+ current-idx)))
391 (try-solo type start-idx (1+ current-idx)))))
392 start-idx)) ; try-solo
394 (define (analyse-moment ri)
395 "Analyse 'apart starting at RI. Return next index. "
396 (let* ((now-state (vector-ref result ri))
397 (vs1 (current-voice-state now-state 1))
398 (vs2 (current-voice-state now-state 2))
399 ;; (vs1 (car (voice-states now-state)))
400 ;; (vs2 (cdr (voice-states now-state)))
401 (notes1 (if vs1 (note-events vs1) '()))
402 (notes2 (if vs2 (note-events vs2) '()))
404 (n2 (length notes2)))
405 ;; (display (list "analyzing step " ri " moment " (when now-state) vs1 vs2 "\n"))
406 (max ; we should always increase.
407 (cond ((and (= n1 0) (= n2 0))
411 (equal? (when vs1) (when now-state))
412 (null? (previous-span-state vs1)))
413 (try-solo 'solo1 ri ri))
415 (equal? (when vs2) (when now-state))
416 (null? (previous-span-state vs2)))
417 (try-solo 'solo2 ri ri))
419 (1+ ri)))) ; analyse-moment
421 (if (< ri (vector-length result))
422 (if (equal? (configuration (vector-ref result ri)) 'apart)
423 (analyse-solo12 (analyse-moment ri))
424 (analyse-solo12 (1+ ri))))) ; analyse-solo12
426 (analyse-spanner-states voice-state-vec1)
427 (analyse-spanner-states voice-state-vec2)
430 (display voice-state-vec1)
432 (display voice-state-vec2)
436 (analyse-time-step 0)
443 (lambda (x) (cons (when x) (configuration x)))
444 (vector->list result)))
445 ;; (if pc-debug (display result))
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;; autochange - fairly related to part combining.
451 (define-public (make-autochange-music music)
452 (define (generate-split-list change-moment event-list acc)
453 (if (null? event-list)
455 (let* ((now-tun (caar event-list))
456 (evs (map car (cdar event-list)))
458 (notes (filter (lambda (x)
459 (equal? (ly:music-property x 'name) 'NoteEvent))
461 (pitch (if (pair? notes)
462 (ly:music-property (car notes) 'pitch)
465 (if (and pitch (not (= (ly:pitch-steps pitch) 0)))
466 (generate-split-list #f
473 (sign (ly:pitch-steps pitch))) acc))
476 (cdr event-list) acc)))))
479 (let* ((m (make-music 'AutoChangeMusic))
480 (context (ly:run-translator (make-non-relative-music music) part-combine-listener))
481 (evs (last-pair noticed))
482 (split (reverse! (generate-split-list
485 (reverse! (cdar evs) '()) '())
488 (set! (ly:music-property m 'element) music)
489 (set! (ly:music-property m 'split-list) split)
494 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496 (define-public (add-quotable name mus)
498 (let* ((tab (eval 'musicQuotes (current-module) ))
499 (context (ly:run-translator (context-spec-music mus 'Voice)
500 part-combine-listener))
501 (evs (last-pair noticed)))
504 (list->vector (reverse! (car evs) '()))))))