1;;;; This file is part of LilyPond, the GNU music typesetter. 2;;;; 3;;;; Copyright (C) 2004--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> 4;;;; 5;;;; LilyPond is free software: you can redistribute it and/or modify 6;;;; it under the terms of the GNU General Public License as published by 7;;;; the Free Software Foundation, either version 3 of the License, or 8;;;; (at your option) any later version. 9;;;; 10;;;; LilyPond is distributed in the hope that it will be useful, 11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of 12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13;;;; GNU General Public License for more details. 14;;;; 15;;;; You should have received a copy of the GNU General Public License 16;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>. 17 18;; todo: figure out how to make module, 19;; without breaking nested ly scopes 20 21(define-class <Voice-state> () 22 (event-list #:init-value '() #:accessor events #:init-keyword #:events) 23 (when-moment #:accessor moment #:init-keyword #:moment) 24 (tuning #:accessor tuning #:init-keyword #:tuning) 25 (split-index #:accessor split-index) 26 (vector-index) 27 (state-vector) 28 ;;; 29 ;; spanner-state is an alist 30 ;; of (SYMBOL . RESULT-INDEX), which indicates where 31 ;; said spanner was started. 32 (spanner-state #:init-value '() #:accessor span-state)) 33 34(define-method (write (x <Voice-state> ) file) 35 (display (moment x) file) 36 (display " evs = " file) 37 (display (events x) file) 38 (display " active = " file) 39 (display (span-state x) file) 40 (display "\n" file)) 41 42;; Return the duration of the longest event in the Voice-state. 43(define-method (duration (vs <Voice-state>)) 44 (define (duration-max event d1) 45 (let ((d2 (ly:event-property event 'duration #f))) 46 (if d2 47 (if (ly:duration<? d1 d2) d2 d1) 48 d1))) 49 50 (fold duration-max (ly:make-duration 0 0 0) (events vs))) 51 52;; Return the moment that the longest event in the Voice-state ends. 53(define-method (end-moment (vs <Voice-state>)) 54 (ly:moment-add (moment vs) (ly:duration-length (duration vs)))) 55 56(define-method (note-events (vs <Voice-state>)) 57 (define (f? x) 58 (ly:in-event-class? x 'note-event)) 59 (filter f? (events vs))) 60 61;; Return a list of note events which is sorted and stripped of 62;; properties that we do not want to prevent combining parts. 63(define-method (comparable-note-events (vs <Voice-state>)) 64 (define (note<? note1 note2) 65 (let ((p1 (ly:event-property note1 'pitch)) 66 (p2 (ly:event-property note2 'pitch))) 67 (cond ((ly:pitch<? p1 p2) #t) 68 ((ly:pitch<? p2 p1) #f) 69 (else (ly:duration<? (ly:event-property note1 'duration) 70 (ly:event-property note2 'duration)))))) 71 ;; TODO we probably should compare articulations too 72 (sort (map (lambda (x) 73 (ly:make-stream-event 74 (ly:make-event-class 'note-event) 75 (list (cons 'duration (ly:event-property x 'duration)) 76 (cons 'pitch (ly:event-property x 'pitch))))) 77 (note-events vs)) 78 note<?)) 79 80(define-method (silence-events (vs <Voice-state>)) 81 (let ((result (filter (lambda(x) 82 (or (ly:in-event-class? x 'rest-event) 83 (ly:in-event-class? x 'multi-measure-rest-event))) 84 (events vs)))) 85 ;; There may be skips in the same part with rests for various 86 ;; reasons. Regard the skips only if there are no rests. 87 (if (not (pair? result)) 88 (set! result (filter (lambda(x) (ly:in-event-class? x 'skip-event)) 89 (events vs)))) 90 result)) 91 92(define-method (any-mmrest-events (vs <Voice-state>)) 93 (define (f? x) 94 (ly:in-event-class? x 'multi-measure-rest-event)) 95 (any f? (events vs))) 96 97(define-method (previous-voice-state (vs <Voice-state>)) 98 (let ((i (slot-ref vs 'vector-index)) 99 (v (slot-ref vs 'state-vector))) 100 (if (< 0 i) 101 (vector-ref v (1- i)) 102 #f))) 103 104;; true if the part has ended 105(define-method (done? (vs <Voice-state>)) 106 (let ((i (slot-ref vs 'vector-index)) 107 (v (slot-ref vs 'state-vector))) 108 ;; the last entry represents the end of the part 109 (= (1+ i) (vector-length v)))) 110 111;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 112 113(define-class <Split-state> () 114 ;; The automatically determined split configuration 115 (configuration #:init-value '() #:accessor configuration) 116 ;; Allow overriding split configuration, takes precedence over configuration 117 (forced-configuration #:init-value #f #:accessor forced-configuration) 118 (when-moment #:accessor moment #:init-keyword #:moment) 119 ;; voice-states are states starting with the Split-state or later 120 ;; 121 (is #:init-keyword #:voice-states #:accessor voice-states) 122 (synced #:init-keyword #:synced #:init-value #f #:getter synced?)) 123 124 125(define-method (write (x <Split-state> ) f) 126 (display (moment x) f) 127 (display " = " f) 128 (display (configuration x) f) 129 (if (synced? x) 130 (display " synced ")) 131 (display "\n" f)) 132 133(define-method (current-or-previous-voice-states (ss <Split-state>)) 134 "Return voice states meeting the following conditions. For a voice 135in sync, return the current voice state. For a voice out of sync, 136return the previous voice state." 137 (let* ((vss (voice-states ss)) 138 (vs1 (car vss)) 139 (vs2 (cdr vss))) 140 (if (and vs1 (not (equal? (moment vs1) (moment ss)))) 141 (set! vs1 (previous-voice-state vs1))) 142 (if (and vs2 (not (equal? (moment vs2) (moment ss)))) 143 (set! vs2 (previous-voice-state vs2))) 144 (cons vs1 vs2))) 145 146;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 147 148 149(define (previous-span-state vs) 150 (let ((p (previous-voice-state vs))) 151 (if p (span-state p) '()))) 152 153(define (make-voice-states evl) 154 (let* ((states (map (lambda (v) 155 (make <Voice-state> 156 #:moment (caar v) 157 #:tuning (cdar v) 158 #:events (map car (cdr v)))) 159 (reverse evl)))) 160 161 ;; add an entry with no events at the moment the last event ends 162 (if (pair? states) 163 (let ((last-real-event (car states))) 164 (set! states 165 (cons (make <Voice-state> 166 #:moment (end-moment last-real-event) 167 #:tuning (tuning last-real-event) 168 #:events '()) 169 states)))) 170 171 ;; TODO: Add an entry at +inf.0 and see if it allows us to remove 172 ;; the many instances of conditional code handling the case that 173 ;; there is no voice state at a given moment. 174 175 (let ((vec (list->vector (reverse! states)))) 176 (do ((i 0 (1+ i))) 177 ((= i (vector-length vec)) vec) 178 (slot-set! (vector-ref vec i) 'vector-index i) 179 (slot-set! (vector-ref vec i) 'state-vector vec))))) 180 181(define (make-split-state vs1 vs2) 182 "Merge lists VS1 and VS2, containing Voice-state objects into vector 183of Split-state objects, crosslinking the Split-state vector and 184Voice-state objects 185" 186 (define (helper ss-idx ss-list idx1 idx2) 187 (let* ((state1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f)) 188 (state2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f)) 189 (min (cond ((and state1 state2) (moment-min (moment state1) (moment state2))) 190 (state1 (moment state1)) 191 (state2 (moment state2)) 192 (else #f))) 193 (inc1 (if (and state1 (equal? min (moment state1))) 1 0)) 194 (inc2 (if (and state2 (equal? min (moment state2))) 1 0)) 195 (ss-object (if min 196 (make <Split-state> 197 #:moment min 198 #:voice-states (cons state1 state2) 199 #:synced (= inc1 inc2)) 200 #f))) 201 (if state1 202 (set! (split-index state1) ss-idx)) 203 (if state2 204 (set! (split-index state2) ss-idx)) 205 (if min 206 (helper (1+ ss-idx) 207 (cons ss-object ss-list) 208 (+ idx1 inc1) 209 (+ idx2 inc2)) 210 ss-list))) 211 (list->vector (reverse! (helper 0 '() 0 0) '()))) 212 213(define (analyse-spanner-states voice-state-vec) 214 215 (define (helper index active) 216 "Analyse EVS at INDEX, given state ACTIVE." 217 218 (define (analyse-tie-start active ev) 219 (if (ly:in-event-class? ev 'tie-event) 220 (acons 'tie (split-index (vector-ref voice-state-vec index)) 221 active) 222 active)) 223 224 (define (analyse-tie-end active ev) 225 (if (ly:in-event-class? ev 'note-event) 226 (assoc-remove! active 'tie) 227 active)) 228 229 (define (analyse-absdyn-end active ev) 230 (if (or (ly:in-event-class? ev 'absolute-dynamic-event) 231 (and (ly:in-event-class? ev 'span-dynamic-event) 232 (equal? STOP (ly:event-property ev 'span-direction)))) 233 (assoc-remove! (assoc-remove! active 'cresc) 'decr) 234 active)) 235 236 (define (active<? a b) 237 (cond ((symbol<? (car a) (car b)) #t) 238 ((symbol<? (car b) (car a)) #f) 239 (else (< (cdr a) (cdr b))))) 240 241 (define (analyse-span-event active ev) 242 (let* ((name (car (ly:event-property ev 'class))) 243 (key (cond ((equal? name 'slur-event) 'slur) 244 ((equal? name 'phrasing-slur-event) 'tie) 245 ((equal? name 'beam-event) 'beam) 246 ((equal? name 'crescendo-event) 'cresc) 247 ((equal? name 'decrescendo-event) 'decr) 248 (else #f))) 249 (sp (ly:event-property ev 'span-direction))) 250 (if (and (symbol? key) (ly:dir? sp)) 251 (if (= sp STOP) 252 (assoc-remove! active key) 253 (acons key 254 (split-index (vector-ref voice-state-vec index)) 255 active)) 256 active))) 257 258 (define (analyse-events active evs) 259 "Run all analyzers on ACTIVE and EVS" 260 (define (run-analyzer analyzer active evs) 261 (if (pair? evs) 262 (run-analyzer analyzer (analyzer active (car evs)) (cdr evs)) 263 active)) 264 (define (run-analyzers analyzers active evs) 265 (if (pair? analyzers) 266 (run-analyzers (cdr analyzers) 267 (run-analyzer (car analyzers) active evs) 268 evs) 269 active)) 270 (sort ;; todo: use fold or somesuch. 271 (run-analyzers (list analyse-absdyn-end analyse-span-event 272 ;; note: tie-start/span comes after tie-end/absdyn. 273 analyse-tie-end analyse-tie-start) 274 active evs) 275 active<?)) 276 277 ;; must copy, since we use assoc-remove! 278 (if (< index (vector-length voice-state-vec)) 279 (begin 280 (set! active (analyse-events active (events (vector-ref voice-state-vec index)))) 281 (set! (span-state (vector-ref voice-state-vec index)) 282 (list-copy active)) 283 (helper (1+ index) active)))) 284 285 (helper 0 '())) 286 287(define recording-group-functions 288 ;;Selected parts from @var{toplevel-music-functions} not requiring @code{parser}. 289 (list 290 (lambda (music) (expand-repeat-chords! '(rhythmic-event) music)) 291 expand-repeat-notes!)) 292 293 294;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 295(define-public (recording-group-emulate music odef) 296 "Interpret @var{music} according to @var{odef}, but store all events 297in a chronological list, similar to the @code{Recording_group_engraver} in 298LilyPond version 2.8 and earlier." 299 (let* 300 ((context-list '()) 301 (now-mom (ly:make-moment 0 0)) 302 (global (ly:make-global-context odef)) 303 (mom-listener (lambda (tev) (set! now-mom (ly:event-property tev 'moment)))) 304 (new-context-listener 305 (lambda (sev) 306 (let* 307 ((child (ly:event-property sev 'context)) 308 (this-moment-list (cons (ly:context-id child) '())) 309 (dummy (set! context-list (cons this-moment-list context-list))) 310 (acc '()) 311 (accumulate-event-listener 312 (lambda (ev) 313 (set! acc (cons (cons ev #t) acc)))) 314 (save-acc-listener 315 (lambda (tev) 316 (if (pair? acc) 317 (let ((this-moment 318 (cons (cons now-mom 319 (ly:context-property child 'instrumentTransposition)) 320 ;; The accumulate-event-listener above creates 321 ;; the list of events in reverse order, so we 322 ;; have to revert it to the original order again 323 (reverse acc)))) 324 (set-cdr! this-moment-list 325 (cons this-moment (cdr this-moment-list))) 326 (set! acc '())))))) 327 (ly:add-listener accumulate-event-listener 328 (ly:context-event-source child) 'StreamEvent) 329 (ly:add-listener save-acc-listener 330 (ly:context-event-source global) 'OneTimeStep))))) 331 (ly:add-listener new-context-listener 332 (ly:context-events-below global) 'AnnounceNewContext) 333 (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare) 334 (ly:interpret-music-expression 335 (make-non-relative-music 336 (fold (lambda (x m) (x m)) music recording-group-functions)) 337 global) 338 context-list)) 339 340(define-public (determine-split-list evl1 evl2 chord-range) 341 "@var{evl1} and @var{evl2} should be ascending. @var{chord-range} is a pair of numbers (min . max) defining the distance in steps between notes that may be combined into a chord or unison." 342 (let* ((pc-debug #f) 343 (voice-state-vec1 (make-voice-states evl1)) 344 (voice-state-vec2 (make-voice-states evl2)) 345 (result (make-split-state voice-state-vec1 voice-state-vec2)) 346 (chord-min-diff (car chord-range)) 347 (chord-max-diff (cdr chord-range))) 348 349 ;; Go through all moments recursively and check if the events of that 350 ;; moment contain a part-combine-force-event override. If so, store its 351 ;; value in the forced-configuration field, which will override. The 352 ;; previous configuration is used to determine non-terminated settings. 353 (define (analyse-forced-combine result-idx prev-res) 354 355 (define (get-forced-event x) 356 (cond 357 ((and (ly:in-event-class? x 'SetProperty) 358 (eq? (ly:event-property x 'symbol) 'partCombineForced)) 359 (cons (ly:event-property x 'value #f) 360 (ly:event-property x 'once #f))) 361 ((and (ly:in-event-class? x 'UnsetProperty) 362 (eq? (ly:event-property x 'symbol) 'partCombineForced)) 363 (cons #f (ly:event-property x 'once #f))) 364 (else #f))) 365 366 (define (part-combine-events vs) 367 (if (not vs) 368 '() 369 (filter-map get-forced-event (events vs)))) 370 ;; end part-combine-events 371 372 ;; forced-result: Take the previous config and analyse whether 373 ;; any change happened.... Return new once and permanent config 374 (define (forced-result evt state) 375 ;; sanity check, evt should always be (new-state . once) 376 (if (not (and (pair? evt) (pair? state))) 377 state 378 (if (cdr evt) 379 ;; Once-event, leave permanent state unchanged 380 (cons (car evt) (cdr state)) 381 ;; permanent change, leave once state unchanged 382 (cons (car state) (car evt))))) 383 ;; end forced-combine-result 384 385 ;; body of analyse-forced-combine: 386 (if (< result-idx (vector-length result)) 387 (let* ((now-state (vector-ref result result-idx)) ; current result 388 ;; Extract all part-combine force events 389 (evts (if (synced? now-state) 390 (append 391 (part-combine-events (car (voice-states now-state))) 392 (part-combine-events (cdr (voice-states now-state)))) 393 '())) 394 ;; result is (once-state permament-state): 395 (state (fold forced-result (cons 'automatic prev-res) evts)) 396 ;; Now let once override permanent changes: 397 (force-state (if (equal? (car state) 'automatic) 398 (cdr state) 399 (car state)))) 400 (set! (forced-configuration (vector-ref result result-idx)) 401 force-state) 402 ;; For the next moment, ignore the once override (car stat) 403 ;; and pass on the permanent override, stored as (cdr state) 404 (analyse-forced-combine (1+ result-idx) (cdr state))))) 405 ;; end analyse-forced-combine 406 407 408 (define (analyse-time-step result-idx) 409 (define (put x . index) 410 "Put the result to X, starting from INDEX backwards. 411 412Only set if not set previously. 413" 414 (let ((i (if (pair? index) (car index) result-idx))) 415 (if (and (<= 0 i) 416 (not (symbol? (configuration (vector-ref result i))))) 417 (begin 418 (set! (configuration (vector-ref result i)) x) 419 (put x (1- i)))))) 420 421 (define (copy-state-from state-vec vs) 422 (define (copy-one-state key-idx) 423 (let* ((idx (cdr key-idx)) 424 (prev-ss (vector-ref result idx)) 425 (prev (configuration prev-ss))) 426 (if (symbol? prev) 427 (put prev)))) 428 (for-each copy-one-state (span-state vs))) 429 430 (define (analyse-notes now-state) 431 (let* ((vs1 (car (voice-states now-state))) 432 (vs2 (cdr (voice-states now-state))) 433 (notes1 (comparable-note-events vs1)) 434 (notes2 (comparable-note-events vs2))) 435 (cond 436 ;; if neither part has notes, do nothing 437 ((and (not (pair? notes1)) (not (pair? notes2)))) 438 439 ;; if one part has notes and the other does not 440 ((or (not (pair? notes1)) (not (pair? notes2))) (put 'apart)) 441 442 ;; if either part has a chord 443 ((or (> (length notes1) 1) 444 (> (length notes2) 1)) 445 (if (and (<= chord-min-diff 0) ; user requests combined unisons 446 (equal? notes1 notes2)) ; both parts have the same chord 447 (put 'chords) 448 (put 'apart))) 449 450 ;; if the durations are different 451 ;; TODO articulations too? 452 ((and (not (equal? (ly:event-property (car notes1) 'duration) 453 (ly:event-property (car notes2) 'duration)))) 454 (put 'apart)) 455 456 (else 457 ;; Is the interval outside of chord-range? 458 (if (let ((diff (ly:pitch-steps 459 (ly:pitch-diff 460 (ly:event-property (car notes1) 'pitch) 461 (ly:event-property (car notes2) 'pitch))))) 462 (or (< diff chord-min-diff) 463 (> diff chord-max-diff) 464 )) 465 (put 'apart) 466 ;; copy previous split state from spanner state 467 (begin 468 (if (previous-voice-state vs1) 469 (copy-state-from voice-state-vec1 470 (previous-voice-state vs1))) 471 (if (previous-voice-state vs2) 472 (copy-state-from voice-state-vec2 473 (previous-voice-state vs2))) 474 (if (and (null? (span-state vs1)) (null? (span-state vs2))) 475 (put 'chords)))))))) 476 477 (if (< result-idx (vector-length result)) 478 (let* ((now-state (vector-ref result result-idx)) 479 (vs1 (car (voice-states now-state))) 480 (vs2 (cdr (voice-states now-state)))) 481 482 (cond ((not vs1) (put 'apart)) 483 ((not vs2) (put 'apart)) 484 (else 485 (let ((active1 (previous-span-state vs1)) 486 (active2 (previous-span-state vs2)) 487 (new-active1 (span-state vs1)) 488 (new-active2 (span-state vs2))) 489 (if #f ; debug 490 (display (list (moment now-state) result-idx 491 active1 "->" new-active1 492 active2 "->" new-active2 493 "\n"))) 494 (if (and (synced? now-state) 495 (equal? active1 active2) 496 (equal? new-active1 new-active2)) 497 (analyse-notes now-state) 498 499 ;; active states different: 500 (put 'apart))) 501 502 ;; go to the next one, if it exists. 503 (analyse-time-step (1+ result-idx))))))) 504 505 (define (analyse-a2 result-idx) 506 (if (< result-idx (vector-length result)) 507 (let* ((now-state (vector-ref result result-idx)) 508 (vs1 (car (voice-states now-state))) 509 (vs2 (cdr (voice-states now-state)))) 510 511 (define (analyse-synced-silence) 512 (let ((rests1 (if vs1 (silence-events vs1) '())) 513 (rests2 (if vs2 (silence-events vs2) '()))) 514 (cond 515 516 ;; equal rests or equal skips, but not one of each 517 ((and (= 1 (length rests1)) 518 (= 1 (length rests2)) 519 (equal? (ly:event-property (car rests1) 'class) 520 (ly:event-property (car rests2) 'class)) 521 (equal? (ly:event-property (car rests1) 'duration) 522 (ly:event-property (car rests2) 'duration))) 523 (set! (configuration now-state) 'unisilence)) 524 525 ;; rests of different durations or mixed with 526 ;; skips or multi-measure rests 527 (else 528 ;; TODO For skips, route the rest to the shared 529 ;; voice and the skip to the voice for its part? 530 (set! (configuration now-state) 'apart-silence)) 531 532 ))) 533 534 (define (analyse-unsynced-silence vs1 vs2) 535 (let ((any-mmrests1 (if vs1 (any-mmrest-events vs1) #f)) 536 (any-mmrests2 (if vs2 (any-mmrest-events vs2) #f))) 537 (cond 538 ;; If a multi-measure rest begins now while the other 539 ;; part has an ongoing multi-measure rest (or has 540 ;; ended), start displaying the one that begins now. 541 ((and any-mmrests1 542 (equal? (moment vs1) (moment now-state)) 543 (or (not vs2) any-mmrests2)) 544 (set! (configuration now-state) 'silence1)) 545 546 ;; as above with parts swapped 547 ((and any-mmrests2 548 (equal? (moment vs2) (moment now-state)) 549 (or (not vs1) any-mmrests1)) 550 (set! (configuration now-state) 'silence2)) 551 ))) 552 553 (if (or vs1 vs2) 554 (let ((notes1 (if vs1 (comparable-note-events vs1) '())) 555 (notes2 (if vs2 (comparable-note-events vs2) '()))) 556 (cond ((and (equal? (configuration now-state) 'chords) 557 (pair? notes1) 558 (equal? notes1 notes2)) 559 (set! (configuration now-state) 'unisono)) 560 561 ((synced? now-state) 562 (if (and (= 0 (length notes1)) 563 (= 0 (length notes2))) 564 (analyse-synced-silence))) 565 566 (else ;; not synchronized 567 (let* ((vss 568 (current-or-previous-voice-states now-state)) 569 (vs1 (car vss)) 570 (vs2 (cdr vss))) 571 (if (and 572 (or (not vs1) (= 0 (length (note-events vs1)))) 573 (or (not vs2) (= 0 (length (note-events vs2))))) 574 (analyse-unsynced-silence vs1 vs2)))) 575 ))) 576 (analyse-a2 (1+ result-idx))))) 577 578 (define (analyse-solo12 result-idx) 579 580 (define (previous-config vs) 581 (let* ((pvs (previous-voice-state vs)) 582 (spi (if pvs (split-index pvs) #f)) 583 (prev-split (if spi (vector-ref result spi) #f))) 584 (if prev-split 585 (configuration prev-split) 586 'apart))) 587 588 (define (put-range x a b) 589 ;; (display (list "put range " x a b "\n")) 590 (do ((i a (1+ i))) 591 ((> i b) b) 592 (set! (configuration (vector-ref result i)) x))) 593 594 (define (put x) 595 ;; (display (list "putting " x "\n")) 596 (set! (configuration (vector-ref result result-idx)) x)) 597 598 (define (current-voice-state now-state voice-num) 599 (define vs ((if (= 1 voice-num) car cdr) 600 (voice-states now-state))) 601 (if (or (not vs) (equal? (moment now-state) (moment vs))) 602 vs 603 (previous-voice-state vs))) 604 605 (define (try-solo type start-idx current-idx) 606 "Find a maximum stretch that can be marked as solo. Only set 607the mark when there are no spanners active. 608 609 return next idx to analyse. 610" 611 (if (< current-idx (vector-length result)) 612 (let* ((now-state (vector-ref result current-idx)) 613 (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2))) 614 (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1))) 615 (silent-notes (if silent-state (note-events silent-state) '())) 616 (solo-notes (if solo-state (note-events solo-state) '()))) 617 ;; (display (list "trying " type " at " (moment now-state) solo-state silent-state "\n")) 618 (cond ((not (equal? (configuration now-state) 'apart)) 619 current-idx) 620 ((> (length silent-notes) 0) start-idx) 621 ((not solo-state) 622 (put-range type start-idx current-idx) 623 current-idx) 624 ((and 625 (null? (span-state solo-state))) 626 627 ;; 628 ;; This includes rests. This isn't a problem: long rests 629 ;; will be shared with the silent voice, and be marked 630 ;; as unisilence. Therefore, long rests won't 631 ;; accidentally be part of a solo. 632 ;; 633 (put-range type start-idx current-idx) 634 (try-solo type (1+ current-idx) (1+ current-idx))) 635 (else 636 (try-solo type start-idx (1+ current-idx))))) 637 ;; try-solo 638 start-idx)) 639 640 (define (analyse-apart-silence result-idx) 641 "Analyse 'apart-silence starting at RESULT-IDX. Return next index." 642 643 (define (analyse-synced-apart-silence vs1 vs2) 644 (let* ((rests1 (silence-events vs1)) 645 (rests2 (silence-events vs2))) 646 (cond 647 ;; multiple rests in the same part 648 ((and (or (not (= 1 (length rests1))) 649 (not (= 1 (length rests2))))) 650 (put 'apart-silence)) 651 652 ;; rest with multi-measure rest: choose the rest 653 ((and (ly:in-event-class? (car rests1) 'rest-event) 654 (ly:in-event-class? (car rests2) 'multi-measure-rest-event)) 655 (put 'silence1)) 656 657 ;; as above with parts swapped 658 ((and (ly:in-event-class? (car rests1) 'multi-measure-rest-event) 659 (ly:in-event-class? (car rests2) 'rest-event)) 660 (put 'silence2)) 661 662 ;; mmrest in both parts: choose the shorter one 663 ;; (equal mmrests are classified as unisilence earlier, 664 ;; so they shouldn't be seen here) 665 ((and (ly:in-event-class? (car rests1) 'multi-measure-rest-event) 666 (ly:in-event-class? (car rests2) 'multi-measure-rest-event)) 667 (if (ly:duration<? (ly:event-property (car rests1) 'duration) 668 (ly:event-property (car rests2) 'duration)) 669 (put 'silence1) 670 (put 'silence2))) 671 672 (else 673 (put 'apart-silence))))) 674 675 (define (analyse-unsynced-apart-silence vs1 vs2) 676 (let* ((prev-state (if (> result-idx 0) 677 (vector-ref result (- result-idx 1)) 678 #f)) 679 (prev-config (if prev-state 680 (configuration prev-state) 681 'apart-silence))) 682 (cond 683 ;; remain in the silence1/2 states until resync 684 ((equal? prev-config 'silence1) 685 (put 'silence1)) 686 687 ((equal? prev-config 'silence2) 688 (put 'silence2)) 689 690 (else 691 (put 'apart-silence))))) 692 693 (let* ((now-state (vector-ref result result-idx)) 694 (vs1 (current-voice-state now-state 1)) 695 (vs2 (current-voice-state now-state 2))) 696 (cond 697 ;; part 1 has ended 698 ((or (not vs1) (done? vs1)) 699 (put 'silence2)) 700 701 ;; part 2 has ended 702 ((or (not vs2) (done? vs2)) 703 (put 'silence1)) 704 705 ((synced? now-state) 706 (analyse-synced-apart-silence vs1 vs2)) 707 708 (else 709 (analyse-unsynced-apart-silence vs1 vs2))) 710 711 (1+ result-idx))) 712 713 (define (analyse-apart result-idx) 714 "Analyse 'apart starting at RESULT-IDX. Return next index." 715 (let* ((now-state (vector-ref result result-idx)) 716 (vs1 (current-voice-state now-state 1)) 717 (vs2 (current-voice-state now-state 2)) 718 ;; (vs1 (car (voice-states now-state))) 719 ;; (vs2 (cdr (voice-states now-state))) 720 (notes1 (if vs1 (note-events vs1) '())) 721 (notes2 (if vs2 (note-events vs2) '())) 722 (n1 (length notes1)) 723 (n2 (length notes2))) 724 ;; (display (list "analyzing step " result-idx " moment " (moment now-state) vs1 vs2 "\n")) 725 (max 726 ;; we should always increase. 727 (cond ((and (= n1 0) (= n2 0)) 728 ;; If we hit this, it means that the previous passes 729 ;; have designated as 'apart what is really 730 ;; 'apart-silence. 731 (analyse-apart-silence result-idx)) 732 ((and (= n2 0) 733 (equal? (moment vs1) (moment now-state)) 734 (null? (previous-span-state vs1))) 735 (try-solo 'solo1 result-idx result-idx)) 736 ((and (= n1 0) 737 (equal? (moment vs2) (moment now-state)) 738 (null? (previous-span-state vs2))) 739 (try-solo 'solo2 result-idx result-idx)) 740 741 (else (1+ result-idx))) 742 ;; analyse-moment 743 (1+ result-idx)))) 744 745 (if (< result-idx (vector-length result)) 746 (let ((conf (configuration (vector-ref result result-idx)))) 747 (cond 748 ((equal? conf 'apart) 749 (analyse-solo12 (analyse-apart result-idx))) 750 ((equal? conf 'apart-silence) 751 (analyse-solo12 (analyse-apart-silence result-idx))) 752 (else 753 (analyse-solo12 (1+ result-idx))))))) ; analyse-solo12 754 755 (analyse-spanner-states voice-state-vec1) 756 (analyse-spanner-states voice-state-vec2) 757 (if #f 758 (begin 759 (display voice-state-vec1) 760 (display "***\n") 761 (display voice-state-vec2) 762 (display "***\n") 763 (display result) 764 (display "***\n"))) 765 766 ;; Extract all forced combine strategies, i.e. events inserted by 767 ;; \partCombine(Apart|Automatic|SoloI|SoloII|Chords)[Once] 768 ;; They will in the end override the automaically determined ones. 769 ;; Initial state for both voices is no override 770 (analyse-forced-combine 0 #f) 771 ;; Now go through all time steps in a loop and find a combination strategy 772 ;; based only on the events of that one moment (i.e. neglecting longer 773 ;; periods of solo/apart, etc.) 774 (analyse-time-step 0) 775 ;; (display result) 776 ;; Check for unisono or unisilence moments 777 (analyse-a2 0) 778 ;;(display result) 779 (analyse-solo12 0) 780 ;; (display result) 781 (set! result (map 782 ;; forced-configuration overrides, if it is set 783 (lambda (x) (cons (moment x) (or (forced-configuration x) (configuration x)))) 784 (vector->list result))) 785 (if #f ;; pc-debug 786 (display result)) 787 result)) 788 789(define-public default-part-combine-mark-state-machine 790 ;; (current-state . ((split-state-event . 791 ;; (output-voice output-event next-state)) ...)) 792 '((Initial . ((solo1 . (solo SoloOneEvent Solo1)) 793 (solo2 . (solo SoloTwoEvent Solo2)) 794 (unisono . (shared UnisonoEvent Unisono)))) 795 (Solo1 . ((apart . (#f #f Initial)) 796 (chords . (#f #f Initial)) 797 (solo2 . (solo SoloTwoEvent Solo2)) 798 (unisono . (shared UnisonoEvent Unisono)))) 799 (Solo2 . ((apart . (#f #f Initial)) 800 (chords . (#f #f Initial)) 801 (solo1 . (solo SoloOneEvent Solo1)) 802 (unisono . (shared UnisonoEvent Unisono)))) 803 (Unisono . ((apart . (#f #f Initial)) 804 (chords . (#f #f Initial)) 805 (solo1 . (solo SoloOneEvent Solo1)) 806 (solo2 . (solo SoloTwoEvent Solo2)))))) 807 808(define-public (make-part-combine-marks state-machine split-list) 809 "Generate a sequence of part combiner events from a split list" 810 811 (define (get-state state-name) 812 (assq-ref state-machine state-name)) 813 814 (let ((full-seq '()) ; sequence of { \context Voice = "x" {} ... } 815 (segment '()) ; sequence within \context Voice = "x" {...} 816 (prev-moment ZERO-MOMENT) 817 (prev-voice #f) 818 (state (get-state 'Initial))) 819 820 (define (commit-segment) 821 "Add the current segment to the full sequence and begin another." 822 (if (pair? segment) 823 (set! full-seq 824 (cons (make-music 'ContextSpeccedMusic 825 'context-id (symbol->string prev-voice) 826 'context-type 'Voice 827 'element (make-sequential-music (reverse! segment))) 828 full-seq))) 829 (set! segment '())) 830 831 (define (handle-split split) 832 (let* ((moment (car split)) 833 (action (assq-ref state (cdr split)))) 834 (if action 835 (let ((voice (car action)) 836 (part-combine-event (cadr action)) 837 (next-state-name (caddr action))) 838 (if part-combine-event 839 (let ((dur (ly:moment-sub moment prev-moment))) 840 ;; start a new segment when the voice changes 841 (if (not (eq? voice prev-voice)) 842 (begin 843 (commit-segment) 844 (set! prev-voice voice))) 845 (if (not (equal? dur ZERO-MOMENT)) 846 (set! segment (cons (make-music 'SkipEvent 847 'duration (make-duration-of-length dur)) segment))) 848 (set! segment (cons (make-music part-combine-event) segment)) 849 850 (set! prev-moment moment))) 851 (set! state (get-state next-state-name)))))) 852 853 (for-each handle-split split-list) 854 (commit-segment) 855 (make-sequential-music (reverse! full-seq)))) 856 857(define-public default-part-combine-context-change-state-machine-one 858 ;; (current-state . ((split-state-event . (output-voice next-state)) ...)) 859 '((Initial . ((apart . (one . Initial)) 860 (apart-silence . (one . Initial)) 861 (apart-spanner . (one . Initial)) 862 (chords . (shared . Initial)) 863 (silence1 . (shared . Initial)) 864 (silence2 . (null . Demoted)) 865 (solo1 . (solo . Initial)) 866 (solo2 . (null . Demoted)) 867 (unisono . (shared . Initial)) 868 (unisilence . (shared . Initial)))) 869 870 ;; After a part has been used as the exclusive input for a 871 ;; passage, we want to use it by default for unisono/unisilence 872 ;; passages because Part_combine_iterator might have killed 873 ;; multi-measure rests in the other part. Here we call such a 874 ;; part "promoted". Part one begins promoted. 875 (Demoted . ((apart . (one . Demoted)) 876 (apart-silence . (one . Demoted)) 877 (apart-spanner . (one . Demoted)) 878 (chords . (shared . Demoted)) 879 (silence1 . (shared . Initial)) 880 (silence2 . (null . Demoted)) 881 (solo1 . (solo . Initial)) 882 (solo2 . (null . Demoted)) 883 (unisono . (null . Demoted)) 884 (unisilence . (null . Demoted)))))) 885 886(define-public default-part-combine-context-change-state-machine-two 887 ;; (current-state . ((split-state-event . (output-voice next-state)) ...)) 888 '((Initial . ((apart . (two . Initial)) 889 (apart-silence . (two . Initial)) 890 (apart-spanner . (two . Initial)) 891 (chords . (shared . Initial)) 892 (silence1 . (null . Initial)) 893 (silence2 . (shared . Promoted)) 894 (solo1 . (null . Initial)) 895 (solo2 . (solo . Promoted)) 896 (unisono . (null . Initial)) 897 (unisilence . (null . Initial)))) 898 899 ;; See the part-one state machine for the meaning of "promoted". 900 (Promoted . ((apart . (two . Promoted)) 901 (apart-silence . (two . Promoted)) 902 (apart-spanner . (two . Promoted)) 903 (chords . (shared . Promoted)) 904 (silence1 . (null . Initial)) 905 (silence2 . (shared . Promoted)) 906 (solo1 . (null . Initial)) 907 (solo2 . (solo . Promoted)) 908 (unisono . (shared . Promoted)) 909 (unisilence . (shared . Promoted)))))) 910 911(define-public (make-part-combine-context-changes state-machine split-list) 912 "Generate a sequence of part combiner context changes from a split list" 913 914 (define (get-state state-name) 915 (assq-ref state-machine state-name)) 916 917 (let ((change-list '()) 918 (prev-voice #f) 919 (state (get-state 'Initial))) 920 921 (define (handle-split split) 922 (let* ((moment (car split)) 923 (action (assq-ref state (cdr split)))) 924 (if action 925 (let ((voice (car action)) 926 (next-state-name (cdr action))) 927 (if (not (eq? voice prev-voice)) 928 (begin 929 (set! change-list (cons (cons moment voice) change-list)) 930 (set! prev-voice voice))) 931 (set! state (get-state next-state-name)))))) 932 933 (for-each handle-split split-list) 934 (reverse! change-list))) 935 936;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 937 938(define-public (add-quotable name mus) 939 (let* ((tab (eval 'musicQuotes (current-module))) 940 (voicename (get-next-unique-voice-name)) 941 ;; recording-group-emulate returns an assoc list (reversed!), so 942 ;; hand it a proper unique context name and extract that key: 943 (ctx-spec (context-spec-music mus 'Voice voicename)) 944 (listener (ly:parser-lookup 'partCombineListener)) 945 (context-list (reverse (recording-group-emulate ctx-spec listener))) 946 (raw-voice (assoc voicename context-list)) 947 (quote-contents (and raw-voice (cdr raw-voice)))) 948 949 ;; If the context-specced quoted music does not contain anything, try to 950 ;; use the first child, i.e. the next in context-list after voicename 951 ;; That's the case e.g. for \addQuote "x" \relative c \new Voice {...} 952 ;; 953 ;; Note that if raw-voice is #f, so is quote-contents, in which 954 ;; case the following loop is skipped. 955 (if (null? quote-contents) 956 (let find-non-empty ((current-tail (member raw-voice context-list))) 957 ;; if voice has contents, use them, otherwise check next ctx 958 (cond ((null? current-tail) #f) 959 ((and (pair? (car current-tail)) 960 (pair? (cdar current-tail))) 961 (set! quote-contents (cdar current-tail))) 962 (else (find-non-empty (cdr current-tail)))))) 963 964 (if (pair? quote-contents) 965 (hash-set! tab name (list->vector (reverse! quote-contents))) 966 (ly:music-warning mus (_ "quoted music `~a' is empty") name)))) 967