]> git.donarmstrong.com Git - lilypond.git/blob - ly/music-functions-init.ly
new function featherDurations
[lilypond.git] / ly / music-functions-init.ly
1 % -*-Scheme-*-
2
3 \version "2.7.39"
4
5 %% need SRFI-1 filter 
6
7 #(use-modules (srfi srfi-1))  
8
9
10 tweak = #(define-music-function (parser location sym val arg)
11            (symbol? scheme? ly:music?)
12
13            "Add @code{sym . val} to the @code{tweaks} property of @var{arg}."
14
15            
16            (set!
17             (ly:music-property arg 'tweaks)
18             (acons sym val
19                    (ly:music-property arg 'tweaks)))
20            arg)
21
22 tag = #(define-music-function (parser location tag arg)
23    (symbol? ly:music?)
24
25    "Add @var{tag} to the @code{tags} property of @var{arg}."
26
27    (set!
28     (ly:music-property arg 'tags)
29     (cons tag
30           (ly:music-property arg 'tags)))
31    arg)
32
33 clef =
34 #(define-music-function (parser location type)
35    (string?)
36    
37    "Set the current clef."
38
39    (make-clef-set type))
40
41 bar =
42 #(define-music-function (parser location type)
43    (string?)
44    (context-spec-music
45     (make-property-set 'whichBar type)
46     'Timing))
47
48 applyMusic =
49 #(define-music-function (parser location func music) (procedure? ly:music?)
50                (func music))
51
52 oldaddlyrics =
53 #(define-music-function (parser location music lyrics) (ly:music? ly:music?)
54
55               (make-music 'OldLyricCombineMusic 
56                           'origin location
57                           'elements (list music lyrics)))
58
59 grace =
60 #(def-grace-function startGraceMusic stopGraceMusic)
61
62 acciaccatura =
63 #(def-grace-function startAcciaccaturaMusic stopAcciaccaturaMusic)
64 appoggiatura =
65 #(def-grace-function startAppoggiaturaMusic stopAppoggiaturaMusic)
66
67 partcombine =
68 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
69                 (make-part-combine-music (list part1 part2)))
70
71 autochange =
72 #(define-music-function (parser location music) (ly:music?)
73                (make-autochange-music music))
74
75 applyContext =
76 #(define-music-function (parser location proc) (procedure?)
77                  (make-music 'ApplyContext 
78                    'origin location
79                    'procedure proc))
80
81 shiftDurations =
82 #(define-music-function (parser location dur dots arg) (integer? integer? ly:music?)
83    ""
84
85    
86    (music-map
87     (lambda (x)
88       (shift-one-duration-log x dur dots)) arg))
89
90 musicMap =
91 #(define-music-function (parser location proc mus) (procedure? ly:music?)
92              (music-map proc mus))
93
94 displayMusic =
95 #(define-music-function (parser location music) (ly:music?)
96                  (display-scheme-music music)
97                  music)
98
99 %% FIXME: guile-1.7 required?
100 %#(use-modules (scm display-lily))invalid module name for use-syntax ((srfi srfi-39))
101
102 #(use-modules (scm display-lily))
103 #(display-lily-init parser)
104 displayLilyMusic =
105 #(define-music-function (parser location music) (ly:music?)
106    (display-lily-music music)
107    music)
108
109 applyOutput =
110 #(define-music-function (parser location proc) (procedure?)
111                 (make-music 'ApplyOutputEvent 
112                   'origin location
113                   'procedure proc))
114
115 overrideProperty =
116 #(define-music-function (parser location name property value)
117    (string? symbol? scheme?)
118
119
120    "Set @var{property} to @var{value} in all grobs named @var{name}.
121 The @var{name} argument is a string of the form @code{\"Context.GrobName\"}
122 or @code{\"GrobName\"}"
123
124    (let*
125        ((name-components (string-split name #\.))
126         (context-name 'Bottom)
127         (grob-name #f))
128
129      (if (> 2 (length name-components))
130          (set! grob-name (string->symbol (car name-components)))
131          (begin
132            (set! grob-name (string->symbol (list-ref name-components 1)))
133            (set! context-name (string->symbol (list-ref name-components 0)))))
134
135      (context-spec-music
136       (make-music 'ApplyOutputEvent
137                   'origin location
138                   'procedure
139                   (lambda (grob orig-context context)
140                     (if (equal?
141                          (cdr (assoc 'name (ly:grob-property grob 'meta)))
142                          grob-name)
143                         (set! (ly:grob-property grob property) value)
144                         )))
145
146       context-name)))
147
148 breathe =
149 #(define-music-function (parser location) ()
150             (make-music 'EventChord 
151               'origin location
152               'elements (list (make-music 'BreathingSignEvent))))
153
154
155 unfoldRepeats =
156 #(define-music-function (parser location music) (ly:music?)
157                   (unfold-repeats music))
158
159 compressMusic =
160 #(define-music-function
161                   (parser location fraction music) (number-pair? ly:music?)
162                   (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction))))
163
164 makeClusters =
165 #(define-music-function
166                 (parser location arg) (ly:music?)
167                 (music-map note-to-cluster arg))
168
169
170 removeWithTag = 
171 #(define-music-function
172   (parser location tag music) (symbol? ly:music?)
173   (music-filter
174    (lambda (m)
175     (let* ((tags (ly:music-property m 'tags))
176            (res (memq tag tags)))
177      (not res)))
178  music))
179               
180 keepWithTag =
181 #(define-music-function
182   (parser location tag music) (symbol? ly:music?)
183   (music-filter
184    (lambda (m)
185     (let* ((tags (ly:music-property m 'tags))
186            (res (memq tag tags)))
187      (or
188       (eq? tags '())
189       res)))
190    music))
191
192
193 %% Todo:
194 %% doing
195 %% define-music-function in a .scm causes crash.
196
197 cueDuring = 
198 #(define-music-function
199   (parser location what dir main-music)
200   (string? ly:dir? ly:music?)
201   (make-music 'QuoteMusic
202               'element main-music 
203               'quoted-context-type 'Voice
204               'quoted-context-id "cue"
205               'quoted-music-name what
206               'quoted-voice-direction dir
207               'origin location))
208
209
210 quoteDuring = #
211 (define-music-function
212   (parser location what main-music)
213   (string? ly:music?)
214   (make-music 'QuoteMusic
215               'element main-music
216               'quoted-music-name what
217               'origin location))
218
219
220
221 pitchedTrill =
222 #(define-music-function
223    (parser location main-note secondary-note)
224    (ly:music? ly:music?)
225    (let*
226        ((get-notes (lambda (ev-chord)
227                      (filter
228                       (lambda (m) (eq? 'NoteEvent (ly:music-property m 'name)))
229                       (ly:music-property ev-chord 'elements))))
230         (sec-note-events (get-notes secondary-note))
231         (trill-events (filter (lambda (m) (memq 'trill-span-event (ly:music-property m 'types)))
232                               (ly:music-property main-note 'elements)))
233
234         (trill-pitch
235          (if (pair? sec-note-events)
236              (ly:music-property (car sec-note-events) 'pitch)
237              )))
238      
239      (if (ly:pitch? trill-pitch)
240          (for-each (lambda (m) (ly:music-set-property! m 'pitch trill-pitch))
241                    trill-events)
242          (begin
243            (ly:warning (_ "Second argument of \\pitchedTrill should be single note: "))
244            (display sec-note-events)))
245
246      main-note))
247
248 killCues =
249 #(define-music-function
250    (parser location music)
251    (ly:music?)
252    (music-map
253     (lambda (mus)
254       (if (string? (ly:music-property mus 'quoted-music-name))
255           (ly:music-property mus 'element)
256           mus)) music))
257    
258
259 afterGraceFraction =
260 #(cons 6 8)
261
262 afterGrace =
263 #(define-music-function
264   (parser location main grace)
265   (ly:music? ly:music?)
266
267   (let*
268       ((main-length (ly:music-length main))
269        (fraction  (ly:parser-lookup parser 'afterGraceFraction)))
270     
271     (make-simultaneous-music
272      (list
273       main
274       (make-sequential-music
275        (list
276
277         (make-music 'SkipMusic
278                     'duration (ly:make-duration
279                                0 0
280                                (* (ly:moment-main-numerator main-length)
281                                   (car fraction))
282                                (* (ly:moment-main-denominator main-length)
283                                   (cdr fraction)) ))
284         (make-music 'GraceMusic
285                     'element grace)))))))
286
287
288 barNumberCheck =
289 #(define-music-function (parser location n) (integer?)
290    (make-music 'ApplyContext 
291                'origin location
292                'procedure 
293                (lambda (c)
294                  (let*
295                      ((cbn (ly:context-property c 'currentBarNumber)))
296                    (if (not (= cbn n))
297                        (ly:input-message location "Barcheck failed got ~a expect ~a"
298                                          cbn n))))))
299
300
301
302 % for regression testing purposes.
303 assertBeamQuant =
304 #(define-music-function (parser location l r) (pair? pair?)
305   (make-grob-property-override 'Beam 'positions
306    (ly:make-simple-closure
307     (ly:make-simple-closure
308      (append
309       (list chain-grob-member-functions `(,cons 0 0))
310       (check-quant-callbacks l r))))))
311     
312 % for regression testing purposes.
313 assertBeamSlope =
314 #(define-music-function (parser location comp) (procedure?)
315   (make-grob-property-override 'Beam 'positions
316    (ly:make-simple-closure
317     (ly:make-simple-closure
318      (append
319       (list chain-grob-member-functions `(,cons 0 0))
320       (check-slope-callbacks comp))))))
321
322
323 parallelMusic =
324 #(define-music-function (parser location voice-ids music) (list? ly:music?)
325   "Define parallel music sequences, separated by '|' (bar check signs),
326 and assign them to the identifiers provided in @var{voice-ids}.
327
328 @var{voice-ids}: a list of music identifiers (symbols containing only letters)
329
330 @var{music}: a music sequence, containing BarChecks as limiting expressions.
331
332 Example:
333   \\parallelMusic #'(A B C) {
334     c c | d d | e e |
335     d d | e e | f f |
336   }
337 <==>
338   A = { c c | d d | }
339   B = { d d | e e | }
340   C = { e e | f f | }
341 "
342   (let* ((voices (apply circular-list (make-list (length voice-ids) (list))))
343          (current-voices voices)
344          (current-sequence (list)))
345     ;;
346     ;; utilities
347     (define (push-music m)
348       "Push the music expression into the current sequence"
349       (set! current-sequence (cons m current-sequence)))
350     (define (change-voice)
351       "Stores the previously built sequence into the current voice and
352        change to the following voice."
353       (list-set! current-voices 0 (cons (make-music 'SequentialMusic 
354                                          'elements (reverse! current-sequence))
355                                         (car current-voices)))
356       (set! current-sequence (list))
357       (set! current-voices (cdr current-voices)))
358     (define (bar-check? m)
359       "Checks whether m is a bar check."
360       (eq? (ly:music-property m 'name) 'BarCheck))
361     (define (music-origin music)
362       "Recursively search an origin location stored in music."
363       (cond ((null? music) #f)
364             ((not (null? (ly:music-property music 'origin)))
365              (ly:music-property music 'origin))
366             (else (or (music-origin (ly:music-property music 'element))
367                       (let ((origins (remove not (map music-origin 
368                                                       (ly:music-property music 'elements)))))
369                         (and (not (null? origins)) (car origins)))))))
370     ;;
371     ;; first, split the music and fill in voices
372     (map-in-order (lambda (m)
373                     (push-music m)
374                     (if (bar-check? m) (change-voice)))
375                   (ly:music-property music 'elements))
376     (if (not (null? current-sequence)) (change-voice))
377     ;; un-circularize `voices' and reorder the voices
378     (set! voices (map-in-order (lambda (dummy seqs)
379                                  (reverse! seqs))
380                                voice-ids voices))
381     ;;
382     ;; set origin location of each sequence in each voice
383     ;; for better type error tracking
384     (for-each (lambda (voice)
385                 (for-each (lambda (seq)
386                             (set! (ly:music-property seq 'origin)
387                                   (or (music-origin seq) location)))
388                           voice))
389               voices)
390     ;;
391     ;; check sequence length
392     (apply for-each (lambda (. seqs)
393                       (let ((moment-reference (ly:music-length (car seqs))))
394                         (for-each (lambda (seq moment)
395                                     (if (not (equal? moment moment-reference))
396                                         (ly:music-message seq 
397                                          "Bars in parallel music don't have the same length")))
398                           seqs (map-in-order ly:music-length seqs))))
399            voices)
400    ;;
401    ;; bind voice identifiers to the voices
402    (map (lambda (voice-id voice)
403           (ly:parser-define! parser voice-id 
404                              (make-music 'SequentialMusic 
405                                'origin location
406                                'elements voice)))
407         voice-ids voices))
408  ;; Return an empty sequence. this function is actually a "void" function.
409  (make-music 'SequentialMusic 'void #t))
410
411
412
413
414 %% this is a stub. Write your own to suit the spacing tweak output.
415 spacingTweaks =
416 #(define-music-function (parser location parameters) (list?)
417    (make-music 'SequentialMusic 'void #t))
418
419 octave =
420 #(define-music-function (parser location pitch-note) (ly:music?)
421    "octave check"
422
423    (make-music 'RelativeOctaveCheck
424                'origin location
425                'pitch (pitch-of-note pitch-note) 
426                ))
427
428 addquote =
429 #(define-music-function (parser location name music) (string? ly:music?)
430    "Add a piece of music to be quoted "
431    (add-quotable name music)
432    (make-music 'SequentialMusic 'void #t))
433
434    
435 parenthesize =
436 #(define-music-function (parser loc arg) (ly:music?)
437    "Tag @var{arg} to be parenthesized."
438
439    (set! (ly:music-property arg 'parenthesize) #t)
440    arg)
441
442
443 featherDurations=
444 #(define-music-function (parser location factor argument) (ly:moment? ly:music?)
445
446    "Rearrange durations in ARGUMENT so there is an
447 acceleration/deceleration. "
448    
449    (let*
450        ((orig-duration (ly:music-length argument))
451         (multiplier (ly:make-moment 1 1)))
452
453      (music-map 
454       (lambda (mus)
455         (if (and (eq? (ly:music-property mus 'name) 'EventChord)
456                  (< 0 (ly:moment-main-denominator (ly:music-length mus))))
457             (begin
458               (ly:music-compress mus multiplier)
459               (set! multiplier (ly:moment-mul factor multiplier)))
460             )
461         mus)
462       argument)
463
464      (ly:music-compress
465       argument
466       (ly:moment-div orig-duration (ly:music-length argument)))
467
468      argument))
469