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