]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* input/regression/spacing-to-grace.ly: new file.
[lilypond.git] / scm / music-functions.scm
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; tuplets.
3
4 (define-public (denominator-tuplet-formatter mus)
5   (number->string (ly-get-mus-property mus 'denominator)))
6
7 (define-public (fraction-tuplet-formatter mus)
8   (string-append (number->string (ly-get-mus-property mus 'numerator))
9                  ":"
10                  (number->string (ly-get-mus-property mus 'denominator))
11                  ))
12
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
15
16 (define-public (shift-duration-log music shift dot)
17   "Recurse through music, adding SHIFT to duration-log and optionally 
18   a dot to any note encountered. This scales the music up by a factor 
19   2^shift * (2 - (1/2)^dot)"
20   (let* ((es (ly-get-mus-property music 'elements))
21          (e (ly-get-mus-property music 'element))
22          (n  (ly-music-name music))
23          (f  (lambda (x)  (shift-duration-log x shift dot)))
24          )
25     (if (or (equal? n "Note_req")
26             (equal? n "Rest_req"))
27         (let* (
28                (d (ly-get-mus-property music 'duration))
29                (cp (duration-factor d))
30                (nd (make-duration (+ shift (duration-log d))
31                                   (+ dot (duration-dot-count d))
32                                   (car cp)
33                                   (cdr cp)))
34                
35                )
36           (ly-set-mus-property! music 'duration nd)
37           ))
38     
39     (if (pair? es)
40         (ly-set-mus-property!
41          music 'elements
42          (map f es)))
43     
44     (if (music? e)
45         (ly-set-mus-property!
46          music 'element
47          (f e)))
48     
49     music))
50
51
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
53 ;; repeats.
54
55 (define-public (unfold-repeats music)
56 "
57 This function replaces all repeats  with unfold repeats. It was 
58 written by Rune Zedeler. "
59   (let* ((es (ly-get-mus-property music 'elements))
60          (e (ly-get-mus-property music 'element))
61          (n  (ly-music-name music)))
62  
63     (if (equal? n "Repeated_music")
64         (begin
65           (if (equal?
66                (ly-get-mus-property music 'iterator-ctor)
67                Chord_tremolo_iterator::constructor)
68               (shift-duration-log music  (intlog2 (ly-get-mus-property music 'repeat-count)) 0)
69               )
70           (ly-set-mus-property!
71            music 'length Repeated_music::unfolded_music_length)
72           (ly-set-mus-property!
73            music 'start-moment-function Repeated_music::first_start)
74           (ly-set-mus-property!
75            music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
76
77     (if (pair? es)
78         (ly-set-mus-property!
79          music 'elements
80          (map unfold-repeats es)))
81
82     (if (music? e)
83         (ly-set-mus-property!
84          music 'element
85          (unfold-repeats e)))
86
87     music))
88
89
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92
93 (define  (pitchify-scripts music)
94   "Copy the pitch fields of the Note_requests into  Text_script_requests, to aid
95 Fingering_engraver."
96   (define (find-note musics)
97     (filter-list (lambda (m) (equal? (ly-music-name m) "Note_req")) musics)
98     )
99   (define (find-scripts musics)
100     (filter-list (lambda (m) (equal? (ly-music-name m) "Text_script_req")) musics))
101
102   (let* (
103          (e (ly-get-mus-property music 'element))
104          (es (ly-get-mus-property music 'elements))
105          (notes (find-note es))
106          (pitch (if (pair? notes) (ly-get-mus-property (car  notes) 'pitch) #f))
107          )
108
109     (if pitch
110         (map (lambda (x) (ly-set-mus-property! x 'pitch pitch)) (find-scripts es))
111         )
112         
113     (if (pair? es)
114         (ly-set-mus-property!
115          music 'elements
116          (map pitchify-scripts es)))
117
118     (if (music? e)
119         (ly-set-mus-property!
120          music 'element
121          (pitchify-scripts e)))
122
123     music))
124
125
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
127 ;; property setting music objs.
128 (define-public (make-grob-property-set grob gprop val)
129   "Make a M-exp that sets GPROP to VAL in GROBS. Does a pop first, i.e.
130 this is not an override 
131 "
132   
133    (let* ((m (make-music-by-name  'OverrideProperty)))
134      (ly-set-mus-property! m 'symbol grob)
135      (ly-set-mus-property! m 'grob-property gprop)
136      (ly-set-mus-property! m 'grob-value val)
137      (ly-set-mus-property! m 'pop-first #t)
138                 
139      m
140    
141    ))
142
143
144 (define-public (make-grob-property-revert grob gprop)
145   "Revert the grob property GPROP for GROB."
146    (let* ((m (make-music-by-name  'OverrideProperty)))
147      (ly-set-mus-property! m 'symbol grob)
148      (ly-set-mus-property! m 'grob-property gprop)
149                 
150      m
151    
152    ))
153    
154 (define-public (make-voice-props-set n)
155   (make-sequential-music
156    (append
157       (map (lambda (x) (make-grob-property-set x 'direction
158                                                (if (odd? n) -1 1)))
159            '(Tie Slur Stem Dots))
160       (list
161        (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
162        (make-grob-property-set 'MultiMeasureRest 'staff-position
163                                (if (odd? n) -4 4)
164                                )
165        
166        )
167    )
168   ))
169
170 (define-public (make-voice-props-revert)
171   (make-sequential-music
172    (list
173       (make-grob-property-revert 'Tie 'direction)
174       (make-grob-property-revert 'Dots 'direction)
175       (make-grob-property-revert 'Stem 'direction)
176       (make-grob-property-revert 'Slur 'direction)          
177       (make-grob-property-revert 'NoteColumn 'horizontal-shift)
178    ))
179   )
180
181 (define-public (context-spec-music m context . rest)
182   "Add \context CONTEXT = foo to M. "
183   
184   (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
185     (ly-set-mus-property! cm 'element m)
186     (ly-set-mus-property! cm 'context-type context)
187     (if (and  (pair? rest) (string? (car rest)))
188         (ly-set-mus-property! cm 'context-id (car rest))
189     )
190     cm
191   ))
192
193 (define-public (make-sequential-music elts)
194   (let*  ((m (make-music-by-name 'SequentialMusic)))
195     (ly-set-mus-property! m 'elements elts)
196     m
197   ))
198
199 (define-public (make-simultaneous-music elts)
200   (let*  ((m (make-music-by-name 'SimultaneousMusic)))
201     (ly-set-mus-property! m 'elements elts)
202     m
203     ))
204
205 (define-public (make-event-chord elts)
206   (let*  ((m (make-music-by-name 'EventChord)))
207     (ly-set-mus-property! m 'elements elts)
208     m
209     ))
210
211
212 (define-public (make-multi-measure-rest duration location)
213   (let*
214       (
215        (start (make-music-by-name 'MultiMeasureRestEvent))
216        (stop  (make-music-by-name 'MultiMeasureRestEvent))
217        (skip ( make-music-by-name 'SkipEvent))
218        (ch (make-music-by-name 'BarCheck))
219        (ch2  (make-music-by-name 'BarCheck))
220        )
221
222     (ly-set-mus-property! start 'span-direction START)
223     (ly-set-mus-property! stop 'span-direction STOP)    
224     (ly-set-mus-property! skip 'duration duration)
225     (map (lambda (x) (ly-set-mus-property! x 'origin location))
226          (list start stop skip ch ch2))
227     (make-sequential-music
228      (list
229       ch
230       (make-event-chord (list start))
231       (make-event-chord (list skip))
232       (make-event-chord (list stop))
233       ch2
234       ))
235     ))
236
237 (define-public (set-mus-properties! m alist)
238   "Set all of ALIST as properties of M." 
239   (if (pair? alist)
240       (begin
241         (ly-set-mus-property! m (caar alist) (cdar alist))
242         (set-mus-properties! m (cdr alist)))
243   ))
244
245 (define-public (music-separator? m)
246   "Is M a separator?"
247   (let* ((ts (ly-get-mus-property m 'types )))
248     (memq 'separator ts)
249   ))
250
251 (define (split-one sep?  l acc)
252   "Split off the first parts before separator and return both parts.
253
254 "
255   (if (null? l)
256       (cons acc '())
257       (if (sep? (car l))
258           (cons acc (cdr l))
259           (split-one sep? (cdr l) (cons (car l) acc))
260           )
261       ))
262
263 (define-public (split-list l sep?)
264   "
265
266 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
267 =>
268  ...
269
270 "
271   (if (null? l)
272       '()
273       (let* ((c (split-one sep? l '())))
274         (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
275         )
276       )
277   )
278
279 ;;; splitting chords into voices.
280
281 (define (voicify-list lst number)
282    "Make a list of Musics.
283
284    voicify-list :: [ [Music ] ] -> number -> [Music]
285    LST is a list music-lists.
286 "
287
288    (if (null? lst) '()
289        (cons (context-spec-music
290               (make-sequential-music
291                (list
292                 (make-voice-props-set number)
293                 (make-simultaneous-music (car lst))))
294
295               "Voice"  (number->string number))
296               (voicify-list (cdr lst) (+ number 1))
297        ))
298    )
299
300 (define (voicify-chord ch)
301   "Split the parts of a chord into different Voices using separator"
302    (let* ((es (ly-get-mus-property ch 'elements)))
303
304
305      (ly-set-mus-property!  ch 'elements
306        (voicify-list (split-list es music-separator?) 0))
307      ch
308    ))
309
310 (define (voicify-music m)
311    "Recursively split chords that are separated with \\ "
312    
313    (if (not (music? m))
314        (begin (display m)
315        (error "not music!"))
316        )
317    (let*
318        ((es (ly-get-mus-property m 'elements))
319         (e (ly-get-mus-property m 'element))
320         )
321      (if (pair? es)
322          (ly-set-mus-property! m 'elements (map voicify-music es)))
323      (if (music? e)
324          (ly-set-mus-property! m 'element  (voicify-music e)))
325      (if
326       (and (equal? (ly-music-name m) "Simultaneous_music")
327            (reduce (lambda (x y ) (or x y))     (map music-separator? es)))
328       (voicify-chord m)
329       )
330
331      m
332      ))
333
334 (define-public (empty-music)
335   (ly-id (make-music-by-name 'Music))
336   )
337 ;;;
338
339 ; Make a function that checks score element for being of a specific type. 
340 (define-public (make-type-checker symbol)
341   (lambda (elt)
342     ;;(display  symbol)
343     ;;(eq? #t (ly-get-grob-property elt symbol))
344     (not (eq? #f (memq symbol (ly-get-grob-property elt 'interfaces))))))
345
346
347 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
348 ;; warn for bare chords at start.
349
350 (define (has-request-chord elts)
351   (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly-music-name x)
352                                                            "Request_chord")) elts)
353   ))
354
355 (define (ly-music-message music msg)
356   (let* (
357       (ip (ly-get-mus-property music 'origin))
358       )
359
360     (if (ly-input-location? ip)
361         (ly-input-message ip msg)
362         (ly-warn msg))
363   ))
364   
365 (define (check-start-chords music)
366   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called  from parser."
367   
368      (let*
369        ((es (ly-get-mus-property music 'elements))
370         (e (ly-get-mus-property music 'element))
371         (name (ly-music-name music)) 
372         )
373
374        (cond 
375          ((equal? name "Context_specced_music") #t)
376          ((equal? name "Simultaneous_music")
377
378           (if (has-request-chord es)
379               (ly-music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
380               (map check-start-chords es)))
381          
382          ((equal? name "Sequential_music")
383            (if (pair? es)
384                (check-start-chords (car es))))
385           (else (if (music? e) (check-start-chords e )))
386        
387        ))
388      music
389      )
390
391
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
393 ;; switch it on here, so parsing and init isn't checked (too slow!)
394
395 ;; automatic music transformations.
396
397 (define (switch-on-debugging m)
398   (set-debug-cell-accesses! 15000)
399   m
400   )
401
402 (define-public toplevel-music-functions
403   (list check-start-chords
404         voicify-music
405
406 ; switch-on-debugging
407         ))
408
409