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