1 ;;;; auto-beam.scm -- Auto-beam-engraver settings
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2000--2008 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))
109 ((end 1 32 9 8) . ,(ly:make-moment 8 8))
111 ((end 1 32 12 8) . ,(ly:make-moment 1 8))
112 ((end 1 32 12 8) . ,(ly:make-moment 2 8))
113 ((end 1 32 12 8) . ,(ly:make-moment 3 8))
114 ((end 1 32 12 8) . ,(ly:make-moment 4 8))
115 ((end 1 32 12 8) . ,(ly:make-moment 5 8))
116 ((end 1 32 12 8) . ,(ly:make-moment 6 8))
117 ((end 1 32 12 8) . ,(ly:make-moment 7 8))
118 ((end 1 32 12 8) . ,(ly:make-moment 8 8))
119 ((end 1 32 12 8) . ,(ly:make-moment 9 8))
120 ((end 1 32 12 8) . ,(ly:make-moment 10 8))
121 ((end 1 32 12 8) . ,(ly:make-moment 11 8))
124 (define (override-property-setting context property setting value)
125 "Like the C++ code that executes \\override, but without type
127 (ly:context-set-property!
129 (cons (cons setting value) (ly:context-property context property))))
131 (define (revert-property-setting context property setting)
132 "Like the C++ code that executes \revert, but without type
135 (define (revert-member alist entry new)
136 "Return ALIST, with ENTRY removed. ALIST is not modified, instead
137 a fresh copy of the list-head is made."
140 ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
141 (else (revert-member (cdr alist) entry (cons (car alist) new)))))
143 (ly:context-set-property!
145 (revert-member (ly:context-property context property) setting '())))
147 (define-public (override-auto-beam-setting setting num den . rest)
150 (make-apply-context (lambda (c)
151 (override-property-setting
153 setting (ly:make-moment num den))))
154 (if (and (pair? rest) (symbol? (car rest)))
158 (define-public (score-override-auto-beam-setting setting num den)
159 (override-auto-beam-setting setting num den 'Score))
161 (define-public (revert-auto-beam-setting setting num den . rest)
166 (revert-property-setting
168 (cons setting (ly:make-moment num den)))))
169 (if (and (pair? rest) (symbol? (car rest)))
173 ;; Determine end moment for auto beaming (or begin moment, but mostly
174 ;; 0== anywhere). In order of decreasing priority:
178 ;; 3. end <type> <num> <den>
179 ;; 4. end * <num> <den>
180 ;; 5. if 1-4 not specified, begin anywhere, end at time determined by
181 ;; beatGrouping and beatLength:
182 ;; if beatGrouping and beatLength are consistent with measureLength,
183 ;; use beatGrouping to determine end of beams.
184 ;; if beatGrouping and beatLength are inconsistent with measureLength,
185 ;; use beatLength to determine end of beams.
190 ;; 1. override for specific duration type
191 ;; 2. generic override
193 ;; [to be defined in config file]
194 ;; 3. exceptions for specific time signature, for specific duration type
195 ;; 4. exceptions for specific time signature
196 ;; 5. easy catch-all rule for non-specified measure types
199 (define-public (default-auto-beam-check context dir test)
200 (define (get name default)
201 (let ((value (ly:context-property context name)))
202 (if (not (null? value)) value default)))
204 (define (ending-moments group-list start-beat beat-length)
205 (if (null? group-list)
207 (let ((new-start (+ start-beat (car group-list))))
208 (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
209 (ending-moments (cdr group-list) new-start beat-length)))))
211 (define (make-end-settings time ending-list moment-den)
212 (if (null? ending-list)
214 (cons (cons (append '(end * *) time)
215 (ly:make-moment (car ending-list) moment-den))
216 (make-end-settings time (cdr ending-list) moment-den))))
218 ;; Don't start auto beams on grace notes
219 (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
222 (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
223 (measure-length (get 'measureLength (ly:make-moment 1 1)))
224 (measure-pos (get 'measurePosition ZERO-MOMENT))
225 (beat-grouping (get 'beatGrouping '()))
226 (settings (get 'autoBeamSettings '()))
227 (function (list (if (= dir START) 'begin 'end)))
228 ;; Calculate implied time signature based on measureLength
229 ;; and beatLength for default value in get
230 (num-mom (ly:moment-div measure-length beat-length))
232 (round (/ (ly:moment-main-numerator num-mom)
233 (ly:moment-main-denominator num-mom)))))
234 (den (ly:moment-main-denominator beat-length))
235 (time-signature-fraction
236 (get 'timeSignatureFraction (cons num den)))
237 (time (list (car time-signature-fraction)
238 (cdr time-signature-fraction)))
239 (type (list (ly:moment-main-numerator test)
240 (ly:moment-main-denominator test)))
241 (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
243 (ly:moment-add measure-length measure-pos)))
244 (grouping-moments (ending-moments beat-grouping 0 beat-length))
245 ;; Calculate implied measure length from beatGrouping
247 (grouping-length (if (null? grouping-moments)
249 (list-ref grouping-moments
250 (1- (length grouping-moments)))))
252 ;; Hmm, should junk user-override feature,
253 ;; or split this in user-override and config section?
254 (append function type '(* *))
255 (append function '(* * * *))
256 (append function type time)
257 (append function '(* *) time)))
258 (predefined-setting (first-assoc lst settings)))
260 ;; always begin or end beams at beginning/ending of measure
261 (= (ly:moment-main-numerator pos) 0)
262 (first-member (map (lambda (x) (cons x pos)) lst) settings))
265 ;; if no entry matches our function + time or type,
267 (not predefined-setting)
268 ;; if entry matches our function + time or type, check moment
269 (if predefined-setting
270 (equal? measure-pos (cdr predefined-setting))
271 ;; if measure-length matches grouping-length, use
272 ;; grouping moments, else use beat-length
273 (if (equal? measure-length grouping-length)
274 (member measure-pos grouping-moments)
275 (= (ly:moment-main-denominator
276 (ly:moment-div pos beat-length)) 1))))))))