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