-
Notifications
You must be signed in to change notification settings - Fork 1
/
clairnote.ly
2223 lines (1882 loc) · 88.1 KB
/
clairnote.ly
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
% This file "clairnote.ly" is a LilyPond include file for producing
% sheet music in Clairnote music notation (http://clairnote.org).
% Version: 20231218
%
% Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021,
% 2022, 2023 Paul Morris,
% except for functions copied and modified from LilyPond source code,
% the LilyPond Snippet Repository, and openLilyLib, as noted in
% comments below.
% Contact information: http://clairnote.org/contact/
%
% This file 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 file 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 file. If not, see <http://www.gnu.org/licenses/>.
\version "2.19.49"
% For docstrings we use ;; instead of the usual "" to allow automated
% minification for LilyBin + Clairnote.
%--- UTILITY FUNCTIONS ----------------
#(define (non-zero? n) (not (zero? n)))
#(define (positive-integer? n) (and (positive? n) (integer? n)))
#(define (map-pair proc pair)
(cons
(proc (car pair))
(proc (cdr pair))))
#(define (cn-pitch-to-semitone pitch)
;; Takes a pitch object and returns a semitone integer that corresponds to
;; the pitch's position on the Clairnote staff. Used for
;; staffLineLayoutFunction. The return value is almost always the semitone
;; returned by (ly:pitch-semitones pitch) except for quarter tone
;; alteration exceptions. 1/4 and 3/4 alterations are quarter tone sharps
;; and their semitone needs to be adjusted down by one.
(let
((alteration (ly:pitch-alteration pitch))
(semitone (ly:pitch-semitones pitch)))
(cond
((= 1/4 alteration) (- semitone 1))
((= 3/4 alteration) (- semitone 1))
(else semitone))))
#(define (cn-notehead-pitch grob)
;; Takes a note head grob and returns its pitch.
(define event (ly:grob-property grob 'cause))
(if (ly:stream-event? event)
(ly:event-property event 'pitch)
(begin
(ly:warning "clairnote.ly cannot access the pitch of a note head grob. (Are you trying to use the Ambitus_engraver? It is incompatible with clairnote.ly.)")
(ly:make-pitch 0 0 0))))
#(define (cn-notehead-semitone grob)
;; Takes a note head grob and returns its semitone.
(cn-pitch-to-semitone (cn-notehead-pitch grob)))
#(define (cn-staff-symbol-property grob prop default)
;; Takes a grob @var{grob}, a symbol @var{prop}, and
;; a @var{default} value. Returns that custom StaffSymbol
;; property or silently falls back to the default value.
(define staff-sym (ly:grob-object grob 'staff-symbol))
(if (ly:grob? staff-sym)
(ly:grob-property staff-sym prop)
default))
#(define (cn-get-base-staff-space grob)
;; Takes a grob and returns the custom StaffSymbol property
;; cn-base-staff-space. Silently falls back to the default of 0.75.
(cn-staff-symbol-property grob 'cn-base-staff-space 0.75))
#(define (cn-magnification grob)
;; Return the current magnification (from magnifyStaff, etc.)
;; via a grob's font size.
(magstep (ly:grob-property grob 'font-size 0)))
#(define (cn-get-staff-clef-adjust staff-octaves clef-octave-shift)
;; Calculate the amount to vertically adjust the position of the clef,
;; key signature, and time signature, in note-spaces / half-staff-spaces.
(+
(* 12 clef-octave-shift)
(if (odd? staff-octaves)
6
(if (> staff-octaves 2) 12 0))))
#(define (cn-staff-clef-adjust-from-grob grob)
(cn-get-staff-clef-adjust
(cn-staff-symbol-property grob 'cn-staff-octaves 2)
(cn-staff-symbol-property grob 'cn-clef-shift 0)))
#(define (cn-note-heads-from-grob grob default)
;; Takes a grob like a Stem and returns a list of
;; NoteHead grobs or default.
(let* ((heads-array (ly:grob-object grob 'note-heads))
(heads-list (if (ly:grob-array? heads-array)
(ly:grob-array->list heads-array)
;; should never/rarely? happen:
default)))
heads-list))
%--- NOTE HEADS AND STEM ATTACHMENT ----------------
#(define cn-whole-note-black-path
'(
moveto 0 0
curveto 0 0.16054432 0.12694192 0.28001904 0.272552 0.35842432
curveto 0.47416576 0.4666984 0.70564816 0.50776784 0.93339712 0.50776784
curveto 1.16114576 0.50776784 1.39636192 0.4666984 1.59797568 0.35842432
curveto 1.74358576 0.28001904 1.87052768 0.16054432 1.87052768 0
curveto 1.87052768 -0.16054432 1.74358576 -0.2800192 1.59797568 -0.35842448
curveto 1.39636192 -0.46669856 1.16114592 -0.507768 0.93339712 -0.507768
curveto 0.70564816 -0.507768 0.47416576 -0.46669856 0.272552 -0.35842448
curveto 0.12694192 -0.2800192 0 -0.16054432 0 0
closepath))
#(define cn-whole-note-white-path
;; white-path is the black path with the center hole added
(append
cn-whole-note-black-path
'(
moveto 1.06033904 -0.36566768
curveto 1.24701856 -0.36566768 1.32542384 -0.2688184 1.32542384 -0.09707328
curveto 1.32542384 0.19788 1.10140848 0.36566752 0.80645504 0.36566752
curveto 0.61977552 0.36566752 0.545104 0.26881824 0.545104 0.09707312
curveto 0.545104 -0.19788016 0.7653856 -0.36566768 1.06033904 -0.36566768
closepath)))
#(define (cn-whole-note-stencil grob white-note)
;; Returns default Clairnote whole note stencils.
(let ((mag (cn-magnification grob))
(wn-path (if white-note
cn-whole-note-white-path
cn-whole-note-black-path)))
(ly:stencil-scale
(make-path-stencil wn-path 0.0001 1 1 #t)
mag mag)))
#(define cn-note-black-path
'(
moveto 1.0161518991984164 0.5004939160736768
curveto 1.1900858991984165 0.45804726744838686 1.3000056991984164 0.36006297281891986 1.3267185991984165 0.22365338254444356
curveto 1.3472530991984164 0.11878494731492373 1.3090816991984164 -0.03242382812749062 1.2346937991984164 -0.14100554722801906
curveto 1.1037044991984164 -0.3321698541497182 0.8786728091984164 -0.46440803197455877 0.6176073691984164 -0.5036333230878486
curveto 0.5243774691984164 -0.517643902935715 0.36590771919841636 -0.5120649140876494 0.28844503919841635 -0.49205136745873423
curveto -0.029717590801583628 -0.40984214143367353 -0.09642331080158363 -0.0917693764989545 0.14752670919841637 0.17990713903061328
curveto 0.2984087391984164 0.347937968940766 0.5439097091984164 0.4755783490866303 0.7757855691984165 0.5065506656056886
curveto 0.8399878691984165 0.5151219432426919 0.9695811491984164 0.5118635199715458 1.0161502991984164 0.5005020382431211
closepath))
#(define cn-note-white-path
'(
moveto 1.026468864255086 0.4998680875655276
curveto 1.215249864255086 0.4618436649454002 1.337174464255086 0.35147108531050375 1.354920364255086 0.20252749405141746
curveto 1.369178964255086 0.08282868839604651 1.312372764255086 -0.07672001395465605 1.209350364255086 -0.20633856802981299
curveto 1.077365164255086 -0.37239062345611024 0.889153024255086 -0.47211463127579484 0.6458905642550861 -0.5048878796193299
curveto 0.585101844255086 -0.5130801213612108 0.548868934255086 -0.5134163330622651 0.443309034255086 -0.5067845101638356
curveto 0.32885581425508603 -0.49958868733223255 0.30882433425508604 -0.4965974421400958 0.260617494255086 -0.47979947287536673
curveto 0.118058624255086 -0.4300386473948317 0.024819864255086005 -0.335419029253747 0.0042339542550860025 -0.21961971038330325
curveto -0.015404825744913999 -0.1091556900709823 0.035236334255086 0.05025573233647185 0.132092634255086 0.18283290751856218
curveto 0.268217284255086 0.3691712451565947 0.47658985425508604 0.48176186299022195 0.730680684255086 0.5062696961187646
curveto 0.823563584255086 0.5152225290660725 0.965453244255086 0.5121589666760266 1.026469764255086 0.4998666604401908
closepath
moveto 0.8920403042550861 0.32723653716982337
curveto 0.801899114255086 0.305937547790631 0.674353834255086 0.25092305532124815 0.517242874255086 0.16559336664623436
curveto 0.199745884255086 -0.006853856240945699 0.109727534255086 -0.09774589911519554 0.151265174255086 -0.2039339094078499
curveto 0.168776074255086 -0.24869436361851288 0.191705974255086 -0.27755407274963595 0.226470474255086 -0.2985602160096806
curveto 0.309656374255086 -0.34884402584120455 0.42197617425508605 -0.33748020734960626 0.634757234255086 -0.25724484236248213
curveto 0.9774722042550861 -0.1280260070658748 1.216026564255086 0.03390026706789495 1.2240259642550861 0.14273918170232358
curveto 1.2287459642550862 0.20700273076812625 1.184881964255086 0.28132959706261473 1.121983764255086 0.3156476039275703
curveto 1.083730764255086 0.33652340307350437 1.077348764255086 0.3379303583723863 1.015085564255086 0.3392592023444909
curveto 0.969948864255086 0.34025914149726677 0.9307790642550859 0.33638021483136094 0.892038904255086 0.32724518554936555
closepath))
#(define (cn-default-note-head-stencil grob white-note)
;; Returns default Clairnote note head stencils.
;; The hollow half-note and solid quarter-note glyphs are modified versions
;; (rotated -4 degrees then scaled vertically by 0.9299)
;; of these glyphs from the Bravura font,
;; licensed under the SIL Open Font License (OFL), see:
;; http://scripts.sil.org/OFL
;; http://www.smufl.org/fonts/
;; http://blog.steinberg.net/2013/05/introducing-bravura-music-font/
(let ((mag (cn-magnification grob))
(nh-path (if white-note
cn-note-white-path
cn-note-black-path)))
(ly:stencil-scale
(make-path-stencil nh-path 0.0001 1 1 #t)
mag mag)))
#(define (cn-lilypond-note-head-stencil grob white-note)
;; Returns 'lilypond' style note head stencils (Emmentaler font).
(if white-note
;; white notes are scaled horizontally to match black ones
(ly:stencil-scale
(ly:font-get-glyph (ly:grob-default-font grob) "noteheads.s1")
0.945 1)
(ly:font-get-glyph (ly:grob-default-font grob) "noteheads.s2")))
#(define (cn-funksol-note-head-stencil grob white-note)
;; Returns 'funksol' style note head stencils.
(ly:font-get-glyph (ly:grob-default-font grob)
(if white-note
"noteheads.s1solFunk"
"noteheads.s2solFunk")))
#(define (cn-stylish-note? grob)
;; Does a note head grob have one of these style properties.
(define style (ly:grob-property-data grob 'style))
;; TODO: better handling of various notehead styles
;; http://lilypond.org/doc/v2.18/Documentation/notation/note-head-styles
;; output-lib.scm
(and (not (null? style))
(memq style '(harmonic
harmonic-black
harmonic-mixed
diamond
cross
xcircle
triangle
slash))))
#(define (cn-whole-note? grob)
;; Note: longer durations than whole note also return #t.
;; duration-log: 0 is whole, 1 is half, 2 is quarter and shorter.
(< (ly:grob-property grob 'duration-log) 1))
% Set to functions depending on the version of Clairnote.
#(define cn-white-note? '())
#(define cn-default-note-head-stencil-callback '())
#(define (cn-make-note-head-stencil-callback
style-fn white-note? width-scale height-scale)
;; Returns a callback function for the note head stencil.
;; style-fn (function) takes a grob and a boolean and
;; returns a black or white note head stencil.
;; white-note? (function) takes a grob and returns
;; whether the note should be white.
;; width-scale and height-scale (numbers) for scaling the stencil.
(lambda (grob)
(cond
((cn-stylish-note? grob) (ly:note-head::print grob))
((cn-whole-note? grob) (cn-whole-note-stencil grob (white-note? grob)))
(else
(let ((stil (style-fn grob (white-note? grob))))
(if (and (= 1 width-scale) (= 1 height-scale))
stil
(ly:stencil-scale stil width-scale height-scale)))))))
#(define (cn-make-note-head-rotation-callback rotn)
;; Returns a callback function for note head rotation,
;; that excludes whole notes and stylish notes.
;; Takes a list of three numbers like '(-9 0 0)
(lambda (grob)
(if (or (cn-whole-note? grob)
(cn-stylish-note? grob))
#f
rotn)))
#(define (cn-make-stem-attachment-callback black-attach white-attach)
;; Returns a callback function for stem attachment,
;; that excludes whole notes and stylish notes.
;; The arguments are pairs of numbers for black and white notes.
(lambda (grob)
(if (or (cn-whole-note? grob)
(cn-stylish-note? grob))
(ly:note-head::calc-stem-attachment grob)
(if (cn-white-note? grob)
white-attach
black-attach))))
%--- ACCIDENTAL STYLE ----------------
%% A custom accidental style that determines when and where
%% accidental signs are rendered or not (i.e. grobs created).
#(define (cn-recent-enough? bar-number alteration-def laziness)
;; Procedure copied from scm/music-functions.scm, renamed with cn- prefix.
(or (number? alteration-def)
(equal? laziness #t)
(<= bar-number (+ (cadr alteration-def) laziness))))
#(define (cn-accidental-invalid? alteration-def)
;; Procedure copied from scm/music-functions.scm, renamed with cn- prefix.
;; Checks an alteration entry for being invalid.
;; Non-key alterations are invalidated when tying into the next bar or
;; when there is a clef change, since neither repetition nor cancellation
;; can be omitted when the same note occurs again.
;; Returns @code{#f} or the reason for the invalidation, a symbol.
(let* ((def (if (pair? alteration-def)
(car alteration-def)
alteration-def)))
(and (symbol? def) def)))
#(define (cn-extract-alteration alteration-def)
;; Procedure copied from scm/music-functions.scm, renamed with cn- prefix.
(cond ((number? alteration-def)
alteration-def)
((pair? alteration-def)
(car alteration-def))
(else 0)))
#(define (cn-to-semitone-alterations cn-alterations accidental-alterations)
;; Converts accidental alteration data to allow lookup by semitone integer
;; which represents a Clairnote staff position.
;; From: ((octave . notename) . (alter barnum . end-moment))
;; To: (semitone alter barnum . end-moment)
;; notename is 0-6 diatonic note number.
;; With clef changes or notes tied across a bar line we get
;; e.g. ((0 . 6) clef 1 . #<Mom 7/8>) with 'clef or 'tied as the
;; alter value to invalidate the entry. Then we have to look up
;; the alter value in cn-alterations, the cnAlterations context property.
;; This is the sole purpose of cnAlterations. It would be much
;; simpler if LilyPond did not destructively overload the alter
;; value like this.
;; (format #t "cn-alterations: ~a \n" cn-alterations)
(map (lambda (entry)
(let*
((octave (caar entry))
(notename (cdar entry))
(alteration-def (cdr entry))
(alter (if (cn-accidental-invalid? alteration-def)
;; The following works because cn-extract-alteration
;; handles entries like: ((octave . notename) . alter)
;; and handles failed lookups (#f) by defaulting to 0.
(cn-extract-alteration
(assoc-ref cn-alterations (cons octave notename)))
(cn-extract-alteration alteration-def)))
(pitch (ly:make-pitch octave notename alter))
(semitone (cn-pitch-to-semitone pitch)))
(cons semitone alteration-def)))
accidental-alterations))
#(define (cn-merge-semi-alts cn-semi-alterations semitone-accidental-alterations)
;; Update cn-semi-alterations by merging semitone-accidental-alterations into it.
;; Their entries are: (semitone alter barnum . end-moment)
(define (get-barnum entry) (caddr entry))
(define (get-end-moment entry) (cdddr entry))
(define (merge-entry! local-entry)
(let* ((semi (car local-entry))
(cn-entry (assv semi cn-semi-alterations)))
;; (format #t "local-entry: ~a \n" local-entry)
;; (format #t "cn-entry: ~a \n\n" cn-entry)
;; We merge when there is no entry for a given semitone,
;; or when there is one with a previous barnum,
;; or when there is one with the same barnum
;; and a previous end-moment.
(if (or (not cn-entry)
(< (get-barnum cn-entry)
(get-barnum local-entry))
(and (= (get-barnum cn-entry)
(get-barnum local-entry))
(ly:moment<? (get-end-moment cn-entry)
(get-end-moment local-entry))))
(set! cn-semi-alterations
(assv-set! cn-semi-alterations semi (cdr local-entry))))))
;; (format #t "cn-semi-alterations: ~a \n" cn-semi-alterations)
;; (format #t "semitone-accidental-alterations: ~a \n\n" semitone-accidental-alterations)
(for-each merge-entry! semitone-accidental-alterations)
cn-semi-alterations)
#(define (cn-check-pitch-against-signature context pitch barnum laziness)
;; A modified version of this function from scm/music-functions.scm.
;; Arguments octaveness and all-naturals have been removed. Currently
;; laziness is always 0. We check active accidentals by semitone,
;; which requires conversion to semitones first, but we check
;; key signature by diatonic notename/number (0-6).
;; Checks the need for an accidental and a @q{restore} accidental
;; against @code{localAlterations} and @code{keyAlterations}.
;; The @var{laziness} is the number of measures for which reminder
;; accidentals are used (i.e., if @var{laziness} is zero, only cancel
;; accidentals in the same measure; if @var{laziness} is three, we
;; cancel accidentals up to three measures after they first appear.
(let*
;; localAlterations includes key signature entries and accidental entries.
;; Filter out the key signature entries to leave just accidental entries.
;; Key signature entries come in two forms:
;; (notename . alter)
;; ((octave . notename) . alter)
;; Accidental entries have the form:
;; ((octave . notename) . (alter barnum . end-moment))
((local-alterations (ly:context-property context 'localAlterations '()))
(accidental? (lambda (entry) (pair? (cdr entry))))
(accidental-alterations (filter accidental? local-alterations))
;; Convert the accidental alterations to a semitone-based version and
;; store it in the cnSemiAlterations context property.
(cn-semi-alterations
(if (null? accidental-alterations)
(begin
(ly:context-set-property! context 'cnSemiAlterations '())
(ly:context-set-property! context 'cnAlterations '())
'())
(let*
((semitone-accidental-alterations
(cn-to-semitone-alterations
(ly:context-property context 'cnAlterations '())
accidental-alterations))
(new-semi-alterations
(cn-merge-semi-alts
(ly:context-property context 'cnSemiAlterations '())
semitone-accidental-alterations)))
;; (format #t "accidental-alterations: ~a \n" accidental-alterations)
;; (format #t "new-semi-alterations: ~a \n" new-semi-alterations)
(ly:context-set-property! context 'cnSemiAlterations new-semi-alterations)
new-semi-alterations)))
(notename (ly:pitch-notename pitch))
(octave (ly:pitch-octave pitch))
(alter (ly:pitch-alteration pitch))
(semitone (cn-pitch-to-semitone pitch))
;; will be #f or (alter barnum . end-moment)
(from-cn-semi-alterations (assoc-get semitone cn-semi-alterations))
;; Get previous alteration for comparison with pitch.
(previous-alteration
(if (and from-cn-semi-alterations
(cn-recent-enough? barnum from-cn-semi-alterations laziness))
from-cn-semi-alterations
(let*
((key-alterations (ly:context-property context 'keyAlterations '())))
;; keyAlterations can contain regular and octave-specific entries.
;; (notename . alter)
;; ((octave . notename) . alter)
;; The value will be #f or an alter number (e.g. 1/2, -1/2, 0).
(or (assoc-get notename key-alterations)
(assoc-get (cons octave notename) key-alterations))))))
;; (format #t "semi: ~a alter: ~a barnum: ~a \n\n" semi alter barnum)
;; Write the current note's alter value into cnAlterations, overwriting any
;; existing entries for that (octave . notename) key.
(ly:context-set-property! context 'cnAlterations
(assoc-set! (ly:context-property context 'cnAlterations)
(cons octave notename)
alter))
;; Return a pair of booleans.
;; The first is always false since we never print an extra natural sign.
;; The second is whether an accidental sign should be printed.
;; We print it if the previous alter is either invalidated or
;; doesn't match the current one.
(if (cn-accidental-invalid? previous-alteration)
'(#f . #t)
(let ((prev-alt (cn-extract-alteration previous-alteration)))
(if (= alter prev-alt)
'(#f . #f)
'(#f . #t))))))
#(define (cn-make-accidental-rule laziness)
;; Slightly modified, octaveness argument has been removed.
;; Starting with Lilypond 2.23.4 the returned function took 3 arguments
;; (context, pitch, and barnum), but in earlier versions it took 4
;; arguments. The 4th argument was measure position, which is not actually
;; necessary for the Clairnote accidental style code. So return a
;; function that takes an optional 4th argument and ignores it, for
;; backwards compatibility. See commit 2151499a:
;; https://gitlab.com/lilypond/lilypond/-/commit/2151499a7ca37a8138cea630be96da5daea88159
;; Create an accidental rule that makes its decision based on a laziness value.
;; @var{laziness} states over how many bars an accidental should be remembered.
;; @code{0}@tie{}is the default -- accidental lasts over 0@tie{}bar lines, that
;; is, to the end of current measure. A positive integer means that the
;; accidental lasts over that many bar lines. @w{@code{-1}} is `forget
;; immediately', that is, only look at key signature. @code{#t} is `forever'.
(lambda (context pitch barnum . rest)
(cn-check-pitch-against-signature context pitch barnum laziness)))
accidental-styles.clairnote-default =
#`(#t (Staff ,(cn-make-accidental-rule 0)) ())
accidental-styles.none = #'(#t () ())
%--- ACCIDENTAL ENGRAVER ----------------
#(define (Cn_accidental_engraver context)
;; This accidental engraver is needed for directional natural signs.
;; For natural accidental signs, if they are "cancelling" a sharp
;; or flat from the current key signature, set a custom grob property
;; on the accidental sign grob that indicates the direction of the
;; natural sign, i.e. whether it is "cancelling" a sharp or a flat
;; from the key signature. Then the grob stencil function takes it
;; from there.
;; The context has to be accessed like this (and not with
;; ly:translator-context) for accidentals to be tracked per staff,
;; (e.g. when we were tracking accidentals per staff).
(make-engraver
(acknowledgers
((accidental-interface engraver grob source-engraver)
(let* ((pitch (cn-notehead-pitch (ly:grob-parent grob Y)))
(note (ly:pitch-notename pitch))
(key-alterations (ly:context-property context 'keyAlterations '()))
(key-sig-alt (assoc-ref key-alterations note))
(accidental-alt (accidental-interface::calc-alteration grob))
(is-natural (= accidental-alt 0)))
(if (and is-natural key-sig-alt)
(ly:grob-set-property! grob 'cn-natural-sign-direction
(cond
((> key-sig-alt 0) "natural-down")
((< key-sig-alt 0) "natural-up")
(else null)))))))))
%--- ACCIDENTAL SIGNS ----------------
#(define cn-acc-sign-stils
;; associative list of accidental sign stencils
(let*
((vertical-line
(make-path-stencil '(moveto 0 -0.5 lineto 0 0.5) 0.2 1 1 #f))
(circle (make-circle-stencil 0.24 0.01 #t))
(diagonal-line
(make-path-stencil '(moveto -0.13 -0.07 lineto 0.13 0.07) 0.33 1 1 #f))
(short-vertical-line
(make-path-stencil '(moveto 0 -0.3 lineto 0 0.3) 0.2 1 1 #f))
(acc-sign (lambda (dot-position)
;; Return a sharp or flat sign stencil.
(ly:stencil-add vertical-line
(ly:stencil-translate circle `(0 . ,dot-position)))))
(double-acc-sign (lambda (stil)
;; Return a double sharp or double flat sign stencil.
(ly:stencil-add
(ly:stencil-translate stil '(-0.25 . 0))
(ly:stencil-translate stil '(0.25 . 0)))))
(sharp (acc-sign 0.5))
(flat (acc-sign -0.5))
(natural (ly:stencil-add diagonal-line
(ly:stencil-translate short-vertical-line '(0.2 . -0.3))
(ly:stencil-translate short-vertical-line '(-0.2 . 0.3))))
(natural-down (ly:stencil-add natural
(ly:stencil-translate circle '(0.2 . -0.6))))
(natural-up (ly:stencil-add natural
(ly:stencil-translate circle '(-0.2 . 0.6)))))
`((1/2 . ,sharp)
(-1/2 . ,flat)
(0 . ,natural)
;; Use natural-down and natural-up for experimental directional natural signs.
("natural-down" . ,natural)
("natural-up" . ,natural)
(1 . ,(double-acc-sign sharp))
(-1 . ,(double-acc-sign flat)))))
#(define (cn-accidental-grob-callback grob)
;; Returns an accidental sign stencil.
(let* ((mag (cn-magnification grob))
(alt (accidental-interface::calc-alteration grob))
(direction (and (= 0 alt)
(ly:grob-property grob 'cn-natural-sign-direction)))
(stencil-key (if (string? direction) direction alt))
(stencil (assoc-ref cn-acc-sign-stils stencil-key)))
(cond
(stencil (ly:stencil-scale stencil mag mag))
;; For quarter tone accidentals: 3/4 sharps and flats get the 1/4 sharp
;; or flat symbol because the note is already raised or lowered 1/2
;; (one staff position) due to the chromatic staff.
((= alt 3/4) (ly:font-get-glyph (ly:grob-default-font grob)
"accidentals.sharp.slashslash.stem"))
((= alt -3/4) (ly:font-get-glyph (ly:grob-default-font grob)
"accidentals.mirroredflat"))
;; Else fall back to traditional accidental sign.
;; Before supporting quarter tones we were scaling this as follows:
;; (ly:stencil-scale (ly:accidental-interface::print grob) 0.63 0.63)
(else (ly:accidental-interface::print grob)))))
%--- KEY SIGNATURES ----------------
#(define (cn-get-keysig-alt-count alt-alist)
;; Return number of sharps/flats in key sig, (+) for sharps, (-) for flats.
(if (null? alt-alist)
0
(* (length alt-alist) 2 (cdr (car alt-alist)))))
#(define (cn-get-major-tonic alt-count)
;; Return number of the tonic note 0-6, as if the key sig were major.
;; (alt-count maj-num)
;; (-7 0) (-5 1) (-3 2) (-1 3) (1 4) (3 5) (5 6) (7 0)
;; (-6 4) (-4 5) (-2 6) (0 0) (2 1) (4 2) (6 3)
(if (odd? alt-count)
(modulo (- (/ (+ alt-count 1) 2) 4) 7)
(modulo (/ alt-count 2) 7)))
#(define (cn-make-keysig-posns prev pattern result x-inc)
;; Calculate x and y positions for keysig dots.
(if (null? pattern)
result
(let*
((whole-step (eqv? (car pattern) prev))
(y-step (if whole-step 2 1))
(x-step (if whole-step 0 x-inc))
(last-xy (last result))
(new-xy (cons (+ x-step (car last-xy)) (+ y-step (cdr last-xy)))))
(cn-make-keysig-posns
(car pattern)
(cdr pattern)
(append result (list new-xy))
x-inc))))
#(define (cn-make-keysig-stack mode alt-list note-space black-tonic tonic-num)
;; Create the stack of circles (and tonic oval) for the key sig.
(let*
((raw-pattern (take (drop '(#t #t #t #f #f #f #f #t #t #t #f #f #f #f) mode) 7))
(raw-first-item (list-ref raw-pattern 0))
;; invert raw-pattern if needed, so that the first item is
;; #t for black tonic and #f for white tonic
(pattern (if (or
(and black-tonic (not raw-first-item))
(and (not black-tonic) raw-first-item))
(map not raw-pattern)
raw-pattern))
(first-item (list-ref pattern 0))
(x-inc (if (and (pair? alt-list) (positive? (cdr (car alt-list)))) -0.8 0.8))
(raw-posns (cn-make-keysig-posns (car pattern) (cdr pattern) '((0 . 0)) x-inc))
(posns-b (map (lambda (p) (cons (car p) (* (cdr p) note-space))) raw-posns))
(posns (if (negative? x-inc)
(map (lambda (p) (cons (+ 1.2 (car p)) (cdr p))) posns-b)
posns-b))
(black-dot (make-oval-stencil 0.34 0.34 0.14 #t))
(white-dot (make-oval-stencil 0.34 0.34 0.15 #f))
(stack-list (map (lambda (xy bw)
(ly:stencil-translate (if bw black-dot white-dot) xy))
posns pattern))
;; add alterations - convert alt-list to a relative basis, tonic = 0, etc.
(relative-alt-list (map (lambda (n)
(cons (modulo (- (car n) tonic-num) 7)
(cdr n)))
alt-list))
(full-alt-list (map (lambda (n)
(assoc-ref relative-alt-list n)) '(0 1 2 3 4 5 6)))
(sharp-line (make-path-stencil '(moveto 0 -0.2 lineto -0.7 -0.9) 0.22 1 1 #f))
(flat-line (make-path-stencil '(moveto 0 0.2 lineto -0.7 0.9) 0.22 1 1 #f))
(alt-stack-list
(map (lambda (stil alt xy)
(cond
((eqv? -1/2 alt)
(ly:stencil-combine-at-edge
stil X -1 (ly:stencil-translate flat-line xy) -0.2))
((eqv? 1/2 alt)
(ly:stencil-combine-at-edge
stil X -1 (ly:stencil-translate sharp-line xy) -0.2))
(else stil)))
stack-list full-alt-list posns))
(combined-stack (fold ly:stencil-add empty-stencil alt-stack-list))
;; horizontal position adjustment
(extent (ly:stencil-extent combined-stack 0))
(positioned-stack
(ly:stencil-translate-axis combined-stack (- (car extent)) X)))
positioned-stack))
#(define (cn-draw-keysig grob)
;; Draws Clairnote key signature stencils.
(let*
((base-staff-space (cn-get-base-staff-space grob))
(tonic-pitch (ly:grob-property grob 'cn-tonic))
;; number of the tonic (0-6) (C-B)
(tonic-num (ly:pitch-notename tonic-pitch))
;; semitone of tonic (0-11) (C-B)
(tonic-semi (modulo (cn-pitch-to-semitone tonic-pitch) 12))
(alt-list (ly:grob-property grob 'alteration-alist))
(alt-count (cn-get-keysig-alt-count alt-list))
(major-tonic-num (cn-get-major-tonic alt-count))
;; number of the mode (0-6)
(mode (modulo (- tonic-num major-tonic-num) 7))
;; the distance between two adjacent notes given vertical staff compression
(note-space (* 0.5 base-staff-space))
(black-tonic (eqv? 0 (modulo tonic-semi 2)))
(raw-stack (cn-make-keysig-stack mode alt-list note-space black-tonic tonic-num))
;; position the sig vertically, C-tonic keys stay in place, the rest are moved down.
(base-vert-adj (if (= 0 tonic-semi) tonic-semi (- tonic-semi 12)))
;; adjust position for odd octave staves and clefs shifted up/down an octave, etc.
(staff-clef-adjust (cn-staff-clef-adjust-from-grob grob))
(vert-adj (* note-space (+ base-vert-adj staff-clef-adjust)))
(stack (ly:stencil-translate-axis raw-stack vert-adj Y)))
;; TODO: is this horizontal adjustment needed?
;; shift the sig to the right for better spacing
;; (ly:stencil-translate-axis stack 0 X)
;; (if (> mode 2)
;; (ly:stencil-translate-axis stack 0.35 X)
;; (ly:stencil-translate-axis stack 0.9 X))
stack))
#(define (cn-key-signature-grob-callback grob)
;; Returns a key signature stencil or #f. Clairnote's staff
;; definition has printKeyCancellation = ##f, which prevents
;; key cancellations, except when changing to C major or
;; A minor. So here we return #f for those key cancellations.
(if (grob::has-interface grob 'key-cancellation-interface)
#f
(let ((stil (cn-draw-keysig grob))
(mag (cn-magnification grob)))
(ly:stencil-scale stil mag mag))))
#(define (Cn_key_signature_engraver context)
;; Sets the tonic for the key on key signature grobs.
;; Spare parts: (ly:context-property context 'printKeyCancellation)
(make-engraver
(acknowledgers
((key-signature-interface engraver grob source-engraver)
(ly:grob-set-property! grob 'cn-tonic
(ly:context-property context 'tonic))))))
%--- CLEFS AND OTTAVA (8VA 8VB 15MA 15MB) ----------------
%% see /scm/parser-clef.scm and /ly/music-functions-init.ly
%% We use an engraver to convert to Clairnote clef properties
%% on the fly, rather than changing the traditional properties
%% themselves at the source. This allows TradStaff to work
%% fully for any clef.
#(define (cn-convert-clef-glyph glyph pos)
;; Takes a standard clefGlyph or cueClefGlyph string and
;; a clefPosition or cueClefPosition integer.
;; Returns the corresponding Clairnote clef glyph string.
;; Return '() when \cueClefUnset.
(if (null? glyph)
'()
(or
(cond
;; G clef glyph
;; -4 french => treble
;; -2 treble => treble
((string=? "clefs.G" glyph)
(if (member pos '(-2 -4)) "clefs.G" #f))
;; F clef glyph
;; 0 varbaritone => bass
;; 2 bass => bass
;; 4 subbass => bass
((string=? "clefs.F" glyph)
(if (member pos '(2 0 4)) "clefs.F" #f))
;; C clef glyph
;; -4 soprano => treble
;; -2 mezzosoprano => alto
;; 0 alto => alto (settings unchanged, but needed)
;; 2 tenor => alto
;; 4 baritone => bass
((string=? "clefs.C" glyph)
(cond
((member pos '(0 2 -2)) "clefs.C")
((= -4 pos) "clefs.G")
((= 4 pos) "clefs.F")
(else #f)))
((string=? "clefs.percussion" glyph) "clefs.percussion")
(else #f))
(begin
(ly:warning "clef unsupported by clairnote.ly, using another clef instead.")
(cond
((string=? "clefs.F" glyph) "clefs.F")
((string=? "clefs.C" glyph) "clefs.C")
(else "clefs.G"))))))
#(define (cn-convert-clef-transposition trans)
;; Takes standard clefTransposition or cueClefTransposition values and
;; converts them.
;; If trans is already a Clairnote value (...-12, 12, 24...) just return trans,
;; else convert from 7 notes per octave to 12. 7-->12, 14-->24. Rounding
;; means only multiples of 12 are ever returned (... -24, -12, 0, 12, 24 ...).
;; Return '() when \cueClefUnset.
(cond
((null? trans) '())
((= 0 (modulo trans 12)) trans)
(else (* 12 (round (/ trans 7))))))
#(define (cn-convert-clef-position glyph clef-adjust)
;; Takes standard clefPosition or cueClefPosition values and converts
;; them. Returns defaults for two-octave staves.
;; Return '() when \cueClefUnset.
(if (null? glyph)
'()
(+ clef-adjust
(cond
((string=? "clefs.G" glyph) -5)
((string=? "clefs.F" glyph) 5)
;; clefs.C and clefs.percussion
(else 0)))))
#(define (cn-convert-middle-c-clef-position glyph clef-adjust trans)
;; Takes standard middleCClefPosition or middleCCuePosition values
;; and converts them. trans is clefTransposition or cueClefTransposition.
;; Returns defaults for two-octave staves.
;; To calculate the clairnote middle c position subtract the clef position
;; from 12 for bass clef or from -12 for treble clef (to adjust the clef
;; position without affecting the position of middle c or other notes).
;; Return '() when \cueClefUnset.
(if (null? glyph)
'()
(+ clef-adjust
(- trans)
(cond
((string=? "clefs.G" glyph) -12)
((string=? "clefs.F" glyph) 12)
;; clefs.C and clefs.percussion
(else 0)))))
#(define (cn-convert-middle-c-offset offset)
;; Takes standard middleCOffset values for Ottava/8va and converts them.
(if (null? offset)
'()
(* offset 12/7)))
#(define (Cn_clef_ottava_engraver context)
;; Overrides clef and ottava settings. A closure stores the previous
;; properties in order to detect changed settings. Uses listeners
;; to modify context properties before grobs are created.
;; In order to have stems change direction at the vertical center
;; of the staff we use different clef settings for staves with odd
;; or even numbers of octaves (with clef-adjust).
(let*
((props (alist->hash-table '((clefGlyph . ())
(clefPosition . ())
(middleCClefPosition . ())
(clefTransposition . ())
(cueClefGlyph . ())
(cueClefPosition . ())
(middleCCuePosition . ())
(cueClefTransposition . ())
;; for ottava, 8va
(middleCOffset . ())
(cnStaffOctaves . ())
(cnClefShift . ()))))
(set-prop! (lambda (kee val)
(hash-set! props kee val)
(ly:context-set-property! context kee val)))
;; Most arguments to the following functions are property name
;; symbols, e.g. glyph will be 'clefGlyph or 'cueClefGlyph.
;; But clef-adjust is a number.
(set-glyph! (lambda (glyph pos)
(set-prop! glyph
(cn-convert-clef-glyph
(ly:context-property context glyph)
(ly:context-property context pos)))))
(set-transposition! (lambda (trans)
(set-prop! trans
(cn-convert-clef-transposition
(ly:context-property context trans)))))
(set-position! (lambda (pos glyph clef-adjust)
(set-prop! pos
(cn-convert-clef-position
(hash-ref props glyph '()) clef-adjust))))
(set-middle-c! (lambda (midc glyph clef-adjust trans)
(set-prop! midc
(cn-convert-middle-c-clef-position
(hash-ref props glyph '())
clef-adjust
(hash-ref props trans '())))))
(changed? (lambda (prop equality-predicate)
(not (equality-predicate
(hash-ref props prop)
(ly:context-property context prop))))))
(make-engraver
(listeners
;; TODO: confirm that rhythmic-event is best event to listen to.
((rhythmic-event engraver event)
(let*
((new-staff-octaves (changed? 'cnStaffOctaves eqv?))
(new-clef-shift (changed? 'cnClefShift eqv?))
(new-mid-c-offset (changed? 'middleCOffset eqv?))
(new-clef (or (changed? 'clefPosition eqv?)
(changed? 'middleCClefPosition eqv?)
(changed? 'clefTransposition eqv?)
(changed? 'clefGlyph equal?)))
(new-cue (or (changed? 'cueClefPosition eqv?)
(changed? 'middleCCuePosition eqv?)
(changed? 'cueClefTransposition eqv?)
(changed? 'cueClefGlyph equal?))))
;; The Clef engraver is called every measure (!),
;; so exit early if nothing changed.
(if (or new-clef new-cue
new-staff-octaves new-clef-shift new-mid-c-offset)
(let
;; clef-adjust shifts clefPosition and middleCClefPosition:
;; up 6 note-positions for odd octave staves
;; up 12 for even octave staves with 4 or more octaves
;; up or down 12 * Staff.cnClefShift
((clef-adjust (cn-get-staff-clef-adjust
(ly:context-property context 'cnStaffOctaves)
(ly:context-property context 'cnClefShift))))
;; Custom Clairnote props don't need conversion, just store them.
(if new-staff-octaves
(hash-set! props 'cnStaffOctaves
(ly:context-property context 'cnStaffOctaves)))
(if new-clef-shift
(hash-set! props 'cnClefShift
(ly:context-property context 'cnClefShift)))
;; The order in which clef properties are set is important!
(if new-clef (set-glyph! 'clefGlyph 'clefPosition))
(if new-cue (set-glyph! 'cueClefGlyph 'cueClefPosition))
(if (or new-clef new-staff-octaves new-clef-shift)
(begin
(set-transposition! 'clefTransposition)
(set-position! 'clefPosition 'clefGlyph clef-adjust)
(set-middle-c! 'middleCClefPosition
'clefGlyph clef-adjust 'clefTransposition)))
(if (or new-cue new-staff-octaves new-clef-shift)
(begin
(set-transposition! 'cueClefTransposition)
(set-position! 'cueClefPosition 'cueClefGlyph clef-adjust)
(set-middle-c! 'middleCCuePosition
'cueClefGlyph clef-adjust 'cueClefTransposition)))
;; Ottava, 8va, 8vb, etc.
(if new-mid-c-offset
(set-prop! 'middleCOffset
(cn-convert-middle-c-offset
(ly:context-property context 'middleCOffset))))
(ly:set-middle-C! context)
)))))
;; Copy clefTransposition context property to a custom Clef grob property.
(acknowledgers
((clef-interface engraver grob source-engraver)
(ly:grob-set-property! grob 'cn-clef-transposition
(ly:context-property context 'clefTransposition))))
)))
%--- CLEF GLYPHS
#(define cn-clef-curves
;; treble
'(("clefs.G" .
(
moveto 1.5506 4.76844
curveto 1.5376 4.76844 1.5066 4.75114 1.5136 4.73384