]> 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
191 endSpanners =
192 #(define-music-function (parser location music) (ly:music?)
193    (if (eq? (ly:music-property music 'name) 'EventChord)
194        (let*
195            ((elts (ly:music-property music 'elements))
196             (start-span-evs (filter (lambda (ev)
197                                 (and (music-has-type ev 'span-event)
198                                      (equal? (ly:music-property ev 'span-direction)
199                                              START)))
200                               elts))
201             (stop-span-evs
202              (map (lambda (m)
203                     (let* ((c (music-clone m)))
204                       (set! (ly:music-property c 'span-direction) STOP)
205                       c))
206                   start-span-evs))
207             (end-ev-chord (make-music 'EventChord
208                                       'elements stop-span-evs))
209             (total (make-music 'SequentialMusic
210                                'elements (list music
211                                                end-ev-chord))))
212          total)
213        
214        (ly:input-message location (_ "argument endSpanners is not an EventChord: ~a" music))))
215
216 featherDurations=
217 #(define-music-function (parser location factor argument) (ly:moment? ly:music?)
218    (_i "Rearrange durations in ARGUMENT so there is an
219 acceleration/deceleration. ")
220    
221    (let*
222        ((orig-duration (ly:music-length argument))
223         (multiplier (ly:make-moment 1 1)))
224
225      (music-map 
226       (lambda (mus)
227         (if (and (eq? (ly:music-property mus 'name) 'EventChord)
228                  (< 0 (ly:moment-main-denominator (ly:music-length mus))))
229             (begin
230               (ly:music-compress mus multiplier)
231               (set! multiplier (ly:moment-mul factor multiplier)))
232             )
233         mus)
234       argument)
235
236      (ly:music-compress
237       argument
238       (ly:moment-div orig-duration (ly:music-length argument)))
239
240      argument))
241
242 grace =
243 #(def-grace-function startGraceMusic stopGraceMusic)
244
245
246 "instrument-definitions" = #'()
247
248 addInstrumentDefinition =
249 #(define-music-function
250    (parser location name lst) (string? list?)
251
252    (set! instrument-definitions (acons name lst instrument-definitions))
253
254    (make-music 'SequentialMusic 'void #t))
255
256
257 instrumentSwitch =
258 #(define-music-function
259    (parser location name) (string?)
260    (let*
261        ((handle  (assoc name instrument-definitions))
262         (instrument-def (if handle (cdr handle) '()))
263         )
264
265      (if (not handle)
266          (ly:input-message "No such instrument: ~a" name))
267      (context-spec-music
268       (make-music 'SimultaneousMusic
269                   'elements
270                   (map (lambda (kv)
271                          (make-property-set
272                           (car kv)
273                           (cdr kv)))
274                        instrument-def))
275       'Staff)))
276
277
278 keepWithTag =
279 #(define-music-function
280   (parser location tag music) (symbol? ly:music?)
281   (music-filter
282    (lambda (m)
283     (let* ((tags (ly:music-property m 'tags))
284            (res (memq tag tags)))
285      (or
286       (eq? tags '())
287       res)))
288    music))
289
290
291
292 killCues =
293 #(define-music-function
294    (parser location music)
295    (ly:music?)
296    (music-map
297     (lambda (mus)
298       (if (string? (ly:music-property mus 'quoted-music-name))
299           (ly:music-property mus 'element)
300           mus)) music))
301    
302
303 makeClusters =
304 #(define-music-function
305                 (parser location arg) (ly:music?)
306                 (music-map note-to-cluster arg))
307
308 musicMap =
309 #(define-music-function (parser location proc mus) (procedure? ly:music?)
310              (music-map proc mus))
311
312
313
314 oldaddlyrics =
315 #(define-music-function (parser location music lyrics) (ly:music? ly:music?)
316
317               (make-music 'OldLyricCombineMusic 
318                           'origin location
319                           'elements (list music lyrics)))
320
321
322 overrideProperty =
323 #(define-music-function (parser location name property value)
324    (string? symbol? scheme?)
325
326
327    (_i "Set @var{property} to @var{value} in all grobs named @var{name}.
328 The @var{name} argument is a string of the form @code{\"Context.GrobName\"}
329 or @code{\"GrobName\"}")
330
331    (let*
332        ((name-components (string-split name #\.))
333         (context-name 'Bottom)
334         (grob-name #f))
335
336      (if (> 2 (length name-components))
337          (set! grob-name (string->symbol (car name-components)))
338          (begin
339            (set! grob-name (string->symbol (list-ref name-components 1)))
340            (set! context-name (string->symbol (list-ref name-components 0)))))
341
342      (make-music 'ApplyOutputEvent
343                  'origin location
344                  'context-type context-name
345                  'procedure
346                  (lambda (grob orig-context context)
347                    (if (equal?
348                         (cdr (assoc 'name (ly:grob-property grob 'meta)))
349                         grob-name)
350                        (set! (ly:grob-property grob property) value))))))
351
352 %% These are music functions (iso music indentifiers), because music identifiers
353 %% are not allowed at top-level.
354 pageBreak =
355 #(define-music-function (location parser) ()
356    (_i "Force a page break. May be used at toplevel (ie between scores or
357 markups), or inside a score.")
358    (make-music 'EventChord
359                'page-marker #t
360                'line-break-permission 'force
361                'page-break-permission 'force
362                'elements (list (make-music 'LineBreakEvent
363                                            'break-permission 'force)
364                                (make-music 'PageBreakEvent
365                                            'break-permission 'force))))
366
367 noPageBreak =
368 #(define-music-function (location parser) ()
369    (_i "Forbid a page break. May be used at toplevel (ie between scores or
370 markups), or inside a score.")
371    (make-music 'EventChord
372                'page-marker #t
373                'page-break-permission 'forbid
374                'elements (list (make-music 'PageBreakEvent
375                                            'break-permission '()))))
376
377 pageTurn =
378 #(define-music-function (location parser) ()
379    (_i "Force a page turn between two scores or top-level markups.")
380    (make-music 'EventChord 
381                'page-marker #t
382                'line-break-permission 'force
383                'page-break-permission 'force
384                'page-turn-permission 'force
385                'elements (list (make-music 'LineBreakEvent
386                                            'break-permission 'force)
387                                (make-music 'PageBreakEvent
388                                            'break-permission 'force)
389                                (make-music 'PageTurnEvent
390                                            'break-permission 'force))))
391
392 noPageTurn =
393 #(define-music-function (location parser) ()
394    (_i "Forbid a page turn. May be used at toplevel (ie between scores or
395 markups), or inside a score.")
396    (make-music 'EventChord
397                'page-marker #t
398                'page-turn-permission 'forbid
399                'elements (list (make-music 'PageTurnEvent
400                                            'break-permission '()))))
401
402 allowPageTurn =
403 #(define-music-function (location parser) ()
404    (_i "Allow a page turn. May be used at toplevel (ie between scores or
405 markups), or inside a score.")
406    (make-music 'EventChord
407                'page-marker #t
408                'page-turn-permission 'allow
409                'elements (list (make-music 'PageTurnEvent
410                                            'break-permission 'allow))))
411
412 removeWithTag = 
413 #(define-music-function
414   (parser location tag music) (symbol? ly:music?)
415   (music-filter
416    (lambda (m)
417     (let* ((tags (ly:music-property m 'tags))
418            (res (memq tag tags)))
419      (not res)))
420  music))
421
422 %% Todo:
423 %% doing
424 %% define-music-function in a .scm causes crash.
425
426 octave =
427 #(define-music-function (parser location pitch-note) (ly:music?)
428    (_i "octave check")
429
430    (make-music 'RelativeOctaveCheck
431                'origin location
432                'pitch (pitch-of-note pitch-note) 
433                ))
434 partcombine =
435 #(define-music-function (parser location part1 part2) (ly:music? ly:music?)
436                 (make-part-combine-music parser
437                                          (list part1 part2)))
438
439               
440 pitchedTrill =
441 #(define-music-function
442    (parser location main-note secondary-note)
443    (ly:music? ly:music?)
444    (let*
445        ((get-notes (lambda (ev-chord)
446                      (filter
447                       (lambda (m) (eq? 'NoteEvent (ly:music-property m 'name)))
448                       (ly:music-property ev-chord 'elements))))
449         (sec-note-events (get-notes secondary-note))
450         (trill-events (filter (lambda (m) (music-has-type m 'trill-span-event))
451                               (ly:music-property main-note 'elements)))
452
453         (trill-pitch
454          (if (pair? sec-note-events)
455              (ly:music-property (car sec-note-events) 'pitch)
456              )))
457      
458      (if (ly:pitch? trill-pitch)
459          (for-each (lambda (m) (ly:music-set-property! m 'pitch trill-pitch))
460                    trill-events)
461          (begin
462            (ly:warning (_ "Second argument of \\pitchedTrill should be single note: "))
463            (display sec-note-events)))
464
465      main-note))
466    
467 parenthesize =
468 #(define-music-function (parser loc arg) (ly:music?)
469    (_i "Tag @var{arg} to be parenthesized.")
470
471    (set! (ly:music-property arg 'parenthesize) #t)
472    arg)
473
474 %% for lambda*
475 #(use-modules (ice-9 optargs))
476
477 parallelMusic =
478 #(define-music-function (parser location voice-ids music) (list? ly:music?)
479   (_i "Define parallel music sequences, separated by '|' (bar check signs),
480 and assign them to the identifiers provided in @var{voice-ids}.
481
482 @var{voice-ids}: a list of music identifiers (symbols containing only letters)
483
484 @var{music}: a music sequence, containing BarChecks as limiting expressions.
485
486 Example:
487
488 @verbatim
489   \\parallelMusic #'(A B C) {
490     c c | d d | e e |
491     d d | e e | f f |
492   }
493 <==>
494   A = { c c | d d | }
495   B = { d d | e e | }
496   C = { e e | f f | }
497 @end verbatim
498 ")
499   (let* ((voices (apply circular-list (make-list (length voice-ids) (list))))
500          (current-voices voices)
501          (current-sequence (list)))
502     ;;
503     ;; utilities
504     (define (push-music m)
505       "Push the music expression into the current sequence"
506       (set! current-sequence (cons m current-sequence)))
507     (define (change-voice)
508       "Stores the previously built sequence into the current voice and
509        change to the following voice."
510       (list-set! current-voices 0 (cons (make-music 'SequentialMusic 
511                                          'elements (reverse! current-sequence))
512                                         (car current-voices)))
513       (set! current-sequence (list))
514       (set! current-voices (cdr current-voices)))
515     (define (bar-check? m)
516       "Checks whether m is a bar check."
517       (eq? (ly:music-property m 'name) 'BarCheck))
518     (define (music-origin music)
519       "Recursively search an origin location stored in music."
520       (cond ((null? music) #f)
521             ((not (null? (ly:music-property music 'origin)))
522              (ly:music-property music 'origin))
523             (else (or (music-origin (ly:music-property music 'element))
524                       (let ((origins (remove not (map music-origin 
525                                                       (ly:music-property music 'elements)))))
526                         (and (not (null? origins)) (car origins)))))))
527     ;;
528     ;; first, split the music and fill in voices
529     (map-in-order (lambda (m)
530                     (push-music m)
531                     (if (bar-check? m) (change-voice)))
532                   (ly:music-property music 'elements))
533     (if (not (null? current-sequence)) (change-voice))
534     ;; un-circularize `voices' and reorder the voices
535     (set! voices (map-in-order (lambda (dummy seqs)
536                                  (reverse! seqs))
537                                voice-ids voices))
538     ;;
539     ;; set origin location of each sequence in each voice
540     ;; for better type error tracking
541     (for-each (lambda (voice)
542                 (for-each (lambda (seq)
543                             (set! (ly:music-property seq 'origin)
544                                   (or (music-origin seq) location)))
545                           voice))
546               voices)
547     ;;
548     ;; check sequence length
549     (apply for-each (lambda* (#:rest seqs)
550                       (let ((moment-reference (ly:music-length (car seqs))))
551                         (for-each (lambda (seq moment)
552                                     (if (not (equal? moment moment-reference))
553                                         (ly:music-message seq 
554                                          "Bars in parallel music don't have the same length")))
555                           seqs (map-in-order ly:music-length seqs))))
556            voices)
557    ;;
558    ;; bind voice identifiers to the voices
559    (map (lambda (voice-id voice)
560           (ly:parser-define! parser voice-id 
561                              (make-music 'SequentialMusic 
562                                'origin location
563                                'elements voice)))
564         voice-ids voices))
565  ;; Return an empty sequence. this function is actually a "void" function.
566  (make-music 'SequentialMusic 'void #t))
567
568
569
570 quoteDuring = #
571 (define-music-function
572   (parser location what main-music)
573   (string? ly:music?)
574   (make-music 'QuoteMusic
575               'element main-music
576               'quoted-music-name what
577               'origin location))
578
579
580
581 resetRelativeOctave  =
582 #(define-music-function
583     (parser location reference-note)
584     (ly:music?)
585     (_i "Set the octave inside a \\relative section.")
586
587    (let*
588     ((notes (ly:music-property reference-note 'elements))
589      (pitch (ly:music-property (car notes) 'pitch)))
590
591     (set! (ly:music-property reference-note 'elements) '())
592     (set! (ly:music-property reference-note
593        'to-relative-callback)
594        (lambda (music last-pitch)
595         pitch))
596
597     reference-note))
598
599
600
601 shiftDurations =
602 #(define-music-function (parser location dur dots arg) (integer? integer? ly:music?)
603    (_i "")
604    
605    (music-map
606     (lambda (x)
607       (shift-one-duration-log x dur dots)) arg))
608
609 spacingTweaks =
610 #(define-music-function (parser location parameters) (list?)
611    (_i "Set the system stretch, by reading the 'system-stretch property of
612 the `parameters' assoc list.")
613    #{
614       \overrideProperty #"Score.NonMusicalPaperColumn"
615         #'line-break-system-details
616         #$(list (cons 'alignment-extra-space (cdr (assoc 'system-stretch parameters)))
617                 (cons 'system-Y-extent (cdr (assoc 'system-Y-extent parameters))))
618    #})
619
620 %% Parser used to read page-layout file, and then retreive score tweaks.
621 #(define page-layout-parser #f)
622
623 includePageLayoutFile = 
624 #(define-music-function (parser location) ()
625    (_i "If page breaks and tweak dump is not asked, and the file
626 <basename>-page-layout.ly exists, include it.")
627    (if (not (ly:get-option 'dump-tweaks))
628        (let ((tweak-filename (format #f "~a-page-layout.ly"
629                                      (ly:parser-output-name parser))))
630          (if (access? tweak-filename R_OK)
631              (begin
632                (ly:message "Including tweak file ~a" tweak-filename)
633                (set! page-layout-parser (ly:parser-clone parser))
634                (ly:parser-parse-string page-layout-parser
635                                        (format #f "\\include \"~a\""
636                                                tweak-filename))))))
637    (make-music 'SequentialMusic 'void #t))
638
639
640 rightHandFinger =
641 #(define-music-function (parser location finger) (number-or-string?)
642    (_i "Define a StrokeFingerEvent")
643    
644    (apply make-music
645           (append
646            (list 
647             'StrokeFingerEvent
648             'origin location)
649            (if  (string? finger)
650                 (list 'text finger)
651                 (list 'digit finger)))))
652
653 scoreTweak =
654 #(define-music-function (parser location name) (string?)
655    (_i "Include the score tweak, if exists.")
656    (if (and page-layout-parser (not (ly:get-option 'dump-tweaks)))
657        (let ((tweak-music (ly:parser-lookup page-layout-parser
658                                             (string->symbol name))))
659          (if (ly:music? tweak-music)
660              tweak-music
661              (make-music 'SequentialMusic)))
662        (make-music 'SequentialMusic)))
663
664 transposedCueDuring =
665 #(define-music-function
666   (parser location what dir pitch-note main-music)
667   (string? ly:dir? ly:music? ly:music?)
668
669   (_i "Insert notes from the part @var{what} into a voice called @code{cue},
670 using the transposition defined by @var{pitch-note}.  This happens
671 simultaneously with @var{main-music}, which is usually a rest.  The
672 argument @var{dir} determines whether the cue notes should be notated
673 as a first or second voice.")
674
675   (make-music 'QuoteMusic
676               'element main-music
677               'quoted-context-type 'Voice
678               'quoted-context-id "cue"
679               'quoted-music-name what
680               'quoted-voice-direction dir
681               'quoted-transposition (pitch-of-note pitch-note)
682               'origin location))
683
684
685
686 transposition =
687 #(define-music-function (parser location pitch-note) (ly:music?)
688    (_i "Set instrument transposition")
689
690    (context-spec-music
691     (make-property-set 'instrumentTransposition
692                        (ly:pitch-negate (pitch-of-note pitch-note)))
693         'Staff))
694
695 tweak = #(define-music-function (parser location sym val arg)
696            (symbol? scheme? ly:music?)
697
698            (_i "Add @code{sym . val} to the @code{tweaks} property of @var{arg}.")
699            
700            (set!
701             (ly:music-property arg 'tweaks)
702             (acons sym val
703                    (ly:music-property arg 'tweaks)))
704            arg)
705
706 tag = #(define-music-function (parser location tag arg)
707    (symbol? ly:music?)
708
709    (_i "Add @var{tag} to the @code{tags} property of @var{arg}.")
710
711    (set!
712     (ly:music-property arg 'tags)
713     (cons tag
714           (ly:music-property arg 'tags)))
715    arg)
716
717
718 unfoldRepeats =
719 #(define-music-function (parser location music) (ly:music?)
720                   (unfold-repeats music))
721
722
723
724 withMusicProperty =
725 #(define-music-function (parser location sym val music) (symbol? scheme? ly:music?)
726    (_i "Set @var{sym} to @var{val} in @var{music}.")
727
728    (set! (ly:music-property music sym) val)
729    music)
730
731