]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
1d0d9d312b646133f784356e838cb745c8fe9231
[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         )
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     (display (ly:music-property m 'name))
422     
423     (if (pair? es)
424         (set! (ly:music-property m 'elements) (map voicify-music es)))
425     (if (ly:music? e)
426         (set! (ly:music-property m 'element)  (voicify-music e)))
427     (if (and (equal? (ly:music-property m 'name) 'SimultaneousMusic)
428              (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
429         (set! m (context-spec-music (voicify-chord m) 'Staff)))
430     m))
431
432 (define-public (empty-music)
433   (ly:export (make-music 'Music)))
434 ;;;
435
436                                         ; Make a function that checks score element for being of a specific type. 
437 (define-public (make-type-checker symbol)
438   (lambda (elt)
439     ;;(display  symbol)
440     ;;(eq? #t (ly:grob-property elt symbol))
441     (not (eq? #f (memq symbol (ly:grob-property elt 'interfaces))))))
442
443 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
444   (if (func grob)
445       (set! (ly:grob-property grob sym) val)))
446
447
448 (define-public ((set-output-property grob-name symbol val)  grob grob-c context)
449   "Usage:
450
451 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
452
453 "
454   (let ((meta (ly:grob-property grob 'meta)))
455     (if (equal?  (cdr (assoc 'name meta)) grob-name)
456         (set! (ly:grob-property grob symbol) val))))
457
458
459 ;;
460 (define-public (smart-bar-check n)
461   "Make  a bar check that checks for a specific bar number. 
462 "
463   (let ((m (make-music 'ApplyContext)))
464     (define (checker tr)
465       (let* ((bn (ly:context-property tr 'currentBarNumber)))
466         (if (= bn n)
467             #t
468             (error
469              (format "Bar check failed, we should have reached ~a, instead at ~a\n"
470                      n bn)))))
471     (set! (ly:music-property m 'procedure) checker)
472     m))
473
474 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
475 ;; warn for bare chords at start.
476
477 (define (has-request-chord elts)
478   (reduce (lambda (x y) (or x y)) #f
479           (map (lambda (x)
480                  (equal? (ly:music-property x 'name) 'RequestChord))
481                elts)))
482
483 (define (ly:music-message music msg)
484   (let ((ip (ly:music-property music 'origin)))
485     (if (ly:input-location? ip)
486         (ly:input-message ip msg)
487         (ly:warn msg))))
488
489 (define (check-start-chords music)
490   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords),
491 without context specification. Called  from parser."
492   (let ((es (ly:music-property music 'elements))
493         (e (ly:music-property music 'element))
494         (name (ly:music-property music 'name)))
495     (cond ((equal? name "Context_specced_music") #t)
496           ((equal? name "Simultaneous_music")
497            (if (has-request-chord es)
498                (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
499                (map check-start-chords es)))
500           ((equal? name "SequentialMusic")
501            (if (pair? es)
502                (check-start-chords (car es))))
503           (else (if (ly:music? e) (check-start-chords e)))))
504   music)
505
506
507
508 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
509 ;;
510 ;; setting stuff for grace context.
511 ;;
512
513 (define (vector-extend v x)
514   "Make a new vector consisting of V, with X added to the end."
515   (let* ((n (vector-length v))
516          (nv (make-vector (+ n 1) '())))
517     (vector-move-left! v 0 n nv 0)
518     (vector-set! nv n x)
519     nv))
520
521 (define (vector-map f v)
522   "Map  F over V. This function returns nothing."
523   (do ((n (vector-length v))
524        (i 0 (+ i 1)))
525       ((>= i n))
526     (f (vector-ref v i))))
527
528 (define (vector-reverse-map f v)
529   "Map  F over V, N to 0 order. This function returns nothing."
530   (do ((i (- (vector-length v) 1) (- i 1)))
531       ((< i 0))
532     (f (vector-ref v i))))
533
534 ;; TODO:  make a remove-grace-property too.
535 (define-public (add-grace-property context-name grob sym val)
536   "Set SYM=VAL for GROB in CONTEXT-NAME. "
537   (define (set-prop context)
538     (let* ((where (ly:context-property-where-defined context 'graceSettings))
539            (current (ly:context-property where 'graceSettings))
540            (new-settings (append current
541                                  (list (list context-name grob sym val)))))
542       (ly:context-set-property! where 'graceSettings new-settings)))
543   (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
544
545
546
547 (defmacro-public def-grace-function (start stop)
548   `(def-music-function (parser location music) (ly:music?)
549      (make-music 'GraceMusic
550                  'origin location
551                  'element (make-music 'SequentialMusic
552                                       'elements (list (ly:music-deep-copy ,start)
553                                                       music
554                                                       (ly:music-deep-copy ,stop))))))
555
556 (defmacro-public def-music-function (args signature . body)
557   "Helper macro for `ly:make-music-function'.
558 Syntax:
559   (def-music-function (parser location arg1 arg2 ...) (arg1-type? arg2-type? ...)
560     ...function body...)
561 "
562   `(ly:make-music-function (list ,@signature)
563                            (lambda (,@args)
564                              ,@body)))
565
566
567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
568
569 (define-public (cue-substitute quote-music)
570   "Must happen after quote-substitute."
571   
572   (if (vector? (ly:music-property quote-music 'quoted-events))
573       (let* ((dir (ly:music-property quote-music 'quoted-voice-direction))
574              (main-voice (if (eq? 1 dir) 1 0))
575              (cue-voice (if (eq? 1 dir) 0 1))
576              (main-music (ly:music-property quote-music 'element))
577              (return-value quote-music))
578         
579         (if (or (eq? 1 dir) (eq? -1 dir))
580             
581             ;; if we have stem dirs, change both quoted and main music
582             ;; to have opposite stems.
583             (begin
584               (set! return-value
585
586                     ;; cannot context-spec Quote-music, since context
587                     ;; for the quotes is determined in the iterator.
588                     (make-sequential-music
589                      (list
590                       (context-spec-music (make-voice-props-set cue-voice) 'Voice "cue")
591                       quote-music
592                       (context-spec-music (make-voice-props-revert)  'Voice "cue"))))
593               (set! main-music
594                     (make-sequential-music
595                      (list
596                       (make-voice-props-set main-voice)
597                       main-music
598                       (make-voice-props-revert))))
599               (set! (ly:music-property quote-music 'element) main-music)))
600
601         return-value)
602       quote-music))
603
604 (define-public ((quote-substitute quote-tab) music)
605   (let* ((quoted-name (ly:music-property music 'quoted-music-name))
606          (quoted-vector (if (string? quoted-name)
607                             (hash-ref quote-tab quoted-name #f)
608                             #f)))
609     
610     (if (string? quoted-name)
611         (if  (vector? quoted-vector)
612              (set! (ly:music-property music 'quoted-events) quoted-vector)
613              (ly:warn "Cannot find quoted music `~S'" quoted-name)))
614
615     music))
616
617
618 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
619 ;; switch it on here, so parsing and init isn't checked (too slow!)
620 ;;
621 ;; automatic music transformations.
622
623 (define (switch-on-debugging m)
624   (if (defined? 'set-debug-cell-accesses!)
625       (set-debug-cell-accesses! 15000))
626   m)
627
628 (define (music-check-error music)
629   (define found #f)
630   (define (signal m)
631     (if (and (ly:music? m)
632              (eq? (ly:music-property m 'error-found) #t))
633         (set! found #t)))
634   
635   (for-each signal (ly:music-property music 'elements))
636   (signal (ly:music-property music 'element))
637
638   (if found
639       (set! (ly:music-property music 'error-found) #t))
640   music)
641
642 (define (precompute-music-length music)
643   (set! (ly:music-property music 'length)
644         (ly:music-length music))
645   music)
646
647 (define-public toplevel-music-functions
648   (list
649    ;; check-start-chords ; ; no longer needed with chord syntax. 
650    (lambda (music parser) (voicify-music music))
651    (lambda (x parser) (music-map glue-mm-rest-texts x))
652    (lambda (x parser) (music-map music-check-error x))
653    (lambda (x parser) (music-map precompute-music-length x))
654    (lambda (music parser)
655
656      (music-map (quote-substitute (ly:parser-lookup parser 'musicQuotes))  music))
657    ;; switch-on-debugging
658    (lambda (x parser) (music-map cue-substitute x))))
659
660 ;;;;;;;;;;;;;;;;;
661 ;; lyrics
662
663 (define (apply-durations lyric-music durations) 
664   (define (apply-duration music)
665     (if (and (not (equal? (ly:music-length music) ZERO-MOMENT))
666              (ly:duration?  (ly:music-property music 'duration)))
667         (begin
668           (set! (ly:music-property music 'duration) (car durations))
669           (set! durations (cdr durations)))))
670   
671   (music-map apply-duration lyric-music))
672
673
674 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
675 ;;
676
677 (define-public ((add-balloon-text object-name text off) grob orig-context cur-context)
678   "Usage: see input/regression/balloon.ly "
679   (let* ((meta (ly:grob-property grob 'meta))
680          (nm (if (pair? meta) (cdr (assoc 'name meta)) "nonexistant"))
681          (cb (ly:grob-property grob 'print-function)))
682     (if (equal? nm object-name)
683         (begin
684           (set! (ly:grob-property grob 'print-function) Balloon_interface::print)
685           (set! (ly:grob-property grob 'balloon-original-callback) cb)
686           (set! (ly:grob-property grob 'balloon-text) text)
687           (set! (ly:grob-property grob 'balloon-text-offset) off)
688           (set! (ly:grob-property grob 'balloon-text-props) '((font-family . roman)))))))
689
690 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
691 ;; accidentals
692
693 (define-public (set-accidentals-properties extra-natural
694                                            auto-accs auto-cauts
695                                            context)
696   (context-spec-music
697    (make-sequential-music
698     (append (if (boolean? extra-natural)
699                 (list (make-property-set 'extraNatural extra-natural))
700                 '())
701             (list (make-property-set 'autoAccidentals auto-accs)
702                   (make-property-set 'autoCautionaries auto-cauts))))
703    context))
704
705 (define-public (set-accidental-style style . rest)
706   "Set accidental style to STYLE. Optionally takes a context argument,
707 e.g. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
708 use GrandStaff as a context. "
709   (let ((context (if (pair? rest)
710                      (car rest) 'Staff))
711         (pcontext (if (pair? rest)
712                       (car rest) 'GrandStaff)))
713     (ly:export
714      (cond
715       ;; accidentals as they were common in the 18th century.
716       ((equal? style 'default)
717        (set-accidentals-properties #t '(Staff (same-octave . 0))
718                                    '() context))
719       ;; accidentals from one voice do NOT get cancelled in other voices
720       ((equal? style 'voice)
721        (set-accidentals-properties #t '(Voice (same-octave . 0))
722                                    '() context))
723       ;; accidentals as suggested by Kurt Stone, Music Notation in the 20th century.
724       ;; This includes all the default accidentals, but accidentals also needs cancelling
725       ;; in other octaves and in the next measure.
726       ((equal? style 'modern)
727        (set-accidentals-properties #f '(Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
728                                    '()  context))
729       ;; the accidentals that Stone adds to the old standard as cautionaries
730       ((equal? style 'modern-cautionary)
731        (set-accidentals-properties #f '(Staff (same-octave . 0))
732                                    '(Staff (any-octave . 0) (same-octave . 1))
733                                    context))
734       ;; Multivoice accidentals to be read both by musicians playing one voice
735       ;; and musicians playing all voices.
736       ;; Accidentals are typeset for each voice, but they ARE cancelled across voices.
737       ((equal? style 'modern-voice)
738        (set-accidentals-properties  #f
739                                     '(Voice (same-octave . 0) (any-octave . 0) (same-octave . 1)
740                                             Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
741                                     '()
742                                     context))
743       ;; same as modernVoiceAccidental eccept that all special accidentals are typeset
744       ;; as cautionaries
745       ((equal? style 'modern-voice-cautionary)
746        (set-accidentals-properties #f
747                                    '(Voice (same-octave . 0))
748                                    '(Voice (any-octave . 0) (same-octave . 1)
749                                            Staff (same-octave . 0) (any-octave . 0) (same-octave . 1))
750                                    context))
751       ;; stone's suggestions for accidentals on grand staff.
752       ;; Accidentals are cancelled across the staves in the same grand staff as well
753       ((equal? style 'piano)
754        (set-accidentals-properties #f
755                                    '(Staff (same-octave . 0)
756                                            (any-octave . 0) (same-octave . 1)
757                                            GrandStaff (any-octave . 0) (same-octave . 1))
758                                    '()
759                                    pcontext))
760       ((equal? style 'piano-cautionary)
761        (set-accidentals-properties #f
762                                    '(Staff (same-octave . 0))
763                                    '(Staff (any-octave . 0) (same-octave . 1)
764                                            GrandStaff (any-octave . 0) (same-octave . 1))
765                                    pcontext))
766       ;; do not set localKeySignature when a note alterated differently from
767       ;; localKeySignature is found.
768       ;; Causes accidentals to be printed at every note instead of
769       ;; remembered for the duration of a measure.
770       ;; accidentals not being remembered, causing accidentals always to be typeset relative to the time signature
771       ((equal? style 'forget)
772        (set-accidentals-properties '()
773                                    '(Staff (same-octave . -1))
774                                    '() context))
775       ;; Do not reset the key at the start of a measure.  Accidentals will be
776       ;; printed only once and are in effect until overridden, possibly many
777       ;; measures later.
778       ((equal? style 'no-reset)
779        (set-accidentals-properties '()
780                                    '(Staff (same-octave . #t))
781                                    '()
782                                    context))
783       (else
784        (ly:warn "Unknown accidental style: ~S" (symbol->string style))
785        (make-sequential-music '()))))))
786
787 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
788
789 (define-public (skip-of-length mus)
790   "Create a skip of exactly the same length as MUS."
791   (let* ((skip
792           (make-music
793            'SkipEvent
794            'duration (ly:make-duration 0 0))))
795
796     (make-event-chord (list (ly:music-compress skip (ly:music-length mus))))))
797
798 (define-public (mmrest-of-length mus)
799   "Create a mmrest of exactly the same length as MUS."
800   
801   (let* ((skip
802           (make-multi-measure-rest
803            (ly:make-duration 0 0) '())))
804     (ly:music-compress skip (ly:music-length mus))
805     skip))