]> git.donarmstrong.com Git - lilypond.git/blob - ly/music-functions-init.ly
Merge http://git.sv.gnu.org/r/lilypond
[lilypond.git] / ly / music-functions-init.ly
1 % -*-Scheme-*-
2
3 \version "2.10.0"
4
5
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 %% this file is alphabetically sorted.
8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9
10 %% need SRFI-1 filter 
11
12 #(use-modules (srfi srfi-1))
13
14 acciaccatura =
15 #(def-grace-function startAcciaccaturaMusic stopAcciaccaturaMusic)
16
17 addQuote =
18 #(define-music-function (parser location name music) (string? ly:music?)
19    (_i "Add a piece of music to be quoted ")
20    (add-quotable parser name music)
21    (make-music 'SequentialMusic 'void #t))
22
23 afterGraceFraction =
24 #(cons 6 8)
25
26 afterGrace =
27 #(define-music-function
28   (parser location main grace)
29   (ly:music? ly:music?)
30
31   (let*
32       ((main-length (ly:music-length main))
33        (fraction  (ly:parser-lookup parser 'afterGraceFraction)))
34     
35     (make-simultaneous-music
36      (list
37       main
38       (make-sequential-music
39        (list
40
41         (make-music 'SkipMusic
42                     'duration (ly:make-duration
43                                0 0
44                                (* (ly:moment-main-numerator main-length)
45                                   (car fraction))
46                                (* (ly:moment-main-denominator main-length)
47                                   (cdr fraction)) ))
48         (make-music 'GraceMusic
49                     'element grace)))))))
50
51 applyMusic =
52 #(define-music-function (parser location func music) (procedure? ly:music?)
53                (func music))
54
55
56 applyOutput =
57 #(define-music-function (parser location ctx proc) (symbol? procedure?)
58                 (make-music 'ApplyOutputEvent
59                   'origin location
60                   'procedure proc
61                   'context-type ctx))
62
63 appoggiatura =
64 #(def-grace-function startAppoggiaturaMusic stopAppoggiaturaMusic)
65
66
67
68 % for regression testing purposes.
69 assertBeamQuant =
70 #(define-music-function (parser location l r) (pair? pair?)
71   (make-grob-property-override 'Beam 'positions
72    (ly:make-simple-closure
73     (ly:make-simple-closure
74      (append
75       (list chain-grob-member-functions `(,cons 0 0))
76       (check-quant-callbacks l r))))))
77     
78 % for regression testing purposes.
79 assertBeamSlope =
80 #(define-music-function (parser location comp) (procedure?)
81   (make-grob-property-override 'Beam 'positions
82    (ly:make-simple-closure
83     (ly:make-simple-closure
84      (append
85       (list chain-grob-member-functions `(,cons 0 0))
86       (check-slope-callbacks comp))))))
87
88
89
90 autochange =
91 #(define-music-function (parser location music) (ly:music?)
92                (make-autochange-music parser music))
93
94 applyContext =
95 #(define-music-function (parser location proc) (procedure?)
96                  (make-music 'ApplyContext 
97                    'origin location
98                    'procedure proc))
99
100
101 balloonText =
102 #(define-music-function (parser location offset text) (number-pair? markup?)
103    
104     (make-music 'AnnotateOutputEvent
105                 'X-offset (car offset)
106                 'Y-offset (cdr offset)
107                 'text text))
108
109 balloonGrobText =
110 #(define-music-function (parser location grob-name offset text) (symbol? number-pair? markup?)
111    
112     (make-music 'AnnotateOutputEvent
113                 'symbol grob-name
114                 'X-offset (car offset)
115                 'Y-offset (cdr offset)
116                 'text text))
117
118 bar =
119 #(define-music-function (parser location type)
120    (string?)
121    (context-spec-music
122     (make-property-set 'whichBar type)
123     'Timing))
124
125
126 barNumberCheck =
127 #(define-music-function (parser location n) (integer?)
128    (make-music 'ApplyContext 
129                'origin location
130                'procedure 
131                (lambda (c)
132                  (let*
133                      ((cbn (ly:context-property c 'currentBarNumber)))
134                    (if (and  (number? cbn) (not (= cbn n)))
135                        (ly:input-message location "Barcheck failed got ~a expect ~a"
136                                          cbn n))))))
137
138
139 %% why a function?
140 breathe =
141 #(define-music-function (parser location) ()
142             (make-music 'EventChord 
143               'origin location
144               'elements (list (make-music 'BreathingEvent))))
145
146 bendAfter =
147 #(define-music-function (parser location delta) (integer?)
148               
149   (make-music 'BendAfterEvent
150    'delta-step delta))
151
152 clef =
153 #(define-music-function (parser location type)
154    (string?)
155    (_i "Set the current clef.")
156
157    (make-clef-set type))
158
159
160 compressMusic =
161 #(define-music-function
162                   (parser location fraction music) (number-pair? ly:music?)
163                   (ly:music-compress music (ly:make-moment (car fraction) (cdr fraction))))
164
165
166 cueDuring = 
167 #(define-music-function
168   (parser location what dir main-music)
169   (string? ly:dir? ly:music?)
170   (make-music 'QuoteMusic
171               'element main-music 
172               'quoted-context-type 'Voice
173               'quoted-context-id "cue"
174               'quoted-music-name what
175               'quoted-voice-direction dir
176               'origin location))
177
178 displayLilyMusic =
179 #(define-music-function (parser location music) (ly:music?)
180    (newline)
181    (display-lily-music music parser)
182    music)
183
184 displayMusic =
185 #(define-music-function (parser location music) (ly:music?)
186    (newline)
187    (display-scheme-music music)
188    music)
189
190 featherDurations=
191 #(define-music-function (parser location factor argument) (ly:moment? ly:music?)
192    (_i "Rearrange durations in ARGUMENT so there is an
193 acceleration/deceleration. ")
194    
195    (let*
196        ((orig-duration (ly:music-length argument))
197         (multiplier (ly:make-moment 1 1)))
198
199      (music-map 
200       (lambda (mus)
201         (if (and (eq? (ly:music-property mus 'name) 'EventChord)
202                  (< 0 (ly:moment-main-denominator (ly:music-length mus))))
203             (begin
204               (ly:music-compress mus multiplier)
205               (set! multiplier (ly:moment-mul factor multiplier)))
206             )
207         mus)
208       argument)
209
210      (ly:music-compress
211       argument
212       (ly:moment-div orig-duration (ly:music-length argument)))
213
214      argument))
215
216 grace =
217 #(def-grace-function startGraceMusic stopGraceMusic)
218
219
220 "instrument-definitions" = #'()
221
222 addInstrumentDefinition =
223 #(define-music-function
224    (parser location name lst) (string? list?)
225
226    (set! instrument-definitions (acons name lst instrument-definitions))
227
228    (make-music 'SequentialMusic 'void #t))
229
230
231 instrumentSwitch =
232 #(define-music-function
233    (parser location name) (string?)
234    (let*
235        ((handle  (assoc name instrument-definitions))
236         (instrument-def (if handle (cdr handle) '()))
237         )
238
239      (if (not handle)
240          (ly:input-message "No such instrument: ~a" name))
241      (context-spec-music
242       (make-music 'SimultaneousMusic
243                   'elements
244                   (map (lambda (kv)
245                          (make-property-set
246                           (car kv)
247                           (cdr kv)))
248                        instrument-def))
249       'Staff)))
250
251
252 keepWithTag =
253 #(define-music-function
254   (parser location tag music) (symbol? ly:music?)
255   (music-filter
256    (lambda (m)
257     (let* ((tags (ly:music-property m 'tags))
258            (res (memq tag tags)))
259      (or
260       (eq? tags '())
261       res)))
262    music))
263
264
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 makeClusters =
278 #(define-music-function
279                 (parser location arg) (ly:music?)
280                 (music-map note-to-cluster arg))
281
282 musicMap =
283 #(define-music-function (parser location proc mus) (procedure? ly:music?)
284              (music-map proc mus))
285
286
287
288 oldaddlyrics =
289 #(define-music-function (parser location music lyrics) (ly:music? ly:music?)
290
291               (make-music 'OldLyricCombineMusic 
292                           'origin location
293                           'elements (list music lyrics)))
294
295
296 overrideProperty =
297 #(define-music-function (parser location name property value)
298    (string? symbol? scheme?)
299
300
301    (_i "Set @var{property} to @var{value} in all grobs named @var{name}.
302 The @var{name} argument is a string of the form @code{\"Context.GrobName\"}
303 or @code{\"GrobName\"}")
304
305    (let*
306        ((name-components (string-split name #\.))
307         (context-name 'Bottom)
308         (grob-name #f))
309
310      (if (> 2 (length name-components))
311          (set! grob-name (string->symbol (car name-components)))
312          (begin
313            (set! grob-name (string->symbol (list-ref name-components 1)))
314            (set! context-name (string->symbol (list-ref name-components 0)))))
315
316      (make-music 'ApplyOutputEvent
317                  'origin location
318                  'context-type context-name
319                  'procedure
320                  (lambda (grob orig-context context)
321                    (if (equal?
322                         (cdr (assoc 'name (ly:grob-property grob 'meta)))
323                         grob-name)
324                        (set! (ly:grob-property grob property) value))))))
325
326
327 removeWithTag = 
328 #(define-music-function
329   (parser location tag music) (symbol? ly:music?)
330   (music-filter
331    (lambda (m)
332     (let* ((tags (ly:music-property m 'tags))
333            (res (memq tag tags)))
334      (not res)))
335  music))
336
337 %% Todo:
338 %% doing
339 %% define-music-function in a .scm causes crash.
340
341 octave =
342 #(define-music-function (parser location pitch-note) (ly:music?)
343    (_i "octave check")
344
345    (make-music 'RelativeOctaveCheck
346                'origin location
347                'pitch (pitch-of-note pitch-note) 
348                ))
349 partcombine =
350 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
351                 (make-part-combine-music parser
352                                          (list part1 part2)))
353
354               
355 pitchedTrill =
356 #(define-music-function
357    (parser location main-note secondary-note)
358    (ly:music? ly:music?)
359    (let*
360        ((get-notes (lambda (ev-chord)
361                      (filter
362                       (lambda (m) (eq? 'NoteEvent (ly:music-property m 'name)))
363                       (ly:music-property ev-chord 'elements))))
364         (sec-note-events (get-notes secondary-note))
365         (trill-events (filter (lambda (m) (memq 'trill-span-event (ly:music-property m 'types)))
366                               (ly:music-property main-note 'elements)))
367
368         (trill-pitch
369          (if (pair? sec-note-events)
370              (ly:music-property (car sec-note-events) 'pitch)
371              )))
372      
373      (if (ly:pitch? trill-pitch)
374          (for-each (lambda (m) (ly:music-set-property! m 'pitch trill-pitch))
375                    trill-events)
376          (begin
377            (ly:warning (_ "Second argument of \\pitchedTrill should be single note: "))
378            (display sec-note-events)))
379
380      main-note))
381    
382 parenthesize =
383 #(define-music-function (parser loc arg) (ly:music?)
384    (_i "Tag @var{arg} to be parenthesized.")
385
386    (set! (ly:music-property arg 'parenthesize) #t)
387    arg)
388
389 %% for lambda*
390 #(use-modules (ice-9 optargs))
391
392 parallelMusic =
393 #(define-music-function (parser location voice-ids music) (list? ly:music?)
394   (_i "Define parallel music sequences, separated by '|' (bar check signs),
395 and assign them to the identifiers provided in @var{voice-ids}.
396
397 @var{voice-ids}: a list of music identifiers (symbols containing only letters)
398
399 @var{music}: a music sequence, containing BarChecks as limiting expressions.
400
401 Example:
402
403 @verbatim
404   \\parallelMusic #'(A B C) {
405     c c | d d | e e |
406     d d | e e | f f |
407   }
408 <==>
409   A = { c c | d d | }
410   B = { d d | e e | }
411   C = { e e | f f | }
412 @end verbatim
413 ")
414   (let* ((voices (apply circular-list (make-list (length voice-ids) (list))))
415          (current-voices voices)
416          (current-sequence (list)))
417     ;;
418     ;; utilities
419     (define (push-music m)
420       "Push the music expression into the current sequence"
421       (set! current-sequence (cons m current-sequence)))
422     (define (change-voice)
423       "Stores the previously built sequence into the current voice and
424        change to the following voice."
425       (list-set! current-voices 0 (cons (make-music 'SequentialMusic 
426                                          'elements (reverse! current-sequence))
427                                         (car current-voices)))
428       (set! current-sequence (list))
429       (set! current-voices (cdr current-voices)))
430     (define (bar-check? m)
431       "Checks whether m is a bar check."
432       (eq? (ly:music-property m 'name) 'BarCheck))
433     (define (music-origin music)
434       "Recursively search an origin location stored in music."
435       (cond ((null? music) #f)
436             ((not (null? (ly:music-property music 'origin)))
437              (ly:music-property music 'origin))
438             (else (or (music-origin (ly:music-property music 'element))
439                       (let ((origins (remove not (map music-origin 
440                                                       (ly:music-property music 'elements)))))
441                         (and (not (null? origins)) (car origins)))))))
442     ;;
443     ;; first, split the music and fill in voices
444     (map-in-order (lambda (m)
445                     (push-music m)
446                     (if (bar-check? m) (change-voice)))
447                   (ly:music-property music 'elements))
448     (if (not (null? current-sequence)) (change-voice))
449     ;; un-circularize `voices' and reorder the voices
450     (set! voices (map-in-order (lambda (dummy seqs)
451                                  (reverse! seqs))
452                                voice-ids voices))
453     ;;
454     ;; set origin location of each sequence in each voice
455     ;; for better type error tracking
456     (for-each (lambda (voice)
457                 (for-each (lambda (seq)
458                             (set! (ly:music-property seq 'origin)
459                                   (or (music-origin seq) location)))
460                           voice))
461               voices)
462     ;;
463     ;; check sequence length
464     (apply for-each (lambda* (#:rest seqs)
465                       (let ((moment-reference (ly:music-length (car seqs))))
466                         (for-each (lambda (seq moment)
467                                     (if (not (equal? moment moment-reference))
468                                         (ly:music-message seq 
469                                          "Bars in parallel music don't have the same length")))
470                           seqs (map-in-order ly:music-length seqs))))
471            voices)
472    ;;
473    ;; bind voice identifiers to the voices
474    (map (lambda (voice-id voice)
475           (ly:parser-define! parser voice-id 
476                              (make-music 'SequentialMusic 
477                                'origin location
478                                'elements voice)))
479         voice-ids voices))
480  ;; Return an empty sequence. this function is actually a "void" function.
481  (make-music 'SequentialMusic 'void #t))
482
483
484
485 quoteDuring = #
486 (define-music-function
487   (parser location what main-music)
488   (string? ly:music?)
489   (make-music 'QuoteMusic
490               'element main-music
491               'quoted-music-name what
492               'origin location))
493
494
495
496 resetRelativeOctave  =
497 #(define-music-function
498     (parser location reference-note)
499     (ly:music?)
500     (_i "Set the octave inside a \\relative section.")
501
502    (let*
503     ((notes (ly:music-property reference-note 'elements))
504      (pitch (ly:music-property (car notes) 'pitch)))
505
506     (set! (ly:music-property reference-note 'elements) '())
507     (set! (ly:music-property reference-note
508        'to-relative-callback)
509        (lambda (music last-pitch)
510         pitch))
511
512     reference-note))
513
514
515
516 shiftDurations =
517 #(define-music-function (parser location dur dots arg) (integer? integer? ly:music?)
518    (_i "")
519    
520    (music-map
521     (lambda (x)
522       (shift-one-duration-log x dur dots)) arg))
523
524 spacingTweaks =
525 #(define-music-function (parser location parameters) (list?)
526    (_i "Set the system stretch, by reading the 'system-stretch property of
527 the `parameters' assoc list.")
528    #{
529       \overrideProperty #"Score.NonMusicalPaperColumn"
530         #'line-break-system-details
531         #$(list (cons 'alignment-extra-space (cdr (assoc 'system-stretch parameters)))
532                 (cons 'system-Y-extent (cdr (assoc 'system-Y-extent parameters))))
533    #})
534
535 %% Parser used to read page-layout file, and then retreive score tweaks.
536 #(define page-layout-parser #f)
537
538 includePageLayoutFile = 
539 #(define-music-function (parser location) ()
540    (_i "If page breaks and tweak dump is not asked, and the file
541 <basename>-page-layout.ly exists, include it.")
542    (if (not (ly:get-option 'dump-tweaks))
543        (let ((tweak-filename (format #f "~a-page-layout.ly"
544                                      (ly:parser-output-name parser))))
545          (if (access? tweak-filename R_OK)
546              (begin
547                (ly:message "Including tweak file ~a" tweak-filename)
548                (set! page-layout-parser (ly:parser-clone parser))
549                (ly:parser-parse-string page-layout-parser
550                                        (format #f "\\include \"~a\""
551                                                tweak-filename))))))
552    (make-music 'SequentialMusic 'void #t))
553
554
555 rightHandFinger =
556 #(define-music-function (parser location finger) (number-or-string?)
557    (_i "Define a StrokeFingerEvent")
558    
559    (apply make-music
560           (append
561            (list 
562             'StrokeFingerEvent
563             'origin location)
564            (if  (string? finger)
565                 (list 'text finger)
566                 (list 'digit finger)))))
567
568 scoreTweak =
569 #(define-music-function (parser location name) (string?)
570    (_i "Include the score tweak, if exists.")
571    (if (and page-layout-parser (not (ly:get-option 'dump-tweaks)))
572        (let ((tweak-music (ly:parser-lookup page-layout-parser
573                                             (string->symbol name))))
574          (if (ly:music? tweak-music)
575              tweak-music
576              (make-music 'SequentialMusic)))
577        (make-music 'SequentialMusic)))
578
579 transposedCueDuring =
580 #(define-music-function
581   (parser location what dir pitch-note main-music)
582   (string? ly:dir? ly:music? ly:music?)
583
584   (_i "Insert notes from the part @var{what} into a voice called @code{cue},
585 using the transposition defined by @var{pitch-note}.  This happens
586 simultaneously with @var{main-music}, which is usually a rest.  The
587 argument @var{dir} determines whether the cue notes should be notated
588 as a first or second voice.")
589
590   (make-music 'QuoteMusic
591               'element main-music
592               'quoted-context-type 'Voice
593               'quoted-context-id "cue"
594               'quoted-music-name what
595               'quoted-voice-direction dir
596               'quoted-transposition (pitch-of-note pitch-note)
597               'origin location))
598
599
600
601 transposition =
602 #(define-music-function (parser location pitch-note) (ly:music?)
603    (_i "Set instrument transposition")
604
605    (context-spec-music
606     (make-property-set 'instrumentTransposition
607                        (ly:pitch-negate (pitch-of-note pitch-note)))
608         'Staff))
609
610 tweak = #(define-music-function (parser location sym val arg)
611            (symbol? scheme? ly:music?)
612
613            (_i "Add @code{sym . val} to the @code{tweaks} property of @var{arg}.")
614            
615            (set!
616             (ly:music-property arg 'tweaks)
617             (acons sym val
618                    (ly:music-property arg 'tweaks)))
619            arg)
620
621 tag = #(define-music-function (parser location tag arg)
622    (symbol? ly:music?)
623
624    (_i "Add @var{tag} to the @code{tags} property of @var{arg}.")
625
626    (set!
627     (ly:music-property arg 'tags)
628     (cons tag
629           (ly:music-property arg 'tags)))
630    arg)
631
632
633 unfoldRepeats =
634 #(define-music-function (parser location music) (ly:music?)
635                   (unfold-repeats music))
636
637
638
639 withMusicProperty =
640 #(define-music-function (parser location sym val music) (symbol? scheme? ly:music?)
641    (_i "Set @var{sym} to @var{val} in @var{music}.")
642
643    (set! (ly:music-property music sym) val)
644    music)
645
646