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