]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
* scm/music-functions.scm (glue-mm-rest-texts): use
[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     (map (lambda (x) (ly:set-mus-property! x 'origin location))
232          (list start stop skip ch ch2 seq))
233     (ly:set-mus-property! start 'span-direction START)
234     (ly:set-mus-property! stop 'span-direction STOP)    
235     (ly:set-mus-property! skip 'duration duration)
236     (ly:set-mus-property! seq 'elements
237      (list
238       ch
239       (make-event-chord (list start))
240       (make-event-chord (list skip))
241       (make-event-chord (list stop))
242       ch2
243       ))
244
245     seq
246     ))
247
248 (define-public (glue-mm-rest-texts music)
249   "Check if we have R1*4-\markup { .. }, and if applicable convert to
250 a property set for MultiMeasureRestNumber."
251   
252   (define (script-to-mmrest-text script-music)
253     "Extract 'direction and 'text   from SCRIPT-MUSIC, and transform into property sets."
254     
255     (let*
256         (
257          (text (ly:get-mus-property script-music 'text))
258          (dir (ly:get-mus-property script-music 'direction))
259          (p (make-music-by-name 'MultiMeasureTextEvent))
260          )
261
262       (if (ly:dir? dir)
263           (ly:set-mus-property! p  'direction dir))
264       (ly:set-mus-property! p 'text text)
265       p
266     ))
267   
268   (if (eq? (ly:get-mus-property music 'name)  'MultiMeasureRestMusicGroup)
269       (let*
270           (
271            (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
272            (es (ly:get-mus-property  music 'elements))
273            (texts (map script-to-mmrest-text  (filter-list text? es)))
274            (others (filter-out-list text? es))
275            )
276         (if (pair? texts)
277             (ly:set-mus-property!
278              music 'elements
279              (cons (make-event-chord texts) others)
280             ))
281       ))
282   music
283   )
284
285
286 (define-public (make-property-set sym val)
287   (let*
288       (
289        (m (make-music-by-name 'PropertySet))
290        )
291     (ly:set-mus-property! m 'symbol sym)
292     (ly:set-mus-property! m 'value val)
293     m
294   ))
295
296 (define-public (make-time-signature-set num den . rest)
297   " Set properties for time signature NUM/DEN.
298 Rest can contain a list of beat groupings 
299
300 "
301   
302   (let*
303       (
304        (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
305        (beat (ly:make-moment 1 den))
306        (len  (ly:make-moment num den))
307        (set2 (make-property-set 'beatLength beat))
308        (set3 (make-property-set 'measureLength len))
309        (set4 (make-property-set 'beatGrouping (if (pair? rest)
310                                                   (car rest)
311                                                   '())))
312        (basic  (list set1 set2 set3 set4))
313        
314        )
315
316     (context-spec-music
317      (make-sequential-music basic) "Timing")))
318
319 (define-public (set-time-signature num den . rest)
320   (ly:export (apply make-time-signature-set `(,num ,den . ,rest)))
321   )
322
323 (define-public (make-penalty-music pen)
324  (let
325      ((m (make-music-by-name 'BreakEvent)))
326    (ly:set-mus-property! m 'penalty pen)
327    m))
328
329 (define-public (make-articulation name)
330   (let* (
331          (m (make-music-by-name 'ArticulationEvent))
332       )
333       (ly:set-mus-property! m 'articulation-type name)
334       m
335   ))
336
337 (define-public (make-span-event type spandir)
338   (let* (
339          (m (make-music-by-name  type))
340          )
341     (ly:set-mus-property! m 'span-direction spandir)
342     m
343     ))
344
345 (define-public (set-mus-properties! m alist)
346   "Set all of ALIST as properties of M." 
347   (if (pair? alist)
348       (begin
349         (ly:set-mus-property! m (caar alist) (cdar alist))
350         (set-mus-properties! m (cdr alist)))
351   ))
352
353 (define-public (music-separator? m)
354   "Is M a separator?"
355   (let* ((ts (ly:get-mus-property m 'types )))
356     (memq 'separator ts)
357   ))
358
359
360 ;;; splitting chords into voices.
361
362 (define (voicify-list lst number)
363    "Make a list of Musics.
364
365    voicify-list :: [ [Music ] ] -> number -> [Music]
366    LST is a list music-lists.
367 "
368
369    (if (null? lst) '()
370        (cons (context-spec-music
371               (make-sequential-music
372                (list
373                 (make-voice-props-set number)
374                 (make-simultaneous-music (car lst))))
375
376               "Voice"  (number->string number))
377               (voicify-list (cdr lst) (+ number 1))
378        ))
379    )
380
381 (define (voicify-chord ch)
382   "Split the parts of a chord into different Voices using separator"
383    (let* ((es (ly:get-mus-property ch 'elements)))
384
385
386      (ly:set-mus-property!  ch 'elements
387        (voicify-list (split-list es music-separator?) 0))
388      ch
389    ))
390
391 (define (voicify-music m)
392    "Recursively split chords that are separated with \\ "
393    
394    (if (not (ly:music? m))
395        (begin (display m)
396        (error "not music!"))
397        )
398    (let*
399        ((es (ly:get-mus-property m 'elements))
400         (e (ly:get-mus-property m 'element))
401         )
402      (if (pair? es)
403          (ly:set-mus-property! m 'elements (map voicify-music es)))
404      (if (ly:music? e)
405          (ly:set-mus-property! m 'element  (voicify-music e)))
406      (if
407       (and (equal? (ly:music-name m) "Simultaneous_music")
408            (reduce (lambda (x y ) (or x y))     (map music-separator? es)))
409       (voicify-chord m)
410       )
411
412      m
413      ))
414
415 (define-public (empty-music)
416   (ly:export (make-music-by-name 'Music))
417   )
418 ;;;
419
420 ; Make a function that checks score element for being of a specific type. 
421 (define-public (make-type-checker symbol)
422   (lambda (elt)
423     ;;(display  symbol)
424     ;;(eq? #t (ly:get-grob-property elt symbol))
425     (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
426
427
428 ;;
429 (define-public (smart-bar-check n)
430   "Make  a bar check that checks for a specific bar number. 
431 "
432   (let*
433       (
434        (m (make-music-by-name 'ApplyContext))
435        )
436     
437     (define (checker tr)
438       (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
439         (if (= bn  n)
440             #t
441             (error
442              (format "Bar check failed, we should have reached ~a, instead at ~a\n"
443                      n bn ))
444             )))
445
446     (ly:set-mus-property! m 'procedure checker)
447     m
448     ))
449
450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 ;; warn for bare chords at start.
452
453 (define (has-request-chord elts)
454   (reduce (lambda (x y) (or x y)) (map (lambda (x) (equal? (ly:music-name x)
455                                                            "Request_chord")) elts)
456   ))
457
458 (define (ly:music-message music msg)
459   (let*
460       (
461       (ip (ly:get-mus-property music 'origin))
462       )
463
464     (if (ly:input-location? ip)
465         (ly:input-message ip msg)
466         (ly:warn msg))
467   ))
468   
469 (define (check-start-chords music)
470   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called  from parser."
471   
472      (let*
473        ((es (ly:get-mus-property music 'elements))
474         (e (ly:get-mus-property music 'element))
475         (name (ly:music-name music)) 
476         )
477
478        (cond 
479          ((equal? name "Context_specced_music") #t)
480          ((equal? name "Simultaneous_music")
481
482           (if (has-request-chord es)
483               (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
484               (map check-start-chords es)))
485          
486          ((equal? name "Sequential_music")
487            (if (pair? es)
488                (check-start-chords (car es))))
489           (else (if (ly:music? e) (check-start-chords e )))
490        
491        ))
492      music
493      )
494
495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496 ;; switch it on here, so parsing and init isn't checked (too slow!)
497
498 ;; automatic music transformations.
499
500 (define (switch-on-debugging m)
501   (set-debug-cell-accesses! 15000)
502   m
503   )
504
505 (define-public toplevel-music-functions
506   (list check-start-chords
507         voicify-music
508         (lambda (x) (music-map glue-mm-rest-texts x))
509 ; switch-on-debugging
510         ))
511
512