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