]> git.donarmstrong.com Git - lilypond.git/blob - scm/music-functions.scm
5af90d22606c00012469bd194f4551a81b077257
[lilypond.git] / scm / music-functions.scm
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
3 (define-public (music-map function music)
4   "Apply @var{function} to @var{music} and all of the music it contains. "
5   (let* ((es (ly:get-mus-property music 'elements))
6          (e (ly:get-mus-property music 'element))
7          )
8
9     (ly:set-mus-property! music 'elements 
10         (map (lambda (y) (music-map  function y)) es))
11         (if (ly:music? e)
12             (ly:set-mus-property! music 'element (music-map function  e)))
13         (function music)
14         ))
15
16 (define-public (music-filter pred? music)
17   "Filter out music expressions that do not satisfy PRED."
18   
19   (define (inner-music-filter pred? music)
20     "Recursive function."
21     (let* ((es (ly:get-mus-property music 'elements))
22            (e (ly:get-mus-property music 'element))
23            (as (ly:get-mus-property music 'articulations))
24            (filtered-as (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) as)))
25            (filtered-e (if (ly:music? e)
26                            (inner-music-filter pred? e)
27                            e))
28            (filtered-es (filter ly:music? (map (lambda (y) (inner-music-filter pred? y)) es)))
29            )
30
31       (ly:set-mus-property! music 'element filtered-e)
32       (ly:set-mus-property! music 'elements filtered-es)
33       (ly:set-mus-property! music 'articulations filtered-as)
34
35       ;; if filtering emptied the expression, we remove it completely.
36       (if (or (pred? music)
37               (and (eq? filtered-es '()) (not (ly:music? e))
38                    (or (not (eq? es '()))
39                        (ly:music? e))))
40           (set! music '()))
41       
42       music))
43
44   (set! music (inner-music-filter pred? music))
45   (if (ly:music? music)
46       music
47       (make-music-by-name 'Music)       ;must return music.
48       ))
49
50 (define-public (remove-tag tag)
51   (lambda (mus)
52     (music-filter
53      (lambda (m)
54        (let* ((tags (ly:get-mus-property m 'tags))
55               (res (memq tag tags)))
56        res)) mus)))
57
58 (define-public (display-music music)
59   "Display music, not done with music-map for clarity of presentation."
60   (display music)
61   (display ": { ")
62   
63   (let* ((es (ly:get-mus-property music 'elements))
64          (e (ly:get-mus-property music 'element))
65          )
66
67     (display (ly:get-mutable-properties music))
68
69     (if (pair?  es)
70         (begin (display "\nElements: {\n")
71                (map display-music es)
72                (display "}\n")
73         ))
74     
75     
76     (if (ly:music? e)
77         (begin
78           (display "\nChild:")
79           (display-music e)
80           )
81         )
82     )
83   (display " }\n")
84   music
85   )
86
87
88
89
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91
92 (define (shift-one-duration-log music shift dot)
93   "  add SHIFT to ly:duration-log and optionally 
94   a dot to any note encountered. This scales the music up by a factor 
95   2^shift * (2 - (1/2)^dot)"
96
97   (let*
98       (
99        (d (ly:get-mus-property music 'duration))
100        )
101     (if (ly:duration? d)
102         (let* (
103                (cp (ly:duration-factor d))
104                (nd (ly:make-duration (+ shift (ly:duration-log d))
105                                      (+ dot (ly:duration-dot-count d))
106                                      (car cp)
107                                      (cdr cp)))
108                
109                )
110           (ly:set-mus-property! music 'duration nd)
111           ))
112     music))
113
114
115
116 (define-public (shift-duration-log music shift dot)
117   (music-map (lambda (x) (shift-one-duration-log x shift dot))
118              music))
119   
120
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;; clusters.
123
124 (define-public (note-to-cluster music)
125   "Replace NoteEvents by ClusterNoteEvents."
126   (if (eq? (ly:get-mus-property music 'name) 'NoteEvent)
127       (let* ((cn (make-music-by-name 'ClusterNoteEvent)))
128
129              (ly:set-mus-property! cn 'pitch (ly:get-mus-property music 'pitch))
130              (ly:set-mus-property! cn 'duration (ly:get-mus-property music 'duration))
131              cn)
132       music))
133
134 (define-public (notes-to-clusters music)
135   (music-map note-to-cluster music))
136
137 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 ;; repeats.
139
140 (define-public (unfold-repeats music)
141 "
142 This function replaces all repeats  with unfold repeats. It was 
143 written by Rune Zedeler. "
144   (let* ((es (ly:get-mus-property music 'elements))
145          (e (ly:get-mus-property music 'element))
146          (n  (ly:music-name music)))
147  
148     (if (equal? n "Repeated_music")
149         (begin
150           (if (equal?
151                (ly:get-mus-property music 'iterator-ctor)
152                Chord_tremolo_iterator::constructor)
153               (shift-duration-log music  (ly:intlog2 (ly:get-mus-property music 'repeat-count)) 0)
154               )
155           (ly:set-mus-property!
156            music 'length Repeated_music::unfolded_music_length)
157           (ly:set-mus-property!
158            music 'start-moment-function Repeated_music::first_start)
159           (ly:set-mus-property!
160            music 'iterator-ctor Unfolded_repeat_iterator::constructor)))
161
162     (if (pair? es)
163         (ly:set-mus-property!
164          music 'elements
165          (map unfold-repeats es)))
166
167     (if (ly:music? e)
168         (ly:set-mus-property!
169          music 'element
170          (unfold-repeats e)))
171
172     music))
173
174
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;; property setting music objs.
177
178 (define-public (make-grob-property-set grob gprop val)
179
180   "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
181 i.e.  this is not an override"
182   
183    (let* ((m (make-music-by-name  'OverrideProperty)))
184      (ly:set-mus-property! m 'symbol grob)
185      (ly:set-mus-property! m 'grob-property gprop)
186      (ly:set-mus-property! m 'grob-value val)
187      (ly:set-mus-property! m 'pop-first #t)
188                 
189      m
190    
191    ))
192
193
194 (define-public (make-grob-property-revert grob gprop)
195   "Revert the grob property GPROP for GROB."
196    (let* ((m (make-music-by-name  'OverrideProperty)))
197      (ly:set-mus-property! m 'symbol grob)
198      (ly:set-mus-property! m 'grob-property gprop)
199                 
200      m
201    
202    ))
203
204
205 (define-public (make-voice-props-set n)
206   (make-sequential-music
207    (append
208       (map (lambda (x) (make-grob-property-set x 'direction
209                                                (if (odd? n) -1 1)))
210            '(Tie Slur Script TextScript Stem Dots))
211       (list
212        (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
213        (make-grob-property-set 'MultiMeasureRest 'staff-position
214                                (if (odd? n) -4 4)
215                                )
216        
217        )
218    )
219   ))
220
221
222 (define-public (make-voice-props-revert)
223   (make-sequential-music
224    (list
225       (make-grob-property-revert 'Tie 'direction)
226       (make-grob-property-revert 'Dots 'direction)
227       (make-grob-property-revert 'Stem 'direction)
228       (make-grob-property-revert 'Slur 'direction)          
229       (make-grob-property-revert 'NoteColumn 'horizontal-shift)
230    ))
231   )
232
233
234 (define-public (context-spec-music m context . rest)
235   "Add \context CONTEXT = foo to M. "
236   
237   (let* ((cm (make-music-by-name 'ContextSpeccedMusic)))
238     (ly:set-mus-property! cm 'element m)
239     (ly:set-mus-property! cm 'context-type context)
240     (if (and  (pair? rest) (string? (car rest)))
241         (ly:set-mus-property! cm 'context-id (car rest))
242     )
243     cm
244   ))
245
246 (define-public (make-apply-context func)
247   (let*
248       ((m (make-music-by-name 'ApplyContext)))
249
250     (ly:set-mus-property! m 'procedure func)
251     m
252   ))
253
254 (define-public (make-sequential-music elts)
255   (let*  ((m (make-music-by-name 'SequentialMusic)))
256     (ly:set-mus-property! m 'elements elts)
257     m
258   ))
259
260 (define-public (make-simultaneous-music elts)
261   (let*  ((m (make-music-by-name 'SimultaneousMusic)))
262     (ly:set-mus-property! m 'elements elts)
263     m
264     ))
265
266 (define-public (make-event-chord elts)
267   (let*  ((m (make-music-by-name 'EventChord)))
268     (ly:set-mus-property! m 'elements elts)
269     m
270     ))
271
272
273 (define-public (make-nonevent-skip dur)
274   (let*  ((m (make-music-by-name 'NonEventSkip)))
275     (ly:set-mus-property! m 'duration dur)
276     m
277   ))
278
279 ;;;;;;;;;;;;;;;;
280
281 ;; mmrest
282 (define-public (make-multi-measure-rest duration location)
283   (let*
284       (
285        (start (make-music-by-name 'MultiMeasureRestEvent))
286        (stop  (make-music-by-name 'MultiMeasureRestEvent))
287        (skip ( make-music-by-name 'SkipEvent))
288        (ch (make-music-by-name 'BarCheck))
289        (ch2  (make-music-by-name 'BarCheck))
290        (seq  (make-music-by-name 'MultiMeasureRestMusicGroup))
291        )
292
293     (map (lambda (x) (ly:set-mus-property! x 'origin location))
294          (list start stop skip ch ch2 seq))
295     (ly:set-mus-property! start 'span-direction START)
296     (ly:set-mus-property! stop 'span-direction STOP)    
297     (ly:set-mus-property! skip 'duration duration)
298     (ly:set-mus-property! seq 'elements
299      (list
300       ch
301       (make-event-chord (list start))
302       (make-event-chord (list skip))
303       (make-event-chord (list stop))
304       ch2
305       ))
306
307     seq
308     ))
309
310 (define-public (glue-mm-rest-texts music)
311   "Check if we have R1*4-\markup { .. }, and if applicable convert to
312 a property set for MultiMeasureRestNumber."
313   
314   (define (script-to-mmrest-text script-music)
315     "Extract 'direction and 'text   from SCRIPT-MUSIC, and transform into property sets."
316     
317     (let*
318         (
319          (text (ly:get-mus-property script-music 'text))
320          (dir (ly:get-mus-property script-music 'direction))
321          (p (make-music-by-name 'MultiMeasureTextEvent))
322          )
323
324       (if (ly:dir? dir)
325           (ly:set-mus-property! p  'direction dir))
326       (ly:set-mus-property! p 'text text)
327       p
328     ))
329   
330   (if (eq? (ly:get-mus-property music 'name)  'MultiMeasureRestMusicGroup)
331       (let*
332           (
333            (text? (lambda (x) (memq 'script-event (ly:get-mus-property x 'types))))
334            (es (ly:get-mus-property  music 'elements))
335            (texts (map script-to-mmrest-text  (filter text? es)))
336            (others (remove text? es))
337            )
338         (if (pair? texts)
339             (ly:set-mus-property!
340              music 'elements
341              (cons (make-event-chord texts) others)
342             ))
343       ))
344   music
345   )
346
347
348 (define-public (make-property-set sym val)
349   (let*
350       (
351        (m (make-music-by-name 'PropertySet))
352        )
353     (ly:set-mus-property! m 'symbol sym)
354     (ly:set-mus-property! m 'value val)
355     m
356   ))
357
358
359
360 (define-public (make-ottava-set octavation)
361   (let*
362       ((m (make-music-by-name 'ApplyContext)))
363     
364   
365   (define (ottava-modify context)
366     "Either reset centralCPosition to the stored original,
367 or remember old centralCPosition, add OCTAVATION to centralCPosition,
368 and set OTTAVATION to `8va', or whatever appropriate."
369     (if (number? (ly:get-context-property  context 'centralCPosition))
370         
371         (if (= octavation 0)
372             (let*
373                 ((where (ly:context-property-where-defined context 'centralCPosition))
374                  (oc0 (ly:get-context-property context 'originalCentralCPosition)))
375
376               (ly:set-context-property! context 'centralCPosition oc0)
377               (ly:unset-context-property where 'originalCentralCPosition)
378               (ly:unset-context-property where 'ottavation))
379
380             (let*
381                 ((where (ly:context-property-where-defined context 'centralCPosition))
382                  (c0 (ly:get-context-property context 'centralCPosition))
383                  (new-c0 (+ c0 (* -7 octavation)))
384                  (string (cdr
385                           (assoc octavation '((2 . "15ma")
386                                               (1 . "8va")
387                                               (0 . #f)
388                                               (-1 . "8va bassa")
389                                               (-2 . "15ma bassa"))))))
390
391               (ly:set-context-property! context 'centralCPosition new-c0)
392               (ly:set-context-property! context 'originalCentralCPosition c0)
393               (ly:set-context-property! context 'ottavation string)
394               
395               ))))
396
397   (ly:set-mus-property! m 'procedure  ottava-modify)
398   (context-spec-music m 'Staff)
399   ))
400
401 (define-public (set-octavation ottavation)
402   (ly:export (make-ottava-set ottavation)))
403
404 (define-public (make-time-signature-set num den . rest)
405   " Set properties for time signature NUM/DEN.
406 Rest can contain a list of beat groupings 
407
408 "
409   
410   (let*
411       (
412        (set1 (make-property-set 'timeSignatureFraction (cons num den) ))
413        (beat (ly:make-moment 1 den))
414        (len  (ly:make-moment num den))
415        (set2 (make-property-set 'beatLength beat))
416        (set3 (make-property-set 'measureLength len))
417        (set4 (make-property-set 'beatGrouping (if (pair? rest)
418                                                   (car rest)
419                                                   '())))
420        (basic  (list set1 set2 set3 set4)))
421
422     (context-spec-music
423      (make-sequential-music basic) 'Timing)))
424
425 (define-public (set-time-signature num den . rest)
426   (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
427
428 (define-public (make-penalty-music pen)
429  (let
430      ((m (make-music-by-name 'BreakEvent)))
431    (ly:set-mus-property! m 'penalty pen)
432    m))
433
434 (define-public (make-articulation name)
435   (let* (
436          (m (make-music-by-name 'ArticulationEvent))
437       )
438       (ly:set-mus-property! m 'articulation-type name)
439       m
440   ))
441
442 (define-public (make-lyric-event string duration)
443   (let* ((m (make-music-by-name 'LyricEvent)))
444
445     (ly:set-mus-property! m 'duration duration)
446     (ly:set-mus-property! m 'text string)
447     m))
448
449 (define-public (make-span-event type spandir)
450   (let* (
451          (m (make-music-by-name  type))
452          )
453     (ly:set-mus-property! m 'span-direction spandir)
454     m
455     ))
456
457 (define-public (set-mus-properties! m alist)
458   "Set all of ALIST as properties of M." 
459   (if (pair? alist)
460       (begin
461         (ly:set-mus-property! m (caar alist) (cdar alist))
462         (set-mus-properties! m (cdr alist)))
463   ))
464
465
466
467 (define-public (music-separator? m)
468   "Is M a separator?"
469   (let* ((ts (ly:get-mus-property m 'types )))
470     (memq 'separator ts)
471   ))
472
473
474 ;;; splitting chords into voices.
475
476 (define (voicify-list lst number)
477    "Make a list of Musics.
478
479    voicify-list :: [ [Music ] ] -> number -> [Music]
480    LST is a list music-lists.
481 "
482
483    (if (null? lst) '()
484        (cons (context-spec-music
485               (make-sequential-music
486                (list
487                 (make-voice-props-set number)
488                 (make-simultaneous-music (car lst))))
489
490               'Voice  (number->string number))
491               (voicify-list (cdr lst) (+ number 1))
492        ))
493    )
494
495 (define (voicify-chord ch)
496   "Split the parts of a chord into different Voices using separator"
497    (let* ((es (ly:get-mus-property ch 'elements)))
498      
499      (ly:set-mus-property!  ch 'elements
500        (voicify-list (split-list es music-separator?) 0))
501      ch
502    ))
503
504 (define (voicify-music m)
505    "Recursively split chords that are separated with \\ "
506    
507    (if (not (ly:music? m))
508        (begin (display m)
509        (error "not music!"))
510        )
511    (let*
512        ((es (ly:get-mus-property m 'elements))
513         (e (ly:get-mus-property m 'element))
514         )
515      (if (pair? es)
516          (ly:set-mus-property! m 'elements (map voicify-music es)))
517      (if (ly:music? e)
518          (ly:set-mus-property! m 'element  (voicify-music e)))
519      (if
520       (and (equal? (ly:music-name m) "Simultaneous_music")
521            (reduce (lambda (x y ) (or x y)) #f (map music-separator? es)))
522       (voicify-chord m)
523       )
524
525      m
526      ))
527
528 (define-public (empty-music)
529   (ly:export (make-music-by-name 'Music))
530   )
531 ;;;
532
533 ; Make a function that checks score element for being of a specific type. 
534 (define-public (make-type-checker symbol)
535   (lambda (elt)
536     ;;(display  symbol)
537     ;;(eq? #t (ly:get-grob-property elt symbol))
538     (not (eq? #f (memq symbol (ly:get-grob-property elt 'interfaces))))))
539
540 (define-public ((outputproperty-compatibility func sym val) grob g-context ao-context)
541   (if (func grob)
542       (ly:set-grob-property! grob sym val)))
543
544
545 (define-public ((set-output-property grob-name symbol val)  grob grob-c context)
546    "Usage:
547
548 \\applyoutput #(set-output-property 'Clef 'extra-offset '(0 . 1))
549
550 "
551    
552    (let*
553        ((meta (ly:get-grob-property grob 'meta)))
554
555      (if (equal?  (cdr (assoc 'name meta)) grob-name)
556          (ly:set-grob-property! grob symbol val)
557          )))
558
559
560 ;;
561 (define-public (smart-bar-check n)
562   "Make  a bar check that checks for a specific bar number. 
563 "
564   (let*
565       (
566        (m (make-music-by-name 'ApplyContext))
567        )
568     
569     (define (checker tr)
570       (let* ((bn (ly:get-context-property tr 'currentBarNumber)))
571         (if (= bn  n)
572             #t
573             (error
574              (format "Bar check failed, we should have reached ~a, instead at ~a\n"
575                      n bn ))
576             )))
577
578     (ly:set-mus-property! m 'procedure checker)
579     m
580     ))
581
582 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
583 ;; warn for bare chords at start.
584
585 (define (has-request-chord elts)
586   (reduce (lambda (x y) (or x y)) #f (map (lambda (x) (equal? (ly:music-name x)
587                                                            "Request_chord")) elts)
588   ))
589
590 (define (ly:music-message music msg)
591   (let*
592       (
593       (ip (ly:get-mus-property music 'origin))
594       )
595
596     (if (ly:input-location? ip)
597         (ly:input-message ip msg)
598         (ly:warn msg))
599   ))
600   
601 (define (check-start-chords music)
602   "Check music expression for a Simultaneous_music containing notes\n(ie. Request_chords), without context specification. Called  from parser."
603   
604      (let*
605        ((es (ly:get-mus-property music 'elements))
606         (e (ly:get-mus-property music 'element))
607         (name (ly:music-name music)) 
608         )
609
610        (cond 
611          ((equal? name "Context_specced_music") #t)
612          ((equal? name "Simultaneous_music")
613
614           (if (has-request-chord es)
615               (ly:music-message music "Starting score with a chord.\nPlease insert an explicit \\context before chord")
616               (map check-start-chords es)))
617          
618          ((equal? name "Sequential_music")
619            (if (pair? es)
620                (check-start-chords (car es))))
621           (else (if (ly:music? e) (check-start-chords e )))
622        
623        ))
624      music
625      )
626
627
628
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;;
631 ;; setting stuff for grace context.
632 ;;
633
634 (define (vector-extend v x)
635   "Make a new vector consisting of V, with X added to the end."
636   (let*
637       ((n (vector-length v))
638        (nv (make-vector (+ n 1) '())))
639
640     
641     (vector-move-left! v 0 n nv 0)
642     (vector-set! nv n x)
643     nv))
644
645
646 (define (vector-map f v)
647   "Map  F over V. This function returns nothing."
648   (do
649       ((n (vector-length v))
650        (i 0 (+ i 1)))
651       ((>= i n))
652   
653     (f (vector-ref v i))))
654
655 (define (vector-reverse-map f v)
656   "Map  F over V, N to 0 order. This function returns nothing."
657   (do
658       ((i (- (vector-length v) 1) (- i 1)))
659       ((< i 0))
660   
661     (f (vector-ref v i))))
662
663 ;; TODO:  make a remove-grace-property too.
664 (define-public (add-grace-property context-name grob sym val)
665   "Set SYM=VAL for GROB in CONTEXT-NAME. "
666   (define (set-prop context)
667     (let*
668         ((where (ly:context-property-where-defined context 'graceSettings))
669          (current (ly:get-context-property where 'graceSettings))
670          (new-settings (vector-extend current (list context-name grob sym val)))
671          )
672       (ly:set-context-property! where 'graceSettings new-settings)))
673     
674     (ly:export (context-spec-music (make-apply-context set-prop) 'Voice)))
675
676
677 (define-public (set-start-grace-properties context)
678   (define (execute-1 x)
679     (let*
680         ((tr (ly:translator-find context (car x))))
681
682       (if (ly:context? tr)
683           (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x))
684           )))
685   
686   (let*
687       ((props (ly:get-context-property context 'graceSettings)))
688     (if (vector? props)
689         (vector-map execute-1 props))))
690
691 (define-public (set-stop-grace-properties context)
692   (define (execute-1 x)
693     (let*
694         ((tr (ly:translator-find context (car x))))
695       (if (ly:context? tr)
696           (ly:context-pushpop-property tr (cadr x) (caddr x))
697           )))
698   
699   (let*
700       ((props (ly:get-context-property context 'graceSettings)))
701     (if (vector? props)
702         (vector-reverse-map execute-1 props))))
703
704 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
705 ;; switch it on here, so parsing and init isn't checked (too slow!)
706 ;;
707 ;; automatic music transformations.
708
709 (define (switch-on-debugging m)
710   (set-debug-cell-accesses! 15000)
711   m)
712
713 (define-public toplevel-music-functions
714   (list check-start-chords
715         voicify-music
716         (lambda (x) (music-map glue-mm-rest-texts x))
717 ; switch-on-debugging
718         ))
719
720