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