]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* lily/parser.yy (command_element): reverse setting of
[lilypond.git] / scm / music-functions.scm
1 ;;;; music-functions.scm --
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 (not (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
64 (define-public (display-music music)
65   "Display music, not done with music-map for clarity of presentation."
66   (display music)
67   (display ": { ")  
68   (let ((es (ly:music-property music 'elements))
69         (e (ly:music-property music 'element)))
70     (display (ly:music-mutable-properties music))
71     (if (pair? es)
72         (begin (display "\nElements: {\n")
73                (map display-music es)
74                (display "}\n")))
75     (if (ly:music? e)
76         (begin
77           (display "\nChild:")
78           (display-music e))))
79   (display " }\n")
80   music)
81
82
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84
85 (define (shift-one-duration-log music shift dot)
86   "  add SHIFT to ly:duration-log and optionally 
87   a dot to any note encountered. This scales the music up by a factor 
88   2^shift * (2 - (1/2)^dot)"
89   (let ((d (ly:music-property music 'duration)))
90     (if (ly:duration? d)
91         (let* ((cp (ly:duration-factor d))
92                (nd (ly:make-duration (+ shift (ly:duration-log d))
93                                      (+ dot (ly:duration-dot-count d))
94                                      (car cp)
95                                      (cdr cp))))
96           (set! (ly:music-property music 'duration) nd)))
97     music))
98
99
100
101 (define-public (shift-duration-log music shift dot)
102   (music-map (lambda (x) (shift-one-duration-log x shift dot))
103              music))
104   
105
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 ;; clusters.
108
109 (define-public (note-to-cluster music)
110   "Replace NoteEvents by ClusterNoteEvents."
111   (if (eq? (ly:music-property music 'name) 'NoteEvent)
112       (make-music 'ClusterNoteEvent
113                   'pitch (ly:music-property music 'pitch)
114                   'duration (ly:music-property music 'duration))
115       music))
116
117 (define-public (notes-to-clusters music)
118   (music-map note-to-cluster music))
119
120 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121 ;; repeats.
122
123 (define-public (unfold-repeats music)
124   "
125 This function replaces all repeats  with unfold repeats. "
126   
127   (let ((es (ly:music-property music 'elements))
128         (e  (ly:music-property music 'element))
129         (n  (ly:music-name music)))
130     (if (equal? n "Repeated_music")
131         (begin
132           
133           (if (equal? (ly:music-property music 'iterator-ctor)
134                       Chord_tremolo_iterator::constructor)
135               (let*
136                   ((seq-arg? (memq 'sequential-music
137                                    (ly:music-property e 'types)))
138                    (count  (ly:music-property music 'repeat-count))
139                    (dot-shift (if (= 0 (remainder count 3))
140                                   -1 0))
141                    )
142
143                 (if (= 0 -1)
144                     (set! count (* 2 (quotient count 3))))
145                 
146                 (shift-duration-log music (+ (if seq-arg? 1 0)
147                                              (ly:intlog2 count)) dot-shift)
148                 
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 Fingering
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 page-pen)
370   (make-music 'BreakEvent
371               'penalty pen
372               'page-penalty page-pen))
373
374 (define-public (make-articulation name)
375   (make-music 'ArticulationEvent
376               'articulation-type name))
377
378 (define-public (make-lyric-event string duration)
379   (make-music 'LyricEvent
380               'duration duration
381               'text string))
382
383 (define-public (make-span-event type spandir)
384   (make-music type
385               'span-direction spandir))
386
387 (define-public (set-mus-properties! m alist)
388   "Set all of ALIST as properties of M." 
389   (if (pair? alist)
390       (begin
391         (set! (ly:music-property m (caar alist)) (cdar alist))
392         (set-mus-properties! m (cdr alist)))))
393
394 (define-public (music-separator? m)
395   "Is M a separator?"
396   (let ((ts (ly:music-property m 'types)))
397     (memq 'separator ts)))
398
399
400 ;;; splitting chords into voices.
401 (define (voicify-list lst number)
402    "Make a list of Musics.
403
404    voicify-list :: [ [Music ] ] -> number -> [Music]
405    LST is a list music-lists.
406
407    NUMBER is 0-base, i.e. Voice=1 (upstems) has number 0.
408 "
409    (if (null? lst)
410        '()
411        (cons (context-spec-music
412               (make-sequential-music
413                (list (make-voice-props-set number)
414                      (make-simultaneous-music (car lst))))
415               'Voice  (number->string (1+ number)))
416              (voicify-list (cdr lst) (1+ number)))))
417
418 (define (voicify-chord ch)
419   "Split the parts of a chord into different Voices using separator"
420   (let ((es (ly:music-property ch 'elements)))
421     (set! (ly:music-property  ch 'elements)
422           (voicify-list (split-list es music-separator?) 0))
423     ch))
424
425 (define-public (voicify-music m)
426   "Recursively split chords that are separated with \\ "
427   (if (not (ly:music? m))
428       (begin (display m)
429              (error "not music!")))
430   (let ((es (ly:music-property m 'elements))
431         (e (ly:music-property m 'element)))
432     (if (pair? es)
433         (set! (ly:music-property m 'elements) (map voicify-music es)))
434     (if (ly:music? e)
435         (set! (ly:music-property m 'element)  (voicify-music e)))
436     (if (and (equal? (ly:music-name m) "Simultaneous_music")
437              (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
438         (set! m (context-spec-music (voicify-chord m) 'Staff)))
439     m))
440
441 (define-public (empty-music)
442   (ly:export (make-music 'Music)))
443 ;;;
444
445 ; Make a function that checks score element for being of a specific type. 
446 (define-public (make-type-checker symbol)
447   (lambda (elt)
448     ;;(display  symbol)
449     ;;(eq? #t (ly:grob-property elt symbol))
450     (not (eq? #f (memq symbol (ly:grob-property elt 'interfaces))))))
451
452 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
453   (if (func grob)
454       (set! (ly:grob-property grob sym) val)))
455
456
457 (define-public ((set-output-property grob-name symbol val)  grob grob-c context)
458    "Usage:
459
460 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
461
462 "
463    (let ((meta (ly:grob-property grob 'meta)))
464      (if (equal?  (cdr (assoc 'name meta)) grob-name)
465          (set! (ly:grob-property grob symbol) val))))
466
467
468 ;;
469 (define-public (smart-bar-check n)
470   "Make  a bar check that checks for a specific bar number. 
471 "
472   (let ((m (make-music 'ApplyContext)))
473     (define (checker tr)
474       (let* ((bn (ly:context-property tr 'currentBarNumber)))
475         (if (= bn n)
476             #t
477             (error
478              (format "Bar check failed, we should have reached ~a, instead at ~a\n"
479                      n bn)))))
480     (set! (ly:music-property m 'procedure) checker)
481     m))
482
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
484 ;; warn for bare chords at start.
485
486 (define (has-request-chord elts)
487   (reduce (lambda (x y) (or x y)) #f
488           (map (lambda (x)
489                  (equal? (ly:music-name x) "Request_chord"))
490                elts)))
491
492 (define (ly:music-message music msg)
493   (let ((ip (ly:music-property music 'origin)))
494     (if (ly:input-location? ip)
495         (ly:input-message ip msg)
496         (ly:warn msg))))
497   
498 (define (check-start-chords music)
499   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords),
500 without context specification. Called  from parser."
501   (let ((es (ly:music-property music 'elements))
502         (e (ly:music-property music 'element))
503         (name (ly:music-name music)))
504     (cond ((equal? name "Context_specced_music") #t)
505           ((equal? name "Simultaneous_music")
506            (if (has-request-chord es)
507                (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
508                (map check-start-chords es)))
509           ((equal? name "Sequential_music")
510            (if (pair? es)
511                (check-start-chords (car es))))
512           (else (if (ly:music? e) (check-start-chords e)))))
513   music)
514
515
516
517 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
518 ;;
519 ;; setting stuff for grace context.
520 ;;
521
522 (define (vector-extend v x)
523   "Make a new vector consisting of V, with X added to the end."
524   (let*
525       ((n (vector-length v))
526        (nv (make-vector (+ n 1) '())))
527     (vector-move-left! v 0 n nv 0)
528     (vector-set! nv n x)
529     nv))
530
531 (define (vector-map f v)
532   "Map  F over V. This function returns nothing."
533   (do ((n (vector-length v))
534        (i 0 (+ i 1)))
535       ((>= i n))
536     (f (vector-ref v i))))
537
538 (define (vector-reverse-map f v)
539   "Map  F over V, N to 0 order. This function returns nothing."
540   (do ((i (- (vector-length v) 1) (- i 1)))
541       ((< i 0))
542     (f (vector-ref v i))))
543
544 ;; TODO:  make a remove-grace-property too.
545 (define-public (add-grace-property context-name grob sym val)
546   "Set SYM=VAL for GROB in CONTEXT-NAME. "
547   (define (set-prop context)
548     (let* ((where (ly:context-property-where-defined context 'graceSettings))
549            (current (ly:context-property where 'graceSettings))
550            (new-settings (vector-extend current (list context-name grob sym val))))
551       (ly:context-set-property! where 'graceSettings new-settings)))
552   (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
553
554
555 (define-public (set-start-grace-properties context)
556   (define (execute-1 x)
557     (let ((tr (ly:context-find context (car x))))
558       (if (ly:context? tr)
559           (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x)))))
560   
561   (let ((props (ly:context-property context 'graceSettings)))
562     (if (vector? props)
563         (vector-map execute-1 props))))
564
565 (define-public (set-stop-grace-properties context)
566   (define (execute-1 x)
567     (let ((tr (ly:context-find context (car x))))
568       (if (ly:context? tr)
569           (ly:context-pushpop-property tr (cadr x) (caddr x)))))
570   
571   (let ((props (ly:context-property context 'graceSettings)))
572     (if (vector? props)
573         (vector-reverse-map execute-1 props))))
574
575
576
577 (defmacro-public def-grace-function (start stop)
578   `(def-music-function (location music) (ly:music?)
579      (make-music 'GraceMusic
580                  'origin location
581                  'element (make-music 'SequentialMusic
582                                       'elements (list (ly:music-deep-copy ,start)
583                                                       music
584                                                       (ly:music-deep-copy ,stop))))))
585
586 (defmacro-public def-music-function (args signature . body)
587   "Helper macro for `ly:make-music-function'.
588 Syntax:
589   (def-music-function (location arg1 arg2 ...) (arg1-type? arg2-type? ...)
590     ...function body...)
591 "
592   `(ly:make-music-function (list ,@signature)
593                            (lambda (,@args)
594                              ,@body)))
595
596
597 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
598
599 (define-public ((quote-substitute quote-tab) music)
600   (let*
601       ((quoted-name (ly:music-property music 'quoted-music-name))
602        (quoted-vector (if (string? quoted-name)
603                           (hash-ref quote-tab quoted-name #f)
604                           #f
605                           ))
606
607        )
608
609     (if (string? quoted-name)
610         (if  (vector? quoted-vector)
611              (set! (ly:music-property music 'quoted-events) quoted-vector)
612              (ly:warn "Cannot find quoted music `~S'" quoted-name)))
613
614     music))
615     
616
617 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618 ;; switch it on here, so parsing and init isn't checked (too slow!)
619 ;;
620 ;; automatic music transformations.
621
622 (define (switch-on-debugging m)
623   (if (defined? 'set-debug-cell-accesses!)
624       (set-debug-cell-accesses! 15000))
625   m)
626
627 (define (music-check-error music)
628   (define found #f)
629   (define (signal m)
630     (if (and (ly:music? m)
631              (eq? (ly:music-property m 'error-found) #t))
632         (set! found #t)))
633   
634   (for-each signal (ly:music-property music 'elements))
635   (signal (ly:music-property music 'element))
636
637   (if found
638       (set! (ly:music-property music 'error-found) #t))
639   music)
640
641 (define-public toplevel-music-functions
642   (list
643    ;; check-start-chords ; ; no longer needed with chord syntax. 
644    (lambda (music parser) (voicify-music music))
645    (lambda (x parser) (music-map glue-mm-rest-texts x))
646    (lambda (x parser) (music-map music-check-error x))
647    (lambda (music parser)
648
649      (music-map (quote-substitute (ly:parser-lookup parser 'musicQuotes))  music))
650    ;; switch-on-debugging
651    ))
652
653 ;;;;;;;;;;;;;;;;;
654 ;; lyrics
655
656 (define (apply-durations lyric-music durations) 
657   (define (apply-duration music)
658     (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
659              (ly:duration?  (ly:music-property music 'duration)))
660         (begin
661           (set! (ly:music-property music 'duration) (car durations))
662           (set! durations (cdr durations)))))
663   
664   (music-map apply-duration lyric-music))
665
666
667 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
668 ;;
669
670 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
671    "Usage: see input/regression/balloon.ly "
672   (let* ((meta (ly:grob-property grob 'meta))
673          (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
674          (cb (ly:grob-property grob 'print-function)))
675     (if (equal? nm object-name)
676         (begin
677           (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
678           (set! (ly:grob-property grob 'balloon-original-callback) cb)
679           (set! (ly:grob-property grob 'balloon-text) text)
680           (set! (ly:grob-property grob 'balloon-text-offset) off)
681           (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
682
683 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
684 ;; accidentals
685
686 (define-public (set-accidentals-properties extra-natural
687                                            auto-accs auto-cauts
688                                            context)
689   (context-spec-music
690    (make-sequential-music
691     (append (if (boolean? extra-natural)
692                 (list (make-property-set 'extraNatural extra-natural))
693                 '())
694             (list (make-property-set 'autoAccidentals auto-accs)
695                   (make-property-set 'autoCautionaries auto-cauts))))
696    context))
697
698 (define-public (set-accidental-style style . rest)
699   "Set accidental style to STYLE. Optionally takes a context argument,
700 e.g. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
701 use GrandStaff as a context. "
702   (let ((context (if (pair? rest)
703                      (car rest) 'Staff))
704         (pcontext (if (pair? rest)
705                       (car rest) 'GrandStaff)))
706     (ly:export
707      (cond
708       ;; accidentals as they were common in the 18th century.
709       ((equal? style 'default)
710        (set-accidentals-properties #t '(Staff (same-octave . 0))
711                                    '() context))
712       ;; accidentals from one voice do NOT get cancelled in other voices
713       ((equal? style 'voice)
714        (set-accidentals-properties #t '(Voice (same-octave . 0))
715                                    '() context))
716       ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
717       ;; This includes all the default accidentals, but accidentals also needs cancelling
718       ;; in other octaves and in the next measure.
719       ((equal? style 'modern)
720        (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
721                                    '()  context))
722       ;; the accidentals that Stone adds to the old standard as cautionaries
723       ((equal? style 'modern-cautionary)
724        (set-accidentals-properties #f '(Staff (same-octave . 0))
725                                    '(Staff (any-octave . 0) (same-octave . 1))
726                                    context))
727       ;; Multivoice accidentals to be read both by musicians playing one voice
728       ;; and musicians playing all voices.
729       ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
730       ((equal? style 'modern-voice)
731        (set-accidentals-properties  #f
732                                     '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
733                                             Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
734                                     '()
735                                     context))
736       ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
737       ;; as cautionaries
738       ((equal? style 'modern-voice-cautionary)
739        (set-accidentals-properties #f
740                                    '(Voice (same-octave . 0) )
741                                    '(Voice (any-octave . 0) (same-octave . 1)
742                                            Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
743                                    context))
744       ;; stone's suggestions for accidentals on grand staff.
745       ;; Accidentals are cancelled across the staves in the same grand staff as well
746       ((equal? style 'piano)
747        (set-accidentals-properties #f
748                                    '(Staff (same-octave . 0)
749                                            (any-octave . 0) (same-octave . 1)
750                                            GrandStaff (any-octave . 0) (same-octave . 1))
751                                    '()
752                                    pcontext))
753       ((equal? style 'piano-cautionary)
754        (set-accidentals-properties #f
755                                    '(Staff (same-octave . 0))
756                                    '(Staff (any-octave . 0) (same-octave . 1)
757                                            GrandStaff (any-octave . 0) (same-octave . 1))
758                                    pcontext))
759       ;; do not set localKeySignature when a note alterated differently from
760       ;; localKeySignature is found.
761       ;; Causes accidentals to be printed at every note instead of
762       ;; remembered for the duration of a measure.
763       ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
764       ((equal? style 'forget)
765        (set-accidentals-properties '()
766                                    '(Staff (same-octave . -1))
767                                    '() context))
768       ;; Do not reset the key at the start of a measure.  Accidentals will be
769       ;; printed only once and are in effect until overridden, possibly many
770       ;; measures later.
771       ((equal? style 'no-reset)
772        (set-accidentals-properties '()
773                                    '(Staff (same-octave . #t))
774                                    '()
775                                    context))
776       (else
777        (ly:warn "Unknown accidental style: ~S" (symbol->string style))
778        (make-sequential-music '()))))))
779
780 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
781
782 (define-public (skip-of-length mus)
783   "Create a skip of exactly the same length as MUS."
784   (let*
785    ((skip
786      (make-music
787       'SkipEvent
788       'duration (ly:make-duration 0 0))))
789
790    (make-event-chord (list (ly:music-compress skip (ly:music-length mus))))
791 ))
792
793
794 (define-public (mmrest-of-length mus)
795   "Create a mmrest of exactly the same length as MUS."
796   
797   (let*
798    ((skip
799      (make-multi-measure-rest
800       (ly:make-duration 0 0) '() )))
801    (ly:music-compress skip (ly:music-length mus))
802    skip
803 ))
804