]> git.donarmstrong.com Git - lilypond.git/blob - ly/music-functions-init.ly
* ly/music-functions-init.ly: Updated \overrideProperty to use the
[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 ctx proc) (symbol? procedure?)
111                 (make-music 'ApplyOutputEvent
112                   'origin location
113                   'procedure proc
114                   'context-type ctx))
115
116 overrideProperty =
117 #(define-music-function (parser location name property value)
118    (string? symbol? scheme?)
119
120
121    "Set @var{property} to @var{value} in all grobs named @var{name}.
122 The @var{name} argument is a string of the form @code{\"Context.GrobName\"}
123 or @code{\"GrobName\"}"
124
125    (let*
126        ((name-components (string-split name #\.))
127         (context-name 'Bottom)
128         (grob-name #f))
129
130      (if (> 2 (length name-components))
131          (set! grob-name (string->symbol (car name-components)))
132          (begin
133            (set! grob-name (string->symbol (list-ref name-components 1)))
134            (set! context-name (string->symbol (list-ref name-components 0)))))
135
136      (make-music 'ApplyOutputEvent
137                  'origin location
138                  'context-type context-name
139                  'procedure
140                  (lambda (grob orig-context context)
141                    (if (equal?
142                         (cdr (assoc 'name (ly:grob-property grob 'meta)))
143                         grob-name)
144                        (set! (ly:grob-property grob property) value))))))
145
146 breathe =
147 #(define-music-function (parser location) ()
148             (make-music 'EventChord 
149               'origin location
150               'elements (list (make-music 'BreathingSignEvent))))
151
152
153 unfoldRepeats =
154 #(define-music-function (parser location music) (ly:music?)
155                   (unfold-repeats music))
156
157 compressMusic =
158 #(define-music-function
159                   (parser location fraction music) (number-pair? ly:music?)
160                   (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction))))
161
162 makeClusters =
163 #(define-music-function
164                 (parser location arg) (ly:music?)
165                 (music-map note-to-cluster arg))
166
167
168 removeWithTag = 
169 #(define-music-function
170   (parser location tag music) (symbol? ly:music?)
171   (music-filter
172    (lambda (m)
173     (let* ((tags (ly:music-property m 'tags))
174            (res (memq tag tags)))
175      (not res)))
176  music))
177               
178 keepWithTag =
179 #(define-music-function
180   (parser location tag music) (symbol? ly:music?)
181   (music-filter
182    (lambda (m)
183     (let* ((tags (ly:music-property m 'tags))
184            (res (memq tag tags)))
185      (or
186       (eq? tags '())
187       res)))
188    music))
189
190
191 %% Todo:
192 %% doing
193 %% define-music-function in a .scm causes crash.
194
195 cueDuring = 
196 #(define-music-function
197   (parser location what dir main-music)
198   (string? ly:dir? ly:music?)
199   (make-music 'QuoteMusic
200               'element main-music 
201               'quoted-context-type 'Voice
202               'quoted-context-id "cue"
203               'quoted-music-name what
204               'quoted-voice-direction dir
205               'origin location))
206
207
208 transposedCueDuring = #
209 (define-music-function
210   (parser location what dir pitch-note main-music)
211   (string? ly:dir? ly:music? ly:music?)
212
213   "Insert notes from the part @var{what} into a voice called @code{cue},
214 using the transposition defined by @var{pitch-note}.  This happens
215 simultaneously with @var{main-music}, which is usually a rest.  The
216 argument @var{dir} determines whether the cue notes should be notated
217 as a first or second voice."
218
219   (make-music 'QuoteMusic
220               'element main-music
221               'quoted-context-type 'Voice
222               'quoted-context-id "cue"
223               'quoted-music-name what
224               'quoted-voice-direction dir
225               'quoted-transposition (pitch-of-note pitch-note)
226               'origin location))
227
228
229 quoteDuring = #
230 (define-music-function
231   (parser location what main-music)
232   (string? ly:music?)
233   (make-music 'QuoteMusic
234               'element main-music
235               'quoted-music-name what
236               'origin location))
237
238
239 pitchedTrill =
240 #(define-music-function
241    (parser location main-note secondary-note)
242    (ly:music? ly:music?)
243    (let*
244        ((get-notes (lambda (ev-chord)
245                      (filter
246                       (lambda (m) (eq? 'NoteEvent (ly:music-property m 'name)))
247                       (ly:music-property ev-chord 'elements))))
248         (sec-note-events (get-notes secondary-note))
249         (trill-events (filter (lambda (m) (memq 'trill-span-event (ly:music-property m 'types)))
250                               (ly:music-property main-note 'elements)))
251
252         (trill-pitch
253          (if (pair? sec-note-events)
254              (ly:music-property (car sec-note-events) 'pitch)
255              )))
256      
257      (if (ly:pitch? trill-pitch)
258          (for-each (lambda (m) (ly:music-set-property! m 'pitch trill-pitch))
259                    trill-events)
260          (begin
261            (ly:warning (_ "Second argument of \\pitchedTrill should be single note: "))
262            (display sec-note-events)))
263
264      main-note))
265
266 killCues =
267 #(define-music-function
268    (parser location music)
269    (ly:music?)
270    (music-map
271     (lambda (mus)
272       (if (string? (ly:music-property mus 'quoted-music-name))
273           (ly:music-property mus 'element)
274           mus)) music))
275    
276
277 afterGraceFraction =
278 #(cons 6 8)
279
280 afterGrace =
281 #(define-music-function
282   (parser location main grace)
283   (ly:music? ly:music?)
284
285   (let*
286       ((main-length (ly:music-length main))
287        (fraction  (ly:parser-lookup parser 'afterGraceFraction)))
288     
289     (make-simultaneous-music
290      (list
291       main
292       (make-sequential-music
293        (list
294
295         (make-music 'SkipMusic
296                     'duration (ly:make-duration
297                                0 0
298                                (* (ly:moment-main-numerator main-length)
299                                   (car fraction))
300                                (* (ly:moment-main-denominator main-length)
301                                   (cdr fraction)) ))
302         (make-music 'GraceMusic
303                     'element grace)))))))
304
305
306 barNumberCheck =
307 #(define-music-function (parser location n) (integer?)
308    (make-music 'ApplyContext 
309                'origin location
310                'procedure 
311                (lambda (c)
312                  (let*
313                      ((cbn (ly:context-property c 'currentBarNumber)))
314                    (if (not (= cbn n))
315                        (ly:input-message location "Barcheck failed got ~a expect ~a"
316                                          cbn n))))))
317
318
319
320 % for regression testing purposes.
321 assertBeamQuant =
322 #(define-music-function (parser location l r) (pair? pair?)
323   (make-grob-property-override 'Beam 'positions
324    (ly:make-simple-closure
325     (ly:make-simple-closure
326      (append
327       (list chain-grob-member-functions `(,cons 0 0))
328       (check-quant-callbacks l r))))))
329     
330 % for regression testing purposes.
331 assertBeamSlope =
332 #(define-music-function (parser location comp) (procedure?)
333   (make-grob-property-override 'Beam 'positions
334    (ly:make-simple-closure
335     (ly:make-simple-closure
336      (append
337       (list chain-grob-member-functions `(,cons 0 0))
338       (check-slope-callbacks comp))))))
339
340
341 parallelMusic =
342 #(define-music-function (parser location voice-ids music) (list? ly:music?)
343   "Define parallel music sequences, separated by '|' (bar check signs),
344 and assign them to the identifiers provided in @var{voice-ids}.
345
346 @var{voice-ids}: a list of music identifiers (symbols containing only letters)
347
348 @var{music}: a music sequence, containing BarChecks as limiting expressions.
349
350 Example:
351   \\parallelMusic #'(A B C) {
352     c c | d d | e e |
353     d d | e e | f f |
354   }
355 <==>
356   A = { c c | d d | }
357   B = { d d | e e | }
358   C = { e e | f f | }
359 "
360   (let* ((voices (apply circular-list (make-list (length voice-ids) (list))))
361          (current-voices voices)
362          (current-sequence (list)))
363     ;;
364     ;; utilities
365     (define (push-music m)
366       "Push the music expression into the current sequence"
367       (set! current-sequence (cons m current-sequence)))
368     (define (change-voice)
369       "Stores the previously built sequence into the current voice and
370        change to the following voice."
371       (list-set! current-voices 0 (cons (make-music 'SequentialMusic 
372                                          'elements (reverse! current-sequence))
373                                         (car current-voices)))
374       (set! current-sequence (list))
375       (set! current-voices (cdr current-voices)))
376     (define (bar-check? m)
377       "Checks whether m is a bar check."
378       (eq? (ly:music-property m 'name) 'BarCheck))
379     (define (music-origin music)
380       "Recursively search an origin location stored in music."
381       (cond ((null? music) #f)
382             ((not (null? (ly:music-property music 'origin)))
383              (ly:music-property music 'origin))
384             (else (or (music-origin (ly:music-property music 'element))
385                       (let ((origins (remove not (map music-origin 
386                                                       (ly:music-property music 'elements)))))
387                         (and (not (null? origins)) (car origins)))))))
388     ;;
389     ;; first, split the music and fill in voices
390     (map-in-order (lambda (m)
391                     (push-music m)
392                     (if (bar-check? m) (change-voice)))
393                   (ly:music-property music 'elements))
394     (if (not (null? current-sequence)) (change-voice))
395     ;; un-circularize `voices' and reorder the voices
396     (set! voices (map-in-order (lambda (dummy seqs)
397                                  (reverse! seqs))
398                                voice-ids voices))
399     ;;
400     ;; set origin location of each sequence in each voice
401     ;; for better type error tracking
402     (for-each (lambda (voice)
403                 (for-each (lambda (seq)
404                             (set! (ly:music-property seq 'origin)
405                                   (or (music-origin seq) location)))
406                           voice))
407               voices)
408     ;;
409     ;; check sequence length
410     (apply for-each (lambda (. seqs)
411                       (let ((moment-reference (ly:music-length (car seqs))))
412                         (for-each (lambda (seq moment)
413                                     (if (not (equal? moment moment-reference))
414                                         (ly:music-message seq 
415                                          "Bars in parallel music don't have the same length")))
416                           seqs (map-in-order ly:music-length seqs))))
417            voices)
418    ;;
419    ;; bind voice identifiers to the voices
420    (map (lambda (voice-id voice)
421           (ly:parser-define! parser voice-id 
422                              (make-music 'SequentialMusic 
423                                'origin location
424                                'elements voice)))
425         voice-ids voices))
426  ;; Return an empty sequence. this function is actually a "void" function.
427  (make-music 'SequentialMusic 'void #t))
428
429
430
431
432 %% this is a stub. Write your own to suit the spacing tweak output.
433 spacingTweaks =
434 #(define-music-function (parser location parameters) (list?)
435    (make-music 'SequentialMusic 'void #t))
436
437 octave =
438 #(define-music-function (parser location pitch-note) (ly:music?)
439    "octave check"
440
441    (make-music 'RelativeOctaveCheck
442                'origin location
443                'pitch (pitch-of-note pitch-note) 
444                ))
445
446 addquote =
447 #(define-music-function (parser location name music) (string? ly:music?)
448    "Add a piece of music to be quoted "
449    (add-quotable name music)
450    (make-music 'SequentialMusic 'void #t))
451
452    
453 parenthesize =
454 #(define-music-function (parser loc arg) (ly:music?)
455    "Tag @var{arg} to be parenthesized."
456
457    (set! (ly:music-property arg 'parenthesize) #t)
458    arg)
459
460
461 featherDurations=
462 #(define-music-function (parser location factor argument) (ly:moment? ly:music?)
463
464    "Rearrange durations in ARGUMENT so there is an
465 acceleration/deceleration. "
466    
467    (let*
468        ((orig-duration (ly:music-length argument))
469         (multiplier (ly:make-moment 1 1)))
470
471      (music-map 
472       (lambda (mus)
473         (if (and (eq? (ly:music-property mus 'name) 'EventChord)
474                  (< 0 (ly:moment-main-denominator (ly:music-length mus))))
475             (begin
476               (ly:music-compress mus multiplier)
477               (set! multiplier (ly:moment-mul factor multiplier)))
478             )
479         mus)
480       argument)
481
482      (ly:music-compress
483       argument
484       (ly:moment-div orig-duration (ly:music-length argument)))
485
486      argument))
487