1 ;;;; part-combiner.scm -- Part combining, staff changes.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2009 Han-Wen Nienhuys <hanwen@xs4all.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:event-property x 'class) 'note-event))
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* ((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))
93 (inc1 (if (and state1 (equal? min (when state1))) 1 0))
94 (inc2 (if (and state2 (equal? min (when state2))) 1 0))
98 #:voice-states (cons state1 state2)
99 #:synced (= inc1 inc2))
102 (set! (split-index state1) ss-idx))
104 (set! (split-index state2) ss-idx))
107 (cons ss-object ss-list)
111 (list->vector (reverse! (helper 0 '() 0 0) '())))
113 (define (analyse-spanner-states voice-state-vec)
115 (define (helper index active)
116 "Analyse EVS at INDEX, given state ACTIVE."
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))
124 (define (analyse-tie-end active ev)
125 (if (equal? (ly:event-property ev 'class) 'note-event)
126 (assoc-remove! active 'tie)
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)
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)))))
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)
149 (sp (ly:event-property ev 'span-direction)))
150 (if (and (symbol? key) (ly:dir? sp))
152 (assoc-remove! active key)
154 (split-index (vector-ref voice-state-vec index))
158 (define (analyse-events active evs)
159 "Run all analyzers on ACTIVE and EVS"
160 (define (run-analyzer analyzer active evs)
162 (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
164 (define (run-analyzers analyzers active evs)
165 (if (pair? analyzers)
166 (run-analyzers (cdr analyzers)
167 (run-analyzer (car analyzers) active evs)
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)
177 ;; must copy, since we use assoc-remove!
178 (if (< index (vector-length voice-state-vec))
180 (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
181 (set! (span-state (vector-ref voice-state-vec index))
183 (helper (1+ index) active))))
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 (define-public (recording-group-emulate music odef)
189 "Interprets music according to odef, but stores all events in a chronological
190 list, similar to the Recording_group_engraver in 2.8 and earlier"
193 (now-mom (ly:make-moment 0 0))
194 (global (ly:make-global-context odef))
195 (mom-listener (ly:make-listener
196 (lambda (tev) (set! now-mom (ly:event-property tev 'moment)))))
197 (new-context-listener
201 ((child (ly:event-property sev 'context))
202 (this-moment-list (cons (ly:context-id child) '()))
203 (dummy (set! context-list (cons this-moment-list context-list)))
205 (accumulate-event-listener
206 (ly:make-listener (lambda (ev)
207 (set! acc (cons (cons ev #t) acc)))))
209 (ly:make-listener (lambda (tev)
213 (ly:context-property child 'instrumentTransposition))
214 ;; The accumulate-event-listener above creates
215 ;; the list of events in reverse order, so we
216 ;; have to revert it to the original order again
218 (set-cdr! this-moment-list
219 (cons this-moment (cdr this-moment-list)))
221 (ly:add-listener accumulate-event-listener
222 (ly:context-event-source child) 'StreamEvent)
223 (ly:add-listener save-acc-listener
224 (ly:context-event-source global) 'OneTimeStep))))))
225 (ly:add-listener new-context-listener
226 (ly:context-events-below global) 'AnnounceNewContext)
227 (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare)
228 (ly:interpret-music-expression (make-non-relative-music music) global)
231 (define-public (make-part-combine-music parser music-list)
232 (let* ((m (make-music 'PartCombineMusic))
233 (m1 (make-non-relative-music (context-spec-music (first music-list) 'Voice "one")))
234 (m2 (make-non-relative-music (context-spec-music (second music-list) 'Voice "two")))
235 (listener (ly:parser-lookup parser 'partCombineListener))
236 (evs2 (recording-group-emulate m2 listener))
237 (evs1 (recording-group-emulate m1 listener)))
239 (set! (ly:music-property m 'elements) (list m1 m2))
240 (set! (ly:music-property m 'split-list)
241 (if (and (assoc "one" evs1) (assoc "two" evs2))
242 (determine-split-list (reverse! (assoc-get "one" evs1) '())
243 (reverse! (assoc-get "two" evs2) '()))
247 (define-public (determine-split-list evl1 evl2)
248 "EVL1 and EVL2 should be ascending"
251 (voice-state-vec1 (make-voice-states evl1))
252 (voice-state-vec2 (make-voice-states evl2))
253 (result (make-split-state voice-state-vec1 voice-state-vec2)))
255 (define (analyse-time-step result-idx)
256 (define (put x . index)
257 "Put the result to X, starting from INDEX backwards.
259 Only set if not set previously.
261 (let ((i (if (pair? index) (car index) result-idx)))
263 (not (symbol? (configuration (vector-ref result i)))))
265 (set! (configuration (vector-ref result i)) x)
268 (define (copy-state-from state-vec vs)
269 (define (copy-one-state key-idx)
270 (let* ((idx (cdr key-idx))
271 (prev-ss (vector-ref result idx))
272 (prev (configuration prev-ss)))
275 (map copy-one-state (span-state vs)))
277 (define (analyse-notes now-state)
278 (let* ((vs1 (car (voice-states now-state)))
279 (vs2 (cdr (voice-states now-state)))
280 (notes1 (note-events vs1))
281 (durs1 (sort (map (lambda (x) (ly:event-property x 'duration))
284 (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
287 (notes2 (note-events vs2))
288 (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
291 (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
294 (cond ((> (length notes1) 1) (put 'apart))
295 ((> (length notes2) 1) (put 'apart))
296 ((= 1 (+ (length notes2) (length notes1))) (put 'apart))
297 ((and (= (length durs1) 1)
299 (not (equal? (car durs1) (car durs2))))
302 (if (and (= (length pitches1) (length pitches2)))
303 (if (and (pair? pitches1)
306 (< chord-threshold (ly:pitch-steps
307 (ly:pitch-diff (car pitches1)
311 (> 0 (ly:pitch-steps (ly:pitch-diff (car pitches1)
315 ;; copy previous split state from spanner state
317 (if (previous-voice-state vs1)
318 (copy-state-from voice-state-vec1
319 (previous-voice-state vs1)))
320 (if (previous-voice-state vs2)
321 (copy-state-from voice-state-vec2
322 (previous-voice-state vs2)))
323 (if (and (null? (span-state vs1)) (null? (span-state vs2)))
324 (put 'chords)))))))))
326 (if (< result-idx (vector-length result))
327 (let* ((now-state (vector-ref result result-idx))
328 (vs1 (car (voice-states now-state)))
329 (vs2 (cdr (voice-states now-state))))
331 (cond ((not vs1) (put 'apart))
332 ((not vs2) (put 'apart))
334 (let ((active1 (previous-span-state vs1))
335 (active2 (previous-span-state vs2))
336 (new-active1 (span-state vs1))
337 (new-active2 (span-state vs2)))
339 (display (list (when now-state) result-idx
340 active1 "->" new-active1
341 active2 "->" new-active2
343 (if (and (synced? now-state)
344 (equal? active1 active2)
345 (equal? new-active1 new-active2))
346 (analyse-notes now-state)
348 ;; active states different:
351 ;; go to the next one, if it exists.
352 (analyse-time-step (1+ result-idx)))))))
354 (define (analyse-a2 result-idx)
355 (if (< result-idx (vector-length result))
356 (let* ((now-state (vector-ref result result-idx))
357 (vs1 (car (voice-states now-state)))
358 (vs2 (cdr (voice-states now-state))))
359 (if (and (equal? (configuration now-state) 'chords)
361 (let ((notes1 (note-events vs1))
362 (notes2 (note-events vs2)))
363 (cond ((and (= 1 (length notes1))
364 (= 1 (length notes2))
365 (equal? (ly:event-property (car notes1) 'pitch)
366 (ly:event-property (car notes2) 'pitch)))
367 (set! (configuration now-state) 'unisono))
368 ((and (= 0 (length notes1))
369 (= 0 (length notes2)))
370 (set! (configuration now-state) 'unisilence)))))
371 (analyse-a2 (1+ result-idx)))))
373 (define (analyse-solo12 result-idx)
375 (define (previous-config vs)
376 (let* ((pvs (previous-voice-state vs))
377 (spi (if pvs (split-index pvs) #f))
378 (prev-split (if spi (vector-ref result spi) #f)))
380 (configuration prev-split)
383 (define (put-range x a b)
384 ;; (display (list "put range " x a b "\n"))
387 (set! (configuration (vector-ref result i)) x)))
390 ;; (display (list "putting " x "\n"))
391 (set! (configuration (vector-ref result result-idx)) x))
393 (define (current-voice-state now-state voice-num)
394 (define vs ((if (= 1 voice-num) car cdr)
395 (voice-states now-state)))
396 (if (or (not vs) (equal? (when now-state) (when vs)))
398 (previous-voice-state vs)))
400 (define (try-solo type start-idx current-idx)
401 "Find a maximum stretch that can be marked as solo. Only set
402 the mark when there are no spanners active.
404 return next idx to analyse.
406 (if (< current-idx (vector-length result))
407 (let* ((now-state (vector-ref result current-idx))
408 (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
409 (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
410 (silent-notes (if silent-state (note-events silent-state) '()))
411 (solo-notes (if solo-state (note-events solo-state) '())))
412 ;; (display (list "trying " type " at " (when now-state) solo-state silent-state "\n"))
413 (cond ((not (equal? (configuration now-state) 'apart))
415 ((> (length silent-notes) 0) start-idx)
417 (put-range type start-idx current-idx)
420 (null? (span-state solo-state)))
423 ;; This includes rests. This isn't a problem: long rests
424 ;; will be shared with the silent voice, and be marked
425 ;; as unisilence. Therefore, long rests won't
426 ;; accidentally be part of a solo.
428 (put-range type start-idx current-idx)
429 (try-solo type (1+ current-idx) (1+ current-idx)))
431 (try-solo type start-idx (1+ current-idx)))))
435 (define (analyse-moment result-idx)
436 "Analyse 'apart starting at RESULT-IDX. Return next index. "
437 (let* ((now-state (vector-ref result result-idx))
438 (vs1 (current-voice-state now-state 1))
439 (vs2 (current-voice-state now-state 2))
440 ;; (vs1 (car (voice-states now-state)))
441 ;; (vs2 (cdr (voice-states now-state)))
442 (notes1 (if vs1 (note-events vs1) '()))
443 (notes2 (if vs2 (note-events vs2) '()))
445 (n2 (length notes2)))
446 ;; (display (list "analyzing step " result-idx " moment " (when now-state) vs1 vs2 "\n"))
448 ;; we should always increase.
449 (cond ((and (= n1 0) (= n2 0))
453 (equal? (when vs1) (when now-state))
454 (null? (previous-span-state vs1)))
455 (try-solo 'solo1 result-idx result-idx))
457 (equal? (when vs2) (when now-state))
458 (null? (previous-span-state vs2)))
459 (try-solo 'solo2 result-idx result-idx))
461 (else (1+ result-idx)))
465 (if (< result-idx (vector-length result))
466 (if (equal? (configuration (vector-ref result result-idx)) 'apart)
467 (analyse-solo12 (analyse-moment result-idx))
468 (analyse-solo12 (1+ result-idx))))) ; analyse-solo12
470 (analyse-spanner-states voice-state-vec1)
471 (analyse-spanner-states voice-state-vec2)
474 (display voice-state-vec1)
476 (display voice-state-vec2)
480 (analyse-time-step 0)
487 (lambda (x) (cons (when x) (configuration x)))
488 (vector->list result)))
494 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496 (define-public (add-quotable parser name mus)
497 (let* ((tab (eval 'musicQuotes (current-module)))
498 (context-list (recording-group-emulate (context-spec-music mus 'Voice)
499 (ly:parser-lookup parser 'partCombineListener))))
500 (if (pair? context-list)
502 ;; cdr : skip name string
503 (list->vector (reverse! (cdar context-list)