]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
2002-07-17 Han-Wen <hanwen@cs.uu.nl>
[lilypond.git] / scm / music-functions.scm
1
2 (define (denominator-tuplet-formatter mus)
3   (number->string (ly-get-mus-property mus 'denominator)))
4
5 (define (fraction-tuplet-formatter mus)
6   (string-append (number->string (ly-get-mus-property mus 'numerator))
7                  ":"
8                  (number->string (ly-get-mus-property mus 'denominator))
9                  ))
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11
12
13 (define (shift-duration-log music shift )
14   "Recurse through music, adding SHIFT to duration-log to any note
15 encountered. This scales the music up by a factor 2^k."
16   (let* ((es (ly-get-mus-property music 'elements))
17          (e (ly-get-mus-property music 'element))
18          (n  (ly-music-name music))
19          (f  (lambda (x)  (shift-duration-log x shift)))
20          )
21     (if (or (equal? n "Note_req")
22             (equal? n "Rest_req"))
23         (let* (
24               (d (ly-get-mus-property music 'duration))
25               (cp (duration-factor d))
26               (nd (make-duration (+ shift (duration-log d))
27                                  (duration-dot-count d)
28                                  (car cp)
29                                  (cdr cp)))
30           
31               )
32           (ly-set-mus-property music 'duration nd)
33         ))
34
35     (if (pair? es)
36         (ly-set-mus-property
37          music 'elements
38          (map f es)))
39
40     (if (music? e)
41         (ly-set-mus-property
42          music 'element
43          (f e)))
44
45     music))
46
47
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 (define (unfold-repeats music)
50 "
51 This function replaces all repeats  with unfold repeats. It was 
52 written by Rune Zedeler. "
53   (let* ((es (ly-get-mus-property music 'elements))
54          (e (ly-get-mus-property music 'element))
55          (n  (ly-music-name music)))
56
57     (if (equal? n "Repeated_music")
58         (begin
59           (if (equal? (ly-get-mus-property 'type music) 'tremolo)
60               (shift-duration-log music (- (intlog2 (ly-get-mus-property 'repeat-count music))))
61               )
62           (ly-set-mus-property
63            music 'length Repeated_music::unfolded_music_length)
64           (ly-set-mus-property
65            music 'start-moment-function Repeated_music::first_start)
66           (ly-set-mus-property
67            music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
68
69     (if (pair? es)
70         (ly-set-mus-property
71          music 'elements
72          (map unfold-repeats es)))
73
74     (if (music? e)
75         (ly-set-mus-property
76          music 'element
77          (unfold-repeats e)))
78
79     music))
80
81
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84
85 (define  (pitchify-scripts music)
86   "Copy the pitch fields of the Note_requests into  Text_script_requests, to aid
87 Fingering_engraver."
88   (define (find-note musics)
89     (filter-list (lambda (m) (equal? (ly-music-name m) "Note_req")) musics)
90     )
91   (define (find-scripts musics)
92     (filter-list (lambda (m) (equal? (ly-music-name m) "Text_script_req")) musics))
93
94   (let* (
95          (e (ly-get-mus-property music 'element))
96          (es (ly-get-mus-property music 'elements))
97          (notes (find-note es))
98          (pitch (if (pair? notes) (ly-get-mus-property (car  notes) 'pitch) #f))
99          )
100
101     (if pitch
102         (map (lambda (x) (ly-set-mus-property x 'pitch pitch)) (find-scripts es))
103         )
104         
105     (if (pair? es)
106         (ly-set-mus-property
107          music 'elements
108          (map pitchify-scripts es)))
109
110     (if (music? e)
111         (ly-set-mus-property
112          music 'element
113          (pitchify-scripts e)))
114
115     music))
116
117
118 ;;;;;;;;;;;;;;;;;
119 ;;;;;;;;;;;;;;;;
120 ;;;;;;;;;;;;;;;;
121
122
123 (define (make-grob-property-set grob gprop val)
124   "Make a M-exp that sets GPROP to VAL in GROBS. Does a pop first, i.e.
125 this is not an override 
126 "
127   
128    (let* ((m (ly-make-music  "Music")))
129      (ly-set-mus-property m 'iterator-ctor Push_property_iterator::constructor)
130      (ly-set-mus-property m 'symbol grob)
131      (ly-set-mus-property m 'grob-property gprop)
132      (ly-set-mus-property m 'grob-value val)
133      (ly-set-mus-property m 'pop-first #t)
134                 
135      m
136    
137    ))
138    
139 (define (make-grob-property-revert grob gprop)
140   "Revert the grob property GPROP for GROB."
141    (let* ((m (ly-make-music  "Music")))
142      (ly-set-mus-property m 'iterator-ctor Pop_property_iterator::constructor)
143      (ly-set-mus-property m 'symbol grob)
144      (ly-set-mus-property m 'grob-property gprop)
145                 
146      m
147    
148    ))
149    
150 (define (make-voice-props-set n)
151   (make-sequential-music
152    (append
153       (map (lambda (x) (make-grob-property-set x 'direction
154                                                (if (odd? n) -1 1)))
155            '(Tie Slur Stem Dots))
156       (list (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2)))
157    )
158   ))
159
160 (define (make-voice-props-revert)
161   (make-sequential-music
162    (list
163       (make-grob-property-revert 'Tie 'direction)
164       (make-grob-property-revert 'Dots 'direction)
165       (make-grob-property-revert 'Stem 'direction)
166       (make-grob-property-revert 'Slur 'direction)          
167       (make-grob-property-revert 'NoteColumn 'horizontal-shift)
168    ))
169   )
170
171 (define (context-spec-music m context . rest)
172   "Add \context CONTEXT = foo to M. "
173   
174   (let* ((cm (ly-make-music "Context_specced_music")))
175     (ly-set-mus-property cm 'element m)
176     (ly-set-mus-property cm 'context-type context)
177     (if (and  (pair? rest) (string? (car rest)))
178         (ly-set-mus-property cm 'context-id (car rest))
179     )
180     cm
181   ))
182
183 (define (make-sequential-music elts)
184   (let*  ((m (ly-make-music "Sequential_music")))
185     (ly-set-mus-property m 'elements elts)
186     m
187   ))
188 (define (make-simultaneous-music elts)
189   (let*  ((m (ly-make-music "Simultaneous_music")))
190     (ly-set-mus-property m 'elements elts)
191     m
192     ))
193 (define (music-separator? m)
194   "Is M a separator."
195   (let* ((n (ly-get-mus-property m 'name )))
196     (and (symbol? n) (equal? 'separator n))
197   ))
198
199 (define (split-one sep?  l acc)
200   "Split off the first parts before separator and return both parts.
201
202 "
203   (if (null? l)
204       (cons acc '())
205       (if (sep? (car l))
206           (cons acc (cdr l))
207           (split-one sep? (cdr l) (cons (car l) acc))
208           )
209       ))
210
211 (define (split-list l sep?)
212   (if (null? l)
213       '()
214       (let* ((c (split-one sep? l '())))
215         (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
216         )
217       )
218   )
219
220 ;; test code
221 ; (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
222
223
224 ;;; splitting chords into voices.
225
226 (define (voicify-list lst number)
227    "Make a list of Musics.
228
229    voicify-list :: [ [Music ] ] -> number -> [Music]
230    LST is a list music-lists.
231 "
232
233    (if (null? lst) '()
234        (cons (context-spec-music
235               (make-sequential-music
236                (list
237                 (make-voice-props-set number)
238                 (make-simultaneous-music (car lst))))
239
240               "Voice"  (number->string number))
241               (voicify-list (cdr lst) (+ number 1))
242        ))
243    )
244
245 (define (voicify-chord ch)
246   "Split the parts of a chord into different Voices using separator"
247    (let* ((es (ly-get-mus-property ch 'elements)))
248
249
250      (ly-set-mus-property  ch 'elements
251        (voicify-list (split-list es music-separator?) 0))
252      ch
253    ))
254
255 (define (voicify-music m)
256    "Recursively split chords that are separated with \\ "
257    
258    (if (not (music? m))
259        (begin (display m)
260        (error "not music!"))
261        )
262    (let*
263        ((es (ly-get-mus-property m 'elements))
264         (e (ly-get-mus-property m 'element))
265         )
266      (if (pair? es)
267          (ly-set-mus-property m 'elements (map voicify-music es)))
268      (if (music? e)
269          (ly-set-mus-property m 'element  (voicify-music e)))
270      (if
271       (and (equal? (ly-music-name m) "Simultaneous_music")
272            (reduce (lambda (x y ) (or x y))     (map music-separator? es)))
273       (voicify-chord m)
274       )
275
276      m
277      ))
278
279 ;;;
280
281 ;;;;;;;;;;;;;;;;
282 ;;;;;;;;;;;;;;;;
283
284 (define (has-request-chord elts)
285   (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly-music-name x)
286                                                            "Request_chord")) elts)
287   ))
288
289 (define (ly-music-message music msg)
290   (let* (
291       (ip (ly-get-mus-property music 'origin))
292       )
293
294     (if (ly-input-location? ip)
295         (ly-input-message ip msg)
296         (ly-warn msg))
297   ))
298   
299 (define (check-start-chords music)
300   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called  from parser."
301   
302      (let*
303        ((es (ly-get-mus-property music 'elements))
304         (e (ly-get-mus-property music 'element))
305         (name (ly-music-name music)) 
306         )
307
308        (cond 
309          ((equal? name "Context_specced_music") #t)
310          ((equal? name "Simultaneous_music")
311
312           (if (has-request-chord es)
313               (ly-music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
314               (map check-start-chords es)))
315          
316          ((equal? name "Sequential_music")
317            (if (pair? es)
318                (check-start-chords (car es))))
319           (else (if (music? e) (check-start-chords e )))
320        
321        ))
322      music
323      )
324
325
326 (define toplevel-music-functions (list check-start-chords voicify-music))