]> git.donarmstrong.com Git - lilypond.git/blob - scm/part-combiner.scm
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scm / part-combiner.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2009 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 when #:init-keyword #:when)
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 (when 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 (define-method (note-events (vs <Voice-state>))
43   (define (f? x)
44     (equal? (ly:event-property  x 'class) 'note-event))
45   (filter f? (events vs)))
46
47 (define-method (previous-voice-state (vs <Voice-state>))
48   (let ((i (slot-ref vs 'vector-index))
49         (v (slot-ref vs 'state-vector)))
50     (if (< 0 i)
51         (vector-ref v (1- i))
52         #f)))
53
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55
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
60   ;;
61   (is #:init-keyword #:voice-states #:accessor voice-states)
62   (synced  #:init-keyword #:synced #:init-value  #f #:getter synced?))
63
64
65 (define-method (write (x <Split-state> ) f)
66   (display (when x) f)
67   (display " = " f)
68   (display (configuration x) f)
69   (if (synced? x)
70       (display " synced "))
71   (display "\n" f))
72
73 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
74
75
76 (define (previous-span-state vs)
77   (let ((p (previous-voice-state vs)))
78     (if p (span-state p) '())))
79
80 (define (make-voice-states evl)
81   (let ((vec (list->vector (map (lambda (v)
82                                   (make <Voice-state>
83                                     #:when (caar v)
84                                     #:tuning (cdar v)
85                                     #:events (map car (cdr v))))
86                                 evl))))
87     (do ((i 0 (1+ i)))
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))))
91
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
95 Voice-state objects
96 "
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))
103                       (else #f)))
104            (inc1 (if (and state1 (equal? min (when state1))) 1 0))
105            (inc2 (if (and state2 (equal? min (when state2))) 1 0))
106            (ss-object (if min
107                           (make <Split-state>
108                             #:when min
109                             #:voice-states (cons state1 state2)
110                             #:synced (= inc1 inc2))
111                           #f)))
112       (if state1
113           (set! (split-index state1) ss-idx))
114       (if state2
115           (set! (split-index state2) ss-idx))
116       (if min
117           (helper (1+ ss-idx)
118                   (cons ss-object ss-list)
119                   (+ idx1 inc1)
120                   (+ idx2 inc2))
121           ss-list)))
122   (list->vector (reverse! (helper 0 '() 0  0) '())))
123
124 (define (analyse-spanner-states voice-state-vec)
125
126   (define (helper index active)
127     "Analyse EVS at INDEX, given state ACTIVE."
128
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))
132                  active)
133           active))
134
135     (define (analyse-tie-end active ev)
136       (if (equal? (ly:event-property ev 'class) 'note-event)
137           (assoc-remove! active 'tie)
138           active))
139
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)
145           active))
146
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)))))
151
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)
159                         (else #f)))
160              (sp (ly:event-property ev 'span-direction)))
161         (if (and (symbol? key) (ly:dir? sp))
162             (if (= sp STOP)
163                 (assoc-remove! active key)
164                 (acons key
165                        (split-index (vector-ref voice-state-vec index))
166                        active))
167             active)))
168
169     (define (analyse-events active evs)
170       "Run all analyzers on ACTIVE and EVS"
171       (define (run-analyzer analyzer active evs)
172         (if (pair? evs)
173             (run-analyzer analyzer (analyzer active (car evs)) (cdr evs))
174             active))
175       (define (run-analyzers analyzers active evs)
176         (if (pair? analyzers)
177             (run-analyzers (cdr analyzers)
178                            (run-analyzer (car analyzers) active evs)
179                            evs)
180             active))
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)
185                       active evs)
186        active<?))
187
188     ;; must copy, since we use assoc-remove!
189     (if (< index (vector-length voice-state-vec))
190         (begin
191           (set! active (analyse-events active (events (vector-ref voice-state-vec index))))
192           (set! (span-state (vector-ref voice-state-vec index))
193                 (list-copy active))
194           (helper (1+ index) active))))
195
196   (helper 0 '()))
197
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"
202   (let*
203      ((context-list '())
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
209        (ly:make-listener
210         (lambda (sev)
211           (let*
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)))
215               (acc '())
216               (accumulate-event-listener
217                   (ly:make-listener (lambda (ev)
218                                        (set! acc (cons (cons ev #t) acc)))))
219               (save-acc-listener
220                   (ly:make-listener (lambda (tev)
221                       (if (pair? acc)
222                         (let ((this-moment
223                               (cons (cons now-mom
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
228                                 (reverse acc))))
229                           (set-cdr! this-moment-list
230                                 (cons this-moment (cdr this-moment-list)))
231                           (set! acc '())))))))
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)
240     context-list))
241
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)))
249
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) '()))
255               '()))
256     m))
257
258 (define-public (determine-split-list evl1 evl2)
259   "EVL1 and EVL2 should be ascending"
260   (let* ((pc-debug #f)
261          (chord-threshold 8)
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)))
265
266     (define (analyse-time-step result-idx)
267       (define (put x . index)
268         "Put the result to X, starting from INDEX backwards.
269
270 Only set if not set previously.
271 "
272         (let ((i (if (pair? index) (car index) result-idx)))
273           (if (and (<= 0 i)
274                    (not (symbol? (configuration (vector-ref result i)))))
275               (begin
276                 (set! (configuration (vector-ref result i)) x)
277                 (put x (1- i))))))
278
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)))
284             (if (symbol? prev)
285                 (put prev))))
286         (map copy-one-state (span-state vs)))
287
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))
293                                  notes1)
294                             ly:duration<?))
295                (pitches1 (sort (map (lambda (x) (ly:event-property x 'pitch))
296                                     notes1)
297                                ly:pitch<?))
298                (notes2 (note-events vs2))
299                (durs2 (sort (map (lambda (x) (ly:event-property x 'duration))
300                                  notes2)
301                             ly:duration<?))
302                (pitches2 (sort (map (lambda (x) (ly:event-property x 'pitch))
303                                     notes2)
304                                ly: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)
309                       (= (length durs2) 1)
310                       (not (equal? (car durs1) (car durs2))))
311                  (put 'apart))
312                 (else
313                  (if (and (= (length pitches1) (length pitches2)))
314                      (if (and (pair? pitches1)
315                               (pair? pitches2)
316                               (or
317                                (< chord-threshold (ly:pitch-steps
318                                                    (ly:pitch-diff (car pitches1)
319                                                                   (car pitches2))))
320
321                                ;; voice crossings:
322                                (> 0 (ly:pitch-steps (ly:pitch-diff (car pitches1)
323                                                                    (car pitches2))))
324                                ))
325                          (put 'apart)
326                          ;; copy previous split state from spanner state
327                          (begin
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)))))))))
336
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))))
341
342             (cond ((not vs1) (put 'apart))
343                   ((not vs2) (put 'apart))
344                   (else
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)))
349                      (if #f ; debug
350                          (display (list (when now-state) result-idx
351                                         active1 "->" new-active1
352                                         active2 "->" new-active2
353                                         "\n")))
354                      (if (and (synced? now-state)
355                               (equal? active1 active2)
356                               (equal? new-active1 new-active2))
357                          (analyse-notes now-state)
358
359                          ;; active states different:
360                          (put 'apart)))
361
362                    ;; go to the next one, if it exists.
363                    (analyse-time-step (1+ result-idx)))))))
364
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)
371                      vs1 vs2)
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)))))
383
384     (define (analyse-solo12 result-idx)
385
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)))
390           (if prev-split
391               (configuration prev-split)
392               'apart)))
393
394       (define (put-range x a b)
395         ;; (display (list "put range "  x a b "\n"))
396         (do ((i a (1+ i)))
397             ((> i b) b)
398           (set! (configuration (vector-ref result i)) x)))
399
400       (define (put x)
401         ;; (display (list "putting "  x "\n"))
402         (set! (configuration (vector-ref result result-idx)) x))
403
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)))
408             vs
409             (previous-voice-state vs)))
410
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.
414
415       return next idx to analyse.
416 "
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))
425                      current-idx)
426                     ((> (length silent-notes) 0) start-idx)
427                     ((not solo-state)
428                      (put-range type start-idx current-idx)
429                      current-idx)
430                     ((and
431                       (null? (span-state solo-state)))
432
433                      ;;
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.
438                      ;;
439                      (put-range type start-idx current-idx)
440                      (try-solo type (1+ current-idx) (1+  current-idx)))
441                     (else
442                      (try-solo type start-idx (1+ current-idx)))))
443             ;; try-solo
444             start-idx))
445
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) '()))
455                (n1 (length notes1))
456                (n2 (length notes2)))
457           ;; (display (list "analyzing step " result-idx "  moment " (when now-state) vs1 vs2  "\n"))
458           (max
459            ;; we should always increase.
460            (cond ((and (= n1 0) (= n2 0))
461                   (put 'apart-silence)
462                   (1+ result-idx))
463                  ((and (= n2 0)
464                        (equal? (when vs1) (when now-state))
465                        (null? (previous-span-state vs1)))
466                   (try-solo 'solo1 result-idx result-idx))
467                  ((and (= n1 0)
468                        (equal? (when vs2) (when now-state))
469                        (null? (previous-span-state vs2)))
470                   (try-solo 'solo2 result-idx result-idx))
471
472                  (else (1+ result-idx)))
473            ;; analyse-moment
474            (1+ result-idx))))
475
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
480
481     (analyse-spanner-states voice-state-vec1)
482     (analyse-spanner-states voice-state-vec2)
483     (if #f
484         (begin
485           (display voice-state-vec1)
486           (display "***\n")
487           (display voice-state-vec2)
488           (display "***\n")
489           (display result)
490           (display "***\n")))
491     (analyse-time-step 0)
492     ;; (display result)
493     (analyse-a2 0)
494     ;;(display result)
495     (analyse-solo12 0)
496     ;; (display result)
497     (set! result (map
498                   (lambda (x) (cons (when x) (configuration x)))
499                   (vector->list result)))
500     (if #f ;; pc-debug
501          (display result))
502     result))
503
504
505 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
506
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)
512         (hash-set! tab name
513                    ;; cdr : skip name string
514                    (list->vector (reverse! (cdar context-list)
515                                            '()))))))