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