]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* scm/music-functions.scm (glue-mm-rest-texts): automatically
[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 (define-public (music-map function music)
16   "Apply @var{function} to @var{music} and all of the music it contains. "
17   (let* ((es (ly:get-mus-property music 'elements))
18          (e (ly:get-mus-property music 'element))
19          )
20
21     (ly:set-mus-property! music 'elements 
22         (map (lambda (y) (music-map  function y)) es))
23         (if (ly:music? e)
24             (ly:set-mus-property! music 'element (music-map function  e)))
25         (function music)
26         ))
27
28 (define-public (display-music music)
29   "Display music, not done with music-map for clarity of presentation."
30   (display music)
31   (display ": { ")
32   
33   (let* ((es (ly:get-mus-property music 'elements))
34          (e (ly:get-mus-property music 'element))
35          )
36
37     (display (ly:get-mutable-properties music))
38
39     (if (pair?  es)
40         (begin (display "\nElements: {\n")
41                (map display-music es)
42                (display "}\n")
43         ))
44     
45     
46     (if (ly:music? e)
47         (begin
48           (display "\nChild:")
49           (display-music e)
50           )
51         )
52     )
53   (display " }\n")
54   music
55   )
56
57
58
59
60   
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62
63 (define (shift-one-duration-log music shift dot)
64   "  add SHIFT to ly:duration-log and optionally 
65   a dot to any note encountered. This scales the music up by a factor 
66   2^shift * (2 - (1/2)^dot)"
67
68   (let*
69       (
70        (d (ly:get-mus-property music 'duration))
71        )
72     (if (ly:duration? d)
73         (let* (
74                (cp (ly:duration-factor d))
75                (nd (ly:make-duration (+ shift (ly:duration-log d))
76                                      (+ dot (ly:duration-dot-count d))
77                                      (car cp)
78                                      (cdr cp)))
79                
80                )
81           (ly:set-mus-property! music 'duration nd)
82           ))
83     music))
84
85
86
87 (define-public (shift-duration-log music shift dot)
88   (music-map (lambda (x) (shift-one-duration-log x shift dot))
89              music))
90   
91
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;; repeats.
94
95 (define-public (unfold-repeats music)
96 "
97 This function replaces all repeats  with unfold repeats. It was 
98 written by Rune Zedeler. "
99   (let* ((es (ly:get-mus-property music 'elements))
100          (e (ly:get-mus-property music 'element))
101          (n  (ly:music-name music)))
102  
103     (if (equal? n "Repeated_music")
104         (begin
105           (if (equal?
106                (ly:get-mus-property music 'iterator-ctor)
107                Chord_tremolo_iterator::constructor)
108               (shift-duration-log music  (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
109               )
110           (ly:set-mus-property!
111            music 'length Repeated_music::unfolded_music_length)
112           (ly:set-mus-property!
113            music 'start-moment-function Repeated_music::first_start)
114           (ly:set-mus-property!
115            music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
116
117     (if (pair? es)
118         (ly:set-mus-property!
119          music 'elements
120          (map unfold-repeats es)))
121
122     (if (ly:music? e)
123         (ly:set-mus-property!
124          music 'element
125          (unfold-repeats e)))
126
127     music))
128
129
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; property setting music objs.
132
133 (define-public (make-grob-property-set grob gprop val)
134
135   "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
136 i.e.  this is not an override"
137   
138    (let* ((m (make-music-by-name  'OverrideProperty)))
139      (ly:set-mus-property! m 'symbol grob)
140      (ly:set-mus-property! m 'grob-property gprop)
141      (ly:set-mus-property! m 'grob-value val)
142      (ly:set-mus-property! m 'pop-first #t)
143                 
144      m
145    
146    ))
147
148
149 (define-public (make-grob-property-revert grob gprop)
150   "Revert the grob property GPROP for GROB."
151    (let* ((m (make-music-by-name  'OverrideProperty)))
152      (ly:set-mus-property! m 'symbol grob)
153      (ly:set-mus-property! m 'grob-property gprop)
154                 
155      m
156    
157    ))
158
159
160 (define-public (make-voice-props-set n)
161   (make-sequential-music
162    (append
163       (map (lambda (x) (make-grob-property-set x 'direction
164                                                (if (odd? n) -1 1)))
165            '(Tie Slur Stem Dots))
166       (list
167        (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
168        (make-grob-property-set 'MultiMeasureRest 'staff-position
169                                (if (odd? n) -4 4)
170                                )
171        
172        )
173    )
174   ))
175
176
177 (define-public (make-voice-props-revert)
178   (make-sequential-music
179    (list
180       (make-grob-property-revert 'Tie 'direction)
181       (make-grob-property-revert 'Dots 'direction)
182       (make-grob-property-revert 'Stem 'direction)
183       (make-grob-property-revert 'Slur 'direction)          
184       (make-grob-property-revert 'NoteColumn 'horizontal-shift)
185    ))
186   )
187
188
189 (define-public (context-spec-music m context . rest)
190   "Add \context CONTEXT = foo to M. "
191   
192   (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
193     (ly:set-mus-property! cm 'element m)
194     (ly:set-mus-property! cm 'context-type context)
195     (if (and  (pair? rest) (string? (car rest)))
196         (ly:set-mus-property! cm 'context-id (car rest))
197     )
198     cm
199   ))
200
201 (define-public (make-sequential-music elts)
202   (let*  ((m (make-music-by-name 'SequentialMusic)))
203     (ly:set-mus-property! m 'elements elts)
204     m
205   ))
206
207 (define-public (make-simultaneous-music elts)
208   (let*  ((m (make-music-by-name 'SimultaneousMusic)))
209     (ly:set-mus-property! m 'elements elts)
210     m
211     ))
212
213 (define-public (make-event-chord elts)
214   (let*  ((m (make-music-by-name 'EventChord)))
215     (ly:set-mus-property! m 'elements elts)
216     m
217     ))
218
219
220 (define-public (make-multi-measure-rest duration location)
221   (let*
222       (
223        (start (make-music-by-name 'MultiMeasureRestEvent))
224        (stop  (make-music-by-name 'MultiMeasureRestEvent))
225        (skip ( make-music-by-name 'SkipEvent))
226        (ch (make-music-by-name 'BarCheck))
227        (ch2  (make-music-by-name 'BarCheck))
228        (seq  (make-music-by-name 'MultiMeasureRestMusicGroup))
229        )
230
231     (ly:set-mus-property! start 'span-direction START)
232     (ly:set-mus-property! stop 'span-direction STOP)    
233     (ly:set-mus-property! skip 'duration duration)
234     (ly:set-mus-property! seq 'elements
235      (list
236       ch
237       (make-event-chord (list start))
238       (make-event-chord (list skip))
239       (make-event-chord (list stop))
240       ch2
241       ))
242
243     seq
244     ))
245
246 (define-public (glue-mm-rest-texts music)
247   "Check if we have R1*4-\markup { .. }, and if applicable convert to
248 a property set for MultiMeasureRestNumber."
249   
250   (define (script-to-mmrest-text script-music)
251     "Extract 'direction and 'text   from SCRIPT-MUSIC, and transform into property sets."
252     
253     (let*
254         (
255          (text (ly:get-mus-property script-music 'text))
256          (dir (ly:get-mus-property script-music 'direction))
257          (p (make-grob-property-set 'MultiMeasureRestNumber 'text text))
258          (d (if (ly:dir? dir)
259                 (make-grob-property-set 'MultiMeasureRestNumber 'direction dir)
260                 #f))
261          (l (list p))
262          )
263       (ly:set-mus-property! p 'once #t)
264       (if d
265           (begin
266             (ly:set-mus-property! d 'once #t)
267             (set! l (cons d l))))
268       
269       (context-spec-music (make-sequential-music l) "Voice")
270     ))
271   
272   (if (eq? (ly:get-mus-property music 'name)  'MultiMeasureRestMusicGroup)
273       (let*
274           (
275            (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
276            (es (ly:get-mus-property  music 'elements))
277            (texts (filter-list text? es))
278            (others (filter-out-list text? es))
279            )
280         (if (pair? texts)
281             (ly:set-mus-property!
282              music 'elements
283              (cons (script-to-mmrest-text (car texts))
284                    others))
285             )
286       ))
287   music
288   )
289
290
291 (define-public (make-property-set sym val)
292   (let*
293       (
294        (m (make-music-by-name 'PropertySet))
295        )
296     (ly:set-mus-property! m 'symbol sym)
297     (ly:set-mus-property! m 'value val)
298     m
299   ))
300
301 (define-public (make-time-signature-set num den . rest)
302   " Set properties for time signature NUM/DEN.
303 Rest can contain a list of beat groupings 
304
305 "
306   
307   (let*
308       (
309        (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
310        (beat (ly:make-moment 1 den))
311        (len  (ly:make-moment num den))
312        (set2 (make-property-set 'beatLength beat))
313        (set3 (make-property-set 'measureLength len))
314        (set4 (make-property-set 'beatGrouping (if (pair? rest)
315                                                   (car rest)
316                                                   '())))
317        (basic  (list set1 set2 set3 set4))
318        
319        )
320
321     (context-spec-music
322      (make-sequential-music basic) "Timing")))
323
324 (define-public (set-time-signature num den . rest)
325   (ly:export (apply make-time-signature-set `(,num ,den . ,rest)))
326   )
327
328 (define-public (make-penalty-music pen)
329  (let
330      ((m (make-music-by-name 'BreakEvent)))
331    (ly:set-mus-property! m 'penalty pen)
332    m))
333
334 (define-public (make-articulation name)
335   (let* (
336          (m (make-music-by-name 'ArticulationEvent))
337       )
338       (ly:set-mus-property! m 'articulation-type name)
339       m
340   ))
341
342 (define-public (make-span-event type spandir)
343   (let* (
344          (m (make-music-by-name  type))
345          )
346     (ly:set-mus-property! m 'span-direction spandir)
347     m
348     ))
349
350 (define-public (set-mus-properties! m alist)
351   "Set all of ALIST as properties of M." 
352   (if (pair? alist)
353       (begin
354         (ly:set-mus-property! m (caar alist) (cdar alist))
355         (set-mus-properties! m (cdr alist)))
356   ))
357
358 (define-public (music-separator? m)
359   "Is M a separator?"
360   (let* ((ts (ly:get-mus-property m 'types )))
361     (memq 'separator ts)
362   ))
363
364
365 ;;; splitting chords into voices.
366
367 (define (voicify-list lst number)
368    "Make a list of Musics.
369
370    voicify-list :: [ [Music ] ] -> number -> [Music]
371    LST is a list music-lists.
372 "
373
374    (if (null? lst) '()
375        (cons (context-spec-music
376               (make-sequential-music
377                (list
378                 (make-voice-props-set number)
379                 (make-simultaneous-music (car lst))))
380
381               "Voice"  (number->string number))
382               (voicify-list (cdr lst) (+ number 1))
383        ))
384    )
385
386 (define (voicify-chord ch)
387   "Split the parts of a chord into different Voices using separator"
388    (let* ((es (ly:get-mus-property ch 'elements)))
389
390
391      (ly:set-mus-property!  ch 'elements
392        (voicify-list (split-list es music-separator?) 0))
393      ch
394    ))
395
396 (define (voicify-music m)
397    "Recursively split chords that are separated with \\ "
398    
399    (if (not (ly:music? m))
400        (begin (display m)
401        (error "not music!"))
402        )
403    (let*
404        ((es (ly:get-mus-property m 'elements))
405         (e (ly:get-mus-property m 'element))
406         )
407      (if (pair? es)
408          (ly:set-mus-property! m 'elements (map voicify-music es)))
409      (if (ly:music? e)
410          (ly:set-mus-property! m 'element  (voicify-music e)))
411      (if
412       (and (equal? (ly:music-name m) "Simultaneous_music")
413            (reduce (lambda (x y ) (or x y))     (map music-separator? es)))
414       (voicify-chord m)
415       )
416
417      m
418      ))
419
420 (define-public (empty-music)
421   (ly:export (make-music-by-name 'Music))
422   )
423 ;;;
424
425 ; Make a function that checks score element for being of a specific type. 
426 (define-public (make-type-checker symbol)
427   (lambda (elt)
428     ;;(display  symbol)
429     ;;(eq? #t (ly:get-grob-property elt symbol))
430     (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
431
432
433 ;;
434 (define-public (smart-bar-check n)
435   "Make  a bar check that checks for a specific bar number. 
436 "
437   (let*
438       (
439        (m (make-music-by-name 'ApplyContext))
440        )
441     
442     (define (checker tr)
443       (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
444         (if (= bn  n)
445             #t
446             (error
447              (format "Bar check failed, we should have reached ~a, instead at ~a\n"
448                      n bn ))
449             )))
450
451     (ly:set-mus-property! m 'procedure checker)
452     m
453     ))
454
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 ;; warn for bare chords at start.
457
458 (define (has-request-chord elts)
459   (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly:music-name x)
460                                                            "Request_chord")) elts)
461   ))
462
463 (define (ly:music-message music msg)
464   (let*
465       (
466       (ip (ly:get-mus-property music 'origin))
467       )
468
469     (if (ly:input-location? ip)
470         (ly:input-message ip msg)
471         (ly:warn msg))
472   ))
473   
474 (define (check-start-chords music)
475   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called  from parser."
476   
477      (let*
478        ((es (ly:get-mus-property music 'elements))
479         (e (ly:get-mus-property music 'element))
480         (name (ly:music-name music)) 
481         )
482
483        (cond 
484          ((equal? name "Context_specced_music") #t)
485          ((equal? name "Simultaneous_music")
486
487           (if (has-request-chord es)
488               (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
489               (map check-start-chords es)))
490          
491          ((equal? name "Sequential_music")
492            (if (pair? es)
493                (check-start-chords (car es))))
494           (else (if (ly:music? e) (check-start-chords e )))
495        
496        ))
497      music
498      )
499
500 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
501 ;; switch it on here, so parsing and init isn't checked (too slow!)
502
503 ;; automatic music transformations.
504
505 (define (switch-on-debugging m)
506   (set-debug-cell-accesses! 15000)
507   m
508   )
509
510 (define-public toplevel-music-functions
511   (list check-start-chords
512         voicify-music
513         (lambda (x) (music-map glue-mm-rest-texts x))
514 ; switch-on-debugging
515         ))
516
517