]> git.donarmstrong.com Git - lilypond.git/blob - scm/time-signature-settings.scm
0c49d585eab456e8fc59772166466407a35803d1
[lilypond.git] / scm / time-signature-settings.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2009--2011 Carl Sorensen <c_sorensen@byu.edu>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18 ;;; specify time signature default settings
19
20 ;;; format:
21 ;;;
22 ;;; alist of
23 ;;;   (time-signature . default-properties) entries.
24 ;;;
25 ;;; where default-properties is an alist containing information about the
26 ;;; time signature.  Each default-properties set can contain the
27 ;;; following entries:
28 ;;;
29 ;;;   (baseMoment . (numerator . denominator))
30 ;;;   (beatStructure . structure-list)
31 ;;;   (beamExceptions . (alist of beam exceptions that don't follow beats))
32 ;;;
33 ;;; The alist of beam exceptions has the following entries:
34 ;;;
35 ;;;   (end . grouping-rules)
36 ;;;   (subdivide . grouping-rules)  (not yet implemented, reserved for future use)
37 ;;;
38 ;;;   grouping-rules is an alist containing (beam-type . grouping-list) entries
39 ;;;
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.
45 ;;;
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, 6/8, and 8/8.
50 ;;;
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,
53 ;;;     7/8, and 8/8.
54 ;;;
55 ;;; If no values are given for baseMoment and measureBeats, default values
56 ;;;   will be assigned:
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.
63 ;;;
64 ;;;       NOTE: numerator is kept in beam-type because of
65 ;;;             tuplets, e.g. (2 . 24) = (2 . 3) * (1 . 8)
66 ;;;             for eighth-note triplets.
67
68 (define-public default-time-signature-settings
69   '(
70     ;; in 2/2 time:
71     ;;   use defaults, but end beams with 32nd notes each 1 4 beat
72     ((2 . 2) .
73              ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8))))))))
74
75     ;; in 2/4, 2/8 and 2/16 time:
76     ;;   use defaults, so no entries are necessary
77
78     ;; in 3 2 time:
79     ;;   use defaults, but end beams with 32nd notes and higher each 1 4 beat
80
81     ((3 . 2) .
82              ((beamExceptions . ((end .  (((1 . 32) . (8 8 8 8 8 8))))))))
83
84     ;; in 3 4 time:
85     ;;   use defaults, but combine all beats into a unit if possible
86     ;;
87     ;;   set all beams to end on beats, but 1 8 to beam entire measure
88     ;;   in order to avoid beaming every beam type for the entire measure, we set
89     ;;   triplets back to every beat.
90     ((3 . 4) .
91              ((beamExceptions . ((end . (((1 . 8) . (6))            ;1/8 note whole measure
92                                          ((1 . 12) . (3 3 3)))))))) ;Anything shorter by beat
93
94     ;; in 3 8  time:
95     ;;   beam entire measure together
96     ((3 . 8) . ((beamExceptions . ((end . (((1 . 8) . (3))))))))
97
98     ;; in 3 16 time:
99     ;;   use defaults -- no entries necessary
100
101     ;; in 4 2 time:
102     ;;   use defaults, but end beams with 16th notes or finer each 1 4 beat
103     ((4 . 2) .
104              ((beamExceptions . ((end . (((1 . 16) . (4 4 4 4 4 4 4 4))))))))
105
106     ;; in 4 4 (common) time:
107     ;;   use defaults, but combine beats 1,2 and 3,4 if only 8th notes
108     ;;   NOTE: Any changes here need to be duplicated in
109     ;;         ly/engraver-init.ly where the default time signature is set
110     ;;         are set
111     ((4 . 4) .
112              ((beamExceptions . ((end . (((1 . 8) . (4 4))  ; 1/8 notes half measure
113                                          ((1 . 12) . (3 3 3 3)))))))) ;Anything shorter by beat
114
115     ;; in 4/8 time:
116     ;;   combine beats 1 and 2, so beam in 2
117     ((4 . 8) . ((beatStructure . (2 2))))
118
119     ;; in 4/8 and 4/16 time:
120     ;;   use defaults, so no entries necessary
121
122     ;; in 6 4 time:
123     ;;   use defaults, but end beams with 32nd or finer each 1/4 beat
124     ((6 . 4) .
125              ((beamExceptions . ((end .  (((1 . 16) . (4 4 4 4 4 4))))))))
126
127     ;; in 6 8 time:
128     ;;   use defaults, but end beams with 32nd or finer each 1 8 beat
129     ((6 . 8) .
130              ((beamExceptions . ((end .  (((1 . 32) . (4 4 4 4 4 4))))))))
131
132     ;; in 6 16 time:
133     ;;   use defaults, so no entry necessary
134
135     ;; in 9 4 time:
136     ;;   use defaults, but end beams with 32nd or finer each 1 4 beat
137     ((9 . 4) .
138              ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8 8 8))))))))
139
140     ;; in 9 8 time
141     ;;   use defaults, but end beams with 32nd notes each 1 8 beat
142     ((9 . 8) .
143              ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4 4 4 4))))))))
144
145     ;; in 9 16 time
146     ;;   use defaults, so no entry necessary
147
148     ;; in 12 4 time:
149     ;;   use defaults, but end beams with 32nd or finer notes each 1 4 beat
150     ((12 . 4) .
151               ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8 8 8 8 8 8 8))))))))
152
153     ;; in 12 8 time:
154     ;;   use defaults, but end beams with 32nd notes each 1 8 beat
155     ((12 . 8) .
156               ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4 4 4 4 4 4 4))))))))
157
158     ;; in 12 16 time:
159     ;;   use defaults; no entry needed
160
161     ;; in 5 8 time:
162     ;;   default: group (3 2)
163     ((5 . 8) .
164              ((baseMoment . (1 . 8))
165               (beatStructure . (3 2))))
166
167     ;; in 8 8 time:
168     ;;   default: group (3 3 2)
169     ((8 . 8) .
170              ((baseMoment . (1 . 8))
171               (beatStructure . (3 3 2))))
172
173     ))  ; end of alist definition
174
175 ;;;
176 ;;;  Accessor and constructor functions
177 ;;;
178
179 (define (get-setting my-symbol time-signature time-signature-settings)
180   "Get setting @code{my-symbol} for @code{time-signature} from
181 @code{time-signature-settings}."
182   (let ((my-time-signature-settings
183           (assoc-get time-signature time-signature-settings '())))
184    (assoc-get my-symbol my-time-signature-settings '())))
185
186 (define-public (make-setting base-fraction
187                              beat-structure
188                              beam-exceptions)
189   (list
190     (cons 'baseMoment base-fraction)
191     (cons 'beatStructure beat-structure)
192     (cons 'beamExceptions beam-exceptions)))
193
194 (define-public (base-fraction time-signature time-signature-settings)
195   "Get @code{baseMoment} fraction value for @var{time-signature} from
196 @var{time-signature-settings}."
197    (let ((return-value (get-setting 'baseMoment
198                                     time-signature
199                                     time-signature-settings)))
200      (if (null? return-value)
201          (cons 1 (cdr time-signature))
202          return-value)))
203
204 (define-public (beat-structure base-fraction time-signature time-signature-settings)
205   "Get @code{beatStructure} value in @var{base-fraction} units
206 for @var{time-signature} from @var{time-signature-settings}."
207   (define (fraction-divide numerator denominator)
208     (/ (* (car numerator) (cdr denominator))
209        (* (cdr numerator) (car denominator))))
210
211   (let ((return-value (get-setting 'beatStructure
212                                    time-signature
213                                    time-signature-settings)))
214     (if (null? return-value)
215         ;; calculate default beatStructure
216         (let* ((numerator (car time-signature))
217                (group-size (if (and (> numerator 3)
218                                     (zero? (remainder numerator 3)))
219                                3
220                                1))
221                (beat-length (cons (* group-size (car base-fraction))
222                                   (cdr base-fraction)))
223                (beat-count (fraction-divide time-signature beat-length)))
224           (if (integer? beat-count)
225               (make-list beat-count group-size)
226               '()))
227         ;; use value obtained from time-signature-settings
228         return-value)))
229
230 (define-public (beam-exceptions time-signature time-signature-settings)
231   "Get @code{beamExceptions} value for @var{time-signature} from
232 @var{time-signature-settings}."
233    (get-setting 'beamExceptions time-signature time-signature-settings))
234
235
236 ;;; Functions for overriding time-signature settings
237 ;;;
238
239 (define (override-property-setting context property setting value)
240   "Like the C++ code that executes \\override, but without type
241 checking."
242   (begin
243      (revert-property-setting context property setting)
244      (ly:context-set-property!
245        context
246        property
247        (cons (cons setting value) (ly:context-property context property)))))
248
249 (define (revert-property-setting context property setting)
250   "Like the C++ code that executes \revert, but without type
251 checking."
252
253   (define (entry-count alist entry-key)
254     "Count the number of entries in alist with a key of
255 ENTRY-KEY."
256     (cond
257       ((null? alist) 0)
258       ((equal? (caar alist) entry-key)
259          (+ 1 (entry-count (cdr alist) entry-key)))
260       (else (entry-count (cdr alist) entry-key))))
261
262   (define (revert-member alist entry-key)
263     "Return ALIST, with the first entry having a key of
264 ENTRY-KEY removed.  ALIST is not modified, instead
265 a fresh copy of the list-head is made."
266     (cond
267       ((null? alist) '())
268       ((equal? (caar alist) entry-key) (cdr alist))
269       (else (cons (car alist)
270                   (revert-member (cdr alist) entry-key)))))
271
272   ;; body of revert-property-setting
273   (let ((current-value (ly:context-property context property)))
274     (if (> (entry-count current-value setting) 1)
275         (ly:context-set-property!
276           context
277           property
278           (revert-member current-value setting)))))
279
280 (define-public (override-time-signature-setting time-signature setting)
281   "Override the time signature settings for the context in
282 @var{time-signature}, with the new setting alist @var{setting}."
283     (context-spec-music
284       (make-apply-context
285         (lambda (c) (override-property-setting
286                       c
287                       'timeSignatureSettings
288                       time-signature
289                       setting)))
290       'Timing))
291
292 (define-public (revert-time-signature-setting time-signature)
293   (context-spec-music
294     (make-apply-context
295       (lambda (c)
296         (revert-property-setting
297           c
298           'timeSignatureSettings
299           time-signature)))
300     'Timing))
301
302
303
304
305 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 ;;; Formatting of complex/compound time signatures
307
308 (define (insert-markups l m)
309   (let ((ll (reverse l)))
310     (let join-markups ((markups (list (car ll)))
311                        (remaining (cdr ll)))
312       (if (pair? remaining)
313           (join-markups (cons (car remaining) (cons m markups)) (cdr remaining))
314           markups))))
315
316 ;;; Use a centered-column inside a left-column, because the centered column
317 ;;; moves its reference point to the center, which the left-column undoes.
318 (define (format-time-fraction time-sig-fraction)
319   (let* ((revargs (reverse (map number->string time-sig-fraction)))
320          (den (car revargs))
321          (nums (reverse (cdr revargs))))
322     (make-override-markup '(baseline-skip . 0)
323       (make-number-markup
324         (make-left-column-markup (list
325           (make-center-column-markup (list
326             (make-line-markup (insert-markups nums "+"))
327             den))))))))
328
329 (define (format-complex-compound-time time-sig)
330   (make-override-markup '(baseline-skip . 0)
331     (make-number-markup
332       (make-line-markup
333         (insert-markups (map format-time-fraction time-sig)
334                         (make-vcenter-markup "+"))))))
335
336 (define-public (format-compound-time time-sig)
337   (cond
338     ((not (pair? time-sig)) (null-markup))
339     ((pair? (car time-sig)) (format-complex-compound-time time-sig))
340     (else (format-time-fraction time-sig))))
341
342
343 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 ;;; Measure length calculation of (possibly complex) compound time signatures
345
346 (define (calculate-time-fraction time-sig-fraction)
347   (let* ((revargs (reverse time-sig-fraction))
348          (den (car revargs))
349          (num (apply + (cdr revargs))))
350     (ly:make-moment num den)))
351
352 (define (calculate-complex-compound-time time-sig)
353   (let add-moment ((moment ZERO-MOMENT)
354                    (remaining (map calculate-time-fraction time-sig)))
355     (if (pair? remaining)
356         (add-moment (ly:moment-add moment (car remaining)) (cdr remaining))
357         moment)))
358
359 (define-public (calculate-compound-measure-length time-sig)
360   (cond
361     ((not (pair? time-sig)) (ly:make-moment 4 4))
362     ((pair? (car time-sig)) (calculate-complex-compound-time time-sig))
363     (else (calculate-time-fraction time-sig))))
364
365
366 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 ;;; Base beat length: Use the smallest denominator from all fraction
368
369 (define (calculate-compound-base-beat-full time-sig)
370   (apply max (map last time-sig)))
371
372 (define-public (calculate-compound-base-beat time-sig)
373   (ly:make-moment 1
374     (cond
375       ((not (pair? time-sig)) 4)
376       ((pair? (car time-sig)) (calculate-compound-base-beat-full time-sig))
377       (else (calculate-compound-base-beat-full (list time-sig))))))
378
379
380 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 ;;; Beat Grouping
382
383 (define (normalize-fraction frac beat)
384   (let* ((thisbeat (car (reverse frac)))
385          (factor (/ beat thisbeat)))
386     (map (lambda (f) (* factor f)) frac)))
387
388 (define (beat-grouping-internal time-sig)
389   ;; Normalize to given beat, extract the beats and join them to one list
390   (let* ((beat (calculate-compound-base-beat-full time-sig))
391          (normalized (map (lambda (f) (normalize-fraction f beat)) time-sig))
392          (beats (map (lambda (f) (reverse (cdr (reverse f)))) normalized)))
393     (apply append beats)))
394
395 (define-public (calculate-compound-beat-grouping time-sig)
396   (cond
397     ((not (pair? time-sig)) '(2 . 2))
398     ((pair? (car time-sig)) (beat-grouping-internal time-sig))
399     (else (beat-grouping-internal (list time-sig)))))