-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharmony-rules.scm
2258 lines (2103 loc) · 80.1 KB
/
harmony-rules.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Lilypond Harmony Rules tests harmony rules of Lilypond scores.
;; Copyright (C) 2021 Stéphane SOPPERA
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(define-module (harmony-rules)
#:use-module (lily)
#:use-module (documentation)
#:use-module (tools)
#:use-module (oop goops)
#:use-module ((srfi srfi-1)
#:select (fold every concatenate))
#:use-module (ice-9 receive)
#:export (format-music
dump-all
<timed>
<timed-music>
<finite-timed-music>
active?
<infinite-timed-music>
<timed-note>
<timed-key>
<timed-figure>
<events>
finite-timed-musics/time
infinite-timed-musics/time
events!
events-split
<voices>
note-event
pitch
movement
movement-interval
interval
pitches
chord-from-notes
chords-from-notes
chord-from-figures
chords-from-figures
get-voices
<interval>
quality<?
compound?
simple
compound
pitches->interval
semi-tones
ascending
figures?
normalize-pitch
<chord>
type
next-octave
previous-octave
music
bass
chord
key-pitches
<parallel-consonance>
parallel-consonance
<augmented-second>
end-moment
moment
notes
figures
keys
bars
voice-count
moments
number
quality
tonic
third
fifth
seventh
inversion
moment-index
voice1
voice2
interval
kind
voice
key-pitch-alist
major-pitch-alist
minor-pitch-alist
augmented-second
unique
write-short
write-long
checkHarmonyRules
automaticChords
splitVoices))
(set-current-module-documentation!
"Module defining functions used to analyse harmony.
It also defines music functions to be used in Lilypond,
\\checkHarmonyRules, \\automaticChords, \\splitVoices.")
(define-method (write-short o)
"Shortcut without port."
(write-format o (current-output-port) #f))
(define-method (write-short o port)
"Public API of @code{write-format}."
(write-format o port #f))
(define-method (write-long o)
"Shortcut without port."
(write-format o (current-output-port) ""))
(define-method (write-long o port)
"Public API of @code{write-format}."
(write-format o port ""))
(define-method (write-format o port indent)
"Default method calling (write)."
(write o port))
;; Lilypond 2.23 does not define <Prob> as a public symbol.
(define <LyMusicClass> (class-of (ly:make-music 'x)))
(define-method (write-format (o <LyMusicClass>) port indent)
"Display @code{ly:music} specifically.
All other types are printed using @code{write}."
(cond
((ly:music? o)
(let ((indent (and indent (string-append indent " "))))
(display "(music " port)
(display (ly:music-property o 'name) port)
(for-each
(lambda (p)
(if (not (memq (car p) '(origin)))
(begin
(if indent
(begin
(display "\n" port)
(display indent port))
(display " " port))
(display "'" port)
(display (car p) port)
(display " " port)
(write-format (cdr p) port indent))))
(ly:music-mutable-properties o))
(display ")" port)))
(else (write o port))))
(define-method (write-format (o <pair>) port indent)
"Specific formatting for @code{<pair>}."
(let ((indent (and indent (string-append indent " "))))
(display "(" port)
(if indent
(begin
(display "\n" port)
(display indent port)))
(write-format (car o) port indent)
(let loop ((head (cdr o)))
(cond
((null? head))
((pair? head)
(if indent
(begin
(display "\n" port)
(display indent port))
(display " " port))
(write-format (car head) port indent)
(loop (cdr head)))
(else
(display " . " port)
(write-format head port indent))))
(display ")" port)))
(define-method (write-format (v <vector>) port indent)
"Specific formatting for @code{<vector>}."
;; FIXME: write a direct format for vectors.
(display "#" port)
(write-format (vector->list v) port indent))
(define-method (write-format (o <object>) port indent)
"Specific formatting for @code{<object>}."
(let ((indent (and indent (string-append indent " "))))
(display "(" port)
(display (class-name (class-of o)) port)
(for-each
(lambda (slot-def)
(let ((slot (slot-definition-name slot-def)))
(if indent
(begin
(display "\n" port)
(display indent port))
(display " " port))
(display slot port)
(display " " port)
(if (slot-bound? o slot)
(write-format (slot-ref o slot) port indent)
(display "<unbound>" port))))
(class-slots (class-of o)))
(display ")" port)))
(define (format-music music . config)
"Format input @var{music}.
The @var{config} parameter is parameters identified by symbols
followed by their value. For example @code{(format-music m 'a 3 'b 2)}
will specify parameter 'a with value 3 and parameter 'b with value 2.
The supported parameters are:
- 'initial-indent: a string used as base indentation. By default an
empty string is used.
- 'hidden-properties: a list of symbols representing the music
properties that shoul be hidden.
- 'one-line: #t to use a one-line format, #f to use multi-line
format. By default #f."
(let ((hidden-properties
'(elements
element
articulations
name
origin
iterator-ctor
length-callback
to-relative-callback
start-callback
elements-callback))
(initial-indent "")
(one-line #f)
(strings '()))
;; Parse CONFIG.
(if (not (list? config))
(error "unexpected CONFIG, should be a list"))
(let ((n (length config))
(initial-config config))
(if (not (even? n))
(error (format "list CONFIG length is not even: ~a" n)))
(do ((head initial-config (cddr head)))
((null? head))
(let ((name (car head))
(value (cadr head)))
(if (not (symbol? name))
(error "list CONFIG must be like '(symbol value symbol value...)"))
(case name
((initial-indent) (set! initial-indent value))
((hidden-properties) (set! hidden-properties (append hidden-properties value)))
((one-line) (set! one-line value))
(else (error (format "unexpected property ~s in CONFIG" name)))))))
;; Do stuff.
(let rec ((music music)
(indent initial-indent))
(let ((n (ly:music-property music 'name))
(e (ly:music-property music 'element))
(es (ly:music-property music 'elements))
(as (ly:music-property music 'articulations)))
(if one-line
(push! (format "[~a" n) strings)
(push! (format "~a* ~a\n" indent n) strings))
(for-each
(lambda (p)
(let ((v (ly:music-property music p)))
(if (and (not (null? v))
(not (memq p hidden-properties)))
(if one-line
(push! (format " ~a:~a" (symbol->string p) v) strings)
(push! (format "~a ~a: ~a\n" indent (symbol->string p) v)
strings)))))
all-music-properties)
(if (not (null? es))
(begin
(if one-line
(push! (format " elements: [" indent) strings)
(push! (format "~a elements:\n" indent) strings))
(for-each (lambda (m) (rec m (string-append indent " "))) es)
(if one-line
(push! "]" strings))))
(if (not (null? as))
(begin
(if one-line
(push! (format " articulations: [") strings)
(push! (format "~a articulations:\n" indent) strings))
(for-each (lambda (m) (rec m (string-append indent " "))) as)
(if one-line
(push! "]" strings))))
(if (not (null? e))
(begin
(if one-line
(push! " element: " strings)
(push! (format "~a element:\n" indent) strings))
(rec e (string-append indent " "))
(if one-line
(push! "]" strings))))
(if one-line
(push! "]" strings))))
(string-join (reverse strings) "")))
(define (dump-all music)
(display (format-music music)))
(define-public (best-duration-of-length moment)
"Return a @code{ly:duration} from the input length as @code{ly:moment}.
Contrary to Lilypond's @code{make-duration-of-length} that scales a
whole note, this function tries to find the best note that fits the
input length."
(if (not (= (ly:moment-grace moment) 0))
(error "this function does not support grace notes"))
(let ((moment-frac (ly:moment-main moment)))
(define (log2 v) (/ (log v) (log 2)))
(cond
((< moment-frac 0) (error "negative moment"))
((= moment-frac 0) (error "moment is zero"))
(else
(let* ((l (inexact->exact (- (floor (log2 moment-frac)))))
(d (inexact->exact
(floor
(- (+ 1
(log2 (- 1 (/ moment-frac
(* 2 (expt 2 (- l)))))))))))
(dur (ly:make-duration l d)))
(if (not (equal? (ly:duration-length dur)
moment))
;; By default (for example with tuplets), we return the
;; basic duration.
(make-duration-of-length moment)
dur))))))
(define-class-with-doc <timed> ()
"Some event that has a time specified by a @code{ly:moment}."
(moment #:init-value (ly:make-moment 0)
#:getter moment
#:init-keyword #:moment))
(define-method (equal? (a <timed>) (b <timed>))
(equal? (moment a) (moment b)))
(define-method (write (e <timed>) port)
(display "#<" port)
(display (class-name (class-of e)) port)
(display " " port)
(write (moment e) port)
(display ">" port))
(define-class-with-doc <timed-music> (<timed>)
"A @code{ly:music} happening at a given @code{ly:moment}."
(music #:getter music
#:init-keyword #:music))
(define-method (equal? (a <timed-music>) (b <timed-music>))
(and (equal? (music a) (music b))
(next-method)))
(define-method (write (e <timed-music>) port)
(display "#<" port)
(display (class-name (class-of e)) port)
(display " " port)
(write (moment e) port)
(display " (make-music '" port)
(display (symbol->string
(ly:music-property (music e) 'name))
port)
(for-each
(lambda (prop)
(display " '" port)
(display (symbol->string (car prop)) port)
(display " " port)
(write (cdr prop) port))
(ly:music-mutable-properties (music e)))
(display ")>" port))
(define-class-with-doc <finite-timed-music> (<timed-music>)
"A @code{<timed-music>} for music that have a 'duration.")
(define-method (duration (ftm <finite-timed-music>))
"Returns the @code{'duration} of the @code{ly:music}."
(if (eq? (ly:music-property (music ftm) 'name) 'EventChord)
;; For chords we use the duration of the first element, if present.
(if (not (null? (ly:music-property (music ftm) 'elements)))
(ly:music-property (car (ly:music-property (music ftm) 'elements)) 'duration)
(ly:make-duration 0 0 0 1))
(ly:music-property (music ftm) 'duration)))
(define-method (end-moment (ftm <finite-timed-music>))
"Returns the end @code{ly:moment} of this finite event (not included)."
(ly:moment-add
(moment ftm)
(ly:duration-length (duration ftm))))
(define-method (active? (ftm <finite-timed-music>) m)
"Returns #t if input @code{ly:moment} @var{m} is in [start, end) of FTM.
Here start is FTM's moment and end is this moment shifted by its
duration."
(and (not (ly:moment<? m (moment ftm)))
(ly:moment<? m (end-moment ftm))))
(define-class-with-doc <infinite-timed-music> (<timed-music>)
"A @code{<timed-music>} for music that has an \"infinite\" duration
(i.e. music that is active until replaced by another music).")
(define-method (active? (itm <infinite-timed-music>) m)
"Returns @code{#t} if input @code{ly:moment} M is greater of equal
to the moment fo ITM."
(not (ly:moment<? m (moment itm))))
(define-class-with-doc <timed-note> (<finite-timed-music>)
"Slot @code{'music} contains @code{'NoteEvent}.")
(define-class-with-doc <timed-key> (<infinite-timed-music>)
"Slot @code{'music} contains @code{'KeyChangeEvent}.")
(define-class-with-doc <timed-figure> (<finite-timed-music>)
"Slot @code{'music} contains @code{'EventChord} with @code{BassFigureEvent} elements.")
(define-class <events> ()
;; A list of <timed-note>.
(notes #:init-value '() #:getter notes #:init-keyword #:notes)
;; A list of <timed-key>.
(keys #:init-value '() #:getter keys #:init-keyword #:keys)
;; A list of <timed-figure>.
(figures #:init-value '() #:getter figures #:init-keyword #:figures)
;; A list of <ly:moment> for specific bars.
(bars #:init-value '() #:getter bars #:init-keyword #:bars))
(define-method (equal? (a <events>) (b <events>))
(and (equal? (notes a) (notes b))
(equal? (keys a) (keys b))
(equal? (figures a) (figures b))
(equal? (bars a) (bars b))))
(define-method (write (o <events>) port)
(display "#<" port)
(display (class-name (class-of o)) port)
(display " #:notes (" port)
(for-each
(lambda (e)
(display "\n " port)
(display e port))
(notes o))
(display ")" port)
(display " #:keys (" port)
(for-each
(lambda (e)
(display "\n " port)
(display e port))
(keys o))
(display ")" port)
(display " #:figures (" port)
(for-each
(lambda (e)
(display "\n " port)
(display e port))
(figures o))
(display ")" port)
(display " #:bars " port)
(display (bars o) port)
(display ">" port))
(define (expand-repeats! music)
"Expand pitchless repeated notes and chords.
This function modifies the input music."
(expand-repeat-notes!
(expand-repeat-chords!
(cons 'rhythmic-event
(ly:parser-lookup '$chord-repeat-events))
music)))
(define (events! music)
"Returns a <events> record.
The input music is modified to expand repeats."
(let ((notes '())
(keys '())
(figures '())
(bars '()))
(let rec ((music (expand-repeats! music))
(start-t (ly:make-moment 0)))
(let ((elts (ly:music-property music 'elements))
(elt (ly:music-property music 'element))
(simultaneous (music-is-of-type? music 'simultaneous-music))
(name (ly:music-property music 'name)))
(cond
((eq? name 'NoteEvent)
(push! (make <timed-note> #:moment start-t #:music music)
notes)
(ly:moment-add
start-t
(ly:duration-length (ly:music-property music 'duration))))
((figures? music)
(push! (make <timed-figure> #:moment start-t #:music music)
figures)
(ly:moment-add
start-t
;; Use the duration of the first BassFigureEvent.
(ly:duration-length (ly:music-property
(car (ly:music-property music 'elements))
'duration))))
((eq? name 'KeyChangeEvent)
(push! (make <timed-key> #:moment start-t #:music music)
keys)
start-t)
((or
;; Lilypond 2.23 uses 'BarEvent with 'bar-type.
(eq? name 'BarEvent)
;; Lilypond <= 2.22 uses 'ContextSpeccedMusic of type
;; 'Timing with property 'whichBar.
(and (eq? name 'ContextSpeccedMusic)
(eq? (ly:music-property music 'context-type) 'Timing)
(eq? (ly:music-property elt 'name) 'PropertySet)
(eq? (ly:music-property elt 'symbol) 'whichBar)))
(push! start-t bars)
start-t)
((music-is-of-type? music 'rhythmic-event)
;; Here we cover all other rhythmic events, including
;; SkipEvent and RestEvent.
(ly:moment-add
start-t
(ly:duration-length (ly:music-property music 'duration))))
((eq? name 'UnfoldedRepeatedMusic)
(do ((i 0 (+ i 1)))
((= i (ly:music-property music 'repeat-count)) start-t)
(set! start-t (rec elt start-t))))
((not (null? elts))
(let ((new-start-t start-t))
(while (not (null? elts))
(let ((end-t (rec (car elts) start-t)))
(if simultaneous
(if (ly:moment<? new-start-t end-t)
(set! new-start-t end-t))
(begin
(set! start-t end-t)
(set! new-start-t end-t))))
(set! elts (cdr elts)))
new-start-t))
((not (null? elt))
(rec elt start-t))
(else start-t))))
(make <events>
#:notes (reverse notes)
#:keys (reverse keys)
#:figures (reverse figures)
#:bars
(do ((head (sort bars (lambda (a b) (ly:moment<? a b))) (cdr head))
(unique '())
(last #f (car head)))
((null? head) (reverse unique))
(if (not (equal? (car head) last))
(push! (car head) unique))))))
(define-method (events-split (events <events>))
"Splits the input <events> at bars and returns a list of new
<events>."
(let ((sorted-notes
(sort (notes events)
(lambda (a b)
(ly:moment<? (moment a) (moment b)))))
(sorted-keys
(sort (keys events)
(lambda (a b)
(ly:moment<? (moment a) (moment b)))))
(sorted-figures
(sort (figures events)
(lambda (a b)
(ly:moment<? (moment a) (moment b)))))
(sorted-bars
(sort (bars events) ly:moment<?)))
(define (end start t)
"Find the first <timed> that has a moment >= t."
(do ((head start (cdr head)))
((or (null? head)
(and (not (null? t))
(not (ly:moment<? (moment (car head))
(car t)))))
head)))
(define (slice start end)
(do ((head start (cdr head))
(ret '()))
((eq? head end)
(reverse ret))
(push! (car head) ret)))
(do ((notes-start sorted-notes notes-end)
(notes-end '())
(figures-start sorted-figures figures-end)
(figures-end '())
(keys-first sorted-keys keys-next)
(keys-next '())
(splits '())
(t sorted-bars (if (null? t) '() (cdr t))))
((null? notes-start) (reverse splits))
;; Find the first note that has a moment >= t.
(set! notes-end (end notes-start t))
;; Find the first note that has a moment >= t.
(set! figures-end (end figures-start t))
;; Find the last key that has a moment <= t.
(set! keys-next
(do ((head keys-first (cdr head))
(last '() head))
((or (null? head)
(and (not (null? t))
(ly:moment<? (car t)
(moment (car head)))))
;; Here head point either to '() or to the the first
;; event with a moment > t. Hence last should point
;; to the last event with a moment <= t.
last)))
;; Create a split.
(let ((notes (slice notes-start notes-end))
(figures (slice figures-start figures-end))
(keys (do ((head keys-first (cdr head))
(keys '()))
((or (null? head)
(and (not (null? t))
(not (ly:moment<? (moment (car head))
(car t)))))
(reverse keys))
(push! (car head) keys))))
(push! (make <events> #:notes notes #:figures figures #:keys keys)
splits)))))
(define (finite-timed-musics/time finite-timed-musics moments)
"Returns a vector of the same length as MOMENTS that contains lists
of the <finite-timed-music> events in FINITE-TIMED-MUSICS that are
active at each moment.
The MOMENTS must be sorted and without duplicates."
(if (or (not (sorted? moments ly:moment<?))
(do ((head moments (cdr head))
(last #f (car head)))
((or (null? head)
(equal? (car head) last))
;; We have a duplicate if we did not stop at end.
(not (null? head)))))
(error "input MOMENTS must be sorted and without duplicates"))
(let ((v (make-vector (length moments))))
(do ((i 0 (+ i 1))
(head moments (cdr head)))
((null? head) v)
;; TODO: this could be optimized by creating start and stop
;; events for each <finite-time-music>.
(vector-set!
v i
(filter (lambda (ftm) (active? ftm (car head)))
finite-timed-musics)))))
(define (last-moment<= itms m)
"Find the last items whose moment is less or equal than M. It
returns (values FOUND TAIL). FOUND is the found item, #f if it does
not exist (all elements are greater than M). TAIL is the tail of the
list to consider for the next search."
(do ((head itms (cdr head))
(last '() head))
((or (null? head) (ly:moment<? m (moment (car head))))
(if (null? last)
(values #f head)
(values (car last) last)))))
(define (infinite-timed-musics/time infinite-timed-musics moments)
"Returns a vector of the same length as MOMENTS that contains the
latest <infinite-timed-music> event in INFINITE-TIMED-MUSICS that is
active at each moment.
The MOMENTS must be sorted and without duplicates."
(if (or (not (sorted? moments ly:moment<?))
(do ((head moments (cdr head))
(last #f (car head)))
((or (null? head)
(equal? (car head) last))
;; We have a duplicate if we did not stop at end.
(not (null? head)))))
(error "input MOMENTS must be sorted and without duplicates"))
(let ((sorted (sort infinite-timed-musics
(lambda (a b) (ly:moment<? (moment a) (moment b)))))
(v (make-vector (length moments))))
(let loop ((i 0)
(moments-head moments)
(sorted-head sorted))
(if (null? moments-head)
v
(receive
(found sorted-next)
(last-moment<= sorted-head (car moments-head))
(vector-set! v i found)
(loop (+ i 1) (cdr moments-head) sorted-next))))))
(define-class <voices> ()
;; Number of voices.
(voice-count #:init-value 0
#:getter voice-count
#:init-keyword #:voice-count)
;; A sorted verctor of ly:moment.
(moments #:init-value (make-vector 0)
#:getter moments
#:init-keyword #:moments)
;; All <timed-note> in a packed vector. For voice index v and moment index t, the
;; corresponding note is at index `v + voice-count * t`.
(notes #:init-value (make-vector 0)
#:getter notes
#:init-keyword #:notes)
;; All <timed-figure> in a vector. Value #f is used to indicate no
;; figure exists at a given moment.
(figures #:init-value (make-vector 0)
#:getter figures
#:init-keyword #:figures)
;; All <timed-key> in a vector. Value #f is used to indicate no key
;; exists at a given moment.
(keys #:init-value (make-vector 0)
#:getter keys
#:init-keyword #:keys))
(define-method (equal? (a <voices>) (b <voices>))
(and (equal? (voice-count a) (voice-count b))
(equal? (moments a) (moments b))
(equal? (notes a) (notes b))
(equal? (figures a) (figures b))
(equal? (keys a) (keys b))))
(define-method (write (o <voices>) port)
(display "#<" port)
(display (class-name (class-of o)) port)
(display " #:voice-count " port)
(display (voice-count o) port)
(display " #:moments " port)
(write (moments o) port)
(display " #:notes " port)
(display (notes o) port)
(display " #:figures " port)
(display (figures o) port)
(display " #:keys " port)
(display (keys o) port)
(display ">" port))
(define-method (note (voices <voices>) (voice <integer>) (moment-index <integer>))
"Return the <timed-note> of the voice at index VOICE and at the
moment indentified by MOMENT-INDEX. #f if the voice is silent."
(vector-ref (notes voices)
(+ voice (* (voice-count voices)
moment-index))))
;; FIXME: support #f.
(define-method (note-event (voices <voices>) (voice <integer>) (moment-index <integer>))
"Return the NoteEvent of the voice at index VOICE and at the moment
indentified by MOMENT-INDEX."
(music (note voices voice moment-index)))
;; FIXME: support #f.
(define-method (pitch (voices <voices>) (voice <integer>) (moment-index <integer>))
"Return the 'pitch of the NoteEvent of the VOICE and at the MOMENT-INDEX."
(ly:music-property (note-event voices voice moment-index) 'pitch))
;; FIXME: support #f.
(define-method (figure (voices <voices>) (moment-index <integer>))
"Return the <timed-figure> at MOMENT-INDEX. Returns #f if there is no such figure."
(vector-ref (figures voices) moment-index))
(define-method (key (voices <voices>) (moment-index <integer>))
"Return the <timed-key> at MOMENT-INDEX. Returns #f if there is no such key."
(vector-ref (keys voices) moment-index))
;; FIXME: support #f returning 'undefined.
(define-method (movement (voices <voices>) (voice <integer>) (moment-index <integer>))
"Return the direction of movement of the voice at index VOICE
between moment MOMENT-INDEX - 1 and MOMENT-INDEX.
The returned value is a symbol, either 'none, 'up or 'down."
(if (= moment-index 0)
'none
(let ((p1 (pitch voices voice (- moment-index 1)))
(p2 (pitch voices voice moment-index)))
(cond
((ly:pitch<? p1 p2) 'up)
((ly:pitch<? p2 p1) 'down)
(else 'none)))))
;; FIXME: support #f.
(define-method (movement-interval (voices <voices>) (voice <integer>) (moment-index <integer>))
"Return the interval of movement of the voice VOICE between moment
MOMENT-INDEX - 1 and MOMENT-INDEX.
Returns unisson if the MOMENT-INDEX is 0."
(if (= moment-index 0)
(make <interval> #:number 1 #:quality 'perfect)
(pitches->interval
(pitch voices voice (- moment-index 1))
(pitch voices voice moment-index))))
;; FIXME: support #f.
(define-method (interval (voices <voices>) (v1 <integer>) (v2 <integer>) (moment-index <integer>))
"Return the interval between voices V1 and V2 at given MOMENT-INDEX."
(pitches->interval
(pitch voices v1 moment-index)
(pitch voices v2 moment-index)))
(define-method (pitches (voices <voices>) (moment-index <integer>))
"Returns the list of pitches of all voices at given MOMENT-INDEX in order of voices."
(let loop ((v (voice-count voices))
(ret '()))
(if (= v 0)
ret
(let ((n (note voices (- v 1) moment-index)))
(if n
(loop (- v 1)
(cons (pitch voices (- v 1) moment-index)
ret))
(loop (- v 1) ret))))))
;; FIXME: add key changes
;; FIXME: add time signatures
(define-method (music (voices <voices>) (voice <integer>))
"Returns a sequential music with all notes of the given VOICE."
(do ((t 0 (+ t 1))
(events (if (and (> (vector-length (moments voices)) 0)
(ly:moment<? (ly:make-moment 0) (vector-ref (moments voices) 0)))
(list (make-music
'SkipEvent
'duration (best-duration-of-length
(vector-ref (moments voices) 0))))
'())))
((= t (vector-length (moments voices)))
(make-sequential-music (reverse events)))
(let ((duration (best-duration-of-length
(if (< t (- (vector-length (moments voices)) 1))
(ly:moment-sub (vector-ref (moments voices) (+ t 1))
(vector-ref (moments voices) t))
;; FIXME: need to store durations.
(ly:make-moment 1/4)))))
(push! (if (note voices voice t)
(make-music 'NoteEvent
'duration duration
'pitch (pitch voices voice t))
(make-music 'SkipEvent 'duration duration))
events))))
(define-method (chords-from (voices <voices>) chord-from)
"Returns a sequential music suitable to be displayed in a ChordNames context.
CHORD-FROM is a function that takes ((voices <voices>) (moment-index
<integer>)) parameters."
(do ((t 0 (+ t 1))
(event-chords
(if (and (> (vector-length (moments voices)) 0)
(ly:moment<? (ly:make-moment 0) (vector-ref (moments voices) 0)))
;; Initialize with a skip if the voice does not start at 0.
(list (make-music
'SkipEvent
'duration (best-duration-of-length
(vector-ref (moments voices) 0))))
'())))
((= t (vector-length (moments voices)))
(make-sequential-music (reverse event-chords)))
(let ((chord (chord-from voices t))
(duration (best-duration-of-length
(if (< t (- (vector-length (moments voices)) 1))
(ly:moment-sub (vector-ref (moments voices) (+ t 1))
(vector-ref (moments voices) t))
;; FIXME: need to store durations.
(ly:make-moment 1/4)))))
(push! (if chord
(music chord duration)
(make-music 'SkipEvent 'duration duration))
event-chords))))
(define-method (chords-from-notes (voices <voices>))
"Returns a sequential music suitable to be displayed in a ChordNames context."
(chords-from voices chord-from-notes))
(define-method (chords-from-figures (voices <voices>))
"Returns a sequential music suitable to be displayed in a ChordNames context."
(chords-from voices chord-from-figures))
(define-method (chord-from-notes (voices <voices>) (moment-index <integer>))
"Returns the <chord> computed from the notes of VOICES at given MOMENT-INDEX."
(chord (pitches voices moment-index)))
(define-method (chord-from-figures (voices <voices>) (moment-index <integer>))
"Returns the <chord> computed from the figures and key of VOICES at
given MOMENT-INDEX.
Returns #f if no bass note exist at a given time."
(and (note voices 0 moment-index) ; Is there a bass note?
(chord (let ((k (key voices moment-index)))
(if k
(music k)
;; Use C major if no key is given.
#{ \key c \major #}))
(pitch voices 0 moment-index)
(let ((fig (figure voices moment-index)))
(and fig
(music fig))))))
;; FIXME: support missing voices (starting with less voices than at the end).
(define-method (get-voices (events <events>))
"Return all voices of a given <events> as an <voices> record."
(let* ((moments
(unique
ly:moment<?
(concatenate
(list
(map moment (notes events))
;; FIXME: include end-moments to support end and silences.
;; (map end-moment (notes events))
(map moment (figures events))
;; (map end-moment (figures events))
))))
(notes-per-moment (finite-timed-musics/time (notes events) moments))
(figures-per-moment (finite-timed-musics/time (figures events) moments))
(voice-count (let loop ((i 0) (count 0))
(if (= i (vector-length notes-per-moment))
count
(loop (+ i 1) (max count
(length (vector-ref notes-per-moment i))))))))
(define (notes)
(do ((i 0 (+ i 1))
(moment-head moments (cdr moment-head))
;; FIXME: replace #f with <rest> or <silence> to use
;; generic functions for automatic behavior.
(ret (make-vector (* voice-count (length moments)) #f)))
((null? moment-head) ret)
(let ((sorted (sort (vector-ref notes-per-moment i)
(lambda (a b)
(ly:pitch<? (ly:music-property (music a) 'pitch)
(ly:music-property (music b) 'pitch))))))
(do ((v 0 (+ v 1))
(notes-head sorted (cdr notes-head)))
((null? notes-head))
(vector-set! ret (+ v (* voice-count i)) (car notes-head))))))
;; FIXME: unit test this
(define (figures)
(do ((i 0 (+ i 1))
(moment-head moments (cdr moment-head))
(ret (make-vector (length moments) #f)))
((null? moment-head) ret)
(let ((figures (vector-ref figures-per-moment i)))
(if (> (length figures) 1)
(for-each (lambda (f)
(ly:input-warning (ly:music-property (music f) 'origin)
"multiple figures at this moment, we ignored this one"))
figures))
(if (not (null? figures))
(vector-set! ret i (car figures))))))
(make <voices>
#:voice-count voice-count
#:moments (list->vector moments)
#:notes (notes)
#:figures (figures)
;; FIXME: unit-test this
#:keys (infinite-timed-musics/time (keys events) moments))))
(define-class <interval> ()
;; The "number" of letter names it encompasses. 1 for unisson, 8 for
;; octave. For compound intervals it contains the compound
;; number. For a ninth (c - d') it will be 9, even though the
;; corresponding simple interval number is 2.
;;
;; This number must be >= 1.
(number #:init-value 1
#:getter number
#:init-keyword #:number)
;; The "quality": 'perfect, 'major, 'minor, 'augmented or 'diminished.
;;
;; Not all qualities are valid, depending on the interval number:
;;
;; - 'perfect can only be used for numbers (for all n >= 0):
;; * 1 + 7n
;; * 4 + 7n
;; * 5 + 7n
;;
;; - 'minor and 'major can only be used for numbers (for all n >= 0):
;; * 2 + 7n
;; * 3 + 7n
;; * 6 + 7n
;; * 7 + 7n
;;
;; - augmented and diminished can be used for all numbers.
(quality #:init-value 'perfect
#:getter quality
#:init-keyword #:quality))
(define (quality<? a b)
"Compare qualities A and B as defined by (quality <interval>)."
(define (quality->integer quality)
(case quality
((diminished) 0)
((minor) 1)
((perfect) 2)
((major) 3)
((augmented) 4)
(else (error (with-output-to-string
(display "unexpected QUALITY ")
(write quality))))))