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))
34 ;; in 2 4, 2 8 and 2 16 time:
38 ;; use beatLength for all except 32nd notes
39 ;; end beams with 32nd notes each 1 4 beat
41 ((end 1 32 3 2) . ,(ly:make-moment 1 4))
42 ((end 1 32 3 2) . ,(ly:make-moment 2 4))
43 ((end 1 32 3 2) . ,(ly:make-moment 3 4))
44 ((end 1 32 3 2) . ,(ly:make-moment 4 4))
45 ((end 1 32 3 2) . ,(ly:make-moment 5 4))
47 ;; in 3 4, 3 8 and 3 16 time:
51 ;; use beatLength for all except 16th and 32nd notes
52 ;; end beams with 16th notes each 1 4 beat
53 ;; end beams with 32nd notes each 1 8 beat
55 ((end 1 16 4 2) . ,(ly:make-moment 1 4))
56 ((end 1 16 4 2) . ,(ly:make-moment 2 4))
57 ((end 1 16 4 2) . ,(ly:make-moment 3 4))
58 ((end 1 16 4 2) . ,(ly:make-moment 4 4))
59 ((end 1 16 4 2) . ,(ly:make-moment 5 4))
60 ((end 1 16 4 2) . ,(ly:make-moment 6 4))
61 ((end 1 16 4 2) . ,(ly:make-moment 7 4))
63 ((end 1 32 4 2) . ,(ly:make-moment 1 8))
64 ((end 1 32 4 2) . ,(ly:make-moment 2 8))
65 ((end 1 32 4 2) . ,(ly:make-moment 3 8))
66 ((end 1 32 4 2) . ,(ly:make-moment 4 8))
67 ((end 1 32 4 2) . ,(ly:make-moment 5 8))
68 ((end 1 32 4 2) . ,(ly:make-moment 6 8))
69 ((end 1 32 4 2) . ,(ly:make-moment 7 8))
70 ((end 1 32 4 2) . ,(ly:make-moment 8 8))
71 ((end 1 32 4 2) . ,(ly:make-moment 9 8))
72 ((end 1 32 4 2) . ,(ly:make-moment 10 8))
73 ((end 1 32 4 2) . ,(ly:make-moment 11 8))
74 ((end 1 32 4 2) . ,(ly:make-moment 12 8))
75 ((end 1 32 4 2) . ,(ly:make-moment 13 8))
76 ((end 1 32 4 2) . ,(ly:make-moment 14 8))
77 ((end 1 32 4 2) . ,(ly:make-moment 15 8))
79 ;; in 4 4 (common) time:
80 ;; use beatLength for all except 32nd notes:
81 ;; end beams with 32nd notes each 1 8 beat
83 ((end 1 32 4 4) . ,(ly:make-moment 1 8))
84 ((end 1 32 4 4) . ,(ly:make-moment 2 8))
85 ((end 1 32 4 4) . ,(ly:make-moment 3 8))
86 ((end 1 32 4 4) . ,(ly:make-moment 4 8))
87 ((end 1 32 4 4) . ,(ly:make-moment 5 8))
88 ((end 1 32 4 4) . ,(ly:make-moment 6 8))
89 ((end 1 32 4 4) . ,(ly:make-moment 7 8))
91 ;; in 4 8 and 4 16 time:
95 ;; use beatGrouping for all except 16th, 32nd notes
96 ;; end beams with 16th or 32nd notes each 1 4 beat
99 ((end 1 16 6 4) . ,(ly:make-moment 1 4))
100 ((end 1 16 6 4) . ,(ly:make-moment 2 4))
101 ((end 1 16 6 4) . ,(ly:make-moment 3 4))
102 ((end 1 16 6 4) . ,(ly:make-moment 4 4))
103 ((end 1 16 6 4) . ,(ly:make-moment 5 4))
105 ((end 1 32 6 4) . ,(ly:make-moment 1 4))
106 ((end 1 32 6 4) . ,(ly:make-moment 2 4))
107 ((end 1 32 6 4) . ,(ly:make-moment 3 4))
108 ((end 1 32 6 4) . ,(ly:make-moment 4 4))
109 ((end 1 32 6 4) . ,(ly:make-moment 5 4))
112 ;; use beatGrouping for all except 32nd notes
113 ;; end beams with 32nd notes each 1 8 beat
115 ((end 1 32 6 8) . ,(ly:make-moment 1 8))
116 ((end 1 32 6 8) . ,(ly:make-moment 2 8))
117 ((end 1 32 6 8) . ,(ly:make-moment 3 8))
118 ((end 1 32 6 8) . ,(ly:make-moment 4 8))
119 ((end 1 32 6 8) . ,(ly:make-moment 5 8))
125 ;; use beatGrouping for all except 16th, 32nd notes
126 ;; end beams with 16th or 32nd notes each 1 4 beat
128 ((end 1 16 9 4) . ,(ly:make-moment 1 4))
129 ((end 1 16 9 4) . ,(ly:make-moment 2 4))
130 ((end 1 16 9 4) . ,(ly:make-moment 3 4))
131 ((end 1 16 9 4) . ,(ly:make-moment 4 4))
132 ((end 1 16 9 4) . ,(ly:make-moment 5 4))
133 ((end 1 16 9 4) . ,(ly:make-moment 6 4))
134 ((end 1 16 9 4) . ,(ly:make-moment 7 4))
135 ((end 1 16 9 4) . ,(ly:make-moment 8 4))
137 ((end 1 32 9 4) . ,(ly:make-moment 1 4))
138 ((end 1 32 9 4) . ,(ly:make-moment 2 4))
139 ((end 1 32 9 4) . ,(ly:make-moment 3 4))
140 ((end 1 32 9 4) . ,(ly:make-moment 4 4))
141 ((end 1 32 9 4) . ,(ly:make-moment 5 4))
142 ((end 1 32 9 4) . ,(ly:make-moment 6 4))
143 ((end 1 32 9 4) . ,(ly:make-moment 7 4))
144 ((end 1 32 9 4) . ,(ly:make-moment 8 4))
147 ;; use beatGrouping for all except 32nd notes
148 ;; end beams with 32nd notes each 1 8 beat
150 ((end 1 32 9 8) . ,(ly:make-moment 1 8))
151 ((end 1 32 9 8) . ,(ly:make-moment 2 8))
152 ((end 1 32 9 8) . ,(ly:make-moment 3 8))
153 ((end 1 32 9 8) . ,(ly:make-moment 4 8))
154 ((end 1 32 9 8) . ,(ly:make-moment 5 8))
155 ((end 1 32 9 8) . ,(ly:make-moment 6 8))
156 ((end 1 32 9 8) . ,(ly:make-moment 7 8))
157 ((end 1 32 9 8) . ,(ly:make-moment 8 8))
163 ;; use beatGrouping for all except 16th, 32nd notes
164 ;; end beams with 16th or 32nd notes each 1 4 beat
166 ((end 1 16 12 4) . ,(ly:make-moment 1 4))
167 ((end 1 16 12 4) . ,(ly:make-moment 2 4))
168 ((end 1 16 12 4) . ,(ly:make-moment 3 4))
169 ((end 1 16 12 4) . ,(ly:make-moment 4 4))
170 ((end 1 16 12 4) . ,(ly:make-moment 5 4))
171 ((end 1 16 12 4) . ,(ly:make-moment 6 4))
172 ((end 1 16 12 4) . ,(ly:make-moment 7 4))
173 ((end 1 16 12 4) . ,(ly:make-moment 8 4))
174 ((end 1 16 12 4) . ,(ly:make-moment 9 4))
175 ((end 1 16 12 4) . ,(ly:make-moment 10 4))
176 ((end 1 16 12 4) . ,(ly:make-moment 11 4))
178 ((end 1 32 12 4) . ,(ly:make-moment 1 4))
179 ((end 1 32 12 4) . ,(ly:make-moment 2 4))
180 ((end 1 32 12 4) . ,(ly:make-moment 3 4))
181 ((end 1 32 12 4) . ,(ly:make-moment 4 4))
182 ((end 1 32 12 4) . ,(ly:make-moment 5 4))
183 ((end 1 32 12 4) . ,(ly:make-moment 6 4))
184 ((end 1 32 12 4) . ,(ly:make-moment 7 4))
185 ((end 1 32 12 4) . ,(ly:make-moment 8 4))
186 ((end 1 32 12 4) . ,(ly:make-moment 9 4))
187 ((end 1 32 12 4) . ,(ly:make-moment 10 4))
188 ((end 1 32 12 4) . ,(ly:make-moment 11 4))
191 ;; use beatGrouping for all except 32nd notes
192 ;; end beams with 32nd notes each 1 8 beat
194 ((end 1 32 12 8) . ,(ly:make-moment 1 8))
195 ((end 1 32 12 8) . ,(ly:make-moment 2 8))
196 ((end 1 32 12 8) . ,(ly:make-moment 3 8))
197 ((end 1 32 12 8) . ,(ly:make-moment 4 8))
198 ((end 1 32 12 8) . ,(ly:make-moment 5 8))
199 ((end 1 32 12 8) . ,(ly:make-moment 6 8))
200 ((end 1 32 12 8) . ,(ly:make-moment 7 8))
201 ((end 1 32 12 8) . ,(ly:make-moment 8 8))
202 ((end 1 32 12 8) . ,(ly:make-moment 9 8))
203 ((end 1 32 12 8) . ,(ly:make-moment 10 8))
204 ((end 1 32 12 8) . ,(ly:make-moment 11 8))
211 (define (override-property-setting context property setting value)
212 "Like the C++ code that executes \\override, but without type
214 (ly:context-set-property!
216 (cons (cons setting value) (ly:context-property context property))))
218 (define (revert-property-setting context property setting)
219 "Like the C++ code that executes \revert, but without type
222 (define (revert-member alist entry new)
223 "Return ALIST, with ENTRY removed. ALIST is not modified, instead
224 a fresh copy of the list-head is made."
227 ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
228 (else (revert-member (cdr alist) entry (cons (car alist) new)))))
230 (ly:context-set-property!
232 (revert-member (ly:context-property context property) setting '())))
234 (define-public (override-auto-beam-setting setting num den . rest)
237 (make-apply-context (lambda (c)
238 (override-property-setting
240 setting (ly:make-moment num den))))
241 (if (and (pair? rest) (symbol? (car rest)))
245 (define-public (score-override-auto-beam-setting setting num den)
246 (override-auto-beam-setting setting num den 'Score))
248 (define-public (revert-auto-beam-setting setting num den . rest)
253 (revert-property-setting
255 (cons setting (ly:make-moment num den)))))
256 (if (and (pair? rest) (symbol? (car rest)))
260 ;; Determine end moment for auto beaming (or begin moment, but mostly
261 ;; 0== anywhere). In order of decreasing priority:
265 ;; 3. end <type> <num> <den>
266 ;; 4. end * <num> <den>
267 ;; 5. if 1-4 not specified, begin anywhere, end at time determined by
268 ;; beatGrouping and beatLength:
269 ;; if beatGrouping and beatLength are consistent with measureLength,
270 ;; use beatGrouping to determine end of beams.
271 ;; if beatGrouping and beatLength are inconsistent with measureLength,
272 ;; use beatLength to determine end of beams.
277 ;; 1. override for specific duration type
278 ;; 2. generic override
280 ;; [to be defined in config file]
281 ;; 3. exceptions for specific time signature, for specific duration type
282 ;; 4. exceptions for specific time signature
283 ;; 5. easy catch-all rule for non-specified measure types
286 (define-public (default-auto-beam-check context dir test)
287 (define (get name default)
288 (let ((value (ly:context-property context name)))
289 (if (not (null? value)) value default)))
291 (define (ending-moments group-list start-beat beat-length)
292 (if (null? group-list)
294 (let ((new-start (+ start-beat (car group-list))))
295 (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
296 (ending-moments (cdr group-list) new-start beat-length)))))
298 (define (make-end-settings time ending-list moment-den)
299 (if (null? ending-list)
301 (cons (cons (append '(end * *) time)
302 (ly:make-moment (car ending-list) moment-den))
303 (make-end-settings time (cdr ending-list) moment-den))))
305 ;; Don't start auto beams on grace notes
306 (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
309 (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
310 (measure-length (get 'measureLength (ly:make-moment 1 1)))
311 (measure-pos (get 'measurePosition ZERO-MOMENT))
312 (beat-grouping (get 'beatGrouping '()))
313 (settings (get 'autoBeamSettings '()))
314 (function (list (if (= dir START) 'begin 'end)))
315 ;; Calculate implied time signature based on measureLength
316 ;; and beatLength for default value in get
317 (num-mom (ly:moment-div measure-length beat-length))
319 (round (/ (ly:moment-main-numerator num-mom)
320 (ly:moment-main-denominator num-mom)))))
321 (den (ly:moment-main-denominator beat-length))
322 (time-signature-fraction
323 (get 'timeSignatureFraction (cons num den)))
324 (time (list (car time-signature-fraction)
325 (cdr time-signature-fraction)))
326 (type (list (ly:moment-main-numerator test)
327 (ly:moment-main-denominator test)))
328 (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
330 (ly:moment-add measure-length measure-pos)))
331 (grouping-moments (ending-moments beat-grouping 0 beat-length))
332 ;; Calculate implied measure length from beatGrouping
334 (grouping-length (if (null? grouping-moments)
336 (list-ref grouping-moments
337 (1- (length grouping-moments)))))
339 ;; Hmm, should junk user-override feature,
340 ;; or split this in user-override and config section?
341 (append function type '(* *))
342 (append function '(* * * *))
343 (append function type time)
344 (append function '(* *) time)))
345 (predefined-setting (first-assoc lst settings)))
347 ;; always begin or end beams at beginning/ending of measure
348 (= (ly:moment-main-numerator pos) 0)
349 (first-member (map (lambda (x) (cons x pos)) lst) settings))
352 ;; if no entry matches our function + time or type,
354 (not predefined-setting)
355 ;; if entry matches our function + time or type, check moment
356 (if predefined-setting
357 (equal? measure-pos (cdr predefined-setting))
358 ;; if measure-length matches grouping-length, use
359 ;; grouping moments, else use beat-length
360 (if (equal? measure-length grouping-length)
361 (member measure-pos grouping-moments)
362 (= (ly:moment-main-denominator
363 (ly:moment-div pos beat-length)) 1))))))))