]> git.donarmstrong.com Git - lilypond.git/blob - scm/auto-beam.scm
Merge branch 'master' into systems-per-page
[lilypond.git] / scm / auto-beam.scm
1 ;;;; auto-beam.scm -- Auto-beam-engraver settings
2 ;;;;
3 ;;;; source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2000--2009 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;;; specify generic beam end times
8
9 ;;; format:
10 ;;;
11 ;;;   function shortest-duration-in-beam time-signature
12 ;;;
13 ;;; where
14 ;;;
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
18 ;;;
19 ;;; unspecified or wildcard entries for duration or time-signature
20 ;;; are given by * *
21
22 ;;; maybe do:  '(end shortest-1 16 time-3 4) ?
23
24 (define-public default-auto-beam-settings
25   `(
26     ;; in 2 2 time:
27     ;;   use beatLength for all except 32nd notes
28     ;;   end beams with 32nd notes each 1 4 beat
29
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))
33
34     ;; in 2 4, 2 8 and 2 16 time:
35     ;;   use beatLength
36
37     ;; in 3 2 time:
38     ;;   use beatLength for all except 32nd notes
39     ;;   end beams with 32nd notes each 1 4 beat
40
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))
46
47     ;; in 3 4, 3 8 and 3 16 time:
48     ;;   use beatLength
49
50     ;; in 4 2 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
54
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))
62
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))
78
79     ;; in 4 4 (common) time:
80     ;;  use beatLength for all except 32nd notes:
81     ;;  end beams with 32nd notes each 1 8 beat
82
83     ((end 1 8 4 4) . ,(ly:make-moment 1 2))
84
85     ((end 1 32 4 4) . ,(ly:make-moment 1 8))
86     ((end 1 32 4 4) . ,(ly:make-moment 2 8))
87     ((end 1 32 4 4) . ,(ly:make-moment 3 8))
88     ((end 1 32 4 4) . ,(ly:make-moment 4 8))
89     ((end 1 32 4 4) . ,(ly:make-moment 5 8))
90     ((end 1 32 4 4) . ,(ly:make-moment 6 8))
91     ((end 1 32 4 4) . ,(ly:make-moment 7 8))
92
93     ;; in 4 8 and 4 16 time:
94     ;;   use beatLength
95
96     ;; in 6 4 time:
97     ;;   use beatGrouping for all except 16th, 32nd notes
98     ;;   end beams with 16th or 32nd notes each 1 4 beat
99
100
101     ((end 1 16 6 4) . ,(ly:make-moment 1 4))
102     ((end 1 16 6 4) . ,(ly:make-moment 2 4))
103     ((end 1 16 6 4) . ,(ly:make-moment 3 4))
104     ((end 1 16 6 4) . ,(ly:make-moment 4 4))
105     ((end 1 16 6 4) . ,(ly:make-moment 5 4))
106
107     ((end 1 32 6 4) . ,(ly:make-moment 1 4))
108     ((end 1 32 6 4) . ,(ly:make-moment 2 4))
109     ((end 1 32 6 4) . ,(ly:make-moment 3 4))
110     ((end 1 32 6 4) . ,(ly:make-moment 4 4))
111     ((end 1 32 6 4) . ,(ly:make-moment 5 4))
112
113     ;; in 6 8 time:
114     ;;   use beatGrouping for all except 32nd notes
115     ;;   end beams with 32nd notes each 1 8 beat
116
117     ((end 1 32 6 8) . ,(ly:make-moment 1 8))
118     ((end 1 32 6 8) . ,(ly:make-moment 2 8))
119     ((end 1 32 6 8) . ,(ly:make-moment 3 8))
120     ((end 1 32 6 8) . ,(ly:make-moment 4 8))
121     ((end 1 32 6 8) . ,(ly:make-moment 5 8))
122
123     ;; in 6 16 time:
124     ;;   use beatGrouping
125
126     ;; in 9 4 time:
127     ;;   use beatGrouping for all except 16th, 32nd notes
128     ;;   end beams with 16th or 32nd notes each 1 4 beat
129
130     ((end 1 16 9 4) . ,(ly:make-moment 1 4))
131     ((end 1 16 9 4) . ,(ly:make-moment 2 4))
132     ((end 1 16 9 4) . ,(ly:make-moment 3 4))
133     ((end 1 16 9 4) . ,(ly:make-moment 4 4))
134     ((end 1 16 9 4) . ,(ly:make-moment 5 4))
135     ((end 1 16 9 4) . ,(ly:make-moment 6 4))
136     ((end 1 16 9 4) . ,(ly:make-moment 7 4))
137     ((end 1 16 9 4) . ,(ly:make-moment 8 4))
138
139     ((end 1 32 9 4) . ,(ly:make-moment 1 4))
140     ((end 1 32 9 4) . ,(ly:make-moment 2 4))
141     ((end 1 32 9 4) . ,(ly:make-moment 3 4))
142     ((end 1 32 9 4) . ,(ly:make-moment 4 4))
143     ((end 1 32 9 4) . ,(ly:make-moment 5 4))
144     ((end 1 32 9 4) . ,(ly:make-moment 6 4))
145     ((end 1 32 9 4) . ,(ly:make-moment 7 4))
146     ((end 1 32 9 4) . ,(ly:make-moment 8 4))
147
148     ;; in 9 8 time:
149     ;;   use beatGrouping for all except 32nd notes
150     ;;   end beams with 32nd notes each 1 8 beat
151
152     ((end 1 32 9 8) . ,(ly:make-moment 1 8))
153     ((end 1 32 9 8) . ,(ly:make-moment 2 8))
154     ((end 1 32 9 8) . ,(ly:make-moment 3 8))
155     ((end 1 32 9 8) . ,(ly:make-moment 4 8))
156     ((end 1 32 9 8) . ,(ly:make-moment 5 8))
157     ((end 1 32 9 8) . ,(ly:make-moment 6 8))
158     ((end 1 32 9 8) . ,(ly:make-moment 7 8))
159     ((end 1 32 9 8) . ,(ly:make-moment 8 8))
160
161     ;; in 9 16 time
162     ;;   use beatGrouping
163
164     ;; in 12 4 time:
165     ;;   use beatGrouping for all except 16th, 32nd notes
166     ;;   end beams with 16th or 32nd notes each 1 4 beat
167
168     ((end 1 16 12 4) . ,(ly:make-moment 1 4))
169     ((end 1 16 12 4) . ,(ly:make-moment 2 4))
170     ((end 1 16 12 4) . ,(ly:make-moment 3 4))
171     ((end 1 16 12 4) . ,(ly:make-moment 4 4))
172     ((end 1 16 12 4) . ,(ly:make-moment 5 4))
173     ((end 1 16 12 4) . ,(ly:make-moment 6 4))
174     ((end 1 16 12 4) . ,(ly:make-moment 7 4))
175     ((end 1 16 12 4) . ,(ly:make-moment 8 4))
176     ((end 1 16 12 4) . ,(ly:make-moment 9 4))
177     ((end 1 16 12 4) . ,(ly:make-moment 10 4))
178     ((end 1 16 12 4) . ,(ly:make-moment 11 4))
179
180     ((end 1 32 12 4) . ,(ly:make-moment 1 4))
181     ((end 1 32 12 4) . ,(ly:make-moment 2 4))
182     ((end 1 32 12 4) . ,(ly:make-moment 3 4))
183     ((end 1 32 12 4) . ,(ly:make-moment 4 4))
184     ((end 1 32 12 4) . ,(ly:make-moment 5 4))
185     ((end 1 32 12 4) . ,(ly:make-moment 6 4))
186     ((end 1 32 12 4) . ,(ly:make-moment 7 4))
187     ((end 1 32 12 4) . ,(ly:make-moment 8 4))
188     ((end 1 32 12 4) . ,(ly:make-moment 9 4))
189     ((end 1 32 12 4) . ,(ly:make-moment 10 4))
190     ((end 1 32 12 4) . ,(ly:make-moment 11 4))
191
192     ;; in 12 8 time:
193     ;;   use beatGrouping for all except 32nd notes
194     ;;   end beams with 32nd notes each 1 8 beat
195
196     ((end 1 32 12 8) . ,(ly:make-moment 1 8))
197     ((end 1 32 12 8) . ,(ly:make-moment 2 8))
198     ((end 1 32 12 8) . ,(ly:make-moment 3 8))
199     ((end 1 32 12 8) . ,(ly:make-moment 4 8))
200     ((end 1 32 12 8) . ,(ly:make-moment 5 8))
201     ((end 1 32 12 8) . ,(ly:make-moment 6 8))
202     ((end 1 32 12 8) . ,(ly:make-moment 7 8))
203     ((end 1 32 12 8) . ,(ly:make-moment 8 8))
204     ((end 1 32 12 8) . ,(ly:make-moment 9 8))
205     ((end 1 32 12 8) . ,(ly:make-moment 10 8))
206     ((end 1 32 12 8) . ,(ly:make-moment 11 8))
207
208     ;; in 12 16 time:
209     ;;   use beatGrouping
210
211     ))
212
213 (define (override-property-setting context property setting value)
214   "Like the C++ code that executes \\override, but without type
215 checking. "
216   (ly:context-set-property!
217    context property
218    (cons (cons setting value) (ly:context-property context property))))
219
220 (define (revert-property-setting context property setting)
221   "Like the C++ code that executes \revert, but without type
222 checking. "
223
224   (define (revert-member alist entry new)
225     "Return ALIST, with ENTRY removed.  ALIST is not modified, instead
226 a fresh copy of the list-head is made."
227     (cond
228      ((null? alist) new)
229      ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
230      (else (revert-member (cdr alist) entry (cons (car alist) new)))))
231
232   (ly:context-set-property!
233    context property
234    (revert-member (ly:context-property context property) setting '())))
235
236 (define-public (override-auto-beam-setting setting num den . rest)
237   (ly:export
238    (context-spec-music
239     (make-apply-context (lambda (c)
240         (override-property-setting
241          c 'autoBeamSettings
242          setting (ly:make-moment num den))))
243     (if (and (pair? rest) (symbol? (car rest)))
244         (car rest)
245         'Voice))))
246
247 (define-public (score-override-auto-beam-setting setting num den)
248   (override-auto-beam-setting setting num den 'Score))
249
250 (define-public (revert-auto-beam-setting setting num den . rest)
251   (ly:export
252    (context-spec-music
253     (make-apply-context
254       (lambda (c)
255         (revert-property-setting
256          c 'autoBeamSettings
257          (cons setting (ly:make-moment num den)))))
258     (if (and (pair? rest) (symbol? (car rest)))
259         (car rest)
260         'Voice))))
261
262 ;;  Determine end moment for auto beaming (or begin moment, but mostly
263 ;;  0== anywhere).  In order of decreasing priority:
264 ;;
265 ;;  1. end <type>   *     *
266 ;;  2. end   *      *     *
267 ;;  3. end <type> <num> <den>
268 ;;  4. end   *    <num> <den>
269 ;;  5. if 1-4 not specified, begin anywhere, end at time determined by
270 ;;          beatGrouping and beatLength:
271 ;;     if beatGrouping and beatLength are consistent with measureLength,
272 ;;        use beatGrouping to determine end of beams.
273 ;;     if beatGrouping and beatLength are inconsistent with measureLength,
274 ;;        use beatLength to determine end of beams.
275 ;;
276 ;;  Rationale:
277 ;;
278 ;;  [user override]
279 ;;  1. override for specific duration type
280 ;;  2. generic override
281 ;;
282 ;;  [to be defined in config file]
283 ;;  3. exceptions for specific time signature, for specific duration type
284 ;;  4. exceptions for specific time signature
285 ;;  5. easy catch-all rule for non-specified measure types
286
287
288 (define-public (default-auto-beam-check context dir test)
289   (define (get name default)
290     (let ((value (ly:context-property context name)))
291       (if (not (null? value)) value default)))
292
293   (define (ending-moments group-list start-beat beat-length)
294     (if (null? group-list)
295         '()
296         (let ((new-start (+ start-beat (car group-list))))
297           (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
298                 (ending-moments (cdr group-list) new-start beat-length)))))
299
300   (define (make-end-settings time ending-list moment-den)
301     (if (null? ending-list)
302         '()
303         (cons (cons (append '(end * *) time)
304                     (ly:make-moment (car ending-list) moment-den))
305               (make-end-settings time (cdr ending-list) moment-den))))
306   
307   ;; Don't start auto beams on grace notes
308   (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
309            (= dir START))
310       #f
311       (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
312              (measure-length (get 'measureLength (ly:make-moment 1 1)))
313              (measure-pos (get 'measurePosition ZERO-MOMENT))
314              (beat-grouping (get 'beatGrouping '()))
315              (settings (get 'autoBeamSettings '()))
316              (function (list (if (= dir START) 'begin 'end)))
317              ;; Calculate implied time signature based on measureLength
318              ;; and beatLength for default value in get
319              (num-mom (ly:moment-div measure-length beat-length))
320              (num (inexact->exact
321                     (round (/ (ly:moment-main-numerator num-mom)
322                               (ly:moment-main-denominator num-mom)))))
323              (den (ly:moment-main-denominator beat-length))
324              (time-signature-fraction 
325                (get 'timeSignatureFraction (cons num den)))
326              (time (list (car time-signature-fraction)
327                          (cdr time-signature-fraction)))
328              (type (list (ly:moment-main-numerator test)
329                          (ly:moment-main-denominator test)))
330              (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
331                       measure-pos
332                       (ly:moment-add measure-length measure-pos)))
333              (grouping-moments (ending-moments beat-grouping 0 beat-length))
334              ;; Calculate implied measure length from beatGrouping
335              ;; and beatLength
336              (grouping-length (if (null? grouping-moments)
337                                   ZERO-MOMENT
338                                   (list-ref grouping-moments 
339                                             (1- (length grouping-moments)))))
340              (lst (list
341                     ;; Hmm, should junk user-override feature,
342                     ;; or split this in user-override and config section?
343                     (append function type '(* *))
344                     (append function '(* * * *))
345                     (append function type time)
346                     (append function '(* *) time)))
347              (predefined-setting (first-assoc lst settings)))
348          (if (or
349                 ;; always begin or end beams at beginning/ending of measure
350                 (= (ly:moment-main-numerator pos) 0)
351                 (first-member (map (lambda (x) (cons x pos)) lst) settings))
352              #t
353              (if (= dir START)
354                  ;; if no entry matches our function + time or type,
355                  ;; start anywhere
356                  (not predefined-setting)
357                  ;; if entry matches our function + time or type, check moment
358                  (if predefined-setting
359                     (equal? measure-pos (cdr predefined-setting))
360                     ;; if measure-length matches grouping-length, use
361                     ;; grouping moments, else use beat-length
362                     (if (equal? measure-length grouping-length)
363                         (member measure-pos grouping-moments)
364                         (= (ly:moment-main-denominator
365                            (ly:moment-div pos beat-length)) 1))))))))