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