Repository navigation
Expand file tree
/
Copy pathmusic-functions.scm
More file actions
2878 lines (2590 loc) · 117 KB
/
Copy pathmusic-functions.scm
File metadata and controls
2878 lines (2590 loc) · 117 KB
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 is part of LilyPond, the GNU music typesetter.
;;;;
;;;; Copyright (C) 1998--2022 Jan Nieuwenhuizen <janneke@gnu.org>
;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
;;;;
;;;; LilyPond 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.
;;;;
;;;; LilyPond 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 LilyPond. If not, see <http://www.gnu.org/licenses/>.
(use-modules (ice-9 optargs)
(ice-9 match)
(srfi srfi-11))
(define-public (music-is-of-type? mus type)
"Does @var{mus} belong to the music class @var{type}?"
(memq type (ly:music-property mus 'types)))
(eval-when (expand load eval)
(define-public (music-type-predicate types)
"Return a predicate function that can be used for checking
music to have one of the types listed in @var{types}."
(if (cheap-list? types)
(lambda (m)
(any (lambda (t) (music-is-of-type? m t)) types))
(lambda (m) (music-is-of-type? m types)))))
(define-public (music-selective-map descend? function music)
"Apply @var{function} recursively to @var{music}, but refrain
from mapping subexpressions of music that does not satisfy
@var{descend?}."
(define (worker m)
(music-selective-map descend? function m))
(if (descend? music)
(let ((arts (ly:music-property music 'articulations))
(es (ly:music-property music 'elements))
(e (ly:music-property music 'element)))
(if (pair? es)
(set! (ly:music-property music 'elements)
(map worker es)))
(if (pair? arts)
(set! (ly:music-property music 'articulations)
(map worker arts)))
(if (ly:music? e)
(set! (ly:music-property music 'element)
(worker e)))))
(recompute-music-length (function music)))
(define-public (music-map function music)
"Apply @var{function} to @var{music} and all of the music it contains.
First it recurses over the children, then the function is applied to
@var{music}."
(music-selective-map ly:music? function music))
(define-public (music-selective-filter descend? pred? music)
"Recursively filter out music expressions that do not satisfy
@var{pred?}, but refrain from filtering the subexpressions of
music that does not satisfy @var{descend?}."
(define (inner-music-filter music)
"Recursive function."
(if (not (descend? music))
(if (not (pred? music))
(set! music '()))
(let* ((es (ly:music-property music 'elements))
(e (ly:music-property music 'element))
(as (ly:music-property music 'articulations))
(filtered-as (filter ly:music? (map inner-music-filter as)))
(filtered-e (if (ly:music? e)
(inner-music-filter e)
e))
(filtered-es (filter ly:music? (map inner-music-filter es))))
(if (not (null? e))
(set! (ly:music-property music 'element) filtered-e))
(if (not (null? es))
(set! (ly:music-property music 'elements) filtered-es))
(if (not (null? as))
(set! (ly:music-property music 'articulations) filtered-as))
;; if filtering invalidated 'element, we remove the music unless
;; there are remaining 'elements in which case we just hope and
;; pray.
(if (or (not (pred? music))
(and (null? filtered-es)
(not (ly:music? filtered-e))
(ly:music? e)))
(set! music '()))
(if (ly:music? music)
(recompute-music-length music))))
music)
(set! music (inner-music-filter music))
(if (ly:music? music)
music
(make-music 'Music))) ;must return music.
(define-public (music-filter pred? music)
"Filter out music expressions that do not satisfy @var{pred?}."
(music-selective-filter ly:music? pred? music))
(define*-public (display-music music #:optional (port (current-output-port)))
"Display @var{music}, not done with @code{music-map} for clarity of
presentation."
(display music port)
(display ": { " port)
(let ((es (ly:music-property music 'elements))
(e (ly:music-property music 'element)))
(display (ly:music-mutable-properties music) port)
(if (pair? es)
(begin (display "\nElements: {\n" port)
(for-each (lambda (m) (display-music m port)) es)
(display "}\n" port)))
(if (ly:music? e)
(begin
(display "\nChild:" port)
(display-music e port))))
(display " }\n" port)
music)
;;;
;;; A scheme music pretty printer
;;;
(define (markup-expression->make-markup markup-expression)
"Transform `markup-expression' into an equivalent, hopefuly readable, scheme expression.
For instance,
\\markup \\bold \\italic hello
==>
(markup #:line (#:bold (#:italic (#:simple \"hello\"))))"
(define (proc->command-keyword proc)
"Return a keyword, e.g., `#:bold`, from the `proc` function,
e.g., `#<procedure bold-markup (layout props arg)>`."
(let ((cmd-markup (symbol->string (procedure-name proc))))
(symbol->keyword (string->symbol (substring cmd-markup 0 (- (string-length cmd-markup)
(string-length "-markup")))))))
(define (transform-arg arg)
(cond ((and (pair? arg) (markup? (car arg))) ;; a markup list
(append-map inner-markup->make-markup arg))
((and (not (string? arg)) (markup? arg)) ;; a markup
(inner-markup->make-markup arg))
(else ;; scheme arg
(music->make-music arg))))
(define (inner-markup->make-markup mrkup)
(if (string? mrkup)
`(#:simple ,mrkup)
(let ((cmd (proc->command-keyword (car mrkup)))
(args (map transform-arg (cdr mrkup))))
`(,cmd ,@args))))
;; body:
(if (string? markup-expression)
markup-expression
`(markup ,@(inner-markup->make-markup markup-expression))))
(define-public (music->make-music obj)
"Generate an expression that, once evaluated, may return an object
equivalent to @var{obj}, that is, for a music expression, a
@code{(make-music ...)} form."
(define (if-nonzero num)
(if (zero? num) '() (list num)))
(cond (;; markup expression
(markup? obj)
(markup-expression->make-markup obj))
(;; music expression
(ly:music? obj)
`(make-music
',(ly:music-property obj 'name)
,@(append-map (lambda (prop)
`(',(car prop)
,(music->make-music (cdr prop))))
(remove (lambda (prop)
(eqv? (car prop) 'origin))
(ly:music-mutable-properties obj)))))
(;; moment
(ly:moment? obj)
`(ly:make-moment
,@(let ((main (ly:moment-main obj))
(grace (ly:moment-grace obj)))
(cond ((zero? grace) (list main))
((negative? grace) (list main grace))
(else ;;positive grace requires 4-arg form
(list (numerator main)
(denominator main)
(numerator grace)
(denominator grace)))))))
(;; note duration
(ly:duration? obj)
`(ly:make-duration ,(ly:duration-log obj)
,@(if (= (ly:duration-scale obj) 1)
(if-nonzero (ly:duration-dot-count obj))
(list (ly:duration-dot-count obj)
(ly:duration-scale obj)))))
(;; note pitch
(ly:pitch? obj)
`(ly:make-pitch ,(ly:pitch-octave obj)
,(ly:pitch-notename obj)
,@(if-nonzero (ly:pitch-alteration obj))))
(;; scheme procedure
(procedure? obj)
(or (procedure-name obj) obj))
(;; a symbol (avoid having an unquoted symbol)
(symbol? obj)
`',obj)
(;; an empty list (avoid having an unquoted empty list)
(null? obj)
`'())
(;; a proper list
(list? obj)
`(list ,@(map music->make-music obj)))
(;; a pair
(pair? obj)
`(cons ,(music->make-music (car obj))
,(music->make-music (cdr obj))))
(else
obj)))
(use-modules (ice-9 pretty-print))
(define*-public (display-scheme-music obj #:optional (port (current-output-port)))
"Display @var{obj}, typically a music expression, in a friendly fashion,
which often can be read back in order to generate an equivalent expression."
(pretty-print (music->make-music obj) port)
(newline port))
;;;
;;; Scheme music expression --> Lily-syntax-using string translator
;;;
(use-modules (srfi srfi-39)
(lily display-lily))
(define*-public (display-lily-music expr #:optional (port (current-output-port)))
"Display the music expression @var{expr} using LilyPond syntax."
(memoize-clef-names supported-clefs)
(parameterize ((*indent* 0)
(*omit-duration* #f))
(display (music->lily-string expr) port)
(newline port)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-public (shift-one-duration-log music shift dot)
"Add @var{shift} to @code{duration-log} of @code{'duration} in
@var{music} and optionally @var{dot} to any note encountered.
The number of dots in the shifted music may not be less than zero."
(let ((d (ly:music-property music 'duration)))
(if (ly:duration? d)
(let* ((cp (ly:duration-scale d))
(nd (ly:make-duration
(+ shift (ly:duration-log d))
(max 0 (+ dot (ly:duration-dot-count d)))
cp)))
(set! (ly:music-property music 'duration) nd)))
;; clear cached length, since it's no longer valid
(set! (ly:music-property music 'length) '())
music))
(define-public (shift-duration-log music shift dot)
(music-map (lambda (x) (shift-one-duration-log x shift dot))
music))
(define-public (volta-spec-music number-list music)
"Add \\volta @var{number-list} to @var{music}."
(make-music 'VoltaSpeccedMusic
'element music
'volta-numbers number-list))
(define-public (make-repeat name times main alts)
"Create a repeat music expression, with all properties initialized
properly."
(let* ((sane-times (max times 1)) ; TODO: Warn?
(type (or (assoc-get name '(("volta" . VoltaRepeatedMusic)
("segno" . SegnoRepeatedMusic)
("unfold" . UnfoldedRepeatedMusic)
("percent" . PercentRepeatedMusic)
("tremolo" . TremoloRepeatedMusic)))
(begin (ly:warning (G_ "unknown repeat type `~S': \
must be volta, unfold, percent, or tremolo") name)
'VoltaRepeatedMusic)))
(alt-music
(if (ly:music? alts)
(begin
;; TODO: Consider accepting plain sequential-music,
;; which would allow this:
;;
;; alts = { a b } % note no \alternative here
;; \repeat volta 2 {} \alternative \alts
;;
(if (not (music-is-of-type? alts 'sequential-alternative-music))
(ly:music-warning alts (G_ "alternative music expected")))
alts)
;; Accept a bare element list for backward compatibility.
(make-music 'SequentialAlternativeMusic
'elements alts
'origin (ly:music-property main 'origin)))))
;; If the user did not specify volta numbers, wrap the
;; alternatives for consistency with the legacy behavior.
(define (elaborate-alternative-music alt-music times)
(let* ((alts (ly:music-property alt-music 'elements))
(lalts (length alts))
(talts (if (< times lalts)
(let ((message (G_ "More alternatives than repeats. \
Junking excess alternatives")))
;; The \repeat and \the alternative are not
;; necessarily close together in the source.
;; Warn twice to point to both.
(ly:music-warning main message)
(ly:music-warning alt-music message)
(set! lalts times)
(take alts times))
alts)))
(define (is-specced music)
(music-is-of-type? music 'volta-specification))
(if (not (any is-specced alts))
(let* ((alt-1-count (1+ (- times lalts)))
;; volta numbers for each alternative (list of lists)
(volta-numbers (cons
(map 1+ (iota alt-1-count))
(map (lambda (i) (list (+ alt-1-count 1 i)))
(iota (- times 1))))))
;; wrap the alternatives and set their volta numbers
(set! talts (map volta-spec-music volta-numbers talts))))
(make-music 'SequentialAlternativeMusic
'elements talts)))
(define (pass-over-repeated-music music)
(not (music-is-of-type? music 'repeated-music)))
(define (map-alternatives m)
(if (music-is-of-type? m 'sequential-alternative-music)
(elaborate-alternative-music m sane-times)
m))
(make-music type
'element (music-selective-map
pass-over-repeated-music
map-alternatives
main)
'repeat-count sane-times
'elements
(ly:music-property
(elaborate-alternative-music alt-music sane-times)
'elements))))
(define (calc-repeat-slash-count music)
"Given the child-list @var{music} in @code{PercentRepeatMusic},
calculate the number of slashes based on the durations. Returns @code{0}
if durations in @var{music} vary, allowing slash beats and double-percent
beats to be distinguished."
(let* ((durs (map duration-of-note
(extract-named-music music '(EventChord NoteEvent
RestEvent SkipEvent))))
(first-dur (car durs)))
(if (every (lambda (d) (equal? d first-dur)) durs)
(max (- (ly:duration-log first-dur) 2) 1)
0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; clusters.
(define-public (note-to-cluster music)
"Replace @code{NoteEvents} by @code{ClusterNoteEvents}."
(if (eq? (ly:music-property music 'name) 'NoteEvent)
(make-music 'ClusterNoteEvent
'pitch (ly:music-property music 'pitch)
'duration (ly:music-property music 'duration))
music))
(define-public (notes-to-clusters music)
(music-map note-to-cluster music))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; repeats.
(define-public (unfold-repeats types music)
"Replace repeats of the types given by @var{types} with unfolded repeats.
If @var{types} is an empty list, @code{repeated-music} is taken, unfolding all."
(let* ((types-list
(if (or (null? types) (not (list? types)))
(list types)
types))
(repeat-types-alist
'((volta . volta-repeated-music)
(segno . segno-repeated-music)
(percent . percent-repeated-music)
(tremolo . tremolo-repeated-music)
(() . repeated-music)))
(repeat-types-hash (alist->hash-table repeat-types-alist)))
(for-each
(lambda (type)
(let ((repeat-type (hashq-ref repeat-types-hash type)))
(if repeat-type
(let ((es (ly:music-property music 'elements))
(e (ly:music-property music 'element)))
(if (music-is-of-type? music repeat-type)
(set! music (make-music 'UnfoldedRepeatedMusic music)))
(if (pair? es)
(set! (ly:music-property music 'elements)
(map (lambda (x) (unfold-repeats types x)) es)))
(if (ly:music? e)
(set! (ly:music-property music 'element)
(unfold-repeats types e))))
(ly:warning (G_ "unknown repeat-type ~a, ignoring.") type))))
types-list)
music))
(define-public (unfold-repeats-fully music)
"Unfold repeats and expand the resulting @code{unfolded-repeated-music}."
(map-some-music
(lambda (m)
(and (music-is-of-type? m 'unfolded-repeated-music)
(make-sequential-music
(ly:music-deep-copy (make-unfolded-set m)))))
(unfold-repeats '() music)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; property setting music objs.
;; Can't use define* behavior since Guile-1.8 has a bug when combining
;; #:optional with #:key and leaving optional args off.
(define-public (check-grob-path path . rest)
"Check a grob path specification @var{path}, a symbol list (or a
single symbol), for validity and possibly complete it. Returns the
completed specification, or @code{#f} if invalid, optionally using
@var{location} for an error message. If an optional keyword argument
@code{#:start @var{start}} is given, the parsing starts at the given
index in the sequence @samp{Context.@/Grob.@/property.@/sub-property...},
with the default of @samp{0} implying the full path.
If there is no valid first element of @var{path} fitting at the given
path location, an optionally given @code{#:default @var{default}} is
used as the respective element instead without checking it for
validity at this position.
The resulting path after possibly prepending @var{default} can be
constrained in length by optional arguments @code{#:min @var{min}} and
@code{#:max @var{max}}, defaulting to @samp{1} and unlimited,
respectively."
(define (unspecial? s)
(not (or (object-property s 'is-grob?)
(object-property s 'backend-type?))))
(define (grob? s)
(object-property s 'is-grob?))
(define (property? s)
(object-property s 'backend-type?))
(define (check c p) (c p))
(let-keywords
(if (or (null? rest) (keyword? (car rest)))
rest
(cdr rest))
#f
((start 0)
default
(min 1)
max)
(let* ((path (if (symbol? path) (list path) path))
(location (and (pair? rest) (not (keyword? (car rest)))
(car rest)))
(checkers
(and (< start 3)
(drop (list unspecial? grob? property?) start)))
(res
(cond
((null? path)
;; tricky. Should we make use of the default when the
;; list is empty? In most cases, this question should be
;; academical as an empty list can only be generated by
;; Scheme and is likely an error. We consider this a case
;; of "no valid first element, and default given".
;; Usually, invalid use cases should be caught later using
;; the #:min argument, and if the user explicitly does not
;; catch this, we just follow through.
(if default (list default) '()))
((not checkers)
;; no checkers, so we have a valid first element and just
;; take the path as-is.
path)
(default
(if ((car checkers) (car path))
(and (every check (cdr checkers) (cdr path))
path)
(and (every check (cdr checkers) path)
(cons default path))))
(else
(and (every check checkers path)
path)))))
(if (and res
(if max (<= min (length res) max)
(<= min (length res))))
res
(begin
(ly:parser-error
(format #f (G_ "bad grob property path ~a")
path)
location)
#f)))))
(define*-public (check-context-path path #:optional location)
"Check a context property path specification @var{path}, a symbol
list (or a single symbol), for validity and possibly complete it.
Returns the completed specification, or @code{#f} when rising an
error (using optionally @var{location})."
(let* ((path (if (symbol? path) (list path) path)))
;; A Guile 1.x bug specific to optargs precludes moving the
;; defines out of the let
(define (property? s)
(object-property s 'translation-type?))
(define (unspecial? s)
(not (property? s)))
(define (check c p) (c p))
(or (case (length path)
((1) (and (property? (car path)) (cons 'Bottom path)))
((2) (and (unspecial? (car path)) (property? (cadr path)) path))
(else #f))
(begin
(ly:parser-error
(format #f (G_ "bad context property ~a")
path)
location)
#f))))
;; Cannot use #:optional and #:key at the same time because of Guile
;; bug in version 1.8
(define-public (check-music-path path . rest)
"Check a music property path specification @var{path}, a symbol
list (or a single symbol), for validity and possibly complete it.
Returns the completed specification, or @code{#f} when rising an
error (using optionally @var{location})."
(define (property? s)
(object-property s 'music-type?))
(define (unspecial? s)
(not (property? s)))
(let-keywords
(if (or (null? rest) (keyword? (car rest)))
rest
(cdr rest))
#f
(default)
(let* ((path (if (symbol? path) (list path) path))
(location (and (pair? rest) (not (keyword? (car rest)))
(car rest))))
(or (case (length path)
((1) (and (property? (car path)) (cons default path)))
((2) (and (unspecial? (car path)) (property? (cadr path)) path))
(else #f))
(begin
(ly:parser-error
(format #f (G_ "bad music property ~a")
path)
location)
#f)))))
(define-public (make-grob-property-set grob gprop val)
"Make a @code{Music} expression that overrides a @var{gprop} to
@var{val} in @var{grob}. Does a pop first, i.e., this is not a
@code{\\temporary \\override}."
(make-music 'OverrideProperty
'symbol grob
'grob-property gprop
'grob-value val
'pop-first #t))
(define-public (make-grob-property-override grob gprop val)
"Make a @code{Music} expression that overrides @var{gprop} to
@var{val} in @var{grob}. This is a @code{\\temporary \\override},
making it possible to @code{\\revert} to any previous value afterwards."
(make-music 'OverrideProperty
'symbol grob
'grob-property gprop
'grob-value val))
(define-public (make-grob-property-revert grob gprop)
"Revert the grob property @var{gprop} for @var{grob}."
(make-music 'RevertProperty
'symbol grob
'grob-property gprop))
(define direction-polyphonic-grobs
'(AccidentalSuggestion
DotColumn
Dots
Fingering
LaissezVibrerTie
LigatureBracket
MultiMeasureRest
PhrasingSlur
RepeatTie
Rest
Script
Slur
Stem
TextScript
Tie
TupletBracket
TrillSpanner))
(define general-grace-settings
`((Voice Stem font-size -3)
(Voice Flag font-size -3)
(Voice NoteHead font-size -3)
(Voice TabNoteHead font-size -4)
(Voice Dots font-size -3)
(Voice Stem length-fraction 0.8)
(Voice Stem no-stem-extend #t)
(Voice Beam beam-thickness 0.384)
(Voice Beam length-fraction 0.8)
(Voice Accidental font-size -4)
(Voice AccidentalCautionary font-size -4)
(Voice Script font-size -3)
(Voice Fingering font-size -8)
(Voice StringNumber font-size -8)))
(define-public score-grace-settings
(append
`((Voice Stem direction ,UP)
(Voice Slur direction ,DOWN))
general-grace-settings))
;; Getting a unique context id name
(define-session unique-counter -1)
(define-public (get-next-unique-voice-name)
(set! unique-counter (1+ unique-counter))
(format #f "uniqueContext~s" unique-counter))
(define-public (make-voice-props-set n)
(make-sequential-music
(append
(map (lambda (x) (make-grob-property-set x 'direction
(if (odd? n) -1 1)))
direction-polyphonic-grobs)
(list
(make-property-set 'graceSettings general-grace-settings)
(make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))))))
(define-public (make-voice-props-override n)
(make-sequential-music
(append
(map (lambda (x) (make-grob-property-override x 'direction
(if (odd? n) -1 1)))
direction-polyphonic-grobs)
(list
(make-property-set 'graceSettings general-grace-settings)
(make-grob-property-override 'NoteColumn 'horizontal-shift (quotient n 2))))))
(define-public (make-voice-props-revert)
(make-sequential-music
(append
(map (lambda (x) (make-grob-property-revert x 'direction))
direction-polyphonic-grobs)
(list (make-property-unset 'graceSettings)
(make-grob-property-revert 'NoteColumn 'horizontal-shift)))))
(define*-public (context-spec-music m context #:optional id mods)
"Add @code{\\context @var{context} = @var{id} \\with @var{mods}} to @var{m}."
(let ((cm (make-music 'ContextSpeccedMusic
'element m
'context-type context)))
(if (string? id)
(set! (ly:music-property cm 'context-id) id))
(if mods
(set! (ly:music-property cm 'property-operations)
(if (ly:context-mod? mods)
(ly:get-context-mods mods)
mods)))
cm))
(define*-public (descend-to-context m context #:optional id mods)
"Like @code{context-spec-music}, but only descending."
(let ((cm (context-spec-music m context id mods)))
(ly:music-set-property! cm 'search-direction DOWN)
cm))
(define-public (make-non-relative-music mus)
(make-music 'UnrelativableMusic
'element mus))
(define-public (make-apply-context func)
(make-music 'ApplyContext
'procedure func))
(define-public (make-sequential-music elts)
(make-music 'SequentialMusic
'elements elts))
(define-public (make-simultaneous-music elts)
(make-music 'SimultaneousMusic
'elements elts))
(define-public (make-event-chord elts)
(make-music 'EventChord
'elements elts))
(define-public (make-skip-music dur)
(make-music 'SkipMusic
'duration dur))
(define-public (make-grace-music music)
(make-music 'GraceMusic
'element music))
;;;;;;;;;;;;;;;;
;; mmrest
(define-public (make-multi-measure-rest duration location)
(make-music 'MultiMeasureRestMusic
'origin location
'duration duration))
(define-public (make-property-set sym val)
(make-music 'PropertySet
'symbol sym
'value val))
(define-public (make-property-unset sym)
(make-music 'PropertyUnset
'symbol sym))
(define-public (make-articulation name . properties)
;; -----------------------------------------------------------------
;; obsoletion handling, may be removed at some point (e.g., for 2.26)
(if (string? name)
(begin
(ly:warning "articulation types should be symbols instead of \
strings since 2.23.6. Please replace (make-articulation \"~a\" ...) \
by (make-articulation '~a ...) or run convert-ly." name name)
(set! name (string->symbol name))))
;; -----------------------------------------------------------------
(apply make-music 'ArticulationEvent
'articulation-type name
properties))
(define-public (make-lyric-event string duration)
(make-music 'LyricEvent
'duration duration
'text string))
(define-public (make-span-event type span-dir)
(make-music type
'span-direction span-dir))
(define-public (override-head-style heads style)
"Override style for @var{heads} to @var{style}."
(make-sequential-music
(if (pair? heads)
(map (lambda (h)
(make-grob-property-override h 'style style))
heads)
(list (make-grob-property-override heads 'style style)))))
(define-public (revert-head-style heads)
"Revert style for @var{heads}."
(make-sequential-music
(if (pair? heads)
(map (lambda (h)
(make-grob-property-revert h 'style))
heads)
(list (make-grob-property-revert heads 'style)))))
(define-public (style-note-heads heads style music)
"Set @var{style} for all @var{heads} in @var{music}. Works both
inside of and outside of chord construct."
;; are we inside a <...>?
(if (eq? (ly:music-property music 'name) 'NoteEvent)
;; yes -> use a tweak
(begin
(set! (ly:music-property music 'tweaks)
(acons 'style style (ly:music-property music 'tweaks)))
music)
;; not in <...>, so use overrides
(make-sequential-music
(list
(override-head-style heads style)
music
(revert-head-style heads)))))
(define-public (get-tweakable-music mus)
"When tweaking music, return a list of music expressions where the
tweaks should be applied. Relevant for music wrappers and event
chords."
(cond ((music-is-of-type? mus 'music-wrapper-music)
(get-tweakable-music (ly:music-property mus 'element)))
((music-is-of-type? mus 'event-chord)
(filter (music-type-predicate 'rhythmic-event)
(ly:music-property mus 'elements)))
(else (list mus))))
(define-public (set-mus-properties! m alist)
"Set all of @var{alist} as properties of @var{m}."
(if (pair? alist)
(begin
(set! (ly:music-property m (caar alist)) (cdar alist))
(set-mus-properties! m (cdr alist)))))
(define-public (music-separator? m)
"Is @var{m} a separator?"
(let ((ts (ly:music-property m 'types)))
(memq 'separator ts)))
;;; expanding repeat chords
(define-public (copy-repeat-chord original-chord repeat-chord duration
event-types)
"Copy all events in @var{event-types} (be sure to include
@code{rhythmic-events}) from @var{original-chord} over to
@var{repeat-chord} with their articulations filtered as well. Any
duration is replaced with the specified @var{duration}."
;; First remove everything from event-types that can already be
;; found in the repeated chord. We don't need to look for
;; articulations on individual events since they can't actually get
;; into a repeat chord given its input syntax.
(define keep-element? (music-type-predicate event-types))
(for-each
(lambda (field)
(for-each (lambda (e)
(for-each (lambda (x)
(set! event-types (delq x event-types)))
(ly:music-property e 'types)))
(ly:music-property repeat-chord field)))
'(elements articulations))
;; now treat the elements
(set! (ly:music-property repeat-chord 'elements)
(let ((elts
(ly:music-deep-copy (filter keep-element?
(ly:music-property original-chord
'elements))
repeat-chord)))
(for-each
(lambda (m)
(let ((arts (ly:music-property m 'articulations)))
(if (pair? arts)
(set! (ly:music-property m 'articulations)
(ly:set-origin! (filter! keep-element? arts)
repeat-chord)))
(if (ly:duration? (ly:music-property m 'duration))
(set! (ly:music-property m 'duration) duration))
(if (ly:music-property m 'cautionary #f)
(set! (ly:music-property m 'cautionary) #f))
(if (ly:music-property m 'force-accidental #f)
(set! (ly:music-property m 'force-accidental) #f))))
elts)
(append! elts (ly:music-property repeat-chord 'elements))))
(let ((arts (filter keep-element?
(ly:music-property original-chord
'articulations))))
(if (pair? arts)
(set! (ly:music-property repeat-chord 'articulations)
(append!
(ly:music-deep-copy arts repeat-chord)
(ly:music-property repeat-chord 'articulations)))))
repeat-chord)
(define-public (expand-repeat-chords! event-types music)
"Walk through @var{music} and fill repeated chords (notable by
having a duration in @code{duration}) with the notes from their
respective predecessor chord."
(let loop ((music music) (last-chord #f))
(if (music-is-of-type? music 'event-chord)
(let ((chord-repeat (ly:music-property music 'duration)))
(cond
((not (ly:duration? chord-repeat))
(if (any (lambda (m) (ly:duration?
(ly:music-property m 'duration)))
(ly:music-property music 'elements))
music
last-chord))
(last-chord
(set! (ly:music-property music 'duration) '())
(copy-repeat-chord last-chord music chord-repeat event-types))
(else
(ly:music-warning music (G_ "Bad chord repetition"))
#f)))
(let ((elt (ly:music-property music 'element)))
(fold loop (if (ly:music? elt) (loop elt last-chord) last-chord)
(ly:music-property music 'elements)))))
music)
;;; This does _not_ copy any articulations. Rationale: one main
;;; incentive for pitch-repeating durations is after ties, such that
;;; 4~2~8. can stand in for a 15/16 note in \partial 4 position. In
;;; this use case, any repeated articulations will be a nuisance.
;;;
;;; String assignments in TabStaff might seem like a worthwhile
;;; exception, but they would be better tackled by the respective
;;; engravers themselves (see issue 3662).
;;;
;;; Repeating chords as well seems problematic for things like
;;; \score {
;;; <<
;;; \new Staff { c4 c c <c e> }
;;; \new RhythmicStaff { 4 4 4 4 }
;;; >>
;;; }
;;;
;;; However, because of MIDI it is not advisable to use RhythmicStaff
;;; without any initial pitch/drum-type. For music functions taking
;;; pure rhythms as an argument, the running of expand-repeat-notes!
;;; at scorification time is irrelevant: at that point of time, the
;;; music function has already run.
(define-public (expand-repeat-notes! music)
"Walk through @var{music} and give pitchless notes (not having a
pitch in @code{pitch} or a drum type in @code{drum-type}) the pitch(es)
from the predecessor note/chord if available."
(let ((last-pitch #f))
(map-some-music
(lambda (m)
(define (set-and-ret last)
(set! last-pitch last)
m)
(cond
((music-is-of-type? m 'event-chord)
(if (any (lambda (m) (music-is-of-type? m 'rhythmic-event))
(ly:music-property m 'elements))
(set! last-pitch m))
m)
((music-is-of-type? m 'note-event)
(cond
((or (ly:music-property m 'pitch #f)
(ly:music-property m 'drum-type #f))
=> set-and-ret)
;; ok, naked rhythm. Go through the various cases of
;; last-pitch
;; nothing available: just keep as-is
((not last-pitch) m)
((ly:pitch? last-pitch)
(set! (ly:music-property m 'pitch) last-pitch)
m)
((symbol? last-pitch)
(set! (ly:music-property m 'drum-type) last-pitch)
m)
;; Ok, this is the big bad one: the reference is a chord.
;; For now, we use the repeat chord logic. That's not
;; really efficient as cleaning out all articulations is
;; quite simpler than what copy-repeat-chord does.
(else
(copy-repeat-chord last-pitch
(make-music 'EventChord
'elements
(ly:music-property m 'articulations)
'origin
(ly:music-property m 'origin))
(ly:music-property m 'duration)
'(rhythmic-event)))))
(else #f)))
music)))
;;; splitting chords into voices.
(define (voicify-list locs lst id)
"Make a list of Musics.
voicify-list :: [ [Music ] ] -> id -> [Music]
LST is a list music-lists.
id is 1-based, i.e., Voice=1 (upstems) has number 1.
id may be a symbol or string giving a specific voice id: in this
case, no \voiceXXX style is selected, merely the context given.
locs is a list of music expressions suitable for giving
error locations (enclosing expression for the first element,
preceding \\\\ separator for the others)
"
(define (voicify-sublist loc sublist id)
(cond ((string? id)
(context-spec-music
(make-simultaneous-music sublist)
'Bottom id))
((symbol? id)
(voicify-sublist loc sublist (symbol->string id)))
((and (integer? id) (exact? id) (positive? id))
(context-spec-music
(make-sequential-music
(list (make-voice-props-set (1- id))
(make-simultaneous-music sublist)))
'Bottom (number->string id)))
(else
(ly:music-warning loc (G_ "Bad voice id: ~a") id)
(context-spec-music (make-simultaneous-music sublist) 'Bottom))))
(cond ((null? lst) '())
((number? id)
(cons (voicify-sublist (car locs) (car lst) id)
(voicify-list (cdr locs) (cdr lst) (1+ id))))
((pair? id)
(cons (voicify-sublist (car locs) (car lst) (car id))
(voicify-list (cdr locs) (cdr lst) (cdr id))))