]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* input/regression/repeat-unfold-tremolo.ly: add dotted case.
[lilypond.git] / scm / music-functions.scm
1 ;;;; music-functions.scm -- implement Scheme output routines for PostScript
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 (use-modules (ice-9 optargs)) 
9
10 ;;; ly:music-property with setter
11 ;;; (ly:music-property my-music 'elements)
12 ;;;   ==> the 'elements property
13 ;;; (set! (ly:music-property my-music 'elements) value)
14 ;;;   ==> set the 'elements property and return it
15 (define-public ly:music-property
16   (make-procedure-with-setter ly:music-property
17                               ly:music-set-property!))
18
19 (define-public ly:grob-property
20   (make-procedure-with-setter ly:grob-property
21                               ly:grob-set-property!))
22
23 (define-public (music-map function music)
24   "Apply @var{function} to @var{music} and all of the music it contains. "
25   (let ((es (ly:music-property music 'elements))
26         (e (ly:music-property music 'element)))
27     (set! (ly:music-property music 'elements) 
28           (map (lambda (y) (music-map function y)) es))
29     (if (ly:music? e)
30         (set! (ly:music-property music 'element)
31               (music-map function  e)))
32     (function music)))
33
34 (define-public (music-filter pred? music)
35   "Filter out music expressions that do not satisfy PRED."
36   
37   (define (inner-music-filter pred? music)
38     "Recursive function."
39     (let* ((es (ly:music-property music 'elements))
40            (e (ly:music-property music 'element))
41            (as (ly:music-property music 'articulations))
42            (filtered-as (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) as)))
43            (filtered-e (if (ly:music? e)
44                            (inner-music-filter pred? e)
45                            e))
46            (filtered-es (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) es))))
47       (set! (ly:music-property music 'element) filtered-e)
48       (set! (ly:music-property music 'elements) filtered-es)
49       (set! (ly:music-property music 'articulations) filtered-as)
50       ;; if filtering emptied the expression, we remove it completely.
51       (if (or (pred? music)
52               (and (eq? filtered-es '()) (not (ly:music? e))
53                    (or (not (eq? es '()))
54                        (ly:music? e))))
55           (set! music '()))
56       music))
57
58   (set! music (inner-music-filter pred? music))
59   (if (ly:music? music)
60       music
61       (make-music 'Music)))       ;must return music.
62
63 (define-public (remove-tag tag)
64   (lambda (mus)
65     (music-filter
66      (lambda (m)
67        (let* ((tags (ly:music-property m 'tags))
68               (res (memq tag tags)))
69          res))
70      mus)))
71
72 (define-public (display-music music)
73   "Display music, not done with music-map for clarity of presentation."
74   (display music)
75   (display ": { ")  
76   (let ((es (ly:music-property music 'elements))
77         (e (ly:music-property music 'element)))
78     (display (ly:music-mutable-properties music))
79     (if (pair? es)
80         (begin (display "\nElements: {\n")
81                (map display-music es)
82                (display "}\n")))
83     (if (ly:music? e)
84         (begin
85           (display "\nChild:")
86           (display-music e))))
87   (display " }\n")
88   music)
89
90
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92
93 (define (shift-one-duration-log music shift dot)
94   "  add SHIFT to ly:duration-log and optionally 
95   a dot to any note encountered. This scales the music up by a factor 
96   2^shift * (2 - (1/2)^dot)"
97   (let ((d (ly:music-property music 'duration)))
98     (if (ly:duration? d)
99         (let* ((cp (ly:duration-factor d))
100                (nd (ly:make-duration (+ shift (ly:duration-log d))
101                                      (+ dot (ly:duration-dot-count d))
102                                      (car cp)
103                                      (cdr cp))))
104           (set! (ly:music-property music 'duration) nd)))
105     music))
106
107
108
109 (define-public (shift-duration-log music shift dot)
110   (music-map (lambda (x) (shift-one-duration-log x shift dot))
111              music))
112   
113
114 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;; clusters.
116
117 (define-public (note-to-cluster music)
118   "Replace NoteEvents by ClusterNoteEvents."
119   (if (eq? (ly:music-property music 'name) 'NoteEvent)
120       (make-music 'ClusterNoteEvent
121                   'pitch (ly:music-property music 'pitch)
122                   'duration (ly:music-property music 'duration))
123       music))
124
125 (define-public (notes-to-clusters music)
126   (music-map note-to-cluster music))
127
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;; repeats.
130
131 (define-public (unfold-repeats music)
132   "
133 This function replaces all repeats  with unfold repeats. "
134   
135   (let ((es (ly:music-property music 'elements))
136         (e  (ly:music-property music 'element))
137         (n  (ly:music-name music)))
138     (if (equal? n "Repeated_music")
139         (begin
140           
141           (if (equal? (ly:music-property music 'iterator-ctor)
142                       Chord_tremolo_iterator::constructor)
143               (let*
144                   ((seq-arg? (memq 'sequential-music
145                                    (ly:music-property e 'types)))
146                    (count  (ly:music-property music 'repeat-count))
147                    (dot-shift (if (= 0 (remainder count 3))
148                                   -1 0))
149                    )
150
151                 (if (= 0 -1)
152                     (set! count (* 2 (quotient count 3))))
153                 
154                 (shift-duration-log music (+ (if seq-arg? 1 0)
155                                              (ly:intlog2 count)) dot-shift)
156                 
157                 (if seq-arg?
158                     (ly:music-compress e (ly:make-moment (length (ly:music-property e 'elements)) 1)))
159                 ))
160           
161           (set! (ly:music-property music 'length)
162                 Repeated_music::unfolded_music_length)
163           (set! (ly:music-property music 'start-moment-function)
164                 Repeated_music::first_start)
165           (set! (ly:music-property music 'iterator-ctor)
166                 Unfolded_repeat_iterator::constructor)))
167     
168     (if (pair? es)
169         (set! (ly:music-property music 'elements)
170               (map unfold-repeats es)))
171     (if (ly:music? e)
172         (set! (ly:music-property music 'element)
173               (unfold-repeats e)))
174     music))
175
176
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;; property setting music objs.
179
180 (define-public (make-grob-property-set grob gprop val)
181   "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
182 i.e.  this is not an override"
183   (make-music 'OverrideProperty
184               'symbol grob
185               'grob-property gprop
186               'grob-value val
187               'pop-first #t))
188
189 (define-public (make-grob-property-override grob gprop val)
190   "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
191 i.e.  this is not an override"
192   (make-music 'OverrideProperty
193               'symbol grob
194               'grob-property gprop
195               'grob-value val))
196
197 (define-public (make-grob-property-revert grob gprop)
198   "Revert the grob property GPROP for GROB."
199   (make-music 'OverrideProperty
200               'symbol grob
201               'grob-property gprop))
202
203 (define direction-polyphonic-grobs
204   '(Stem Tie Rest Slur Script TextScript Dots DotColumn
205          ))
206
207 (define-public (make-voice-props-set n)
208   (make-sequential-music
209    (append
210     (map (lambda (x) (make-grob-property-set x 'direction
211                                              (if (odd? n) -1 1)))
212          direction-polyphonic-grobs)
213     (list
214      (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
215      (make-grob-property-set 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))
216      
217      )))) 
218
219
220 (define-public (make-voice-props-revert)
221   (make-sequential-music
222    (append
223     (map (lambda (x) (make-grob-property-revert x 'direction))
224          direction-polyphonic-grobs)
225     (list (make-grob-property-revert 'NoteColumn 'horizontal-shift))
226     (list (make-grob-property-revert 'MultiMeasureRest 'staff-position)))))
227
228
229 (define*-public (context-spec-music m context #:optional id)
230   "Add \\context CONTEXT = ID to M. "
231   (let ((cm (make-music 'ContextSpeccedMusic
232                         'element m
233                         'context-type context)))
234     (if (string? id)
235         (set! (ly:music-property cm 'context-id) id))
236     cm))
237
238
239 (define-public (descend-to-context m context)
240   "Like context-spec-music, but only descending. "
241   (let ((cm (context-spec-music m context)))
242     (ly:music-set-property! cm 'descend-only #t)
243     cm))
244
245 (define-public (make-non-relative-music mus)
246   (make-music 'UnrelativableMusic
247               'element mus
248   ))
249
250 (define-public (make-apply-context func)
251   (make-music 'ApplyContext
252               'procedure func))
253
254 (define-public (make-sequential-music elts)
255   (make-music 'SequentialMusic
256               'elements elts))
257
258 (define-public (make-simultaneous-music elts)
259   (make-music 'SimultaneousMusic
260               'elements elts))
261
262 (define-public (make-event-chord elts)
263   (make-music 'EventChord
264               'elements elts))
265
266 (define-public (make-skip-music dur)
267   (make-music 'SkipMusic
268               'duration dur))
269
270 (define-public (make-grace-music music)
271   (make-music 'GraceMusic
272               'element music))
273
274 ;;;;;;;;;;;;;;;;
275
276 ;; mmrest
277 (define-public (make-multi-measure-rest duration location)
278   (make-music 'MultiMeasureRestMusicGroup
279               'origin location
280               'elements (list (make-music 'BarCheck
281                                           'origin location)
282                               (make-event-chord (list (make-music 'MultiMeasureRestEvent
283                                                                   'origin location
284                                                                   'duration duration)))
285                               (make-music 'BarCheck
286                                           'origin location))))
287
288 (define-public (glue-mm-rest-texts music)
289   "Check if we have R1*4-\\markup { .. }, and if applicable convert to
290 a property set for MultiMeasureRestNumber."
291   (define (script-to-mmrest-text script-music)
292     "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
293     (let ((dir (ly:music-property script-music 'direction))
294           (p   (make-music 'MultiMeasureTextEvent
295                            'text (ly:music-property script-music 'text))))
296       (if (ly:dir? dir)
297           (set! (ly:music-property p 'direction) dir))
298       p))
299   (if (eq? (ly:music-property music 'name) 'MultiMeasureRestMusicGroup)
300       (let* ((text? (lambda (x) (memq 'script-event (ly:music-property x 'types))))
301              (es (ly:music-property  music 'elements))
302              (texts (map script-to-mmrest-text  (filter text? es)))
303              (others (remove text? es)))
304         (if (pair? texts)
305             (set! (ly:music-property music 'elements)
306                   (cons (make-event-chord texts) others)))))
307   music)
308
309
310 (define-public (make-property-set sym val)
311   (make-music 'PropertySet
312               'symbol sym
313               'value val))
314
315 (define-public (make-ottava-set octavation)
316   (let ((m (make-music 'ApplyContext)))
317     (define (ottava-modify context)
318       "Either reset middleCPosition to the stored original, or remember
319 old middleCPosition, add OCTAVATION to middleCPosition, and set
320 OTTAVATION to `8va', or whatever appropriate."      
321       (if (number? (ly:context-property  context 'middleCPosition))
322           (if (= octavation 0)
323               (let ((where (ly:context-property-where-defined context 'middleCPosition))
324                     (oc0 (ly:context-property context 'originalCentralCPosition)))
325                 (ly:context-set-property! context 'middleCPosition oc0)
326                 (ly:context-unset-property where 'originalCentralCPosition)
327                 (ly:context-unset-property where 'ottavation))
328               (let* ((where (ly:context-property-where-defined context 'middleCPosition))
329                      (c0 (ly:context-property context 'middleCPosition))
330                      (new-c0 (+ c0 (* -7 octavation)))
331                      (string (cdr (assoc octavation '((2 . "15ma")
332                                                       (1 . "8va")
333                                                       (0 . #f)
334                                                       (-1 . "8va bassa")
335                                                       (-2 . "15ma bassa"))))))
336                 (ly:context-set-property! context 'middleCPosition new-c0)
337                 (ly:context-set-property! context 'originalCentralCPosition c0)
338                 (ly:context-set-property! context 'ottavation string)))))
339     (set! (ly:music-property m 'procedure) ottava-modify)
340     (context-spec-music m 'Staff)))
341
342 (define-public (set-octavation ottavation)
343   (ly:export (make-ottava-set ottavation)))
344
345 (define-public (make-time-signature-set num den . rest)
346   "Set properties for time signature NUM/DEN.  Rest can contain a list
347 of beat groupings "
348   (let* ((set1 (make-property-set 'timeSignatureFraction (cons num den)))
349          (beat (ly:make-moment 1 den))
350          (len  (ly:make-moment num den))
351          (set2 (make-property-set 'beatLength beat))
352          (set3 (make-property-set 'measureLength len))
353          (set4 (make-property-set 'beatGrouping (if (pair? rest)
354                                                     (car rest)
355                                                     '())))
356          (basic  (list set1 set2 set3 set4)))
357     (descend-to-context
358      (context-spec-music (make-sequential-music basic) 'Timing) 'Score)))
359
360 (define-public (make-mark-set label)
361   "Make the music for the \\mark command."  
362   (let* ((set (if (integer? label)
363                   (context-spec-music (make-property-set 'rehearsalMark label)
364                                       'Score)
365                   #f))
366          (ev (make-music 'MarkEvent))
367          (ch (make-event-chord (list ev))))
368     (if set
369         (make-sequential-music (list set ch))
370         (begin
371           (set! (ly:music-property ev 'label) label)
372           ch))))
373
374 (define-public (set-time-signature num den . rest)
375   (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
376
377 (define-public (make-penalty-music pen)
378   (make-music 'BreakEvent
379               'penalty pen))
380
381 (define-public (make-articulation name)
382   (make-music 'ArticulationEvent
383               'articulation-type name))
384
385 (define-public (make-lyric-event string duration)
386   (make-music 'LyricEvent
387               'duration duration
388               'text string))
389
390 (define-public (make-span-event type spandir)
391   (make-music type
392               'span-direction spandir))
393
394 (define-public (set-mus-properties! m alist)
395   "Set all of ALIST as properties of M." 
396   (if (pair? alist)
397       (begin
398         (set! (ly:music-property m (caar alist)) (cdar alist))
399         (set-mus-properties! m (cdr alist)))))
400
401 (define-public (music-separator? m)
402   "Is M a separator?"
403   (let ((ts (ly:music-property m 'types)))
404     (memq 'separator ts)))
405
406
407 ;;; splitting chords into voices.
408 (define (voicify-list lst number)
409    "Make a list of Musics.
410
411    voicify-list :: [ [Music ] ] -> number -> [Music]
412    LST is a list music-lists.
413
414    NUMBER is 0-base, i.e. Voice=1 (upstems) has number 0.
415 "
416    (if (null? lst)
417        '()
418        (cons (context-spec-music
419               (make-sequential-music
420                (list (make-voice-props-set number)
421                      (make-simultaneous-music (car lst))))
422               'Voice  (number->string (1+ number)))
423              (voicify-list (cdr lst) (1+ number)))))
424
425 (define (voicify-chord ch)
426   "Split the parts of a chord into different Voices using separator"
427   (let ((es (ly:music-property ch 'elements)))
428     (set! (ly:music-property  ch 'elements)
429           (voicify-list (split-list es music-separator?) 0))
430     ch))
431
432 (define-public (voicify-music m)
433   "Recursively split chords that are separated with \\ "
434   (if (not (ly:music? m))
435       (begin (display m)
436              (error "not music!")))
437   (let ((es (ly:music-property m 'elements))
438         (e (ly:music-property m 'element)))
439     (if (pair? es)
440         (set! (ly:music-property m 'elements) (map voicify-music es)))
441     (if (ly:music? e)
442         (set! (ly:music-property m 'element)  (voicify-music e)))
443     (if (and (equal? (ly:music-name m) "Simultaneous_music")
444              (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
445         (set! m (context-spec-music (voicify-chord m) 'Staff)))
446     m))
447
448 (define-public (empty-music)
449   (ly:export (make-music 'Music)))
450 ;;;
451
452 ; Make a function that checks score element for being of a specific type. 
453 (define-public (make-type-checker symbol)
454   (lambda (elt)
455     ;;(display  symbol)
456     ;;(eq? #t (ly:grob-property elt symbol))
457     (not (eq? #f (memq symbol (ly:grob-property elt 'interfaces))))))
458
459 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
460   (if (func grob)
461       (set! (ly:grob-property grob sym) val)))
462
463
464 (define-public ((set-output-property grob-name symbol val)  grob grob-c context)
465    "Usage:
466
467 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
468
469 "
470    (let ((meta (ly:grob-property grob 'meta)))
471      (if (equal?  (cdr (assoc 'name meta)) grob-name)
472          (set! (ly:grob-property grob symbol) val))))
473
474
475 ;;
476 (define-public (smart-bar-check n)
477   "Make  a bar check that checks for a specific bar number. 
478 "
479   (let ((m (make-music 'ApplyContext)))
480     (define (checker tr)
481       (let* ((bn (ly:context-property tr 'currentBarNumber)))
482         (if (= bn n)
483             #t
484             (error
485              (format "Bar check failed, we should have reached ~a, instead at ~a\n"
486                      n bn)))))
487     (set! (ly:music-property m 'procedure) checker)
488     m))
489
490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
491 ;; warn for bare chords at start.
492
493 (define (has-request-chord elts)
494   (reduce (lambda (x y) (or x y)) #f
495           (map (lambda (x)
496                  (equal? (ly:music-name x) "Request_chord"))
497                elts)))
498
499 (define (ly:music-message music msg)
500   (let ((ip (ly:music-property music 'origin)))
501     (if (ly:input-location? ip)
502         (ly:input-message ip msg)
503         (ly:warn msg))))
504   
505 (define (check-start-chords music)
506   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords),
507 without context specification. Called  from parser."
508   (let ((es (ly:music-property music 'elements))
509         (e (ly:music-property music 'element))
510         (name (ly:music-name music)))
511     (cond ((equal? name "Context_specced_music") #t)
512           ((equal? name "Simultaneous_music")
513            (if (has-request-chord es)
514                (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
515                (map check-start-chords es)))
516           ((equal? name "Sequential_music")
517            (if (pair? es)
518                (check-start-chords (car es))))
519           (else (if (ly:music? e) (check-start-chords e)))))
520   music)
521
522
523
524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
525 ;;
526 ;; setting stuff for grace context.
527 ;;
528
529 (define (vector-extend v x)
530   "Make a new vector consisting of V, with X added to the end."
531   (let*
532       ((n (vector-length v))
533        (nv (make-vector (+ n 1) '())))
534     (vector-move-left! v 0 n nv 0)
535     (vector-set! nv n x)
536     nv))
537
538 (define (vector-map f v)
539   "Map  F over V. This function returns nothing."
540   (do ((n (vector-length v))
541        (i 0 (+ i 1)))
542       ((>= i n))
543     (f (vector-ref v i))))
544
545 (define (vector-reverse-map f v)
546   "Map  F over V, N to 0 order. This function returns nothing."
547   (do ((i (- (vector-length v) 1) (- i 1)))
548       ((< i 0))
549     (f (vector-ref v i))))
550
551 ;; TODO:  make a remove-grace-property too.
552 (define-public (add-grace-property context-name grob sym val)
553   "Set SYM=VAL for GROB in CONTEXT-NAME. "
554   (define (set-prop context)
555     (let* ((where (ly:context-property-where-defined context 'graceSettings))
556            (current (ly:context-property where 'graceSettings))
557            (new-settings (vector-extend current (list context-name grob sym val))))
558       (ly:context-set-property! where 'graceSettings new-settings)))
559   (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
560
561
562 (define-public (set-start-grace-properties context)
563   (define (execute-1 x)
564     (let ((tr (ly:context-find context (car x))))
565       (if (ly:context? tr)
566           (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x)))))
567   
568   (let ((props (ly:context-property context 'graceSettings)))
569     (if (vector? props)
570         (vector-map execute-1 props))))
571
572 (define-public (set-stop-grace-properties context)
573   (define (execute-1 x)
574     (let ((tr (ly:context-find context (car x))))
575       (if (ly:context? tr)
576           (ly:context-pushpop-property tr (cadr x) (caddr x)))))
577   
578   (let ((props (ly:context-property context 'graceSettings)))
579     (if (vector? props)
580         (vector-reverse-map execute-1 props))))
581
582 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
583 ;; switch it on here, so parsing and init isn't checked (too slow!)
584 ;;
585 ;; automatic music transformations.
586
587 (define (switch-on-debugging m)
588   (if (defined? 'set-debug-cell-accesses!)
589       (set-debug-cell-accesses! 15000))
590   m)
591
592 (define-public toplevel-music-functions
593   (list
594    ;; check-start-chords ; ; no longer needed with chord syntax. 
595    voicify-music
596    (lambda (x) (music-map glue-mm-rest-texts x))
597    ;; switch-on-debugging
598    ))
599
600 ;;;;;;;;;;;;;;;;;
601 ;; lyrics
602
603 (define (apply-durations lyric-music durations) 
604   (define (apply-duration music)
605     (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
606              (ly:duration?  (ly:music-property music 'duration)))
607         (begin
608           (set! (ly:music-property music 'duration) (car durations))
609           (set! durations (cdr durations)))))
610   
611   (music-map apply-duration lyric-music))
612
613
614 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
615 ;;
616
617 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
618    "Usage: see input/regression/balloon.ly "
619   (let* ((meta (ly:grob-property grob 'meta))
620          (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
621          (cb (ly:grob-property grob 'print-function)))
622     (if (equal? nm object-name)
623         (begin
624           (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
625           (set! (ly:grob-property grob 'balloon-original-callback) cb)
626           (set! (ly:grob-property grob 'balloon-text) text)
627           (set! (ly:grob-property grob 'balloon-text-offset) off)
628           (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
629
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 ;; accidentals
632
633 (define-public (set-accidentals-properties extra-natural
634                                            auto-accs auto-cauts
635                                            context)
636   (context-spec-music
637    (make-sequential-music
638     (append (if (boolean? extra-natural)
639                 (list (make-property-set 'extraNatural extra-natural))
640                 '())
641             (list (make-property-set 'autoAccidentals auto-accs)
642                   (make-property-set 'autoCautionaries auto-cauts))))
643    context))
644
645 (define-public (set-accidental-style style . rest)
646   "Set accidental style to STYLE. Optionally takes a context argument,
647 e.g. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
648 use PianoStaff as a context. "
649   (let ((context (if (pair? rest)
650                      (car rest) 'Staff))
651         (pcontext (if (pair? rest)
652                       (car rest) 'PianoStaff)))
653     (ly:export
654      (cond
655       ;; accidentals as they were common in the 18th century.
656       ((equal? style 'default)
657        (set-accidentals-properties #t '(Staff (same-octave . 0))
658                                    '() context))
659       ;; accidentals from one voice do NOT get cancelled in other voices
660       ((equal? style 'voice)
661        (set-accidentals-properties #t '(Voice (same-octave . 0))
662                                    '() context))
663       ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
664       ;; This includes all the default accidentals, but accidentals also needs cancelling
665       ;; in other octaves and in the next measure.
666       ((equal? style 'modern)
667        (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
668                                    '()  context))
669       ;; the accidentals that Stone adds to the old standard as cautionaries
670       ((equal? style 'modern-cautionary)
671        (set-accidentals-properties #f '(Staff (same-octave . 0))
672                                    '(Staff (any-octave . 0) (same-octave . 1))
673                                    context))
674       ;; Multivoice accidentals to be read both by musicians playing one voice
675       ;; and musicians playing all voices.
676       ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
677       ((equal? style 'modern-voice)
678        (set-accidentals-properties  #f
679                                     '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
680                                             Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
681                                     '()
682                                     context))
683       ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
684       ;; as cautionaries
685       ((equal? style 'modern-voice-cautionary)
686        (set-accidentals-properties #f
687                                    '(Voice (same-octave . 0) )
688                                    '(Voice (any-octave . 0) (same-octave . 1)
689                                            Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
690                                    context))
691       ;; stone's suggestions for accidentals on grand staff.
692       ;; Accidentals are cancelled across the staves in the same grand staff as well
693       ((equal? style 'piano)
694        (set-accidentals-properties #f
695                                    '( Staff (same-octave . 0) (any-octave . 0) (same-octave . 1)
696                                             PianoStaff (any-octave . 0) (same-octave . 1))
697                                    '()
698                                    pcontext))
699       ((equal? style 'piano-cautionary)
700        (set-accidentals-properties #f
701                                    '(Staff (same-octave . 0))
702                                    '(Staff (any-octave . 0) (same-octave . 1)
703                                            PianoStaff (any-octave . 0) (same-octave . 1))
704                                    pcontext))
705       ;; do not set localKeySignature when a note alterated differently from
706       ;; localKeySignature is found.
707       ;; Causes accidentals to be printed at every note instead of
708       ;; remembered for the duration of a measure.
709       ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
710       ((equal? style 'forget)
711        (set-accidentals-properties '()
712                                    '(Staff (same-octave . -1))
713                                    '() context))
714       ;; Do not reset the key at the start of a measure.  Accidentals will be
715       ;; printed only once and are in effect until overridden, possibly many
716       ;; measures later.
717       ((equal? style 'no-reset)
718        (set-accidentals-properties '()
719                                    '(Staff (same-octave . #t))
720                                    '()
721                                    context))
722       (else
723        (ly:warn (string-append "Unknown accidental style: " (symbol->string style)))
724        (make-sequential-music '()))))))
725
726