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