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