1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
18 ;; todo: figure out how to make module,
19 ;; without breaking nested ly scopes
21 (define-class <Voice-state> ()
22 (event-list #:init-value '() #:accessor events #:init-keyword #:events)
23 (when-moment #:accessor when #:init-keyword #:when)
24 (tuning #:accessor tuning #:init-keyword #:tuning)
25 (split-index #:accessor split-index)
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))
34 (define-method (write (x <Voice-state> ) file)
35 (display (when x) file)
36 (display " evs = " file)
37 (display (events x) file)
38 (display " active = " file)
39 (display (span-state x) file)
42 (define-method (note-events (vs <Voice-state>))
44 (equal? (ly:event-property x 'class) 'note-event))
45 (filter f? (events vs)))
47 (define-method (previous-voice-state (vs <Voice-state>))
48 (let ((i (slot-ref vs 'vector-index))
49 (v (slot-ref vs 'state-vector)))
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 (define-class <Split-state> ()
57 (configuration #:init-value '() #:accessor configuration)
58 (when-moment #:accessor when #:init-keyword #:when)
59 ;; voice-states are states starting with the Split-state or later
61 (is #:init-keyword #:voice-states #:accessor voice-states)
62 (synced #:init-keyword #:synced #:init-value #f #:getter synced?))
65 (define-method (write (x <Split-state> ) f)
68 (display (configuration x) f)
73 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76 (define (previous-span-state vs)
77 (let ((p (previous-voice-state vs)))
78 (if p (span-state p) '())))
80 (define (make-voice-states evl)
81 (let ((vec (list->vector (map (lambda (v)
85 #:events (map car (cdr v))))
88 ((= i (vector-length vec)) vec)
89 (slot-set! (vector-ref vec i) 'vector-index i)
90 (slot-set! (vector-ref vec i) 'state-vector vec))))
92 (define (make-split-state vs1 vs2)
93 "Merge lists VS1 and VS2, containing Voice-state objects into vector
94 of Split-state objects, crosslinking the Split-state vector and
97 (define (helper ss-idx ss-list idx1 idx2)
98 (let* ((state1 (if (< idx1 (vector-length vs1)) (vector-ref vs1 idx1) #f))
99 (state2 (if (< idx2 (vector-length vs2)) (vector-ref vs2 idx2) #f))
100 (min (cond ((and state1 state2) (moment-min (when state1) (when state2)))
101 (state1 (when state1))
102 (state2 (when state2))
104 (inc1 (if (and state1 (equal? min (when state1))) 1 0))
105 (inc2 (if (and state2 (equal? min (when state2))) 1 0))
109 #:voice-states (cons state1 state2)
110 #:synced (= inc1 inc2))
113 (set! (split-index state1) ss-idx))
115 (set! (split-index state2) ss-idx))
118 (cons ss-object ss-list)
122 (list->vector (reverse! (helper 0 '() 0 0) '())))
124 (define (analyse-spanner-states voice-state-vec)
126 (define (helper index active)
127 "Analyse EVS at INDEX, given state ACTIVE."
129 (define (analyse-tie-start active ev)
130 (if (equal? (ly:event-property ev 'class) 'tie-event)
131 (acons 'tie (split-index (vector-ref voice-state-vec index))
135 (define (analyse-tie-end active ev)
136 (if (equal? (ly:event-property ev 'class) 'note-event)
137 (assoc-remove! active 'tie)
140 (define (analyse-absdyn-end active ev)
141 (if (or (equal? (ly:event-property ev 'class) 'absolute-dynamic-event)
142 (and (equal? (ly:event-property ev 'class) 'crescendo-event)
143 (equal? STOP (ly:event-property ev 'span-direction))))
144 (assoc-remove! (assoc-remove! active 'cresc) 'decr)
147 (define (active<? a b)
148 (cond ((symbol<? (car a) (car b)) #t)
149 ((symbol<? (car b) (car b)) #f)
150 (else (< (cdr a) (cdr b)))))
152 (define (analyse-span-event active ev)
153 (let* ((name (ly:event-property ev 'class))
154 (key (cond ((equal? name 'slur-event) 'slur)
155 ((equal? name 'phrasing-slur-event) 'tie)
156 ((equal? name 'beam-event) 'beam)
157 ((equal? name 'crescendo-event) 'cresc)
158 ((equal? name 'decrescendo-event) 'decr)
160 (sp (ly:event-property ev 'span-direction)))
161 (if (and (symbol? key) (ly:dir? sp))
163 (assoc-remove! active key)
165 (split-index (vector-ref voice-state-vec index))
169 (define (analyse-events active evs)
170 "Run all analyzers on ACTIVE and EVS"
171 (define (run-analyzer analyzer active evs)
173 (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
175 (define (run-analyzers analyzers active evs)
176 (if (pair? analyzers)
177 (run-analyzers (cdr analyzers)
178 (run-analyzer (car analyzers) active evs)
181 (sort ;; todo: use fold or somesuch.
182 (run-analyzers (list analyse-absdyn-end analyse-span-event
183 ;; note: tie-start/span comes after tie-end/absdyn.
184 analyse-tie-end analyse-tie-start)
188 ;; must copy, since we use assoc-remove!
189 (if (< index (vector-length voice-state-vec))
191 (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
192 (set! (span-state (vector-ref voice-state-vec index))
194 (helper (1+ index) active))))
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 (define-public (recording-group-emulate music odef)
200 "Interprets music according to odef, but stores all events in a chronological
201 list, similar to the Recording_group_engraver in 2.8 and earlier"
204 (now-mom (ly:make-moment 0 0))
205 (global (ly:make-global-context odef))
206 (mom-listener (ly:make-listener
207 (lambda (tev) (set! now-mom (ly:event-property tev 'moment)))))
208 (new-context-listener
212 ((child (ly:event-property sev 'context))
213 (this-moment-list (cons (ly:context-id child) '()))
214 (dummy (set! context-list (cons this-moment-list context-list)))
216 (accumulate-event-listener
217 (ly:make-listener (lambda (ev)
218 (set! acc (cons (cons ev #t) acc)))))
220 (ly:make-listener (lambda (tev)
224 (ly:context-property child 'instrumentTransposition))
225 ;; The accumulate-event-listener above creates
226 ;; the list of events in reverse order, so we
227 ;; have to revert it to the original order again
229 (set-cdr! this-moment-list
230 (cons this-moment (cdr this-moment-list)))
232 (ly:add-listener accumulate-event-listener
233 (ly:context-event-source child) 'StreamEvent)
234 (ly:add-listener save-acc-listener
235 (ly:context-event-source global) 'OneTimeStep))))))
236 (ly:add-listener new-context-listener
237 (ly:context-events-below global) 'AnnounceNewContext)
238 (ly:add-listener mom-listener (ly:context-event-source global) 'Prepare)
239 (ly:interpret-music-expression (make-non-relative-music music) global)
242 (define-public (make-part-combine-music parser music-list)
243 (let* ((m (make-music 'PartCombineMusic))
244 (m1 (make-non-relative-music (context-spec-music (first music-list) 'Voice "one")))
245 (m2 (make-non-relative-music (context-spec-music (second music-list) 'Voice "two")))
246 (listener (ly:parser-lookup parser 'partCombineListener))
247 (evs2 (recording-group-emulate m2 listener))
248 (evs1 (recording-group-emulate m1 listener)))
250 (set! (ly:music-property m 'elements) (list m1 m2))
251 (set! (ly:music-property m 'split-list)
252 (if (and (assoc "one" evs1) (assoc "two" evs2))
253 (determine-split-list (reverse! (assoc-get "one" evs1) '())
254 (reverse! (assoc-get "two" evs2) '()))
258 (define-public (determine-split-list evl1 evl2)
259 "EVL1 and EVL2 should be ascending"
262 (voice-state-vec1 (make-voice-states evl1))
263 (voice-state-vec2 (make-voice-states evl2))
264 (result (make-split-state voice-state-vec1 voice-state-vec2)))
266 (define (analyse-time-step result-idx)
267 (define (put x . index)
268 "Put the result to X, starting from INDEX backwards.
270 Only set if not set previously.
272 (let ((i (if (pair? index) (car index) result-idx)))
274 (not (symbol? (configuration (vector-ref result i)))))
276 (set! (configuration (vector-ref result i)) x)
279 (define (copy-state-from state-vec vs)
280 (define (copy-one-state key-idx)
281 (let* ((idx (cdr key-idx))
282 (prev-ss (vector-ref result idx))
283 (prev (configuration prev-ss)))
286 (map copy-one-state (span-state vs)))
288 (define (analyse-notes now-state)
289 (let* ((vs1 (car (voice-states now-state)))
290 (vs2 (cdr (voice-states now-state)))
291 (notes1 (note-events vs1))
292 (durs1 (sort (map (lambda (x) (ly:event-property x 'duration))
295 (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
298 (notes2 (note-events vs2))
299 (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
302 (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
305 (cond ((> (length notes1) 1) (put 'apart))
306 ((> (length notes2) 1) (put 'apart))
307 ((= 1 (+ (length notes2) (length notes1))) (put 'apart))
308 ((and (= (length durs1) 1)
310 (not (equal? (car durs1) (car durs2))))
313 (if (and (= (length pitches1) (length pitches2)))
314 (if (and (pair? pitches1)
317 (< chord-threshold (ly:pitch-steps
318 (ly:pitch-diff (car pitches1)
322 (> 0 (ly:pitch-steps (ly:pitch-diff (car pitches1)
326 ;; copy previous split state from spanner state
328 (if (previous-voice-state vs1)
329 (copy-state-from voice-state-vec1
330 (previous-voice-state vs1)))
331 (if (previous-voice-state vs2)
332 (copy-state-from voice-state-vec2
333 (previous-voice-state vs2)))
334 (if (and (null? (span-state vs1)) (null? (span-state vs2)))
335 (put 'chords)))))))))
337 (if (< result-idx (vector-length result))
338 (let* ((now-state (vector-ref result result-idx))
339 (vs1 (car (voice-states now-state)))
340 (vs2 (cdr (voice-states now-state))))
342 (cond ((not vs1) (put 'apart))
343 ((not vs2) (put 'apart))
345 (let ((active1 (previous-span-state vs1))
346 (active2 (previous-span-state vs2))
347 (new-active1 (span-state vs1))
348 (new-active2 (span-state vs2)))
350 (display (list (when now-state) result-idx
351 active1 "->" new-active1
352 active2 "->" new-active2
354 (if (and (synced? now-state)
355 (equal? active1 active2)
356 (equal? new-active1 new-active2))
357 (analyse-notes now-state)
359 ;; active states different:
362 ;; go to the next one, if it exists.
363 (analyse-time-step (1+ result-idx)))))))
365 (define (analyse-a2 result-idx)
366 (if (< result-idx (vector-length result))
367 (let* ((now-state (vector-ref result result-idx))
368 (vs1 (car (voice-states now-state)))
369 (vs2 (cdr (voice-states now-state))))
370 (if (and (equal? (configuration now-state) 'chords)
372 (let ((notes1 (note-events vs1))
373 (notes2 (note-events vs2)))
374 (cond ((and (= 1 (length notes1))
375 (= 1 (length notes2))
376 (equal? (ly:event-property (car notes1) 'pitch)
377 (ly:event-property (car notes2) 'pitch)))
378 (set! (configuration now-state) 'unisono))
379 ((and (= 0 (length notes1))
380 (= 0 (length notes2)))
381 (set! (configuration now-state) 'unisilence)))))
382 (analyse-a2 (1+ result-idx)))))
384 (define (analyse-solo12 result-idx)
386 (define (previous-config vs)
387 (let* ((pvs (previous-voice-state vs))
388 (spi (if pvs (split-index pvs) #f))
389 (prev-split (if spi (vector-ref result spi) #f)))
391 (configuration prev-split)
394 (define (put-range x a b)
395 ;; (display (list "put range " x a b "\n"))
398 (set! (configuration (vector-ref result i)) x)))
401 ;; (display (list "putting " x "\n"))
402 (set! (configuration (vector-ref result result-idx)) x))
404 (define (current-voice-state now-state voice-num)
405 (define vs ((if (= 1 voice-num) car cdr)
406 (voice-states now-state)))
407 (if (or (not vs) (equal? (when now-state) (when vs)))
409 (previous-voice-state vs)))
411 (define (try-solo type start-idx current-idx)
412 "Find a maximum stretch that can be marked as solo. Only set
413 the mark when there are no spanners active.
415 return next idx to analyse.
417 (if (< current-idx (vector-length result))
418 (let* ((now-state (vector-ref result current-idx))
419 (solo-state (current-voice-state now-state (if (equal? type 'solo1) 1 2)))
420 (silent-state (current-voice-state now-state (if (equal? type 'solo1) 2 1)))
421 (silent-notes (if silent-state (note-events silent-state) '()))
422 (solo-notes (if solo-state (note-events solo-state) '())))
423 ;; (display (list "trying " type " at " (when now-state) solo-state silent-state "\n"))
424 (cond ((not (equal? (configuration now-state) 'apart))
426 ((> (length silent-notes) 0) start-idx)
428 (put-range type start-idx current-idx)
431 (null? (span-state solo-state)))
434 ;; This includes rests. This isn't a problem: long rests
435 ;; will be shared with the silent voice, and be marked
436 ;; as unisilence. Therefore, long rests won't
437 ;; accidentally be part of a solo.
439 (put-range type start-idx current-idx)
440 (try-solo type (1+ current-idx) (1+ current-idx)))
442 (try-solo type start-idx (1+ current-idx)))))
446 (define (analyse-moment result-idx)
447 "Analyse 'apart starting at RESULT-IDX. Return next index. "
448 (let* ((now-state (vector-ref result result-idx))
449 (vs1 (current-voice-state now-state 1))
450 (vs2 (current-voice-state now-state 2))
451 ;; (vs1 (car (voice-states now-state)))
452 ;; (vs2 (cdr (voice-states now-state)))
453 (notes1 (if vs1 (note-events vs1) '()))
454 (notes2 (if vs2 (note-events vs2) '()))
456 (n2 (length notes2)))
457 ;; (display (list "analyzing step " result-idx " moment " (when now-state) vs1 vs2 "\n"))
459 ;; we should always increase.
460 (cond ((and (= n1 0) (= n2 0))
464 (equal? (when vs1) (when now-state))
465 (null? (previous-span-state vs1)))
466 (try-solo 'solo1 result-idx result-idx))
468 (equal? (when vs2) (when now-state))
469 (null? (previous-span-state vs2)))
470 (try-solo 'solo2 result-idx result-idx))
472 (else (1+ result-idx)))
476 (if (< result-idx (vector-length result))
477 (if (equal? (configuration (vector-ref result result-idx)) 'apart)
478 (analyse-solo12 (analyse-moment result-idx))
479 (analyse-solo12 (1+ result-idx))))) ; analyse-solo12
481 (analyse-spanner-states voice-state-vec1)
482 (analyse-spanner-states voice-state-vec2)
485 (display voice-state-vec1)
487 (display voice-state-vec2)
491 (analyse-time-step 0)
498 (lambda (x) (cons (when x) (configuration x)))
499 (vector->list result)))
505 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
507 (define-public (add-quotable parser name mus)
508 (let* ((tab (eval 'musicQuotes (current-module)))
509 (context-list (recording-group-emulate (context-spec-music mus 'Voice)
510 (ly:parser-lookup parser 'partCombineListener))))
511 (if (pair? context-list)
513 ;; cdr : skip name string
514 (list->vector (reverse! (cdar context-list)