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