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