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