1 ;;;; auto-beam.scm -- Auto-beam-engraver settings
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2000--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; specify generic beam end times
11 ;;; function shortest-duration-in-beam time-signature
15 ;;; function = begin or end
16 ;;; shortest-duration-in-beam = numerator denominator; e.g.: 1 16
17 ;;; time-signature = numerator denominator, e.g.: 4 4
19 ;;; unspecified or wildcard entries for duration or time-signature
22 ;;; maybe do: '(end shortest-1 16 time-3 4) ?
24 (define-public default-auto-beam-settings
27 ;; use beatLength for all except 32nd notes
28 ;; end beams with 32nd notes each 1 4 beat
30 ((end 1 32 2 2) . ,(ly:make-moment 1 4))
31 ((end 1 32 2 2) . ,(ly:make-moment 2 4))
32 ((end 1 32 2 2) . ,(ly:make-moment 3 4))
35 ;; use beatLength for all except 16th and 32nd notes
36 ;; end beams with 16th notes each 1 4 beat
37 ;; end beams with 32nd notes each 1 8 beat
39 ((end 1 16 3 2) . ,(ly:make-moment 1 4))
40 ((end 1 16 3 2) . ,(ly:make-moment 2 4))
41 ((end 1 16 3 2) . ,(ly:make-moment 3 4))
42 ((end 1 16 3 2) . ,(ly:make-moment 4 4))
43 ((end 1 16 3 2) . ,(ly:make-moment 5 4))
45 ((end 1 32 3 2) . ,(ly:make-moment 1 8))
46 ((end 1 32 3 2) . ,(ly:make-moment 2 8))
47 ((end 1 32 3 2) . ,(ly:make-moment 3 8))
48 ((end 1 32 3 2) . ,(ly:make-moment 4 8))
49 ((end 1 32 3 2) . ,(ly:make-moment 5 8))
50 ((end 1 32 3 2) . ,(ly:make-moment 6 8))
51 ((end 1 32 3 2) . ,(ly:make-moment 7 8))
52 ((end 1 32 3 2) . ,(ly:make-moment 8 8))
53 ((end 1 32 3 2) . ,(ly:make-moment 9 8))
54 ((end 1 32 3 2) . ,(ly:make-moment 10 8))
55 ((end 1 32 3 2) . ,(ly:make-moment 11 8))
58 ;; use beatLength for all except 32nd notes
59 ;; end beams with 32nd notes each 1 8 beat
61 ((end 1 32 2 4) . ,(ly:make-moment 1 8))
62 ((end 1 32 2 4) . ,(ly:make-moment 2 8))
63 ((end 1 32 2 4) . ,(ly:make-moment 3 8))
66 ;; override beatLength which would end beams at 1 4 beats
67 ;; end beams with 16th notes each 1 4 beat
68 ;; end beams with 32nd notes each 1 8 beat
70 ((end * * 3 4) . ,(ly:make-moment 3 4))
71 ((end 1 16 3 4) . ,(ly:make-moment 1 4))
72 ((end 1 16 3 4) . ,(ly:make-moment 2 4))
73 ((end 1 32 3 4) . ,(ly:make-moment 1 8))
74 ((end 1 32 3 4) . ,(ly:make-moment 2 8))
75 ((end 1 32 3 4) . ,(ly:make-moment 3 8))
76 ((end 1 32 3 4) . ,(ly:make-moment 4 8))
77 ((end 1 32 3 4) . ,(ly:make-moment 5 8))
80 ;; override beatLength which would end beams at 1 4 beats
81 ;; end all beams at 1 2 beat
82 ;; end beams with 8th triplets each 1 4 beat
83 ;; end beams with 16th notes each 1 4 beat
84 ;; end beams with 32nd notes each 1 8 beat
86 ((end * * 4 4) . ,(ly:make-moment 1 2))
88 ((end 1 12 4 4) . ,(ly:make-moment 1 4))
89 ((end 1 12 4 4) . ,(ly:make-moment 3 4))
91 ((end 1 16 4 4) . ,(ly:make-moment 1 4))
92 ((end 1 16 4 4) . ,(ly:make-moment 3 4))
94 ((end 1 32 4 4) . ,(ly:make-moment 1 8))
95 ((end 1 32 4 4) . ,(ly:make-moment 2 8))
96 ((end 1 32 4 4) . ,(ly:make-moment 3 8))
97 ((end 1 32 4 4) . ,(ly:make-moment 5 8))
98 ((end 1 32 4 4) . ,(ly:make-moment 6 8))
99 ((end 1 32 4 4) . ,(ly:make-moment 7 8))
102 ;; override beatLength which would end beams at 1 8 beats
104 ((end * * 3 8) . ,(ly:make-moment 3 8))
107 ;; override beatLength which would end beams at 1 8 beats
108 ;; end all beams at 1 4 beat
109 ;; end beams with 32nd notes each 1 8 beat
111 ((end * * 4 8) . ,(ly:make-moment 1 4))
112 ((end 1 32 4 8) . ,(ly:make-moment 1 8))
113 ((end 1 32 4 8) . ,(ly:make-moment 3 8))
115 ;; in 6 8, 9 8 and 12 8 time:
116 ;; use beatGrouping for all except 32nd notes
117 ;; end beams with 32nd notes each 1 8 beat
119 ((end 1 32 6 8) . ,(ly:make-moment 1 8))
120 ((end 1 32 6 8) . ,(ly:make-moment 2 8))
121 ((end 1 32 6 8) . ,(ly:make-moment 3 8))
122 ((end 1 32 6 8) . ,(ly:make-moment 4 8))
123 ((end 1 32 6 8) . ,(ly:make-moment 5 8))
125 ((end 1 32 9 8) . ,(ly:make-moment 1 8))
126 ((end 1 32 9 8) . ,(ly:make-moment 2 8))
127 ((end 1 32 9 8) . ,(ly:make-moment 3 8))
128 ((end 1 32 9 8) . ,(ly:make-moment 4 8))
129 ((end 1 32 9 8) . ,(ly:make-moment 5 8))
130 ((end 1 32 9 8) . ,(ly:make-moment 6 8))
131 ((end 1 32 9 8) . ,(ly:make-moment 7 8))
132 ((end 1 32 9 8) . ,(ly:make-moment 8 8))
134 ((end 1 32 12 8) . ,(ly:make-moment 1 8))
135 ((end 1 32 12 8) . ,(ly:make-moment 2 8))
136 ((end 1 32 12 8) . ,(ly:make-moment 3 8))
137 ((end 1 32 12 8) . ,(ly:make-moment 4 8))
138 ((end 1 32 12 8) . ,(ly:make-moment 5 8))
139 ((end 1 32 12 8) . ,(ly:make-moment 6 8))
140 ((end 1 32 12 8) . ,(ly:make-moment 7 8))
141 ((end 1 32 12 8) . ,(ly:make-moment 8 8))
142 ((end 1 32 12 8) . ,(ly:make-moment 9 8))
143 ((end 1 32 12 8) . ,(ly:make-moment 10 8))
144 ((end 1 32 12 8) . ,(ly:make-moment 11 8))
147 ;; end all beams each 1 8 beat
149 ((end * * 4 16) . ,(ly:make-moment 1 8))
153 (define (override-property-setting context property setting value)
154 "Like the C++ code that executes \\override, but without type
156 (ly:context-set-property!
158 (cons (cons setting value) (ly:context-property context property))))
160 (define (revert-property-setting context property setting)
161 "Like the C++ code that executes \revert, but without type
164 (define (revert-member alist entry new)
165 "Return ALIST, with ENTRY removed. ALIST is not modified, instead
166 a fresh copy of the list-head is made."
169 ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
170 (else (revert-member (cdr alist) entry (cons (car alist) new)))))
172 (ly:context-set-property!
174 (revert-member (ly:context-property context property) setting '())))
176 (define-public (override-auto-beam-setting setting num den . rest)
179 (make-apply-context (lambda (c)
180 (override-property-setting
182 setting (ly:make-moment num den))))
183 (if (and (pair? rest) (symbol? (car rest)))
187 (define-public (score-override-auto-beam-setting setting num den)
188 (override-auto-beam-setting setting num den 'Score))
190 (define-public (revert-auto-beam-setting setting num den . rest)
195 (revert-property-setting
197 (cons setting (ly:make-moment num den)))))
198 (if (and (pair? rest) (symbol? (car rest)))
202 ;; Determine end moment for auto beaming (or begin moment, but mostly
203 ;; 0== anywhere). In order of decreasing priority:
207 ;; 3. end <type> <num> <den>
208 ;; 4. end * <num> <den>
209 ;; 5. if 1-4 not specified, begin anywhere, end at time determined by
210 ;; beatGrouping and beatLength:
211 ;; if beatGrouping and beatLength are consistent with measureLength,
212 ;; use beatGrouping to determine end of beams.
213 ;; if beatGrouping and beatLength are inconsistent with measureLength,
214 ;; use beatLength to determine end of beams.
219 ;; 1. override for specific duration type
220 ;; 2. generic override
222 ;; [to be defined in config file]
223 ;; 3. exceptions for specific time signature, for specific duration type
224 ;; 4. exceptions for specific time signature
225 ;; 5. easy catch-all rule for non-specified measure types
228 (define-public (default-auto-beam-check context dir test)
229 (define (get name default)
230 (let ((value (ly:context-property context name)))
231 (if (not (null? value)) value default)))
233 (define (ending-moments group-list start-beat beat-length)
234 (if (null? group-list)
236 (let ((new-start (+ start-beat (car group-list))))
237 (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
238 (ending-moments (cdr group-list) new-start beat-length)))))
240 (define (make-end-settings time ending-list moment-den)
241 (if (null? ending-list)
243 (cons (cons (append '(end * *) time)
244 (ly:make-moment (car ending-list) moment-den))
245 (make-end-settings time (cdr ending-list) moment-den))))
247 ;; Don't start auto beams on grace notes
248 (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
251 (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
252 (measure-length (get 'measureLength (ly:make-moment 1 1)))
253 (measure-pos (get 'measurePosition ZERO-MOMENT))
254 (beat-grouping (get 'beatGrouping '()))
255 (settings (get 'autoBeamSettings '()))
256 (function (list (if (= dir START) 'begin 'end)))
257 ;; Calculate implied time signature based on measureLength
258 ;; and beatLength for default value in get
259 (num-mom (ly:moment-div measure-length beat-length))
261 (round (/ (ly:moment-main-numerator num-mom)
262 (ly:moment-main-denominator num-mom)))))
263 (den (ly:moment-main-denominator beat-length))
264 (time-signature-fraction
265 (get 'timeSignatureFraction (cons num den)))
266 (time (list (car time-signature-fraction)
267 (cdr time-signature-fraction)))
268 (type (list (ly:moment-main-numerator test)
269 (ly:moment-main-denominator test)))
270 (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
272 (ly:moment-add measure-length measure-pos)))
273 (grouping-moments (ending-moments beat-grouping 0 beat-length))
274 ;; Calculate implied measure length from beatGrouping
276 (grouping-length (if (null? grouping-moments)
278 (list-ref grouping-moments
279 (1- (length grouping-moments)))))
281 ;; Hmm, should junk user-override feature,
282 ;; or split this in user-override and config section?
283 (append function type '(* *))
284 (append function '(* * * *))
285 (append function type time)
286 (append function '(* *) time)))
287 (predefined-setting (first-assoc lst settings)))
289 ;; always begin or end beams at beginning/ending of measure
290 (= (ly:moment-main-numerator pos) 0)
291 (first-member (map (lambda (x) (cons x pos)) lst) settings))
294 ;; if no entry matches our function + time or type,
296 (not predefined-setting)
297 ;; if entry matches our function + time or type, check moment
298 (if predefined-setting
299 (equal? measure-pos (cdr predefined-setting))
300 ;; if measure-length matches grouping-length, use
301 ;; grouping moments, else use beat-length
302 (if (equal? measure-length grouping-length)
303 (member measure-pos grouping-moments)
304 (= (ly:moment-main-denominator
305 (ly:moment-div pos beat-length)) 1))))))))