]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* lily/context-specced-music-iterator.cc (class
[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 (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2)))
161    )
162   ))
163
164 (define-public (make-voice-props-revert)
165   (make-sequential-music
166    (list
167       (make-grob-property-revert 'Tie 'direction)
168       (make-grob-property-revert 'Dots 'direction)
169       (make-grob-property-revert 'Stem 'direction)
170       (make-grob-property-revert 'Slur 'direction)          
171       (make-grob-property-revert 'NoteColumn 'horizontal-shift)
172    ))
173   )
174
175 (define-public (context-spec-music m context . rest)
176   "Add \context CONTEXT = foo to M. "
177   
178   (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
179     (ly-set-mus-property! cm 'element m)
180     (ly-set-mus-property! cm 'context-type context)
181     (if (and  (pair? rest) (string? (car rest)))
182         (ly-set-mus-property! cm 'context-id (car rest))
183     )
184     cm
185   ))
186
187 (define-public (make-sequential-music elts)
188   (let*  ((m (make-music-by-name 'SequentialMusic)))
189     (ly-set-mus-property! m 'elements elts)
190     m
191   ))
192
193 (define-public (make-simultaneous-music elts)
194   (let*  ((m (make-music-by-name 'SimultaneousMusic)))
195     (ly-set-mus-property! m 'elements elts)
196     m
197     ))
198
199 (define-public (set-mus-properties! m alist)
200   (if (pair? alist)
201       (begin
202         (ly-set-mus-property! m (caar alist) (cdar alist))
203         (set-mus-properties! m (cdr alist)))
204   ))
205
206 (define-public (music-separator? m)
207   "Is M a separator."
208   (let* ((ts (ly-get-mus-property m 'types )))
209     (memq 'separator ts)
210   ))
211
212 (define (split-one sep?  l acc)
213   "Split off the first parts before separator and return both parts.
214
215 "
216   (if (null? l)
217       (cons acc '())
218       (if (sep? (car l))
219           (cons acc (cdr l))
220           (split-one sep? (cdr l) (cons (car l) acc))
221           )
222       ))
223
224 (define-public (split-list l sep?)
225   "
226
227 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
228 =>
229  ...
230
231 "
232   (if (null? l)
233       '()
234       (let* ((c (split-one sep? l '())))
235         (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
236         )
237       )
238   )
239
240 ;;; splitting chords into voices.
241
242 (define (voicify-list lst number)
243    "Make a list of Musics.
244
245    voicify-list :: [ [Music ] ] -> number -> [Music]
246    LST is a list music-lists.
247 "
248
249    (if (null? lst) '()
250        (cons (context-spec-music
251               (make-sequential-music
252                (list
253                 (make-voice-props-set number)
254                 (make-simultaneous-music (car lst))))
255
256               "Voice"  (number->string number))
257               (voicify-list (cdr lst) (+ number 1))
258        ))
259    )
260
261 (define (voicify-chord ch)
262   "Split the parts of a chord into different Voices using separator"
263    (let* ((es (ly-get-mus-property ch 'elements)))
264
265
266      (ly-set-mus-property!  ch 'elements
267        (voicify-list (split-list es music-separator?) 0))
268      ch
269    ))
270
271 (define (voicify-music m)
272    "Recursively split chords that are separated with \\ "
273    
274    (if (not (music? m))
275        (begin (display m)
276        (error "not music!"))
277        )
278    (let*
279        ((es (ly-get-mus-property m 'elements))
280         (e (ly-get-mus-property m 'element))
281         )
282      (if (pair? es)
283          (ly-set-mus-property! m 'elements (map voicify-music es)))
284      (if (music? e)
285          (ly-set-mus-property! m 'element  (voicify-music e)))
286      (if
287       (and (equal? (ly-music-name m) "Simultaneous_music")
288            (reduce (lambda (x y ) (or x y))     (map music-separator? es)))
289       (voicify-chord m)
290       )
291
292      m
293      ))
294
295 (define-public (empty-music)
296   (ly-id (make-music-by-name 'Music))
297   )
298 ;;;
299
300 ; Make a function that checks score element for being of a specific type. 
301 (define-public (make-type-checker symbol)
302   (lambda (elt)
303     ;;(display  symbol)
304     ;;(eq? #t (ly-get-grob-property elt symbol))
305     (not (eq? #f (memq symbol (ly-get-grob-property elt 'interfaces))))))
306
307
308 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
309 ;; warn for bare chords at start.
310
311 (define (has-request-chord elts)
312   (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly-music-name x)
313                                                            "Request_chord")) elts)
314   ))
315
316 (define (ly-music-message music msg)
317   (let* (
318       (ip (ly-get-mus-property music 'origin))
319       )
320
321     (if (ly-input-location? ip)
322         (ly-input-message ip msg)
323         (ly-warn msg))
324   ))
325   
326 (define (check-start-chords music)
327   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called  from parser."
328   
329      (let*
330        ((es (ly-get-mus-property music 'elements))
331         (e (ly-get-mus-property music 'element))
332         (name (ly-music-name music)) 
333         )
334
335        (cond 
336          ((equal? name "Context_specced_music") #t)
337          ((equal? name "Simultaneous_music")
338
339           (if (has-request-chord es)
340               (ly-music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
341               (map check-start-chords es)))
342          
343          ((equal? name "Sequential_music")
344            (if (pair? es)
345                (check-start-chords (car es))))
346           (else (if (music? e) (check-start-chords e )))
347        
348        ))
349      music
350      )
351
352
353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
354 ;; switch it on here, so parsing and init isn't checked (too slow!)
355
356 ;; automatic music transformations.
357
358 (define (switch-on-debugging m)
359   (set-debug-cell-accesses! 15000)
360   m
361   )
362
363 (define-public toplevel-music-functions
364   (list check-start-chords
365         voicify-music
366
367 ; switch-on-debugging
368         ))
369