1 %%% -*- Mode: Scheme -*-
3 %%% part-combiner.scm -- Part combining, staff changes.
5 %%% source file of the GNU LilyPond music typesetter
7 %%% (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 #(use-modules (oop goops))
9 #(define-class <Voice-state> ()
10 (event-list #:init-value '() #:accessor events #:init-keyword #:events)
11 (when-moment #:accessor when #:init-keyword #:when)
12 (tuning #:accessor tuning #:init-keyword #:tuning)
13 (split-index #:accessor split-index)
17 ;; spanner-state is an alist
18 ;; of (SYMBOL . RESULT-INDEX), which indicates where
19 ;; said spanner was started.
20 (spanner-state #:init-value '() #:accessor span-state))
22 #(define-method (write (x <Voice-state> ) file)
23 (display (when x) file)
24 (display " evs = " file)
25 (display (events x) file)
26 (display " active = " file)
27 (display (span-state x) file)
30 #(define-method (note-events (vs <Voice-state>))
32 (equal? (ly:event-property x 'class) 'note-event))
33 (filter f? (events vs)))
35 #(define-method (previous-voice-state (vs <Voice-state>))
36 (let ((i (slot-ref vs 'vector-index))
37 (v (slot-ref vs 'state-vector)))
42 %%%;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 #(define-class <Split-state> ()
45 (configuration #:init-value '() #:accessor configuration)
46 (when-moment #:accessor when #:init-keyword #:when)
47 ;; voice-states are states starting with the Split-state or later
49 (is #:init-keyword #:voice-states #:accessor voice-states)
50 (synced #:init-keyword #:synced #:init-value #f #:getter synced?))
53 #(define-method (write (x <Split-state> ) f)
56 (display (configuration x) f)
61 %%%;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64 #(define (previous-span-state vs)
65 (let ((p (previous-voice-state vs)))
66 (if p (span-state p) '())))
68 #(define (make-voice-states evl)
69 (let ((vec (list->vector (map (lambda (v)
73 #:events (map car (cdr v))))
76 ((= i (vector-length vec)) vec)
77 (slot-set! (vector-ref vec i) 'vector-index i)
78 (slot-set! (vector-ref vec i) 'state-vector vec))))
80 #(define (make-split-state vs1 vs2)
81 "Merge lists VS1 and VS2, containing Voice-state objects into vector
82 of Split-state objects, crosslinking the Split-state vector and
85 (define (helper ss-idx ss-list idx1 idx2)
86 (let* ((state1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f))
87 (state2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f))
88 (min (cond ((and state1 state2) (moment-min (when state1) (when state2)))
89 (state1 (when state1))
90 (state2 (when state2))
92 (inc1 (if (and state1 (equal? min (when state1))) 1 0))
93 (inc2 (if (and state2 (equal? min (when state2))) 1 0))
97 #:voice-states (cons state1 state2)
98 #:synced (= inc1 inc2))
101 (set! (split-index state1) ss-idx))
103 (set! (split-index state2) ss-idx))
106 (cons ss-object ss-list)
110 (list->vector (reverse! (helper 0 '() 0 0) '())))
112 #(define (analyse-spanner-states voice-state-vec)
114 (define (helper index active)
115 "Analyse EVS at INDEX, given state ACTIVE."
117 (define (analyse-tie-start active ev)
118 (if (equal? (ly:event-property ev 'class) 'tie-event)
119 (acons 'tie (split-index (vector-ref voice-state-vec index))
123 (define (analyse-tie-end active ev)
124 (if (equal? (ly:event-property ev 'class) 'note-event)
125 (assoc-remove! active 'tie)
128 (define (analyse-absdyn-end active ev)
129 (if (or (equal? (ly:event-property ev 'class) 'absolute-dynamic-event)
130 (and (equal? (ly:event-property ev 'class) 'crescendo-event)
131 (equal? STOP (ly:event-property ev 'span-direction))))
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:event-property ev 'class))
142 (key (cond ;((equal? name 'slur-event) 'slur)
143 ((equal? name 'phrasing-slur-event) 'tie)
144 ((equal? name 'beam-event) 'beam)
145 ((equal? name 'crescendo-event) 'cresc)
146 ((equal? name 'decrescendo-event) 'decr)
148 (sp (ly:event-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
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 list, similar to the Recording_group_engraver in 2.8 and earlier"
192 (now-mom (ly:make-moment 0 0))
193 (global (ly:make-global-context odef))
194 (mom-listener (ly:make-listener
196 (set! now-mom (ly:event-property tev 'moment)))))
197 (new-context-listener
201 ((child (ly:event-property sev 'context))
203 (cons (ly:context-id child) '()))
205 (set! context-list (cons this-moment-list context-list)))
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)
212 (let ((this-moment (cons (cons now-mom (ly:context-property child 'instrumentTransposition))
214 (set-cdr! this-moment-list (cons this-moment (cdr this-moment-list)))
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)
223 #(define-public (make-part-combine-music parser music-list)
224 (let* ((m (make-music 'PartCombineMusic))
225 (m1 (make-non-relative-music (context-spec-music (first music-list) 'Voice "one")))
226 (m2 (make-non-relative-music (context-spec-music (second music-list) 'Voice "two")))
227 (listener (ly:parser-lookup parser 'partCombineListener))
228 (evs2 (recording-group-emulate m2 listener))
229 (evs1 (recording-group-emulate m1 listener)))
231 (set! (ly:music-property m 'elements) (list m1 m2))
232 (set! (ly:music-property m 'split-list)
233 (if (and (assoc "one" evs1) (assoc "two" evs2))
234 (determine-split-list (reverse! (cdr (assoc "one" evs1)) '())
235 (reverse! (cdr (assoc "two" evs2)) '()))
239 #(define-public (determine-split-list evl1 evl2)
240 "EVL1 and EVL2 should be ascending"
243 (voice-state-vec1 (make-voice-states evl1))
244 (voice-state-vec2 (make-voice-states evl2))
245 (result (make-split-state voice-state-vec1 voice-state-vec2)))
247 (define (analyse-time-step result-idx)
248 (define (put x . index)
249 "Put the result to X, starting from INDEX backwards.
250 Only set if not set previously."
251 (let ((i (if (pair? index) (car index) result-idx)))
253 (not (symbol? (configuration (vector-ref result i)))))
255 (set! (configuration (vector-ref result i)) x)
258 (define (copy-state-from state-vec vs)
259 (define (copy-one-state key-idx)
260 (let* ((idx (cdr key-idx))
261 (prev-ss (vector-ref result idx))
262 (prev (configuration prev-ss)))
265 (map copy-one-state (span-state vs)))
267 (define (analyse-notes now-state)
268 (let* ((vs1 (car (voice-states now-state)))
269 (vs2 (cdr (voice-states now-state)))
270 (notes1 (note-events vs1))
271 (durs1 (sort (map (lambda (x) (ly:event-property x 'duration))
274 (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
277 (notes2 (note-events vs2))
278 (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
281 (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
284 (cond ((> (length notes1) 1) (put 'apart))
285 ((> (length notes2) 1) (put 'apart))
286 ((= 1 (+ (length notes2) (length notes1))) (put 'apart))
287 ((and (= (length durs1) 1)
289 (not (equal? (car durs1) (car durs2))))
292 (if (and (= (length pitches1) (length pitches2)))
293 (if (and (pair? pitches1)
297 (ly:pitch-diff (car pitches1)
300 ;; copy previous split state from spanner state
302 (if (previous-voice-state vs1)
303 (copy-state-from voice-state-vec1
304 (previous-voice-state vs1)))
305 (if (previous-voice-state vs2)
306 (copy-state-from voice-state-vec2
307 (previous-voice-state vs2)))
308 (if (and (null? (span-state vs1)) (null? (span-state vs2)))
309 (put 'chords)))))))))
310 (if (< result-idx (vector-length result))
311 (let* ((now-state (vector-ref result result-idx))
312 (vs1 (car (voice-states now-state)))
313 (vs2 (cdr (voice-states now-state))))
314 (cond ((not vs1) (put 'apart))
315 ((not vs2) (put 'apart))
317 (let ((active1 (previous-span-state vs1))
318 (active2 (previous-span-state vs2))
319 (new-active1 (span-state vs1))
320 (new-active2 (span-state vs2)))
321 (if (and (synced? now-state)
322 (equal? active1 active2)
323 (equal? new-active1 new-active2))
324 (analyse-notes now-state)
325 ;; active states different:
327 ;; go to the next one, if it exists.
328 (analyse-time-step (1+ result-idx)))))))
330 (define (analyse-a2 result-idx)
331 (if (< result-idx (vector-length result))
332 (let* ((now-state (vector-ref result result-idx))
333 (vs1 (car (voice-states now-state)))
334 (vs2 (cdr (voice-states now-state))))
335 (if (and (equal? (configuration now-state) 'chords)
337 (let ((notes1 (note-events vs1))
338 (notes2 (note-events vs2)))
339 (cond ((and (= 1 (length notes1))
340 (= 1 (length notes2))
341 (equal? (ly:event-property (car notes1) 'pitch)
342 (ly:event-property (car notes2) 'pitch)))
343 (set! (configuration now-state) 'unisono))
344 ((and (= 0 (length notes1))
345 (= 0 (length notes2)))
346 (set! (configuration now-state) 'unisilence)))))
347 (analyse-a2 (1+ result-idx)))))
349 (define (analyse-solo12 result-idx)
351 (define (previous-config vs)
352 (let* ((pvs (previous-voice-state vs))
353 (spi (if pvs (split-index pvs) #f))
354 (prev-split (if spi (vector-ref result spi) #f)))
356 (configuration prev-split)
359 (define (put-range x a b)
360 ;; (display (list "put range " x a b "\n"))
363 (set! (configuration (vector-ref result i)) x)))
366 ;; (display (list "putting " x "\n"))
367 (set! (configuration (vector-ref result result-idx)) x))
369 (define (current-voice-state now-state voice-num)
370 (define vs ((if (= 1 voice-num) car cdr)
371 (voice-states now-state)))
372 (if (or (not vs) (equal? (when now-state) (when vs)))
374 (previous-voice-state vs)))
376 (define (try-solo type start-idx current-idx)
377 "Find a maximum stretch that can be marked as solo. Only set
378 the mark when there are no spanners active.
380 return next idx to analyse.
382 (if (< current-idx (vector-length result))
383 (let* ((now-state (vector-ref result current-idx))
384 (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
385 (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
386 (silent-notes (if silent-state (note-events silent-state) '()))
387 (solo-notes (if solo-state (note-events solo-state) '())))
388 (cond ((not (equal? (configuration now-state) 'apart))
390 ((> (length silent-notes) 0) start-idx)
392 (put-range type start-idx current-idx)
395 (null? (span-state solo-state)))
397 ;; This includes rests. This isn't a problem: long rests
398 ;; will be shared with the silent voice, and be marked
399 ;; as unisilence. Therefore, long rests won't
400 ;; accidentally be part of a solo.
402 (put-range type start-idx current-idx)
403 (try-solo type (1+ current-idx) (1+ current-idx)))
405 (try-solo type start-idx (1+ current-idx)))))
409 (define (analyse-moment result-idx)
410 "Analyse 'apart starting at RESULT-IDX. Return next index. "
411 (let* ((now-state (vector-ref result result-idx))
412 (vs1 (current-voice-state now-state 1))
413 (vs2 (current-voice-state now-state 2))
414 (notes1 (if vs1 (note-events vs1) '()))
415 (notes2 (if vs2 (note-events vs2) '()))
417 (n2 (length notes2)))
419 ;; we should always increase.
420 (cond ((and (= n1 0) (= n2 0))
424 (equal? (when vs1) (when now-state))
425 (null? (previous-span-state vs1)))
426 (try-solo 'solo1 result-idx result-idx))
428 (equal? (when vs2) (when now-state))
429 (null? (previous-span-state vs2)))
430 (try-solo 'solo2 result-idx result-idx))
432 (else (1+ result-idx)))
436 (if (< result-idx (vector-length result))
437 (if (equal? (configuration (vector-ref result result-idx)) 'apart)
438 (analyse-solo12 (analyse-moment result-idx))
439 (analyse-solo12 (1+ result-idx))))) ; analyse-solo12
441 (analyse-spanner-states voice-state-vec1)
442 (analyse-spanner-states voice-state-vec2)
443 (analyse-time-step 0)
447 (lambda (x) (cons (when x) (configuration x)))
448 (vector->list result)))
453 #(define-public (add-quotable parser name mus)
454 (let* ((tab (eval 'musicQuotes (current-module)))
455 (context-list (recording-group-emulate (context-spec-music mus 'Voice)
456 (ly:parser-lookup parser 'partCombineListener))))
457 (if (pair? context-list)
459 ;; cdr : skip name string
460 (list->vector (reverse! (cdar context-list)
464 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
465 (make-part-combine-music parser (list music1 music2)))
467 %%%;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469 %%%;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#(use-modules (oop goops))
471 #(define (prob->music prob)
472 (let ((music (make-music (ly:prob-property prob 'name))))
473 (map (lambda (property)
474 (set! (ly:music-property music (car property)) (cdr property)))
475 (ly:prob-mutable-properties prob))
478 #(define (partcombine->music music1 music2 layout)
479 (let* ((context-list '())
481 (now-mom (ly:make-moment 0 0))
482 (global (ly:make-global-context layout))
483 (mom-listener (ly:make-listener
485 (set! now-mom (ly:event-property tev 'moment)))))
486 (new-context-listener
490 ((child (ly:event-property sev 'context))
492 (accumulate-event-listener
493 (ly:make-listener (lambda (ev)
494 (if (ly:prob? (ly:event-property ev 'music-cause))
496 (cons (prob->music (ly:event-property ev 'music-cause))
498 (save-acc-listener (ly:make-listener
502 (set! result (cons (list (string->symbol (ly:context-id child))
506 (set! musics (list))))))))
507 (ly:add-listener accumulate-event-listener (ly:context-event-source child) 'music-event)
508 (ly:add-listener save-acc-listener (ly:context-event-source global) 'OneTimeStep))))))
509 (ly:add-listener new-context-listener (ly:context-events-below global) 'AnnounceNewContext)
510 (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare)
511 (ly:interpret-music-expression (make-part-combine-music parser (list music1 music2)) global)
514 #(define (duration->moment ly-duration)
515 (let* ((log2 (ly:duration-log ly-duration))
516 (dots (ly:duration-dot-count ly-duration))
517 (num.den (ly:duration-factor ly-duration))
522 (let ((rational-moment (let* ((m (expt 2 (- log2)))
523 (factor (/ num den)))
525 (delta (/ m 2) (/ delta 2)))
527 (set! m (+ m delta)))
529 (ly:make-moment (numerator rational-moment)
530 (denominator rational-moment))))))
532 #(define (moment->duration moment)
533 ;; this is not correct
534 (ly:make-duration 0 0 (ly:moment-main-numerator moment) (ly:moment-main-denominator moment)))
536 #(define (music-duration music)
537 (let ((this-duration (ly:music-property music 'duration)))
538 (if (ly:duration? this-duration)
540 (let* ((child (ly:music-property music 'element))
541 (child-duration (and (ly:music? child) (music-duration child))))
542 (if (ly:duration? child-duration)
544 (let loop ((children (ly:music-property music 'elements)))
547 (let ((child-duration (music-duration (car children))))
548 (if (ly:duration? child-duration)
550 (loop (cdr children)))))))))))
552 #(define-class <music-accumulator> ()
553 (music-events #:init-value '() #:accessor events-of)
554 (last-moment #:init-value (ly:make-moment 0 0) #:accessor last-moment-of))
556 #(define-method (add-music-event (music-acc <music-accumulator>) music-event moment)
557 (let* ((last-music-duration (and (not (null? (events-of music-acc)))
558 (music-duration (car (events-of music-acc)))))
559 (skip-moment (ly:moment-sub (ly:moment-sub moment (last-moment-of music-acc))
560 (if (ly:duration? last-music-duration)
561 (duration->moment last-music-duration)
562 (ly:make-moment 0 0))))
563 (skip-duration (moment->duration skip-moment)))
564 (if (and (not (= 0 (ly:moment-main-numerator skip-moment))) (ly:duration? skip-duration))
565 (set! (events-of music-acc)
566 (cons (make-music 'EventChord 'elements
567 (list (make-music 'SkipEvent 'duration skip-duration)))
568 (events-of music-acc))))
569 (set! (last-moment-of music-acc) moment)
570 (set! (events-of music-acc) (cons music-event (events-of music-acc)))))
572 #(define-method (music-sequence (music-acc <music-accumulator>))
573 (make-music 'SequentialMusic 'elements (reverse (events-of music-acc))))
575 #(define-method (music-voice (music-acc <music-accumulator>) name)
576 (make-music 'ContextSpeccedMusic
578 'property-operations '()
581 'element (music-sequence music-acc)))
583 #(define (combine-two-musics music1 music2)
584 (let ((music-acc (make <music-accumulator>))
585 (music-acc2 (make <music-accumulator>)))
586 (map (lambda (id-mom-musics)
587 (let* ((id (first id-mom-musics))
588 (moment (second id-mom-musics))
589 (music-events (third id-mom-musics))
590 (music (make-music 'EventChord 'elements music-events)))
592 ((two) (add-music-event music-acc2 music moment))
593 ((one shared solo) (add-music-event music-acc music moment)))))
594 (partcombine->music music1 music2 $defaultlayout))
595 (list (music-sequence music-acc)
596 (music-sequence music-acc2))))
599 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
600 (let* ((seqs1+2 (combine-two-musics part1 part2))
601 (notes1+2 (first seqs1+2))
602 (notes2 (second seqs1+2)))
603 #{ << $notes1+2 $notes2 >> #}))