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