1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2009--2011 Carl Sorensen <c_sorensen@byu.edu>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 ;;; specify time signature default settings
23 ;;; (time-signature . default-properties) entries.
25 ;;; where default-properties is an alist containing information about the
26 ;;; time signature. Each default-properties set can contain the
27 ;;; following entries:
29 ;;; (baseMoment . (numerator . denominator))
30 ;;; (beatStructure . structure-list)
31 ;;; (beamExceptions . (alist of beam exceptions that don't follow beats))
33 ;;; The alist of beam exceptions has the following entries:
35 ;;; (end . grouping-rules)
36 ;;; (subdivide . grouping-rules) (not yet implemented, reserved for future use)
38 ;;; grouping-rules is an alist containing (beam-type . grouping-list) entries
40 ;;; beam-type is (numerator . denominator)
41 ;;; grouping-list is a list that specifies the
42 ;;; number of stems of the given duration that are grouped in a beamed unit.
43 ;;; For an exception, the duration used is beam-type. For measureBeats,
44 ;;; the duration used is baseMoment.
46 ;;; If an exception is specified for a given beam-type, it will apply to all
47 ;;; beams of shorter durations that don't have an individual exception, so
48 ;;; ((1 . 8) . (3 3 2))
49 ;;; will cause all primary beams to be broken at 3/8, 5/8, and 8/8.
51 ;;; ((1. 32) . (16 8 4 4))
52 ;;; will cause all 1/32, 1/64, and 1/128 beams to be broken at 1/2, 3/4,
55 ;;; If no values are given for baseMoment and measureBeats, default values
57 ;;; baseMoment gets the value (ly:make-moment 1 time-signature-denominator)
58 ;;; beatStructure gets a list of (3 3 3 ...), where the number of entries is the
59 ;;; number of beats, each containing 3 base-moments, if the time
60 ;;; signature numerator is greater than 3 and divisible by 3, and
61 ;;; a list of (1 1 1 ...), where the number of entries is the
62 ;;; number of base moments in a measure otherwise.
64 ;;; If no value is given for beatCombinations, no beats will be combined without
65 ;;; beamExceptions rules.
67 ;;; NOTE: numerator is kept in beam-type because of
68 ;;; tuplets, e.g. (2 . 24) = (2 . 3) * (1 . 8)
69 ;;; for eighth-note triplets.
71 (define-public default-time-signature-settings
74 ;; use defaults, but end beams with 32nd notes each 1 4 beat
76 ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8))))))))
78 ;; in 2/4, 2/8 and 2/16 time:
79 ;; use defaults, so no entries are necessary
82 ;; use defaults, but end beams with 32nd notes and higher each 1 4 beat
85 ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8))))))))
88 ;; use defaults, but combine all beats into a unit if possible
90 ;; set all beams to end on beats, but 1 8 to beam entire measure
91 ;; in order to avoid beaming every beam type for the entire measure, we set
92 ;; triplets back to every beat.
94 ((beamExceptions . ((end . (((1 . 8) . (6)) ;1/8 note whole measure
95 ((1 . 12) . (3 3 3)))))))) ;Anything shorter by beat
98 ;; beam entire measure together
99 ((3 . 8) . ((beamExceptions . ((end . (((1 . 8) . (3))))))))
102 ;; use defaults -- no entries necessary
105 ;; use defaults, but end beams with 16th notes or finer each 1 4 beat
107 ((beamExceptions . ((end . (((1 . 16) . (4 4 4 4 4 4 4 4))))))))
109 ;; in 4 4 (common) time:
110 ;; use defaults, but combine beats 1,2 and 3,4 if only 8th notes
111 ;; NOTE: Any changes here need to be duplicated in
112 ;; ly/engraver-init.ly where the default time signature is set
115 ((beamExceptions . ((end . (((1 . 8) . (4 4)) ; 1/8 notes half measure
116 ((1 . 12) . (3 3 3 3)))))))) ;Anything shorter by beat
119 ;; combine beats 1 and 2, so beam in 2
120 ((4 . 8) . ((beatStructure . (2 2))))
122 ;; in 4/8 and 4/16 time:
123 ;; use defaults, so no entries necessary
126 ;; use defaults, but end beams with 32nd or finer each 1/4 beat
128 ((beamExceptions . ((end . (((1 . 16) . (4 4 4 4 4 4))))))))
131 ;; use defaults, but end beams with 32nd or finer each 1 8 beat
133 ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4))))))))
136 ;; use defaults, so no entry necessary
139 ;; use defaults, but end beams with 32nd or finer each 1 4 beat
141 ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8 8 8))))))))
144 ;; use defaults, but end beams with 32nd notes each 1 8 beat
146 ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4 4 4 4))))))))
149 ;; use defaults, so no entry necessary
152 ;; use defaults, but end beams with 32nd or finer notes each 1 4 beat
154 ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8 8 8 8 8 8 8))))))))
157 ;; use defaults, but end beams with 32nd notes each 1 8 beat
159 ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4 4 4 4 4 4 4))))))))
162 ;; use defaults; no entry needed
165 ;; default: group (3 2)
167 ((baseMoment . (1 . 8))
168 (beatStructure . (3 2))))
171 ;; default: group (3 3 2)
173 ((baseMoment . (1 . 8))
174 (beatStructure . (3 3 2))))
176 )) ; end of alist definition
179 ;;; Accessor and constructor functions
182 (define (get-setting my-symbol time-signature time-signature-settings)
183 "Get setting @code{my-symbol} for @code{time-signature} from
184 @code{time-signature-settings}."
185 (let ((my-time-signature-settings
186 (assoc-get time-signature time-signature-settings '())))
187 (assoc-get my-symbol my-time-signature-settings '())))
189 (define-public (make-setting base-fraction
193 (cons 'baseMoment base-fraction)
194 (cons 'beatStructure beat-structure)
195 (cons 'beamExceptions beam-exceptions)))
197 (define-public (base-fraction time-signature time-signature-settings)
198 "Get @code{baseMoment} fraction value for @var{time-signature} from
199 @var{time-signature-settings}."
200 (let ((return-value (get-setting 'baseMoment
202 time-signature-settings)))
203 (if (null? return-value)
204 (cons 1 (cdr time-signature))
207 (define-public (beat-structure base-fraction time-signature time-signature-settings)
208 "Get @code{beatStructure} value in @var{base-fraction} units
209 for @var{time-signature} from @var{time-signature-settings}."
210 (define (fraction-divide numerator denominator)
211 (/ (* (car numerator) (cdr denominator))
212 (* (cdr numerator) (car denominator))))
214 (let ((return-value (get-setting 'beatStructure
216 time-signature-settings)))
217 (if (null? return-value)
218 ;; calculate default beatStructure
219 (let* ((numerator (car time-signature))
220 (group-size (if (and (> numerator 3)
221 (zero? (remainder numerator 3)))
224 (beat-length (cons (* group-size (car base-fraction))
225 (cdr base-fraction)))
226 (beat-count (fraction-divide time-signature beat-length)))
227 (if (integer? beat-count)
228 (make-list beat-count group-size)
230 ;; use value obtained from time-signature-settings
233 (define-public (beam-exceptions time-signature time-signature-settings)
234 "Get @code{beamExceptions} value for @var{time-signature} from
235 @var{time-signature-settings}."
236 (get-setting 'beamExceptions time-signature time-signature-settings))
239 ;;; Functions for overriding time-signature settings
242 (define (override-property-setting context property setting value)
243 "Like the C++ code that executes \\override, but without type
246 (revert-property-setting context property setting)
247 (ly:context-set-property!
250 (cons (cons setting value) (ly:context-property context property)))))
252 (define (revert-property-setting context property setting)
253 "Like the C++ code that executes \revert, but without type
256 (define (entry-count alist entry-key)
257 "Count the number of entries in alist with a key of
261 ((equal? (caar alist) entry-key)
262 (+ 1 (entry-count (cdr alist) entry-key)))
263 (else (entry-count (cdr alist) entry-key))))
265 (define (revert-member alist entry-key)
266 "Return ALIST, with the first entry having a key of
267 ENTRY-KEY removed. ALIST is not modified, instead
268 a fresh copy of the list-head is made."
271 ((equal? (caar alist) entry-key) (cdr alist))
272 (else (cons (car alist)
273 (revert-member (cdr alist) entry-key)))))
275 ;; body of revert-property-setting
276 (let ((current-value (ly:context-property context property)))
277 (if (> (entry-count current-value setting) 1)
278 (ly:context-set-property!
281 (revert-member current-value setting)))))
283 (define-public (override-time-signature-setting time-signature setting)
284 "Override the time signature settings for the context in
285 @var{time-signature}, with the new setting alist @var{setting}."
288 (lambda (c) (override-property-setting
290 'timeSignatureSettings
295 (define-public (revert-time-signature-setting time-signature)
299 (revert-property-setting
301 'timeSignatureSettings
308 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 ;;; Formatting of complex/compound time signatures
311 (define (insert-markups l m)
312 (let ((ll (reverse l)))
313 (let join-markups ((markups (list (car ll)))
314 (remaining (cdr ll)))
315 (if (pair? remaining)
316 (join-markups (cons (car remaining) (cons m markups)) (cdr remaining))
319 ;;; Use a centered-column inside a left-column, because the centered column
320 ;;; moves its reference point to the center, which the left-column undoes.
321 (define (format-time-fraction time-sig-fraction)
322 (let* ((revargs (reverse (map number->string time-sig-fraction)))
324 (nums (reverse (cdr revargs))))
325 (make-override-markup '(baseline-skip . 0)
327 (make-left-column-markup (list
328 (make-center-column-markup (list
329 (make-line-markup (insert-markups nums "+"))
332 (define (format-complex-compound-time time-sig)
333 (make-override-markup '(baseline-skip . 0)
336 (insert-markups (map format-time-fraction time-sig)
337 (make-vcenter-markup "+"))))))
339 (define-public (format-compound-time time-sig)
341 ((not (pair? time-sig)) (null-markup))
342 ((pair? (car time-sig)) (format-complex-compound-time time-sig))
343 (else (format-time-fraction time-sig))))
346 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 ;;; Measure length calculation of (possibly complex) compound time signatures
349 (define (calculate-time-fraction time-sig-fraction)
350 (let* ((revargs (reverse time-sig-fraction))
352 (num (apply + (cdr revargs))))
353 (ly:make-moment num den)))
355 (define (calculate-complex-compound-time time-sig)
356 (let add-moment ((moment ZERO-MOMENT)
357 (remaining (map calculate-time-fraction time-sig)))
358 (if (pair? remaining)
359 (add-moment (ly:moment-add moment (car remaining)) (cdr remaining))
362 (define-public (calculate-compound-measure-length time-sig)
364 ((not (pair? time-sig)) (ly:make-moment 4 4))
365 ((pair? (car time-sig)) (calculate-complex-compound-time time-sig))
366 (else (calculate-time-fraction time-sig))))
369 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 ;;; Base beat length: Use the smallest denominator from all fraction
372 (define (calculate-compound-base-beat-full time-sig)
373 (apply max (map last time-sig)))
375 (define-public (calculate-compound-base-beat time-sig)
378 ((not (pair? time-sig)) 4)
379 ((pair? (car time-sig)) (calculate-compound-base-beat-full time-sig))
380 (else (calculate-compound-base-beat-full (list time-sig))))))
383 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 (define (normalize-fraction frac beat)
387 (let* ((thisbeat (car (reverse frac)))
388 (factor (/ beat thisbeat)))
389 (map (lambda (f) (* factor f)) frac)))
391 (define (beat-grouping-internal time-sig)
392 ;; Normalize to given beat, extract the beats and join them to one list
393 (let* ((beat (calculate-compound-base-beat-full time-sig))
394 (normalized (map (lambda (f) (normalize-fraction f beat)) time-sig))
395 (beats (map (lambda (f) (reverse (cdr (reverse f)))) normalized)))
396 (apply append beats)))
398 (define-public (calculate-compound-beat-grouping time-sig)
400 ((not (pair? time-sig)) '(2 . 2))
401 ((pair? (car time-sig)) (beat-grouping-internal time-sig))
402 (else (beat-grouping-internal (list time-sig)))))