]> git.donarmstrong.com Git - lilypond.git/blob - scm/time-signature-settings.scm
Merge branch 'master' into lilypond/translation
[lilypond.git] / scm / time-signature-settings.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2009--2010 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, 5/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 ;;; If no value is given for beatCombinations, no beats will be combined without
65 ;;;   beamExceptions rules.
66 ;;;
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.
70
71 (define-public default-time-signature-settings
72   '(
73     ;; in 2/2 time:
74     ;;   use defaults, but end beams with 32nd notes each 1 4 beat
75     ((2 . 2) .
76              ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8))))))))
77
78     ;; in 2/4, 2/8 and 2/16 time:
79     ;;   use defaults, so no entries are necessary
80
81     ;; in 3 2 time:
82     ;;   use defaults, but end beams with 32nd notes and higher each 1 4 beat
83
84     ((3 . 2) .
85              ((beamExceptions . ((end .  (((1 . 32) . (8 8 8 8 8 8))))))))
86
87     ;; in 3 4 time:
88     ;;   use defaults, but combine all beats into a unit if possible
89     ;;
90     ;;   set all beams to end on beats, but 1 8 to beam entire measure
91     ;;   in order to avoid beaming every beat for entier measure, we set
92     ;;   triplets back to every beat.
93     ((3 . 4) .
94              ((beamExceptions . ((end . (((1 . 8) . (6))
95                                          ((1 . 12) . (3 3 3))))))))
96
97     ;; in 3 8  time:
98     ;;   beam entire measure together
99     ((3 . 8) . ((beamExceptions . ((end . (((1 . 8) . (3))))))))
100
101     ;; in 3 16 time:
102     ;;   use defaults -- no entries necessary
103
104     ;; in 4 2 time:
105     ;;   use defaults, but end beams with 16th notes or finer each 1 4 beat
106     ((4 . 2) .
107              ((beamExceptions . ((end . (((1 . 16) . (4 4 4 4 4 4 4 4))))))))
108
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
113     ;;         are set
114     ((4 . 4) .
115              ((beamExceptions . ((end . (((1 . 8) . (4 4))
116                                          ((1 . 12) . (3 3 3 3))))))))
117
118     ;; in 4/8 time:
119     ;;   combine beats 1 and 2, so beam in 2
120     ((4 . 8) . ((beatStructure . (2 2))))
121
122     ;; in 4/8 and 4/16 time:
123     ;;   use defaults, so no entries necessary
124
125     ;; in 6 4 time:
126     ;;   use defaults, but end beams with 32nd or finer each 1/4 beat
127     ((6 . 4) .
128              ((beamExceptions . ((end .  (((1 . 16) . (4 4 4 4 4 4))))))))
129
130     ;; in 6 8 time:
131     ;;   use defaults, but end beams with 32nd or finer each 1 8 beat
132     ((6 . 8) .
133              ((beamExceptions . ((end .  (((1 . 32) . (4 4 4 4 4 4))))))))
134
135     ;; in 6 16 time:
136     ;;   use defaults, so no entry necessary
137
138     ;; in 9 4 time:
139     ;;   use defaults, but end beams with 32nd or finer each 1 4 beat
140     ((9 . 4) .
141              ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8 8 8))))))))
142
143     ;; in 9 8 time
144     ;;   use defaults, but end beams with 32nd notes each 1 8 beat
145     ((9 . 8) .
146              ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4 4 4 4))))))))
147
148     ;; in 9 16 time
149     ;;   use defaults, so no entry necessary
150
151     ;; in 12 4 time:
152     ;;   use defaults, but end beams with 32nd or finer notes each 1 4 beat
153     ((12 . 4) .
154               ((beamExceptions . ((end . (((1 . 32) . (8 8 8 8 8 8 8 8 8 8 8 8))))))))
155
156     ;; in 12 8 time:
157     ;;   use defaults, but end beams with 32nd notes each 1 8 beat
158     ((12 . 8) .
159               ((beamExceptions . ((end . (((1 . 32) . (4 4 4 4 4 4 4 4 4 4 4 4))))))))
160
161     ;; in 12 16 time:
162     ;;   use defaults; no entry needed
163
164     ;; in 5 8 time:
165     ;;   default: group (3 2)
166     ((5 . 8) .
167              ((baseMoment . (1 . 8))
168               (beatStructure . (3 2))))
169
170     ;; in 8 8 time:
171     ;;   default: group (3 3 2)
172     ((8 . 8) .
173              ((baseMoment . (1 . 8))
174               (beatStructure . (3 3 2))))
175
176     ))  ; end of alist definition
177
178 ;;;
179 ;;;  Accessor and constructor functions
180 ;;;
181
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 '())))
188
189 (define-public (make-setting base-fraction
190                              beat-structure
191                              beam-exceptions)
192   (list
193     (cons 'baseMoment base-fraction)
194     (cons 'beatStructure beat-structure)
195     (cons 'beamExceptions beam-exceptions)))
196
197 (define-public (base-fraction time-signature time-signature-settings)
198   "Get @code{baseMoment} fraction value for @code{time-signature} from
199 @code{time-signature-settings}."
200    (let ((return-value (get-setting 'baseMoment
201                                     time-signature
202                                     time-signature-settings)))
203      (if (null? return-value)
204          (cons 1 (cdr time-signature))
205          return-value)))
206
207 (define-public (beat-structure base-fraction time-signature time-signature-settings)
208   "Get beatStructure value in @code{base-fraction} units
209 for @code{time-signature} from
210 @code{time-signature-settings}."
211   (define (fraction-divide numerator denominator)
212     (/ (* (car numerator) (cdr denominator))
213        (* (cdr numerator) (car denominator))))
214
215   (let ((return-value (get-setting 'beatStructure
216                                    time-signature
217                                    time-signature-settings)))
218     (if (null? return-value)
219         ;; calculate default beatStructure
220         (let* ((numerator (car time-signature))
221                (group-size (if (and (> numerator 3)
222                                     (zero? (remainder numerator 3)))
223                                3
224                                1))
225                (beat-length (cons (* group-size (car base-fraction))
226                                   (cdr base-fraction)))
227                (beat-count (fraction-divide time-signature beat-length)))
228           (if (integer? beat-count)
229               (make-list beat-count group-size)
230               '()))
231         ;; use value obtained from time-signature-settings
232         return-value)))
233
234 (define-public (beam-exceptions time-signature time-signature-settings)
235   "Get beamExceptions value for @code{time-signature} from
236 @code{time-signature-settings}."
237    (get-setting 'beamExceptions time-signature time-signature-settings))
238
239
240 ;;; Functions for overriding time-signature settings
241 ;;;
242
243 (define (override-property-setting context property setting value)
244   "Like the C++ code that executes \\override, but without type
245 checking. "
246   (begin
247      (revert-property-setting context property setting)
248      (ly:context-set-property!
249        context
250        property
251        (cons (cons setting value) (ly:context-property context property)))))
252
253 (define (revert-property-setting context property setting)
254   "Like the C++ code that executes \revert, but without type
255 checking. "
256
257   (define (revert-member alist entry new)
258     "Return ALIST, with ENTRY removed.  ALIST is not modified, instead
259 a fresh copy of the list-head is made."
260     (cond
261       ((null? alist) new)
262       ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
263       (else (revert-member (cdr alist) entry (cons (car alist) new)))))
264
265   (ly:context-set-property!
266     context property
267     (revert-member (ly:context-property context property) setting '())))
268
269 (define-public (override-time-signature-setting time-signature setting . rest)
270   "Override the time signature settings for the context in @var{rest},
271 with the new setting alist @var{setting}. "
272     (context-spec-music
273       (make-apply-context
274         (lambda (c) (override-property-setting
275                       c
276                       'timeSignatureSettings
277                       time-signature
278                       setting)))
279       (if (and (pair? rest) (symbol? (car rest)))
280           (car rest)
281           'Voice)))
282
283 (define-public (score-override-time-signature-setting
284                  time-signature setting)
285   (override-time-signature-setting
286     time-signature setting 'Score))
287
288 (define-public (revert-time-signature-setting
289                   time-signature . rest)
290   (context-spec-music
291     (make-apply-context
292       (lambda (c)
293         (revert-property-setting
294           c
295           'timeSignatureSettings
296           time-signature)))
297     (if (and (pair? rest) (symbol? (car rest)))
298         (car rest)
299         'Voice)))