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