1 ;;;; auto-beam.scm -- Auto-beam-engraver settings
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2000--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; specify generic beam begin and 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 ;; end beams each 1 2 note
28 ;; end beams with 16th notes each 1 4 note
29 ;; end beams with 32nd notes each 1 8 note
31 ((end * * 3 2) . ,(ly:make-moment 1 2))
32 ((end * * 3 2) . ,(ly:make-moment 2 2))
34 ((end 1 16 3 2) . ,(ly:make-moment 1 4))
35 ((end 1 16 3 2) . ,(ly:make-moment 1 2))
36 ((end 1 16 3 2) . ,(ly:make-moment 3 4))
37 ((end 1 16 3 2) . ,(ly:make-moment 5 4))
39 ((end 1 32 3 2) . ,(ly:make-moment 1 8))
40 ((end 1 32 3 2) . ,(ly:make-moment 1 4))
41 ((end 1 32 3 2) . ,(ly:make-moment 3 8))
42 ((end 1 32 3 2) . ,(ly:make-moment 1 2))
43 ((end 1 32 3 2) . ,(ly:make-moment 5 8))
44 ((end 1 32 3 2) . ,(ly:make-moment 3 4))
45 ((end 1 32 3 2) . ,(ly:make-moment 7 8))
46 ((end 1 32 3 2) . ,(ly:make-moment 9 8))
47 ((end 1 32 3 2) . ,(ly:make-moment 5 4))
48 ((end 1 32 3 2) . ,(ly:make-moment 11 8))
50 ((end * * 3 4) . ,(ly:make-moment 3 4))
52 ((end 1 16 3 4) . ,(ly:make-moment 1 4))
53 ((end 1 16 3 4) . ,(ly:make-moment 1 2))
55 ((end 1 32 3 4) . ,(ly:make-moment 1 8))
56 ((end 1 32 3 4) . ,(ly:make-moment 1 4))
57 ((end 1 32 3 4) . ,(ly:make-moment 3 8))
58 ((end 1 32 3 4) . ,(ly:make-moment 1 2))
59 ((end 1 32 3 4) . ,(ly:make-moment 5 8))
61 ((end * * 3 8) . ,(ly:make-moment 3 8))
64 ;; end beams each 1 2 note
65 ;; end beams with 32nd notes each 1 8 note
66 ;; end beams with 1 8 triplets each 1 4 note
68 ((end * * 4 4) . ,(ly:make-moment 1 2))
69 ((end 1 12 4 4) . ,(ly:make-moment 1 4))
70 ((end 1 12 4 4) . ,(ly:make-moment 3 4))
72 ((end 1 16 4 4) . ,(ly:make-moment 1 4))
73 ((end 1 16 4 4) . ,(ly:make-moment 3 4))
75 ((end 1 32 4 4) . ,(ly:make-moment 1 8))
76 ((end 1 32 4 4) . ,(ly:make-moment 1 4))
77 ((end 1 32 4 4) . ,(ly:make-moment 3 8))
78 ((end 1 32 4 4) . ,(ly:make-moment 5 8))
79 ((end 1 32 4 4) . ,(ly:make-moment 3 4))
80 ((end 1 32 4 4) . ,(ly:make-moment 7 8))
82 ((end * * 2 4) . ,(ly:make-moment 1 4))
83 ((end 1 32 2 4) . ,(ly:make-moment 1 8))
84 ((end 1 32 2 4) . ,(ly:make-moment 3 8))
86 ((end * * 4 8) . ,(ly:make-moment 1 4))
87 ((end 1 32 4 8) . ,(ly:make-moment 1 8))
88 ((end 1 32 4 8) . ,(ly:make-moment 3 8))
90 ((end * * 4 16) . ,(ly:make-moment 1 8))
92 ;; in 6 8, 9 8 and 12 8 time:
93 ;; use beatGrouping for all except 1 32nd notes
94 ;; end beams with 32nd notes every 1 8 note
96 ((end 1 32 6 8) . ,(ly:make-moment 1 8))
97 ((end 1 32 6 8) . ,(ly:make-moment 2 8))
98 ((end 1 32 6 8) . ,(ly:make-moment 3 8))
99 ((end 1 32 6 8) . ,(ly:make-moment 4 8))
100 ((end 1 32 6 8) . ,(ly:make-moment 5 8))
102 ((end 1 32 9 8) . ,(ly:make-moment 1 8))
103 ((end 1 32 9 8) . ,(ly:make-moment 2 8))
104 ((end 1 32 9 8) . ,(ly:make-moment 3 8))
105 ((end 1 32 9 8) . ,(ly:make-moment 4 8))
106 ((end 1 32 9 8) . ,(ly:make-moment 5 8))
107 ((end 1 32 9 8) . ,(ly:make-moment 6 8))
108 ((end 1 32 9 8) . ,(ly:make-moment 7 8))
110 ((end 1 32 12 8) . ,(ly:make-moment 1 8))
111 ((end 1 32 12 8) . ,(ly:make-moment 2 8))
112 ((end 1 32 12 8) . ,(ly:make-moment 3 8))
113 ((end 1 32 12 8) . ,(ly:make-moment 4 8))
114 ((end 1 32 12 8) . ,(ly:make-moment 5 8))
115 ((end 1 32 12 8) . ,(ly:make-moment 6 8))
116 ((end 1 32 12 8) . ,(ly:make-moment 7 8))
117 ((end 1 32 12 8) . ,(ly:make-moment 9 8))
118 ((end 1 32 12 8) . ,(ly:make-moment 10 8))
119 ((end 1 32 12 8) . ,(ly:make-moment 11 8))
122 (define (override-property-setting context property setting value)
123 "Like the C++ code that executes \\override, but without type
125 (ly:context-set-property!
127 (cons (cons setting value) (ly:context-property context property))))
129 (define (revert-property-setting context property setting)
130 "Like the C++ code that executes \revert, but without type
133 (define (revert-member alist entry new)
134 "Return ALIST, with ENTRY removed. ALIST is not modified, instead
135 a fresh copy of the list-head is made."
138 ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
139 (else (revert-member (cdr alist) entry (cons (car alist) new)))))
141 (ly:context-set-property!
143 (revert-member (ly:context-property context property) setting '())))
145 (define-public (override-auto-beam-setting setting num den . rest)
148 (make-apply-context (lambda (c)
149 (override-property-setting
151 setting (ly:make-moment num den))))
152 (if (and (pair? rest) (symbol? (car rest)))
156 (define-public (score-override-auto-beam-setting setting num den)
157 (override-auto-beam-setting setting num den 'Score))
159 (define-public (revert-auto-beam-setting setting num den . rest)
162 (make-apply-context (lambda (c)
163 (revert-property-setting
165 (cons setting (ly:make-moment num den)))))
166 (if (and (pair? rest) (symbol? (car rest)))
170 ;; Determine end moment for auto beaming (or begin moment, but mostly
171 ;; 0== anywhere). In order of decreasing priority:
175 ;; 3. end <type> <num> <den>
176 ;; 4. end * <num> <den>
177 ;; 5. if 1-4 not specified, begin anywhere, end at time determined by
178 ;; beatGrouping and beatLength:
179 ;; if beatGrouping and beatLength are consistent with measureLength,
180 ;; use beatGrouping to determine end of beams.
181 ;; if beatGrouping and beatLength are inconsistent with measureLength,
182 ;; use beatLength to determine end of beams.
187 ;; 1. override for specific duration type
188 ;; 2. generic override
190 ;; [to be defined in config file]
191 ;; 3. exceptions for specific time signature, for specific duration type
192 ;; 4. exceptions for specific time signature
193 ;; 5. easy catch-all rule for non-specified measure types
196 (define-public (default-auto-beam-check context dir test)
197 (define (get name default)
198 (let ((value (ly:context-property context name)))
199 (if (not (null? value)) value default)))
201 (define (ending-moments group-list start-beat beat-length)
202 (if (null? group-list)
204 (let ((new-start (+ start-beat (car group-list))))
205 (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
206 (ending-moments (cdr group-list) new-start beat-length)))))
208 (define (make-end-settings time ending-list moment-den)
209 (if (null? ending-list)
211 (cons (cons (append '(end * *) time)
212 (ly:make-moment (car ending-list) moment-den))
213 (make-end-settings time (cdr ending-list) moment-den))))
215 ;; Don't start auto beams on grace notes
216 (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
219 (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
220 (measure-length (get 'measureLength (ly:make-moment 1 1)))
221 (measure-pos (get 'measurePosition ZERO-MOMENT))
222 (beat-grouping (get 'beatGrouping '()))
223 (settings (get 'autoBeamSettings '()))
224 (function (list (if (= dir START) 'begin 'end)))
225 ;; Calculate implied time signature based on measureLength
226 ;; and beatLength for default value in get
227 (num-mom (ly:moment-div measure-length beat-length))
229 (round (/ (ly:moment-main-numerator num-mom)
230 (ly:moment-main-denominator num-mom)))))
231 (den (ly:moment-main-denominator beat-length))
232 (time-signature-fraction
233 (get 'timeSignatureFraction (cons num den)))
234 (time (list (car time-signature-fraction)
235 (cdr time-signature-fraction)))
236 (type (list (ly:moment-main-numerator test)
237 (ly:moment-main-denominator test)))
238 (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
240 (ly:moment-add measure-length measure-pos)))
241 (grouping-moments (ending-moments beat-grouping 0 beat-length))
242 ;; Calculate implied measure length from beatGrouping
244 (grouping-length (if (null? grouping-moments)
246 (list-ref grouping-moments
247 (1- (length grouping-moments)))))
249 ;; Hmm, should junk user-override feature,
250 ;; or split this in user-override and config section?
251 (append function type '(* *))
252 (append function '(* * * *))
253 (append function type time)
254 (append function '(* *) time)))
255 (predefined-setting (first-assoc lst settings)))
257 ;; always begin or end beams at beginning/ending of measure
258 (= (ly:moment-main-numerator pos) 0)
259 (first-member (map (lambda (x) (cons x pos)) lst) settings))
262 ;; if no entry matches our function + time or type,
264 (not predefined-setting)
265 ;; if entry matches our function + time or type, check moment
266 (if predefined-setting
267 (equal? measure-pos (cdr predefined-setting))
268 ;; if measure-length matches grouping-length, use
269 ;; grouping moments, else use beat-length
270 (if (equal? measure-length grouping-length)
271 (member measure-pos grouping-moments)
272 (= (ly:moment-main-denominator
273 (ly:moment-div pos beat-length)) 1))))))))