]> git.donarmstrong.com Git - lilypond.git/blob - ly/articulate.ly
Merge branch 'translation' of ssh://git.sv.gnu.org/srv/git/lilypond into translation
[lilypond.git] / ly / articulate.ly
1 %
2 % Copyright (C) 2008, 2009, 2010, 2011 NICTA
3 % Author: Peter Chubb <peter.chubb AT nicta.com.au>
4 % $Id: articulate.ly,v 1.7 2011-03-24 00:40:00 peterc Exp $
5 %
6 %
7 %  This program is free software; you can redistribute it and/or modify
8 %  it under the terms of the GNU General Public License, version 3,
9 %  as published by the Free Software Foundation.
10 %
11 %  WARNING: this file under GPLv3 only, not GPLv3+
12 %
13 %  This program is distributed in the hope that it will be useful,
14 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 %  See the GNU General Public License for more details.  It is
17 %  available in the Lilypond source tree, or at
18 %  http://www.gnu.org/licenses/gpl-3.0.html
19 %
20 % This script tries to make MIDI output from LilyPond a little more realistic.
21 % It tries to take articulations (slurs, staccato, etc) into account, by
22 % replacing notes  with sequential music of suitably time-scaled note plus
23 % skip.
24 %
25 % It also tries to unfold trills turns etc., and take rallentendo
26 % and accelerando into account.
27 %
28 % As my scheme knowledge is poor (I was teaching myself as I went), there
29 % is much scope for improvement.
30
31 %
32 %%%USAGE
33 % In the \score section do:
34 %  \unfoldRepeats \articulate <<
35 %       all the rest of the score
36 % >>
37 % or use the lilywrap script.
38 %
39 % TO DO (prioritised, the ones that'll make the most difference first)
40 %
41 % * Dynamics.
42 %   * Fix quantisation for dynamics on single note (replace note
43 %     with tied 128th notes?) -- started, needs work.
44 %   * Make \fp do the right thing (loud start, then quiet).
45 %
46 % * Inegalite.  Notes on-beat steal time from notes off-beat.
47 %   Degree of stealing is a parameter: from 1.0 (straight)
48 %   to 1.5 (extreme swing).  Also fix tenuto to use this.
49 %
50 % * add accel (to match rall), and molto rall. I've never seen
51 %   molto accel but some composer somewhere has probably used it.
52 %
53 % * Fermata, and Fermata Lunga
54 % * Add more synonyms for accel and rall: rit ritard stringendo
55 %
56 % * Phrasing.
57 %   * Rall at end of piece
58 %   * Very slight accel into a phrase, rall out of it.
59 %   * Dynamics during a phrase????  unclear how these should be in
60 %     the general case
61 %
62 % * Trill algorithm needs work.
63 %
64 % * Cope with more ornaments/articulations.
65 %    inverted-turns, etc.
66 %   -- accent needs better control of dynamics.
67 %   -- Articulations: mezzo-staccato, portato.
68 %   -- Handling of generic ornaments (in lily, `\stopped'; in
69 %               most early music:  ornament this note (trill, turn
70 %               or mordent as the player wishes))
71 % * Automatic gruppetto at end of trill; better handling of
72 %      initial/final grace notes on trill
73 % * Automatic ornaments.
74 %   * Spot cadences and ornament
75 %   * Look for quaver-dotted note for trills, for example.
76 %   * Fill in steps. (Needs lookahead/lookbehind.)
77 % * `afterturn' -- a turn after the start of a note.
78 % * accidentals for trills and turns
79
80 % CHANGELOG
81 %  * David Kastrup: basic 2.15.28 compatibility by using event-chord-wrap!
82 %    This should really be done by rewriting the code more thoroughly.
83 %  * From Iain Nicol: appoggiatura timings were out; add staccatissimo; fix
84 %    trillSpanner endpoints.
85 %  * Also handle Breathing events (by throwing them away).  This isn't ideal;
86 %    one should really shorten the note before a little.  But I don't know
87 %    how to do lookahead in scheme.
88 %  * Also ignore explicit line breaks.
89 %  * Add Mordents (reported by Patrick Karl)
90 %
91
92 \version "2.16.0"
93
94 #(use-modules (ice-9 debug))
95 #(use-modules (scm display-lily))
96
97 % PARAMETERS
98 % How much to compress notes marked Staccato.  CPE Bach says `as short as
99 % may conveniently be played, as if the keys were too hot to touch'.
100 % Most modern sources say 1/2 the notated length of a note.
101 #(define ac:staccatoFactor '(1 . 2))
102
103 % How much to compress notes marked staccatissimo.
104 #(define ac:staccatissimoFactor '(1 . 4))
105
106 % And tenuto (if we ever implement time stealing, this should be >1.0)
107 #(define ac:tenutoFactor '(1 . 1))
108
109 % How much to articulate normal notes.  CPE Bach says 1/2 (and
110 % staccato should be `as short as may conveniently be played') but this
111 % sounds too short for modern music.  7/8 sounds about right.
112 #(define ac:normalFactor '(7 . 8))
113
114 % How much to slow down for a rall. or a poco rall.
115 % (or speed up for accel or poco accel)
116 #(define ac:rallFactor (ly:make-moment 60 100)) % 40% slowdown
117 #(define ac:pocoRallFactor (ly:make-moment 90 100)) % 10% slowdown
118
119 % The absolute time for a twiddle in a trill, in minutes.
120 % Start with 1/4 seconds == 1/240 minutes
121 #(define ac:maxTwiddleTime (ly:make-moment 1 240))
122
123
124 % Internal variables, don't touch.
125 % (should probably be part of a context somehow)
126
127 % Whether to slur, or not
128 #(define ac:inSlur #f)
129 #(define ac:inPhrasingSlur #f)
130
131 % Whether the current noteevent is in a trill spanner
132 #(define ac:inTrill #f)
133
134 % assume start in C major.  Key change events override this.
135 % Could get from context, but don't know how.
136 #(define ac:current-key (make-music
137           'KeyChangeEvent
138           'tonic
139           (ly:make-pitch -1 0 0)
140           'pitch-alist
141           (list (cons 0 0)
142                 (cons 1 0)
143                 (cons 2 0)
144                 (cons 3 0)
145                 (cons 4 0)
146                 (cons 5 0)
147                 (cons 6 0))))
148
149
150 #(define ac:currentTempo (ly:make-moment 15 1)) % 4 = 60, measured wholes per minute
151 #(define ac:lastTempo ac:currentTempo) % for 'a tempo' or 'tempo I'
152
153 % The duration of the current note.  Start at a crotchet
154 % for no good reason.
155 #(define ac:currentDuration (ly:make-duration 2 0 1 1))
156
157 % Debugging: display a moment plus some text.
158 % Returns its moment argument so can be used in-line.
159 #(define (display-moment  text m)
160   (display text)
161   (display (list (ly:moment-main-numerator m) "/" (ly:moment-main-denominator m)))
162   m
163 )
164
165 % Track tempo (and maybe later, other context properties)
166 % as they change.  Needs to better cope with saving only Tempo I,
167 % otherwise "a tempo" goes back to the tempo before the last change.
168 #(define (ac:adjust-props sym music)
169   (case sym
170    ((tempoWholesPerMinute)
171     (set! ac:currentTempo (ly:music-property music 'value))
172     (set! ac:lastTempo ac:currentTempo)
173   )))
174
175 % Raise note one step in the current diatonic scale.
176 #(define (ac:up note)
177   (let* ((pitch (ly:music-property note 'pitch))
178          (notename (ly:pitch-notename pitch))
179          (new-notename (if (eq? notename 6) 0 (+ 1 notename)))
180          (alterations (ly:music-property ac:current-key 'pitch-alist))
181          (new-alteration (cdr (assq new-notename alterations)))
182          (new-octave (if (eq? new-notename 0) (+ 1 (ly:pitch-octave pitch))
183                       (ly:pitch-octave pitch)))
184        )
185    (set! (ly:music-property note 'pitch)(ly:make-pitch new-octave new-notename new-alteration))))
186
187
188 % Lower note one step in the current diatonic scale.
189 #(define (ac:down note)
190   (begin  (let* ((pitch (ly:music-property note 'pitch))
191          (notename (ly:pitch-notename pitch))
192          (new-notename (if (eq? notename 0) 6 (- notename 1)))
193          (alterations (ly:music-property ac:current-key 'pitch-alist))
194          (new-alteration (cdr (assq new-notename alterations)))
195          (new-octave (if (eq? new-notename 6) (- (ly:pitch-octave pitch) 1)
196                       (ly:pitch-octave pitch)))
197        )
198    (set! (ly:music-property note 'pitch)(ly:make-pitch new-octave new-notename new-alteration))))
199 )
200
201 % Shorten a note, and save the note's original duration in ac:currentDuration
202 #(define (ac:articulate-one-note m fraction)
203   "Replace m with m*fraction"
204   (if  (eq? 'NoteEvent (ly:music-property m 'name))
205    (let*
206     ((dur (ly:music-property m 'duration))
207      (l (ly:duration-log dur))
208      (d (ly:duration-dot-count dur))
209      (factor (ly:duration-factor dur))
210      (num (car fraction))
211      (denom (cdr fraction)))
212     (begin
213      (set! ac:currentDuration dur)
214      (set! (ly:music-property m 'duration)
215       (ly:make-duration l d
216        (* num (car factor))
217        (* denom (cdr factor))))))
218    m))
219
220 % helper routine to set duration.
221 #(define (ac:setduration music duration)
222   "Set a note's duration."
223   (let ((eventtype (ly:music-property music 'name)))
224    (if
225     (or
226      (eq? eventtype 'NoteEvent)
227      (eq? eventtype 'RestEvent)
228      (eq? eventtype 'SkipEvent))
229     (set! (ly:music-property music 'duration) duration))))
230
231 % Add an articulation event to a note.
232 % Used in afterGrace to mark all notes as tenuto, so they're not shortened
233 #(define (ac:add-articulation type music)
234     (music-map (lambda (m)
235                 (if (eq? 'EventChord (ly:music-property m 'name))
236                  (set! (ly:music-property m 'elements)
237                   (append (ly:music-property m 'elements)
238                    (list (make-music 'ArticulationEvent 'articulation-type type)))))
239                 m)
240      music))
241
242 % Convert a long note to an equivalent set of short notes, tied together.
243 % This is needed to get smooth dynamics changes.
244 % Need to deal properly with stuff other than the notes (dynamics, markup etc)
245 % Still experimental, so disabled for now.
246 #(define (ac:to128 music) music)
247
248 #(define (ac:to128_disabled music)
249   (if (or (eq? 'SkipEvent (ly:music-property music 'name))
250         (eq? 'NoteEvent (ly:music-property music 'name)))
251    (let* ((dur (ly:music-property music 'duration))
252           (log2 (ly:duration-log dur))
253          (shiftcount (- 6 log2))
254          (lastm (ly:music-deep-copy (shift-duration-log music shiftcount 0))))
255    (set! (ly:music-property music 'elements)
256     (cons (make-music 'TieEvent) (ly:music-property music 'elements)))
257    (make-sequential-music (list
258                            (make-repeat "unfold" (1- (expt 2 shiftcount))
259                             (make-sequential-music (list music)) '())
260                            lastm)))
261  music))
262
263
264 % absolute time in minutes of a length of music, as a rational number (moment)
265 #(define (ac:abstime music)
266   (ly:moment-div (ly:music-length music) ac:currentTempo))
267
268 % convert absolute time (in minutes) to a moment in the current tempo
269 #(define (ac:abs->mom m)
270   (ly:moment-mul m ac:currentTempo))
271
272
273 % a moment that is ac:maxTwiddletime seconds at the current tempo.
274 #(define (ac:targetTwiddleTime)
275   (ac:abs->mom ac:maxTwiddleTime))
276
277
278 % Nearest twiddletime (in minutes) achievable with power-of-2 divisions of
279 % the original music.  (twiddletime is the time for one pair of notes
280 % in a trill)
281 % If the music has a precomputed twiddletime (e.g., from \afterGrace) use that.
282 #(define (ac:twiddletime music)
283   (let* ((tr (filter (lambda (x)
284                      (and (eq? 'ArticulationEvent (ly:music-property x 'name))
285                       (string= "trill" (ly:music-property x 'articulation-type))))
286               (ly:music-property music 'elements)))
287          (pre-t (if (pair? tr) (ly:music-property (car tr) 'twiddle)
288                  '()))
289          (t (ac:targetTwiddleTime)))
290    (if (ly:moment? pre-t)
291     pre-t
292     (let loop ((len (ly:music-length music)))
293      (if (ly:moment<? t len)
294       (loop (ly:moment-mul len (ly:make-moment 1 2)))
295       len)))))
296
297
298
299 % Note: I'm assuming early music practice of starting on the auxiliary note.
300 % Needs to add gruppetto if it's a long trill (TODO)
301 #(define (ac:trill music)
302   " Replace music with time-compressed repeats of the music,
303     maybe accelerating if the length is longer than a crotchet "
304   (let* ((hemisemidur (ly:make-duration 5 0 1 1))
305          (orig-len  (ly:music-length music))
306          (t (ac:twiddletime music))
307          (uppernote '())
308          (note_moment (ly:moment-mul t (ly:make-moment 1 2)))
309          (c1 (ly:moment-div orig-len note_moment))
310          (c2 (inexact->exact
311               (round (/ (ly:moment-main-numerator c1)
312                       (* 2 (ly:moment-main-denominator c1))))))
313          (count (if (< c2 2) 2 c2)))
314
315    (set! (ly:music-property music 'elements)
316     (filter (lambda (y) (eq? 'NoteEvent (ly:music-property y 'name)))
317      (ly:music-property music 'elements)))
318    (map (lambda (y) (ac:setduration y hemisemidur))
319     (ly:music-property music 'elements))
320    (set! uppernote (ly:music-deep-copy music))
321    (map (lambda (y) (ac:up y))
322     (filter
323      (lambda (z) (eq? 'NoteEvent (ly:music-property z 'name)))
324      (ly:music-property uppernote 'elements)))
325
326    (let* ((trillMusicElements
327           (let loop ((so_far (list uppernote music))
328                      (c count))
329            (if (> c 1)
330             (loop (append (list (ly:music-deep-copy uppernote) (ly:music-deep-copy music)) so_far) (1- c))
331             so_far)))
332           (trillMusic (make-sequential-music trillMusicElements))
333           (newlen (ly:music-length trillMusic))
334           (factor (ly:moment-div  orig-len newlen)))
335     (ly:music-compress trillMusic factor)
336 ; accelerating the music seems to put lily into an infinite loop in
337 ; its layout and midi engines.
338 ;    (let* ((realfactor (exp (* (/ 1.0 count) (log 0.75))))
339 ;          (factor (ly:make-moment (inexact->exact (round (* 1024 realfactor)))
340 ;                   1024)))
341 ;     (ac:accel trillMusic factor))
342  )))
343
344 %
345 % Generate a tempoChangeEvent and its associated property setting.
346 %
347 #(define (ac:tempoChange tempo)
348   (make-sequential-music
349    (list (make-music 'TempoChangeEvent
350           'metronome-count
351           tempo
352           'tempo-unit
353           (ly:make-duration 0 0 1 1))
354     (context-spec-music
355     (make-property-set 'tempoWholesPerMinute  tempo) 'Score))))
356
357 % If there's an articulation, use it.
358 % If in a slur, use (1 . 1) instead.
359 % Treat phrasing slurs as slurs, but allow explicit articulation.
360 % (Maybe should treat staccato under a phrasing slur as mezzo-staccato?)
361 %
362 % Expect an EventChord.
363 %
364 % trills, turns, ornaments etc.  are also treated as Articulations.
365 % Split into two functions:
366 %  ac:getactions traverses the elements in the EventChord
367 %               and calculates the parameters.
368 %  ac:articulate-chord applies the actions to each NoteEvent in
369 %               the EventChord.
370 #(define (ac:getactions music)
371   (let  loop ((factor ac:normalFactor)
372               (newelements '())
373               (es (ly:music-property music 'elements))
374               (actions '()))
375    (if (null? es)
376     (begin
377      (set! (ly:music-property music 'elements) (reverse newelements))
378      (cond
379       (ac:inTrill (cons 'trill actions))
380       ((and (eq? factor ac:normalFactor) (or ac:inSlur ac:inPhrasingSlur))
381        (append actions (list 'articulation  '(1 . 1)) ))
382       (else (append actions (list 'articulation  factor)))))
383     ; else part
384     (let ((e (car es))
385           (tail (cdr es)))
386      (case (ly:music-property e 'name)
387
388       ((BeamEvent) ; throw away beam events, or they'll be duplicated by turn or trill
389        (loop factor newelements tail actions))
390
391       ((LineBreakEvent FingeringEvent MarkEvent BreathingEvent TieEvent SkipEvent RestEvent) ; pass through some events.
392        (loop (cons 1 1) (cons e newelements) tail actions))
393
394       ((ArticulationEvent)
395        (let ((articname (ly:music-property e 'articulation-type)))
396         ; TODO: add more here
397         (cond
398          ((string= articname "staccato")
399           (loop ac:staccatoFactor newelements tail actions))
400          ((string= articname "staccatissimo")
401           (loop ac:staccatissimoFactor newelements tail actions))
402          ((string= articname "tenuto")
403           (loop ac:tenutoFactor newelements tail actions))
404          ((string= articname "mordent")
405           (loop (cons 1 1) newelements tail (cons 'mordent actions)))
406          ((string= articname "prall")
407           (loop (cons 1 1) newelements tail (cons 'prall actions)))
408          ((string= articname "trill")
409           (loop (cons 1 1) newelements tail (cons 'trill actions)))
410          ((string= articname "turn")
411           (loop (cons 1 1) newelements tail (cons 'turn actions)))
412          (else (loop factor (cons e newelements) tail actions)))))
413
414       ((TextScriptEvent)
415        (let ((t (ly:music-property e 'text)))
416         (if (not (string? t))
417          (loop factor (cons e newelements) tail actions)
418          (begin
419           (cond
420            ((or
421              (string= t "rall")
422              (string= t "Rall")
423              (string= t "rit.")
424              (string= t "rall."))
425             (loop factor (cons e newelements) tail (cons 'rall actions)))
426            ((or
427              (string= t "accelerando")
428              (string= t "accel")
429              (string= t "accel."))
430             (loop factor (cons e newelements) tail (cons 'accel actions)))
431            ((or
432              (string= t "poco accel."))
433             (loop factor (cons e newelements) tail (cons 'pocoAccel actions)))
434            ((or
435              (string= t "poco rall.")
436              (string= t "poco rit."))
437             (loop factor (cons e newelements) tail (cons 'pocoRall actions)))
438            ((or (string= t "a tempo")
439              (string= t "tempo I"))
440           (loop factor (cons e newelements) tail (cons 'aTempo actions)))
441            (else (loop factor (cons e newelements) tail actions)))))))
442
443       ((SlurEvent)
444        (let ((direction (ly:music-property e 'span-direction)))
445         (set! ac:inSlur (eq? direction -1))
446         (loop factor newelements tail actions)))
447
448       ((TrillSpanEvent)
449        (let ((direction (ly:music-property e 'span-direction)))
450         (set! ac:inTrill (eq? direction -1))
451         (if ac:inTrill
452          (loop factor newelements tail (cons 'trill actions))
453          (loop factor (cons e newelements) tail actions))))
454
455       ((PhrasingSlurEvent)
456        (let ((direction (ly:music-property e 'span-direction)))
457         (set! ac:inPhrasingSlur (eq? direction -1))
458         (loop factor newelements tail actions)))
459
460       (else (loop factor (cons e newelements) tail actions)))))))
461
462
463
464 #(define (ac:articulate-chord music)
465   (begin
466    (cond
467
468     ((eq? 'EventChord (ly:music-property music 'name))
469      (let loop ((actions (ac:getactions music)))
470       (if (null? actions)
471         (if (ly:moment> (ly:music-length music) (make-moment 1 4))
472          (ac:to128  music)
473          music)
474
475       (case (car actions)
476
477        ((articulation)
478         (map
479          (lambda (x) (ac:articulate-one-note x (cadr actions)))
480          (ly:music-property music 'elements))
481         (let*
482          ((num (caadr actions))
483           (denom (cdadr actions))
484           (mult (ly:duration-factor ac:currentDuration))
485           (newnum (* (- denom num) (car mult)))
486           (newdenom (* (cdr mult) denom))
487           (len (ly:duration-log ac:currentDuration))
488           (dots (ly:duration-dot-count ac:currentDuration)))
489
490          (if (not (eq? num denom))
491           (make-sequential-music
492            (list (ac:to128 music)
493            (make-music 'EventChord 'elements
494             (list
495              (make-music 'RestEvent 'duration (ly:make-duration len dots newnum newdenom))))))
496           music)))
497
498        ((accel)
499         (set! ac:lastTempo ac:currentTempo)
500         (set! ac:currentTempo (ly:moment-div ac:currentTempo ac:rallFactor))
501         (let ((pset (ac:tempoChange ac:currentTempo)))
502          (if (null? (cdr actions))
503           (make-sequential-music (list pset music))
504           (make-sequential-music
505            (list pset (loop (cdr actions)))))))
506
507        ((pocoAccel)
508         (set! ac:lastTempo ac:currentTempo)
509         (set! ac:currentTempo (ly:moment-div ac:currentTempo ac:pocoRallFactor))
510         (let ((pset (ac:tempoChange ac:currentTempo)))
511          (if (null? (cdr actions))
512           (make-sequential-music (list pset music))
513           (make-sequential-music
514            (list pset (loop (cdr actions)))))))
515
516        ((rall)
517         (set! ac:lastTempo ac:currentTempo)
518         (set! ac:currentTempo (ly:moment-mul ac:currentTempo ac:rallFactor))
519         (let ((pset (ac:tempoChange ac:currentTempo)))
520          (if (null? (cdr actions))
521           (make-sequential-music (list pset music))
522           (make-sequential-music
523            (list pset (loop (cdr actions)))))))
524
525        ((pocoRall)
526         (set! ac:lastTempo ac:currentTempo)
527         (set! ac:currentTempo (ly:moment-mul ac:currentTempo ac:pocoRallFactor))
528         (let ((pset (ac:tempoChange ac:currentTempo)))
529          (if (null? (cdr actions))
530           (make-sequential-music (list pset music))
531           (make-sequential-music
532            (list pset (loop (cdr actions)))))))
533
534        ((aTempo)
535         (set! ac:currentTempo ac:lastTempo)
536
537         (let ((pset (ac:tempoChange ac:currentTempo)))
538          (if (null? (cdr actions))
539           (make-sequential-music (list pset music))
540           (make-sequential-music
541            (list pset (loop (cdr actions)))))))
542
543        ((trill)
544          (ac:trill music))
545
546        ((prall)
547         ; A pralltriller symbol can either mean an inverted mordent
548         ; or a half-shake -- a short, two twiddle trill.
549         ; We implement as a half-shake.
550         (let*
551          ((totallength (ly:music-length music))
552           (newlen (ly:moment-sub totallength (ly:make-moment 3 32)))
553           (newdur (ly:make-duration
554                    0 0
555                    (ly:moment-main-numerator newlen)
556                    (ly:moment-main-denominator newlen)))
557           (gracedur (ly:make-duration 5 0 1 1))
558           (gracenote (ly:music-deep-copy music))
559           (abovenote (ly:music-deep-copy music))
560           (mainnote (ly:music-deep-copy music))
561           (prall (make-sequential-music (list gracenote abovenote)))
562         )
563           (music-map (lambda (n)
564            (if (eq? 'NoteEvent (ly:music-property n 'name))
565             (set! (ly:music-property n 'duration) gracedur))
566                       n)
567            abovenote)
568           (music-map (lambda (n)
569            (if (eq? 'NoteEvent (ly:music-property n 'name))
570             (set! (ly:music-property n 'duration) gracedur))
571                       n)
572            gracenote)
573           (music-map (lambda (n)
574            (if (eq? 'NoteEvent (ly:music-property n 'name))
575             (set! (ly:music-property n 'duration) newdur))
576                       n)
577            mainnote)
578
579           (map (lambda (y) (ac:up y))
580            (filter
581             (lambda (z) (eq? 'NoteEvent (ly:music-property z 'name)))
582             (ly:music-property abovenote 'elements)))
583           (make-sequential-music (list abovenote gracenote abovenote mainnote))))
584
585        ((mordent)
586         (let*
587          ((totaldur (ly:music-property
588                 (car (ly:music-property music 'elements)) 'duration))
589           (dur (ly:duration-length totaldur))
590           (newlen (ly:moment-sub dur (ly:make-moment 2 32)))
591           (newdur (ly:make-duration
592                 0 0
593                    (ly:moment-main-numerator newlen)
594                    (ly:moment-main-denominator newlen)))
595           (gracenote (ly:music-deep-copy music))
596           (belownote (ly:music-deep-copy music))
597           (mainnote (ly:music-deep-copy music))
598           (mordent (make-sequential-music (list gracenote belownote)))
599         )
600          (begin
601           (music-map (lambda (n)
602            (if (eq? 'NoteEvent (ly:music-property n 'name))
603             (set! (ly:music-property n 'duration)
604              (ly:make-duration 5 0 1 1)))
605                       n)
606            mordent)
607           (music-map (lambda (n)
608            (if (eq? 'NoteEvent (ly:music-property n 'name))
609             (set! (ly:music-property n 'duration) newdur))
610                       n)
611            mainnote)
612           (map (lambda (y) (ac:down y))
613            (filter
614             (lambda (z) (eq? 'NoteEvent (ly:music-property z 'name)))
615             (ly:music-property belownote 'elements)))
616           (make-sequential-music (list mordent mainnote)))))
617        ((turn)
618         (let*
619          ((dur (ly:music-property
620                 (car (ly:music-property music 'elements)) 'duration))
621           (factor (ly:duration-factor dur))
622           (newdur (ly:make-duration (+ (ly:duration-log dur) 2)
623                    (ly:duration-dot-count dur) (car factor)(cdr factor))))
624          (begin
625           (map (lambda (y) (ac:setduration y newdur))
626            (ly:music-property music 'elements))
627           (let* ((above (ly:music-deep-copy music))
628                  (below (ly:music-deep-copy music))
629                  (newmusic (make-sequential-music (list above music below music))))
630            (begin
631             (map (lambda (y) (ac:down y))
632              (filter
633               (lambda (z) (eq? 'NoteEvent (ly:music-property z 'name)))
634               (ly:music-property below 'elements)))
635             (map (lambda (y) (ac:up y))
636              (filter
637               (lambda (z) (eq? 'NoteEvent (ly:music-property z 'name)))
638               (ly:music-property above 'elements)))
639             newmusic)))))
640      ))))
641
642     ((eq? 'KeyChangeEvent (ly:music-property music 'name))
643      (set! ac:current-key music)
644      music
645    )
646
647     ((eq? 'PropertySet (ly:music-property music 'name))
648      (ac:adjust-props (ly:music-property music 'symbol) music)
649      music)
650
651     (else music))
652  ))
653
654
655
656 % At last ... here's the music function that applies all the above to a
657 % score.
658 articulate = #(define-music-function (parser location music)
659                (ly:music?)
660                "Adjust times of note to add tenuto, staccato and
661                 normal articulations.
662                 "
663                (set! music (event-chord-wrap! music parser))
664                (music-map ac:articulate-chord music)
665                )
666
667
668 % Override \afterGrace to be in terms of audio, not spacing.
669 % Special handling for a gruppetto after a trill.
670 afterGrace =
671 #(define-music-function
672   (parser location main grace)
673   (ly:music? ly:music?)
674
675   (set! main (event-chord-wrap! main parser))
676   (set! grace (event-chord-wrap! grace parser))
677   (let*
678    ((main-length (ly:music-length main))
679     (grace-orig-length (ly:music-length grace))
680     (gracelen (ac:twiddletime main))
681     (grace-factor (ly:moment-div gracelen grace-orig-length))
682     (new-main-length (ly:moment-sub main-length gracelen))
683     (factor (ly:moment-div new-main-length main-length))
684   )
685    (map (lambda (y) (set! (ly:music-property y 'twiddle) gracelen))
686          (filter (lambda (z)
687                   (and
688                    (eq? 'ArticulationEvent (ly:music-property z 'name))
689                    (string= "trill" (ly:music-property z 'articulation-type))))
690           (ly:music-property main 'elements)))
691    (ac:add-articulation "tenuto" grace)
692    (make-sequential-music  (list (ly:music-compress main factor) (ly:music-compress grace grace-factor)))))
693
694 % An appoggiatura takes half the duration of the main note,
695 % or 1/3 if the note is dotted (i.e., half the undotted equivalent time)
696 % Somewhere around the end of the 19th, start of 20th century the rules
697 % changed, but my main interest is early music.
698 appoggiatura =
699 #(define-music-function (parser location grace main)
700   (ly:music? ly:music?)
701   (set! grace (event-chord-wrap! grace parser))
702   (set! main (event-chord-wrap! main parser))
703   (let* ((maindur (ly:music-length main))
704          (grace-orig-len (ly:music-length grace))
705          (main-orig-len (ly:music-length main))
706          (numerator (ly:moment-main-numerator maindur))
707          (factor (if (eq? (remainder numerator 3) 0)
708                   (ly:make-moment 1 3) (ly:make-moment 1 2))))
709    (ly:music-compress grace
710     (ly:moment-mul factor (ly:moment-div main-orig-len grace-orig-len)))
711    (ly:music-compress main (ly:moment-sub (ly:make-moment 1 1) factor))
712
713     (set! (ly:music-property grace 'elements)
714      (append (ly:music-property grace 'elements)
715       (list (make-music 'SlurEvent 'span-direction -1))))
716     (set! (ly:music-property main 'elements)
717      (append (ly:music-property main 'elements)
718       (list (make-music 'SlurEvent 'span-direction 1))))
719      (make-sequential-music (list grace main))))
720