-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lisp
1190 lines (1088 loc) · 54.2 KB
/
util.lisp
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
;; -*- lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;**************************************************************************************************
;; FOMUS
;; util.lisp
;;**************************************************************************************************
;; *****COMMENTING...*****
(in-package :fomus)
(compile-settings)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DEBUGGING
;; almost all functions expected everything to be sorted--this checks for that
#+debug
(defun check-same (list str &key (key #'identity) (test #'eql))
(unless (let ((l (mapcar key list)))
(if l
(let ((fl (first l)))
(every (lambda (x) (funcall test x fl)) (rest l)))
t))
(error "CHECK-SAME failed at ~A" str)))
#+debug
(defun check-order (list str fun)
(loop for (e1 e2) on list while e2 unless (or (funcall fun e1 e2) (not (funcall fun e2 e1))) do (error "CHECK-ORDER failed at ~A" str)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FIND GHOSTVIEW
;; set to postscript viewing application
(eval-when (:load-toplevel :execute)
(defparameter +ghostview-exe+
#+(or darwin macos) (find-exe "open")
#+(and (or linux unix) (not (or darwin macos)) (not cygwin)) (or (find-exe "ggv") (find-exe "kgv") (find-exe "gv") (find-exe "evince") (find-exe "display") (find-exe "ghostview") "gv")
#+(or mswindows win32 cygwin) (or (find-exe "gsview32.exe" "Ghostgum") (find-exe "gv.exe") "gsview.exe"))
(defparameter +acroread-exe+
#+(or darwin macos) (find-exe "open")
#+(and (or linux unix) (not (or darwin macos)) (not cygwin)) (or (find-exe "acroread") (find-exe "gpdf") "acroread")
#+(or mswindows win32 cygwin) (or (find-exe "AcroRd32.exe" "Adobe") "AcroRd32.exe")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PROGRESS DOTS, IMMEDIATE OUTPUT
;; print a progress dot only if some certain time interval has elapsed since the last dot
(declaim (type (integer 0) +progress-int+))
(defparameter +progress-int+ 5) ; seconds
;; after good progress, reward with a dot!
(declaim (inline print-dot))
(defun print-dot () (when (>= *verbose* 1) (progress ".")))
;; wrapper for format--some lisps buffer the output so user can't see progress
(defun out (&rest args) (apply #'format t args) (finish-output))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; WHITE NOTE/CHROM NOTE CONVERSION
(declaim (type (vector (or (integer 0 6) null)) +note-to-white+)
(type (vector integer) +white-to-note+))
(defparameter +note-to-white+ (vector 0 nil 1 nil 2 3 nil 4 nil 5 nil 6)) ; vector of white notes in chromatic scale
(defparameter +white-to-note+ (vector 0 2 4 5 7 9 11)) ; vector of chromatic notes given white note index (0 - 6)
(declaim (type cons +acc-single+ +acc-double+ +acc-qtones-single+ +acc-qtones-double+))
(defparameter +acc-single+ '(0 -1 1)) ; all possible modifications when only using single flats and sharps
(defparameter +acc-double+ '(0 -1 1 -2 2)) ; all possible modifications when using double flats and sharps
(defparameter +acc-qtones-single+ '(0 -1 1 (0 . -1/2) (0 . 1/2) (-1 . -1/2) (1 . 1/2))) ; acc-single + quartertones
(defparameter +acc-qtones-double+ '(0 -1 1 -2 2 (0 . -1/2) (0 . 1/2) (-1 . -1/2) (1 . 1/2))) ; acc-double + quartertones
;; convert note value (mod 12) into a white-note value (mod 7)
;; expects p to actually be a valid white note (error if it isn't)
(defun notetowhite (p)
(declare (type integer p))
(multiple-value-bind (o n) (floor p 12)
(+ (* o 7) #-debug (svref +note-to-white+ n) #+debug (or (svref +note-to-white+ n) (error "Error in NOTETOWHITE")))))
;; convert write-note (mod 7) value into a note value (mod 12)
(defun whitetonote (w)
(declare (type integer w))
(multiple-value-bind (o n) (floor w 7)
(+ (* o 12) (svref +white-to-note+ n))))
(declaim (type (vector boolean) +nokey-quality+))
(defparameter +interval-quality+ (vector nil t t nil nil t t)) ; given diatonic interval number as index, contains nil if it can be perfect or t if it's maj/min
;; return white note spelling (0 - 6) or nil if not possible
;; note = note value
;; acc = accidental modification (-2 to 2)
(defun notespelling (note acc)
(declare (type rational note) (type (integer -2 2) acc))
(multiple-value-bind (o n) (floor (- note acc) 12)
(let ((x (svref +note-to-white+ n)))
(when x (values x o)))))
;; acc = (-2 to 2) or (-1 . -1/2), etc.
(defun qnotespelling (note acc)
(declare (type rational note) (type (cons (integer -2 2) (rational -1/2 1/2)) acc))
(multiple-value-bind (o n) (floor (- note (car acc) (cdr acc)) 12)
(let ((x (when (integerp n) (svref +note-to-white+ n))))
(when x (values x o)))))
;; converts an accidental value or quartertone to a normalized quartertone format
;; x = (-2 to 2) or (-1 . -1/2), etc.
;; returns: (-2 . 0), (-1 . 0), (-1 . -1/2), etc.
(defun convert-qtone (x)
(declare (type (or (cons (integer -2 2) (rational -1/2 1/2)) (integer -2 2)) x))
(if (consp x) x (cons x 0)))
;; finds the diatonic interval between two note/accidental pairs
;; return values: interval number (0 - 6), interval quality (0 = perfect, -1/1 = min./maj., -2/2... = dim./aug., etc.)
(defun interval (note1 acc1 note2 acc2)
(declare (type rational note1 note2) (type (integer -2 2) acc1 acc2))
(multiple-value-bind (s1 o1) (notespelling note1 acc1)
(multiple-value-bind (s2 o2) (notespelling note2 acc2)
(multiple-value-bind (sp1 sp2 n1 n2)
(let ((p1 (+ s1 (* o1 7)))
(p2 (+ s2 (* o2 7))))
(if (= p1 p2)
(if (< note1 note2)
(values p1 p2 note1 note2)
(values p2 p1 note2 note1))
(if (< p1 p2)
(values p1 p2 note1 note2)
(values p2 p1 note2 note1))))
(let ((b (mod (- sp2 sp1) 7)))
(values b
(let ((x (- (- n2 n1) (svref +white-to-note+ b) (* (floor (- sp2 sp1) 7) 12))))
(if (svref +interval-quality+ b)
(if (>= x 0) (1+ x) x) ; maj./min.
(cond ((> x 0) (1+ x)) ; aug./dim.
((< x 0) (1- x))
(t 0))))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UTILITY
;; returns: t if num is a power of 2 (ex. 1/4, 1/2, 1, 2, 8, etc.)
(defun expof2 (num)
(declare (type rational num))
(loop
for n = (if (< num 1) (/ num) num) then (/ n 2)
when (= n 1) do (return t)
until (< n 1)))
;; returns: list of lowest common denominators (duplicates are removed)
;; examples: (lowmult 5) = (5), (lowmult 6) = (2 3), (lowmult 10) = (2 5), (lowmult 100) = (2 5)
(defun lowmult (n)
(declare (type rational n))
(loop
for i from 2
#-clisp while #-clisp (<= i n)
for j = #-clisp (/ n i) #+clisp (if (<= i n) (/ n i) (loop-finish))
when (integerp j)
collect i
and do (loop do (setf n j j (/ n i)) while (integerp j))))
;; returns: prime numbers up to ubound excluding 1
;; (defun primes2 (ubound)
;; (declare (type (integer 2) ubound))
;; (loop
;; for i from 2 to ubound
;; when (notany (lambda (e) (declare (type (integer 2) e)) (= (mod i e) 0)) pl)
;; collect i into pl
;; finally (return pl)))
;; returns: odd numbers up to ubound (with exception of 2)
(defun notdivby2s (ubound)
(declare (type (integer 2) ubound))
(cons 2 (loop for i from 3 to ubound unless (integerp (/ i 2)) collect i)))
;; given list of conses representing (possibly overlapping) segments/distances, find all the "holes" in between these segments
;; list = list of conses (start . end)
;; o1 = starting value
;; o2 = end (can be <= o1 if don't care)
;; returns: list of conses (the "holes")
(defun get-holes (list o1 o2)
(declare (type list list) (type real o1 o2))
(loop
with o = o1
for (e1 . e2) of-type (real . real) in list
when (< o e1) collect (cons o e1) into r
do (setf o e2)
finally
(return (if (< o o2) (nconc r (list (cons o o2))) r))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PROPERTIES/MARKS
;; use the following functions to access/set properties and marks
(declaim (inline addprop))
(defgeneric addprop (obj prop))
(defmethod addprop ((obj meas) prop)
(declare (type (or symbol cons) prop))
(push prop (meas-props obj)))
(defmethod addprop ((obj part) prop)
(declare (type (or symbol cons) prop))
(push prop (part-props obj)))
(defmethod addprop ((obj mark) prop)
(declare (type (or symbol cons) prop))
(push prop (event-marks obj)))
(defmethod addprop ((obj timesig-repl) prop)
(declare (type (or symbol cons) prop))
(push prop (timesig-props obj)))
(declaim (inline obj-props))
(defgeneric obj-props (obj))
(defmethod obj-props ((obj meas)) (meas-props obj))
(defmethod obj-props ((obj part)) (part-props obj))
(defmethod obj-props ((obj timesig-repl)) (timesig-props obj))
(defmethod obj-props ((obj mark)) (event-marks obj))
(defun getprop (obj propid)
(declare (type (or meas part timesig-repl mark) obj) (type (or symbol cons) propid))
(let* ((mi (force-list propid))
(li (length mi)))
(find-if (lambda (x) (declare (type (or symbol cons) x)) (let ((xx (force-list x))) (and (>= (length xx) li) (every #'equal xx mi))))
(obj-props obj))))
(defun getprops (obj propid)
(declare (type (or meas part timesig-repl mark) obj) (type (or symbol cons) propid))
(let* ((mi (force-list propid))
(li (length mi)))
(loop for x of-type (or symbol cons) in (obj-props obj)
when (let ((xx (force-list x))) (and (>= (length xx) li) (every #'equal xx mi))) collect x)))
(defun rmprop-aux (pr propid)
(declare (type list pr) (type (or symbol cons) propid))
(let* ((mi (force-list propid))
(li (length mi)))
(remove-if (lambda (x) (declare (type (or symbol cons) x)) (let ((xx (force-list x))) (and (>= (length xx) li) (every #'equal xx mi)))) pr)))
(declaim (inline rmprop))
(defgeneric rmprop (obj propid))
(defmethod rmprop ((obj meas) propid)
(declare (type (or symbol cons) propid))
(setf (meas-props obj) (rmprop-aux (meas-props obj) propid)))
(defmethod rmprop ((obj part) propid)
(declare (type (or symbol cons) propid))
(setf (part-props obj) (rmprop-aux (part-props obj) propid)))
(defmethod rmprop ((obj timesig-repl) propid)
(declare (type (or symbol cons) propid))
(setf (timesig-props obj) (rmprop-aux (timesig-props obj) propid)))
(defmethod rmprop ((obj mark) propid)
(declare (type (or symbol cons) propid))
(setf (event-marks obj) (rmprop-aux (event-marks obj) propid)))
(declaim (inline popprop-aux))
(defgeneric popprop-aux (obj props))
(defmethod popprop-aux ((obj meas) props)
(declare (type list props))
(setf (meas-props obj) props))
(defmethod popprop-aux ((obj part) props)
(declare (type list props))
(setf (part-props obj) props))
(defmethod popprop-aux ((obj timesig-repl) props)
(declare (type list props))
(setf (timesig-props obj) props))
(defmethod popprop-aux ((obj mark) props)
(declare (type list props))
(setf (event-marks obj) props))
(defun popprop (obj propid)
(declare (type (or meas part timesig-repl mark) obj) (type (or symbol cons) propid))
(let* ((mi (force-list propid))
(li (length mi))
(f (find-if (lambda (x) (declare (type (or symbol cons) x)) (let ((xx (force-list x))) (and (>= (length xx) li) (every #'equal xx mi))))
(obj-props obj))))
(popprop-aux obj (remove f (obj-props obj) :test #'equal))
f))
;; combine marks/properties in list of objects (measures, parts, timesigs, mark objects), removing duplicates
;; returns: the new marks list
(defun combprops (objlist)
(declare (type list objlist))
(remove-duplicates (loop for o of-type (or meas part timesig-repl mark) in objlist nconc (copy-list (obj-props o))) :test #'equal))
;; aliasa for note/rest objects
(declaim (inline addmark getmark getmarks rmmark combmarks popmark))
(defun addmark (&rest x) (apply #'addprop x))
(defun getmark (&rest x) (apply #'getprop x))
(defun getmarks (&rest x) (apply #'getprops x))
(defun rmmark (&rest x) (apply #'rmprop x))
(defun combmarks (&rest x) (apply #'combprops x))
(defun popmark (&rest x) (apply #'popprop x))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EVENTS
;; ts = a timesig objects
;; returns: number of beats in a measure
(defun timesig-nbeats (ts)
(declare (type timesig-repl ts))
(/ (timesig-num ts) (* (timesig-den ts) (timesig-beat* ts))))
;; ts = a timesig object
;; returns: metrical divisions, determined by lookup or a function that returns a reasonable default
(defun timesig-div* (ts)
(declare (type timesig-repl ts))
(or (force-list2all (timesig-div ts))
(let ((nb (timesig-nbeats ts)))
(or (lookup nb *default-meas-divs*)
(lookup nb +default-meas-divs+)
(loop with d0 = (denominator nb)
for d = 2 then (* d 2) #-clisp until #-clisp (> d d0)
for x = #-clisp (let ((bb (* nb d)))
(or (lookup bb *default-meas-divs*)
(lookup bb +default-meas-divs+)))
#+clisp (if (> d d0) (loop-finish)
(let ((bb (* nb d)))
(or (lookup bb *default-meas-divs*)
(lookup bb +default-meas-divs+))))
when x do (return (loop for y of-type list in x collect (mapcar (lambda (z) (declare (type (rational 0) z)) (/ z d)) y))))))))
(declaim (type (rational (0)) *effective-grace-dur-mul*))
(defparameter *effective-grace-dur-mul* 1/2) ; setting--multiplier for effective duration of grace notes--use this in any algorithm that needs a small durational value for grace notes
(declaim (inline event-tupfrac))
;; ev = note/rest event
;; returns: duration/time multiplier for tuplet note/rest
(defun event-tupdurmult (ev) (declare (type ex-base ev)) (if (listp (car (event-tup ev))) (cdr (event-tup ev)) (event-tup ev)))
;; returns: the tuplet fraction (portion of the whole tuplet that this event takes up)
(defun event-tupfrac (ev) (declare (type ex-base ev)) (car (event-tup ev)))
;; dur = duration
;; dmu = time/duration multipliers (a list representing the multiplier of each nested level)
;; returns: actual duration taking tuplet time modification into account
(declaim (inline event-effectdur event-writtendur* event-writtennote))
(defun effectdur (dur dmu)
(declare (type (rational (0)) dur) (type list dmu))
(loop
with d = dur
for e of-type (rational (0)) in dmu
do (setf d (* d e))
finally (return d)))
;; dmu = dmu argument override, needed by some functions
;; returns: effective duration of an event (taking tuplets into account)
(defun event-effectdur (ev &optional (dmu t))
(declare (type (or noteex restex) ev) (type (or boolean list) dmu))
(effectdur (event-dur* ev) (if (truep dmu) (event-tupdurmult ev) dmu)))
;; returns: grace note duration multiplied by effective-grace-dur-mul setting
(defun event-graceeffdur (ev)
(declare (type dur-base ev))
(let ((gd (event-gracedur ev)))
(when gd (* gd *effective-grace-dur-mul*))))
;; ev = note/rest event
;; ts = timesig object
;; dmu = dmu argument override, needed by some functions
;; returns: written duration (1/4 = quarter, 1/8 = eighth, 3/8 = dotted quarter, etc.)
(defun event-writtendur (ev ts &optional (dmu t))
(declare (type (or noteex restex) ev) (type (or timesig-repl (rational (0))) ts) (type (or boolean list) dmu))
(* (or (event-gracedur ev) (event-effectdur ev dmu))
(if (timesigp ts) (timesig-beat* ts) ts)))
;; figures out if dots are needed
;; w = written duration (1/4 = quarter, 1/8 = eighth, 3/8 = dotted quarter, etc.)
;; return values: base written duration (1/4 = quarter, 1/8 = eighth, 3/8 = dotted quarter, etc.), number of dots
(defun writtendur* (w)
(declare (type (rational (0)) w))
(if (expof2 w) (values w 0)
(let ((w1 (* w 2/3)))
(if (expof2 w1) (values w1 1)
(let ((w2 (* w 4/7)))
(if (expof2 w2) (values w2 2)
#+debug (error "Error in WRITTENDUR*")))))))
;; ev = note/rest event
;; ts = timesig object
;; return values: base written duration (1/4 = quarter, 1/8 = eighth, 3/8 = dotted quarter, etc.), number of dots
(defun event-writtendur* (ev ts) ; returns values writtendur, number-of-dots
(declare (type (or noteex restex) ev) (type timesig-repl ts))
(writtendur* (event-writtendur ev ts)))
;; ev = note event
;; returns: notated note (ex.: g-natural, g-sharp and g-flat all return the note value for g-natural)
(defun event-writtennote (ev)
(declare (type noteex ev))
(- (event-note* ev) (event-acc ev) (event-addacc ev)))
;; ev = note event (containing a chord)
;; returns: list of notated notes in chord
(defun event-writtennotes (ev)
(declare (type noteex ev))
(loop
for e of-type rational in (event-notes* ev)
and a of-type rational in (event-accs ev)
and a2 of-type rational in (event-addaccs ev)
collect (- e a a2)))
;; ev = notes/rest event
;; ts = timesig object
;; ext = extra number of beams to subtract from actual value (useful for a few functions)
;; returns: number of beams belonging to note (rests have 0 beams)
(defun event-nbeams (ev ts &optional (ext 0))
(declare (type (or noteex restex) ev) (type timesig-repl ts))
(if (notep ev) (max (- (roundint (log (event-writtendur* ev ts) 1/2)) 2 ext) 0) 0))
;; dur = given duration of entire tuplet
;; dmu = time/duration multipliers (a list representing the multiplier of each nested level)
;; ts = timesig object
;; returns: unit of tuplet (1/8 = eighth note, etc.)
(defun unitwritdur (dur dmu ts) ; ndmu = the level that applies
(declare (type (rational (0)) dur) (type list dmu) (type timesig-repl ts))
(/ (* (effectdur dur dmu) (timesig-beat* ts)) ; written dur w/o dots info of entire tuplet
(numerator (first dmu))))
;; ev = note/rest event
;; returns: whether or note event represents a chord
(declaim (inline chordp))
(defun chordp (ev)
(declare (type (or noteex restex) ev))
(and (notep ev) (consp (event-tielt ev))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SORTING
;; all-purpose sorter
;; sorts by (more or less): offset, gracep, voice num., staff num., duration, notep/restp, notes
;; pass to sort function
(defun sort-offdur (x y)
(declare (type (or noteex restex) x y))
(if (= (event-off x) (event-off y))
(let ((gx (event-grace x))
(gy (event-grace y)))
(if (eql gx gy)
(multiple-value-bind (sx vx) (let ((z (event-voice* x))) (if (numberp z) (values (event-staff x) z) (values 0 0)))
(multiple-value-bind (sy vy) (let ((z (event-voice* y))) (if (numberp z) (values (event-staff y) z) (values 0 0)))
(if (= vx vy)
(if (= sx sy)
(let ((dx (or (event-gracedur x) (event-dur* x)))
(dy (or (event-gracedur y) (event-dur* y))))
(if (= dx dy)
(cond ((and (restp x) (notep y)) nil)
((restp y) t)
(t (let ((nx (remove-if #'symbolp (if (chordp x) (event-notes* x) (list (event-note* x))))) ; at this point, should only be notes
(ny (remove-if #'symbolp (if (chordp y) (event-notes* y) (list (event-note* y))))))
(loop
for ex of-type real in nx and ey of-type real in ny
when (< ex ey) do (return t)
finally (return (< (length nx) (length ny)))))))
(< dx dy)))
(< sx sy))
(< vx vy))))
(cond ((null gy) t)
((null gx) nil)
(t (< gx gy)))))
(< (event-off x) (event-off y))))
;; generic property list sorter
;; use to guarantee property lists/mark lists can be compared for equality w/ equal
;; props = property list
;; returns: sorted property/marks list
(defun sort-props (props)
(declare (type list props))
(labels ((xx (x y)
(declare (type (or symbol cons) x y))
(flet ((ty (a)
(declare (type (or symbol real string list) a))
(etypecase a (keyword 0) (symbol 1) (string 2) (integer 3) (rational 4) (float 5) (list 6))))
(loop for ee1 on (force-list x) and ee2 on (force-list y)
for e1 of-type (or symbol real string list) = (first ee1) and e2 of-type (or symbol real string list) = (first ee2)
for t1 = (ty e1) and t2 = (ty e2)
when (/= t1 t2) do (return (< t1 t2))
when (listp e1) do (return (xx e1 e2))
else when (if (numberp e1) (/= e1 e2) (string/= e1 e2))
do (return (if (numberp e1) (< e1 e2) (string< e1 e2)))
finally (< (length ee1) (length ee2))))))
(sort (copy-list props) #'xx)))
;; alias for marks
(declaim (inline sort-marks))
(defun sort-marks (marks) (declare (type list marks)) (sort-props marks))
;; remove all marks that aren't specific things that appear in the score
;; marks = marks list
;; returns: another marks list
(declaim (inline important-marks))
(defun important-marks (marks)
(declare (type list marks))
(remove-if-not (lambda (x) (find (first (force-list x)) +marks-important+)) marks))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CHORDS/SPLITTING
;; accepts events of same offset/duration to assemble into a chord
;; list = sorted list of events of same offset/duration
;; rests are discarded, function is destructive on mark-lists--assumes throwing away note-events to make chord-event
;; returns: note event (containing multiple notes)
(defun make-chord (list &optional voice)
(declare (type list list) (type (or null (integer 1)) voice))
(multiple-value-bind (n r) (split-list (sort list #'sort-offdur) #'notep)
(if n
(let ((x (sort (loop ; make more efficient?
for e of-type noteex in n
if (chordp e) append (event-note* e) into r1 and append (event-note e) into r2 and append (event-tielt e) into r3 and append (event-tiert e) into r4
else collect (event-note* e) into r1 and collect (event-note e) into r2 and collect (event-tielt e) into r3 and collect (event-tiert e) into r4
finally
(return (mapcar (lambda (y1 y2 y3 y4) (cons (cons y1 y2) (cons y3 y4))) r1 r2 r3 r4)))
#'< :key #'caar)))
(copy-event (first n)
:voice (or voice (event-voice (first n)))
:marks (combmarks n)
:note (mapcar #'cdar x)
:tielt (mapcar #'cadr x)
:tiert (mapcar #'cddr x)))
(copy-event (first r) :marks (combmarks r)))))
;; splits an event into two events (possibly tied), given an offset split point
;; event = note/rest event
;; off = split point
;; tup/dmu = a tuplet fraction/a tuplet duration multiplier to add (used by note-splitting function to create tuplets as necessary)
;; returns: cons of two events
(defun split-event (event off &optional tup dmu)
(declare (type (or noteex restex) event) (type (rational 0) off) (type list tup dmu))
(let ((eo (event-endoff event))
(o (event-off event)))
(cond ((<= eo off) (cons (copy-event event :tup (cons tup dmu)) nil))
((<= off o) (cons nil (copy-event event)))
(t (etypecase event
(note (cons (copy-event event
:dur (- off o) ; shouldn't be dealing with grace note
:tiert (if (chordp event) (make-list (length (event-tiert event)) :initial-element (not (event-autodur event)))
(not (event-autodur event)))
:tup (cons (when tup (cons (* (first tup) (/ (- off o) (- eo o))) (rest tup))) #|tup|# dmu))
(if (event-autodur event)
(make-restex nil :off off :dur (- eo off) :voice (event-voice event) :tup (event-tup event))
(copy-event event :off off :dur (- eo off)
:tielt (if (chordp event) (make-list (length (event-tielt event)) :initial-element t) t)))))
(rest (cons (copy-event event :dur (- off o) :tup (cons (when tup (cons (* (first tup) (/ (- off o) (- eo o))) (rest tup))) #|tup|# dmu)
:marks (if (event-marks event) (cons :splitlt (event-marks event))))
(copy-event event :off off :dur (- eo off)
:marks (if (event-marks event) (cons :splitrt (event-marks event)))))))))))
;; splits events--retains tuplet information from the original event
;; event = note/rest event
;; off = split point
;; returns: cons of two events
(defun split-event2 (event off)
(declare (type noteex event) (type (rational 0) off))
(let ((u (car (event-tup event)))
(d (cdr (event-tup event))))
(let ((re (split-event event off u d)))
(setf (event-tup (cdr re)) (cons (cons (- (first u) (first (event-tupfrac (car re)))) (rest u)) d))
re)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; USER MARKS
;; *****COMMENT POINT*****
;; passes all events between and intersecting with startsyms and endsyms
;; events sent to fun aren't in order
(defun get-usermarks (events sym startsym contsym endsym fun name) ; extrasort distinguishes between :staff 1, :staff 2, etc.
(declare (type list events) (type symbol sym startsym contsym endsym) (type (function ((or noteex restex) list) t) fun) (type (or string null) name))
(loop for ((o1 . me) (o2)) of-type (((rational 0) . list) ((rational 0)))
on (mapcar #'cdr
(let ((xx (merge-linear
(loop for e of-type cons in
(sort (loop for e of-type (or noteex restex) in (reverse events)
for me = (let ((o1 (popmark e endsym)) (o2 (popmark e contsym))) (or o1 o2))
and ms = (popmark e startsym)
and my = (popmark e sym)
when me collect (cons :e (list (event-endoff e)))
when ms collect (cons :s (cons (event-off e) (force-list ms)))
when my collect (cons :o (cons (event-off e) (cons (event-endoff e) (force-list my)))))
(lambda (x y)
(declare (type cons x y))
(let ((xx (second x)) (yy (second y)))
(declare (type (rational 0) xx yy))
(if (= xx yy)
(< (ecase (car x) (:e 0) (:o 1) (:s 2)) (ecase (car y) (:e 0) (:o 1) (:s 2)))
(< xx yy)))))
if (eq (car e) :o) collect (cons :s (cons (cadr e) (cdddr e))) and collect (cons :e (cddr e))
else collect e)
(lambda (x y)
(declare (type cons x y))
(when (eq (car x) (car y))
(if (eq (car x) :s) x y))))))
(unless (or (null xx) (eq (car (first xx)) :s)) (error "Missing starting mark ~S in part ~S" startsym name))
xx))
by #'cddr
do (map nil (lambda (x) (declare (type (or noteex restex) x)) (funcall fun x (rest me)))
(remove-if-not (lambda (e) (declare (type (or noteex restex) e)) (and (> (event-endoff e) o1) (or (null o2) (< (event-off e) o2)))) events))))
;; clean
;; deletes marks at incorrect places in tied notes/chords
;; expects measures and chords
(defun clean-ties (pts)
(declare (type list pts))
(loop for p of-type partex in pts
do (loop for m of-type meas in (part-meas p)
do (loop
for e of-type (or noteex restex) in (meas-events m)
when (and (notep e) (or (and (event-tielt e) (and-list (force-list (event-tielt e)))) (getmark e :endtremolo)))
do (mapc (lambda (x) (declare (type (or symbol cons) x)) (rmmark e x)) +marks-first-tie+)
when (and (notep e) (or (and (event-tiert e) (and-list (force-list (event-tiert e)))) (getmark e :starttremolo)))
do (mapc (lambda (x) (declare (type (or symbol cons) x)) (rmmark e x)) +marks-last-tie+)
when (and (restp e) (popmark e :splitrt))
do (mapc (lambda (x) (declare (type symbol x)) (rmmark e x)) +marks-first-rest+)
when (and (restp e) (popmark e :splitlt))
do (mapc (lambda (x) (declare (type symbol x)) (rmmark e x)) +marks-last-rest+)
do (loop for sp in (list +marks-spanner-voices+ +marks-spanner-staves+) do
(loop for (startsym contsym endsym) of-type (symbol symbol symbol) in sp
do (loop for n in (getmarks e startsym) do (rmmark e (list contsym (second (force-list n)))))
do (loop for n in (getmarks e endsym) do (rmmark e (list contsym (second (force-list n)))))))))
(print-dot)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; STAVES
(defun sep-staves (parts)
(declare (type list parts))
(loop
for p of-type partex in parts
if (part-events p)
nconc (mapcar
(lambda (e) (declare (type cons e)) (copy-part p :events (sort e #'sort-offdur)))
(split-into-groups (part-events p) #'event-staff))
else collect p
do (print-dot)))
;; separates before measures exist
(defun sep-voices (parts)
(declare (type list parts))
(loop
for p of-type partex in parts
if (part-events p)
nconc (mapcar
(lambda (e) (declare (type cons e)) (copy-part p :events (sort e #'sort-offdur)))
(split-into-groups (part-events p) #'event-voice*))
else collect p
do (print-dot)))
;; reassembles after measures exist
(defun assemble-parts (parts)
(declare (type list parts))
(loop
for p of-type cons in (split-into-groups parts #'part-userord)
for f of-type partex = (first p)
when (list>1p p)
do (setf (part-events f)
(if (measp (first (part-events f)))
(apply #'mapcar
(lambda (&rest ms)
(let ((m (first ms)))
(declare (type meas m))
(setf (meas-events m) (sort (mapcan #'meas-events ms) #'sort-offdur))
m)) ; same part, measures are copies of each other
(mapcar #'part-meas p))
(sort (mapcan #'part-events p) #'sort-offdur))
(part-props f) (combprops p))
collect f
do (print-dot)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TIMESIGS
;; divs to use in a replacement time-signature
;; to is number of beats in measure
(defun repl-divs (divs to)
(declare (type list divs) (type (rational (0)) to))
(delete-duplicates
(loop for e of-type cons in divs collect
(loop
with s = 0
for x of-type (rational (0)) in e
until (>= (+ s x) to)
collect x into xx
do (incf s x)
finally (return (nconc xx (list (- to s))))))
:test #'equal))
;; minimum automatic measure (before timesig change)
;; in number of beats
(declaim (type (rational (0)) *min-auto-timesig-dur*)
(type boolean *auto-override-timesigs*))
(defparameter *min-auto-timesig-dur* 2)
(defparameter *auto-override-timesigs* t)
;; return sorted timesigs that match up with part
(declaim (type timesig-repl *default-timesig*))
(defparameter *default-timesig* (make-timesig-repl))
(defun get-timesigs-aux (timesigs parts fun)
(declare (type list timesigs parts) (type (function (part timesig-repl (rational 0) (rational 0)) t) fun))
(let ((mx 0)
(mxo (mloop for p of-type part in parts maximize (mloop for e of-type dur-base in (part-events p) maximize (event-endoff e)))))
(flet ((ut (si p eo1 lo)
(declare (type timesig si) (type part p) (type (or (rational 0) null) eo1 lo))
(loop
with eo0 = (when eo1 (- eo1 (or *min-auto-timesig-dur* 0))) ; eo0 = adjusted end-offset (for auto-meas)
for o = (or lo 0) then and
#-clisp while #-clisp (if eo1 (< o eo1) (or (<= o 0) (< o mxo))) ; loop creating measures
for nb = #-clisp (timesig-nbeats si) #+clisp (if (if eo1 (< o eo1) (or (<= o 0) (< o mxo))) (timesig-nbeats si) (loop-finish))
for nd = (+ o nb) ; nd = next downbeat, loop and create measures
for and = (if (and eo0 (> nd eo0)) eo1 nd) ; and = actual next downbeat (>= nd)
for at = (if (/= nb (- and o)) ; at = actual time-signature
(let ((x (find (- and o) (force-list (timesig-repl si)) :key #'timesig-nbeats)))
(if x (copy-timesig x
:off o :div (or (timesig-div x) (repl-divs (timesig-div si) (- and o)))
:parts (timesig-partids si) :repl nil :props nil)
(let ((n (* (/ (- and o) (timesig-nbeats si)) (timesig-num si))))
(copy-timesig si
:off o
:time (cons (numerator n) (* (denominator n) (timesig-den si)))
:div (repl-divs (timesig-div si) (- and o))
:repl nil :props nil))))
si)
when (> and mx) do (setf mx and)
do (funcall fun p at o and) ; part, timesig, o1, o2
finally (return (when and (cons and at))))))
(loop with dts = (make-timesigex* *default-timesig*)
for p in parts
and (lo . at) in (loop
for p of-type part in parts
collect (loop
with at
for (ts nx) of-type (timesig (or timesig null))
on (let ((z (let ((x (merge-linear
(sort (delete-if-not (lambda (x) (or (null (timesig-partids x)) (find (part-partid p) (timesig-partids x))))
(copy-list timesigs)) ; ts = current time sig, n = next group
#'< :key #'timesig-off)
(lambda (x y) (if (= (timesig-off x) (timesig-off y))
(cond ((and (null (timesig-partids x)) (timesig-partids y)) y)
((and (timesig-partids x) (null (timesig-partids y))) x)
(t (error "Conflicting time signature/part IDs assignment at offset ~S, part ~S"
(timesig-foff x) (part-name p)))))))))
(if (or (null x) (> (timesig-off (first x)) 0))
(cons (copy-timesig dts :off 0 :props nil) x)
x))))
(if *auto-override-timesigs*
(loop for (e1 e2) of-type (timesig (or timesig null)) on z
when (or (<= (timesig-off e1) 0)
(null e2)
(>= (- (timesig-off e2) (timesig-off e1)) (or *min-auto-timesig-dur* 0)))
collect e1)
z))
do (let ((x (ut ts p (when nx (timesig-off nx)) (car at))))
(when x (setf at x))) ; (print-dot)
finally (return at)))
do (ut at p mx lo) #|(print-dot)|#))))
;; return hash-table of reverse-order timesig lists (no repeats) indexed by part objects
(defun get-timesigs (timesigs parts)
(declare (type list timesigs parts))
(let ((h (make-hash-table :test 'eq)))
(get-timesigs-aux timesigs parts
(lambda (p ts o1 o2)
(declare (type part p) (type timesig ts) (ignore o1 o2))
(let ((i (gethash p h)))
(declare (type list i))
(unless (eq ts (first i))
(setf (gethash p h) (cons ts i))))))
h))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MISCELLANEOUS
(declaim (inline is-percussion))
(defun is-percussion (part)
(declare (type partex part))
(or (eq (instr-sym (part-instr part)) :percussion)
(eq (instr-clefs (part-instr part)) :percussion)))
(defun get-dur (ev ts)
(declare (type dur-base ev) (type timesig-repl ts))
(let ((g (when (consp (event-dur ev)) (second (event-dur ev)))))
(if g
(cons (dur-to-num (first (event-dur ev)) (timesig-beat ts)) g)
(dur-to-num (event-dur ev) (timesig-beat ts)))))
(defun beat-division (ts)
(declare (type timesig-repl ts))
(if (listp *beat-division*)
(if (timesig-comp ts)
(second *beat-division*)
(first *beat-division*))
(if (timesig-comp ts)
(* *beat-division* 3/2)
*beat-division*)))
(defun tuplet-division (tup)
(declare (type (integer 2) tup))
(or (lookup tup *default-tuplet-divs* #|:test #'equal|#)
(lookup tup +default-tuplet-divs+ #|:test #'equal|#)
(delete-duplicates
(loop for i in '(2 1) thereis
(loop for tu from i to (ceiling tup 2)
when (expof2 tu) collect (list tu (- tup tu)) and collect (list (- tup tu) tu)))
:test #'equal)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CHECK SETTINGS
(defun check-setting-types ()
(loop for (sy ty er) in +settings+ do
(let ((v (symbol-value (find-symbol (conc-strings "*" (symbol-name sy) "*") :fomus))))
(or (check-type* v ty) (error "Found ~S, expected ~A in setting ~S" v (or er ty) sy)))))
(defun check-settings ()
(loop for d of-type cons in *default-meas-divs* do
(loop with n of-type (rational (0)) = (first d)
for r of-type cons in (rest d) unless (= (apply #'+ r) n) do (error "Invalid divisions ~S in setting :DEFAULT-MEAS-DIVS" d)))
(loop for d of-type cons in *default-tuplet-divs* do
(loop with n of-type (integer 1) = (first d)
for r of-type cons in (rest d) unless (= (apply #'+ r) n) do (error "Invalid divisions ~S in setting :DEFAULT-TUPLET-DIVS" d)))
(unless (and (if (listp *default-beat*) (and (expof2 (car *default-beat*)) (expof2 (* (cdr *default-beat*) 2/3))) (expof2 *default-beat*)))
(error "Invalid value ~S in setting :DEFAULT-BEAT" *default-beat*))
(when (< *min-tuplet-dur* (/ *beat-division*))
(format t "~&;; WARNING: Value ~S of setting :MIN-TUPLET-DUR is too small for beat-division of ~S--changing to ~S"
*min-tuplet-dur* *beat-division* (setf *min-tuplet-dur* (/ *beat-division*))))
(when (< *max-tuplet-dur* *min-tuplet-dur*)
(format t "~&;; WARNING: Value ~S of setting :MAX-TUPLET-DUR is smaller than value of setting :MIN-TUPLET-DUR--changing to ~S"
*max-tuplet-dur* (setf *max-tuplet-dur* *min-tuplet-dur*))))
(defmacro set-instruments (&body forms)
`(let ((*instruments*
(loop for e of-type (or instr cons) in *instruments*
if (consp e) collect (apply #'copy-instr (find (first e) +instruments+ :key #'instr-sym) (rest e))
else collect e))
(*percussion*
(loop for e of-type (or perc cons) in *percussion*
if (consp e) collect (apply #'copy-perc (find (first e) +percussion+ :key #'perc-sym) (rest e))
else collect e)))
,@forms))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; INTERNAL OBJECT CONSTRUCTORS
;; make functions
(declaim (inline make-eventex*))
(defgeneric make-eventex* (ev ts pa))
(defmethod make-eventex* ((ev note) ts pa)
(declare (type timesig-repl ts) (type partex pa))
(make-noteex ev
:id (obj-id ev) :partid (event-partid ev)
:off (event-off ev)
:dur (get-dur ev ts)
:marks (event-marks ev)
:voice (event-voice ev)
:note (if (is-percussion pa) (event-note ev)
(parse-usernote (event-note ev)))))
(defmethod make-eventex* ((ev rest) ts pa)
(declare (type timesig-repl ts) (ignore pa))
(make-restex ev
:id (obj-id ev) :partid (event-partid ev)
:off (event-off ev)
:dur (get-dur ev ts)
:marks (event-marks ev)
:voice (event-voice ev))) ; rest
;; ts is reverse list of timesigs for this part
(defun make-partex* (part userord evs ts) ; destroys evs
(declare (type part part) (type (integer 0) userord) (type list evs ts))
(let ((pp (make-partex part
:name (part-name part) ; part
:abbrev (part-abbrev part)
:opts (part-opts part)
:instr (flet ((er (s) (error "Invalid instrument ~S in part ~S" s (part-name part))))
(flet ((gi (s)
(declare (type (or symbol (integer 0 127)) s))
(if (symbolp s)
(or (find s *instruments* :key #'instr-sym)
(find s +instruments+ :key #'instr-sym)
(er s))
(or (find s *instruments* :test (lambda (k i)
(declare (type (integer 0 127) k) (type instr i))
(find k (force-list (instr-midiprgch-im i)))))
(find s +instruments+ :test (lambda (k i)
(declare (type (integer 0 127) k) (type instr i))
(find k (force-list (instr-midiprgch-im i)))))
(er s)))))
(make-instrex*
(typecase (part-instr part)
(null *default-instr*)
(instr (part-instr part))
((or symbol number) (gi (part-instr part)))
(list (let ((z (apply #'copy-instr (gi (first (part-instr part))) (rest (part-instr part)))))
(check-type* z +instr-type+)
z))
(otherwise (er (part-instr part))))
part)))
:props (part-props part)
:partid (part-partid part) :userord userord)))
(setf (part-events pp)
(sort (mapcar (lambda (e) (declare (type (or note rest) e))
(make-eventex* e (loop for s of-type timesig in ts until (<= (timesig-off s) (event-off e)) finally (return s)) pp))
(nconc evs (part-events part)))
#'sort-offdur))
pp))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TIMESIG FUNCTIONS
(declaim (type boolean *auto-timesig-comp*))
(defparameter *auto-timesig-comp* t)
(defun timesig-check (ts)
(declare (type timesig-repl ts))
(flet ((er () (error "Invalid beat ~S in BEAT slot of TIMESIG at offset ~S" (timesig-beat ts) (timesig-foff ts))))
(when (timesig-beat ts)
(if (timesig-comp ts)
(unless (= (timesig-beat ts) (/ 3 (timesig-den ts))) (er))
(unless (or (= (timesig-beat ts) 0) (expof2 (timesig-beat ts))) (er)))))
(loop with nb = (timesig-nbeats ts)
for d of-type cons in (timesig-div ts)
unless (= (apply #'+ d) nb) do (error "Invalid division ~S in DIV slot of TIMESIG at offset ~S" (timesig-div ts) (timesig-foff ts))))
(defgeneric make-timesigex* (ts))
(defmethod make-timesigex* ((ts timesig))
(let ((nt (copy-timesig ts
:comp (if (eq (timesig-comp ts) 'default)
(when *auto-timesig-comp*
(let ((x (/ (car (timesig-time ts)) 3)))
(when (and (integerp x) (> x 1)) t)))
(timesig-comp ts))
:partids (force-list (timesig-partids ts))
:off (roundto (timesig-off ts) (/ (beat-division ts)))
:div (force-list2all (timesig-div ts))
:time (cons (first (timesig-time ts)) (second (timesig-time ts)))
:repl (let ((x (mapcar #'make-timesigex* (force-list (timesig-repl ts)))))
(if (list1p x) (first x) x)))))
(timesig-check nt)
(when *old-objects* (setf (gethash nt *old-objects*) ts))
nt))
(defmethod make-timesigex* ((ts timesig-repl))
(let ((nt (copy-timesig-repl ts
:comp (if (eq (timesig-comp ts) 'default)
(when *auto-timesig-comp*
(let ((x (/ (car (timesig-time ts)) 3)))
(when (and (integerp x) (> x 1)) t)))
(timesig-comp ts))
:div (force-list2all (timesig-div ts))
:time (cons (first (timesig-time ts)) (second (timesig-time ts)))
:props (copy-tree (timesig-props ts)))))
(timesig-check nt)
nt))
(defun keysig-accs (li)
(declare (type list li))
(loop for (a . b) of-type ((integer 0 11) . (integer -1 1))
in (loop for e of-type symbol in li append
(ecase e
((:cmaj :amin) nil)
((:gmaj :emin) '((6 . 1)))
((:dmaj :bmin) '((6 . 1) (1 . 1)))
((:amaj :f+min) '((6 . 1) (1 . 1) (8 . 1)))
((:emaj :c+min) '((6 . 1) (1 . 1) (8 . 1) (3 . 1)))
((:bmaj :g+min) '((6 . 1) (1 . 1) (8 . 1) (3 . 1) (10 . 1)))
((:f+maj :d+min) '((6 . 1) (1 . 1) (8 . 1) (3 . 1) (10 . 1) (5 . 1)))
((:c+maj :a+min) '((6 . 1) (1 . 1) (8 . 1) (3 . 1) (10 . 1) (5 . 1) (0 . 1)))
((:c-maj :a-min) '((10 . -1) (3 . -1) (8 . -1) (1 . -1) (6 . -1) (11 . -1) (4 . -1)))
((:g-maj :e-min) '((10 . -1) (3 . -1) (8 . -1) (1 . -1) (6 . -1) (11 . -1)))
((:d-maj :b-min) '((10 . -1) (3 . -1) (8 . -1) (1 . -1) (6 . -1)))
((:a-maj :fmin) '((10 . -1) (3 . -1) (8 . -1) (1 . -1)))
((:e-maj :cmin) '((10 . -1) (3 . -1) (8 . -1)))
((:b-maj :gmin) '((10 . -1) (3 . -1)))
((:fmaj :dmin) '((10 . -1)))
(:c+ '((1 . 1))) (:d- '((1 . -1)))
(:d+ '((3 . 1))) (:e- '((3 . -1)))
(:f+ '((6 . 1))) (:g- '((6 . -1)))
(:g+ '((8 . 1))) (:a- '((8 . -1)))
(:a+ '((10 . 1))) (:b- '((10 . -1)))))
nconc (loop for i from 0 below 128 by 12 collect (cons (+ i a) b))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; USER UTILITIES
(defun list-fomus-settings ()
"Info function:
Lists all of FOMUS's current default settings and values"
(let* ((tc (+ 2 (max (1+ (mloop for x in +settings+ maximize (length (symbol-name (first x))))) 4)))
(tl (+ tc 1 (max (mloop for (xxx t1 t2) in +settings+ maximize (length (or t2 (princ-to-string t1)))) 4))))
(format t "; NAME~VTTYPE~VTDEFAULT VALUE~%~%" tc tl)
(loop for (sy t1 t2) in +settings+ unless (find sy +deprecated-repl+ :key #'car) do
(format t "; ~A~VT~A~VT~A~%" sy tc (or t2 t1) tl (remove-newlines (prin1-to-string (symbol-value (find-symbol (conc-strings "*" (symbol-name sy) "*") :fomus))))))))
(defun list-fomus-instruments ()
"Info function:
Lists all of FOMUS's instruments and their definitions"
(set-instruments
(loop with li = (remove-duplicates (append *instruments* +instruments+) :key #'instr-sym :from-end t)
with c = (+ (mloop for e in li maximize (length (symbol-name (instr-sym e)))) 3)
for e in li
do (format t "; ~A~VT~A~%"
(instr-sym e) c
(conc-stringlist
(loop for (s sn) on (rest +instr-keys+)
collect (format nil (if sn "~A: ~S " "~A: ~S") (string-downcase s) (slot-value e (intern (symbol-name s) :fomus)))))))))
(defun get-instr-syms ()
"Utility function:
Returns symbol IDs for all of FOMUS's instruments"
(set-instruments
(mapcar #'instr-sym (remove-duplicates (append *instruments* +instruments+) :key #'instr-sym :from-end t))))
(defun list-fomus-percussion ()
"Info function:
Lists all of FOMUS's percussion instruments and their definitions"
(set-instruments
(loop with li = (remove-duplicates (append *percussion* +percussion+) :key #'perc-sym :from-end t)
with c = (+ (mloop for e in li maximize (length (symbol-name (perc-sym e)))) 3)
for e in li
do (format t "; ~A~VT~A~%"
(perc-sym e) c
(conc-stringlist
(loop for (s sn) on (rest +perc-keys+)
collect (format nil (if sn "~A: ~S " "~A: ~S") (string-downcase s) (slot-value e (intern (symbol-name s) :fomus)))))))))
(defun get-perc-syms ()
"Utility function: