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