]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* lily/context-specced-music-iterator.cc (construct_children):
[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 (append current
551                                  (list (list context-name grob sym val)))))
552       (ly:context-set-property! where 'graceSettings new-settings)))
553   (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
554
555
556
557 (defmacro-public def-grace-function (start stop)
558   `(def-music-function (location music) (ly:music?)
559      (make-music 'GraceMusic
560                  'origin location
561                  'element (make-music 'SequentialMusic
562                                       'elements (list (ly:music-deep-copy ,start)
563                                                       music
564                                                       (ly:music-deep-copy ,stop))))))
565
566 (defmacro-public def-music-function (args signature . body)
567   "Helper macro for `ly:make-music-function'.
568 Syntax:
569   (def-music-function (location arg1 arg2 ...) (arg1-type? arg2-type? ...)
570     ...function body...)
571 "
572   `(ly:make-music-function (list ,@signature)
573                            (lambda (,@args)
574                              ,@body)))
575
576
577 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
578
579 (define-public (cue-substitute quote-music)
580   (if (vector? (ly:music-property quote-music 'quoted-events))
581       (let*
582           ((dir (ly:music-property quote-music 'quoted-voice-direction))
583            (main-voice (if (eq? 1 dir) 2 1))
584            (cue-voice (if (eq? 1 dir) 1 2))
585            (main-music (ly:music-property quote-music 'element))
586            (return-value quote-music)
587            )
588         
589         (if (or (eq? 1 dir) (eq? -1 dir))
590
591             ;; if we have stem dirs, change both quoted and main music
592             ;; to have opposite stems.
593             (begin
594               (set! return-value
595                     (make-sequential-music
596                      (list
597                       (context-spec-music (make-voice-props-set cue-voice) 'Voice "cue")
598                       quote-music
599                       (context-spec-music (make-voice-props-revert)  'Voice "cue"))
600                      ))
601               (set! main-music
602                     (make-sequential-music
603                      (list
604                       (make-voice-props-set main-voice)
605                       main-music
606                       (make-voice-props-revert)))
607                     )
608               (set! (ly:music-property quote-music 'element) main-music)))
609
610         return-value)
611       quote-music))
612
613 (define-public ((quote-substitute quote-tab) music)
614   (let*
615       ((quoted-name (ly:music-property music 'quoted-music-name))
616        (quoted-vector (if (string? quoted-name)
617                           (hash-ref quote-tab quoted-name #f)
618                           #f
619                           )))
620
621     (if (string? quoted-name)
622         (if  (vector? quoted-vector)
623              (set! (ly:music-property music 'quoted-events) quoted-vector)
624              (ly:warn "Cannot find quoted music `~S'" quoted-name)))
625
626     music))
627     
628
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;; switch it on here, so parsing and init isn't checked (too slow!)
631 ;;
632 ;; automatic music transformations.
633
634 (define (switch-on-debugging m)
635   (if (defined? 'set-debug-cell-accesses!)
636       (set-debug-cell-accesses! 15000))
637   m)
638
639 (define (music-check-error music)
640   (define found #f)
641   (define (signal m)
642     (if (and (ly:music? m)
643              (eq? (ly:music-property m 'error-found) #t))
644         (set! found #t)))
645   
646   (for-each signal (ly:music-property music 'elements))
647   (signal (ly:music-property music 'element))
648
649   (if found
650       (set! (ly:music-property music 'error-found) #t))
651   music)
652
653 (define-public toplevel-music-functions
654   (list
655    ;; check-start-chords ; ; no longer needed with chord syntax. 
656    (lambda (music parser) (voicify-music music))
657    (lambda (x parser) (music-map glue-mm-rest-texts x))
658    (lambda (x parser) (music-map music-check-error x))
659    (lambda (x parser) (music-map cue-substitute x))
660    (lambda (music parser)
661
662      (music-map (quote-substitute (ly:parser-lookup parser 'musicQuotes))  music))
663    ;; switch-on-debugging
664    ))
665
666 ;;;;;;;;;;;;;;;;;
667 ;; lyrics
668
669 (define (apply-durations lyric-music durations) 
670   (define (apply-duration music)
671     (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
672              (ly:duration?  (ly:music-property music 'duration)))
673         (begin
674           (set! (ly:music-property music 'duration) (car durations))
675           (set! durations (cdr durations)))))
676   
677   (music-map apply-duration lyric-music))
678
679
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
681 ;;
682
683 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
684    "Usage: see input/regression/balloon.ly "
685   (let* ((meta (ly:grob-property grob 'meta))
686          (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
687          (cb (ly:grob-property grob 'print-function)))
688     (if (equal? nm object-name)
689         (begin
690           (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
691           (set! (ly:grob-property grob 'balloon-original-callback) cb)
692           (set! (ly:grob-property grob 'balloon-text) text)
693           (set! (ly:grob-property grob 'balloon-text-offset) off)
694           (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
695
696 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
697 ;; accidentals
698
699 (define-public (set-accidentals-properties extra-natural
700                                            auto-accs auto-cauts
701                                            context)
702   (context-spec-music
703    (make-sequential-music
704     (append (if (boolean? extra-natural)
705                 (list (make-property-set 'extraNatural extra-natural))
706                 '())
707             (list (make-property-set 'autoAccidentals auto-accs)
708                   (make-property-set 'autoCautionaries auto-cauts))))
709    context))
710
711 (define-public (set-accidental-style style . rest)
712   "Set accidental style to STYLE. Optionally takes a context argument,
713 e.g. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
714 use GrandStaff as a context. "
715   (let ((context (if (pair? rest)
716                      (car rest) 'Staff))
717         (pcontext (if (pair? rest)
718                       (car rest) 'GrandStaff)))
719     (ly:export
720      (cond
721       ;; accidentals as they were common in the 18th century.
722       ((equal? style 'default)
723        (set-accidentals-properties #t '(Staff (same-octave . 0))
724                                    '() context))
725       ;; accidentals from one voice do NOT get cancelled in other voices
726       ((equal? style 'voice)
727        (set-accidentals-properties #t '(Voice (same-octave . 0))
728                                    '() context))
729       ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
730       ;; This includes all the default accidentals, but accidentals also needs cancelling
731       ;; in other octaves and in the next measure.
732       ((equal? style 'modern)
733        (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
734                                    '()  context))
735       ;; the accidentals that Stone adds to the old standard as cautionaries
736       ((equal? style 'modern-cautionary)
737        (set-accidentals-properties #f '(Staff (same-octave . 0))
738                                    '(Staff (any-octave . 0) (same-octave . 1))
739                                    context))
740       ;; Multivoice accidentals to be read both by musicians playing one voice
741       ;; and musicians playing all voices.
742       ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
743       ((equal? style 'modern-voice)
744        (set-accidentals-properties  #f
745                                     '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
746                                             Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
747                                     '()
748                                     context))
749       ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
750       ;; as cautionaries
751       ((equal? style 'modern-voice-cautionary)
752        (set-accidentals-properties #f
753                                    '(Voice (same-octave . 0) )
754                                    '(Voice (any-octave . 0) (same-octave . 1)
755                                            Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
756                                    context))
757       ;; stone's suggestions for accidentals on grand staff.
758       ;; Accidentals are cancelled across the staves in the same grand staff as well
759       ((equal? style 'piano)
760        (set-accidentals-properties #f
761                                    '(Staff (same-octave . 0)
762                                            (any-octave . 0) (same-octave . 1)
763                                            GrandStaff (any-octave . 0) (same-octave . 1))
764                                    '()
765                                    pcontext))
766       ((equal? style 'piano-cautionary)
767        (set-accidentals-properties #f
768                                    '(Staff (same-octave . 0))
769                                    '(Staff (any-octave . 0) (same-octave . 1)
770                                            GrandStaff (any-octave . 0) (same-octave . 1))
771                                    pcontext))
772       ;; do not set localKeySignature when a note alterated differently from
773       ;; localKeySignature is found.
774       ;; Causes accidentals to be printed at every note instead of
775       ;; remembered for the duration of a measure.
776       ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
777       ((equal? style 'forget)
778        (set-accidentals-properties '()
779                                    '(Staff (same-octave . -1))
780                                    '() context))
781       ;; Do not reset the key at the start of a measure.  Accidentals will be
782       ;; printed only once and are in effect until overridden, possibly many
783       ;; measures later.
784       ((equal? style 'no-reset)
785        (set-accidentals-properties '()
786                                    '(Staff (same-octave . #t))
787                                    '()
788                                    context))
789       (else
790        (ly:warn "Unknown accidental style: ~S" (symbol->string style))
791        (make-sequential-music '()))))))
792
793 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
794
795 (define-public (skip-of-length mus)
796   "Create a skip of exactly the same length as MUS."
797   (let*
798    ((skip
799      (make-music
800       'SkipEvent
801       'duration (ly:make-duration 0 0))))
802
803    (make-event-chord (list (ly:music-compress skip (ly:music-length mus))))
804 ))
805
806
807 (define-public (mmrest-of-length mus)
808   "Create a mmrest of exactly the same length as MUS."
809   
810   (let*
811    ((skip
812      (make-multi-measure-rest
813       (ly:make-duration 0 0) '() )))
814    (ly:music-compress skip (ly:music-length mus))
815    skip
816 ))
817