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