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