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))
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)
164 (make-apply-context (lambda (c)
165 (revert-property-setting
167 (cons setting (ly:make-moment num den)))))
168 (if (and (pair? rest) (symbol? (car rest)))
172 ;; Determine end moment for auto beaming (or begin moment, but mostly
173 ;; 0== anywhere). In order of decreasing priority:
177 ;; 3. end <type> <num> <den>
178 ;; 4. end * <num> <den>
179 ;; 5. if 1-4 not specified, begin anywhere, end at time determined by
180 ;; beatGrouping and beatLength:
181 ;; if beatGrouping and beatLength are consistent with measureLength,
182 ;; use beatGrouping to determine end of beams.
183 ;; if beatGrouping and beatLength are inconsistent with measureLength,
184 ;; use beatLength to determine end of beams.
189 ;; 1. override for specific duration type
190 ;; 2. generic override
192 ;; [to be defined in config file]
193 ;; 3. exceptions for specific time signature, for specific duration type
194 ;; 4. exceptions for specific time signature
195 ;; 5. easy catch-all rule for non-specified measure types
198 (define-public (default-auto-beam-check context dir test)
199 (define (get name default)
200 (let ((value (ly:context-property context name)))
201 (if (not (null? value)) value default)))
203 (define (ending-moments group-list start-beat beat-length)
204 (if (null? group-list)
206 (let ((new-start (+ start-beat (car group-list))))
207 (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
208 (ending-moments (cdr group-list) new-start beat-length)))))
210 (define (make-end-settings time ending-list moment-den)
211 (if (null? ending-list)
213 (cons (cons (append '(end * *) time)
214 (ly:make-moment (car ending-list) moment-den))
215 (make-end-settings time (cdr ending-list) moment-den))))
217 ;; Don't start auto beams on grace notes
218 (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
221 (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
222 (measure-length (get 'measureLength (ly:make-moment 1 1)))
223 (measure-pos (get 'measurePosition ZERO-MOMENT))
224 (beat-grouping (get 'beatGrouping '()))
225 (settings (get 'autoBeamSettings '()))
226 (function (list (if (= dir START) 'begin 'end)))
227 ;; Calculate implied time signature based on measureLength
228 ;; and beatLength for default value in get
229 (num-mom (ly:moment-div measure-length beat-length))
231 (round (/ (ly:moment-main-numerator num-mom)
232 (ly:moment-main-denominator num-mom)))))
233 (den (ly:moment-main-denominator beat-length))
234 (time-signature-fraction
235 (get 'timeSignatureFraction (cons num den)))
236 (time (list (car time-signature-fraction)
237 (cdr time-signature-fraction)))
238 (type (list (ly:moment-main-numerator test)
239 (ly:moment-main-denominator test)))
240 (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
242 (ly:moment-add measure-length measure-pos)))
243 (grouping-moments (ending-moments beat-grouping 0 beat-length))
244 ;; Calculate implied measure length from beatGrouping
246 (grouping-length (if (null? grouping-moments)
248 (list-ref grouping-moments
249 (1- (length grouping-moments)))))
251 ;; Hmm, should junk user-override feature,
252 ;; or split this in user-override and config section?
253 (append function type '(* *))
254 (append function '(* * * *))
255 (append function type time)
256 (append function '(* *) time)))
257 (predefined-setting (first-assoc lst settings)))
259 ;; always begin or end beams at beginning/ending of measure
260 (= (ly:moment-main-numerator pos) 0)
261 (first-member (map (lambda (x) (cons x pos)) lst) settings))
264 ;; if no entry matches our function + time or type,
266 (not predefined-setting)
267 ;; if entry matches our function + time or type, check moment
268 (if predefined-setting
269 (equal? measure-pos (cdr predefined-setting))
270 ;; if measure-length matches grouping-length, use
271 ;; grouping moments, else use beat-length
272 (if (equal? measure-length grouping-length)
273 (member measure-pos grouping-moments)
274 (= (ly:moment-main-denominator
275 (ly:moment-div pos beat-length)) 1))))))))