]> git.donarmstrong.com Git - lilypond.git/blob - scm/time-signature-settings.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / time-signature-settings.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2009--2015 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 the length as a rational number
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 ;;;     Tuplets are referenced using their actual (scaled) length, so
56 ;;;     a 3/2 tuplet of the 1/8 kind would get exceptions looked up
57 ;;;     under 1/12.
58 ;;;
59 ;;; If no values are given for baseMoment and measureBeats, default values
60 ;;;   will be assigned:
61 ;;;   baseMoment gets the value (/ time-signature-denominator)
62 ;;;   beatStructure gets a list of (3 3 3 ...), where the number of entries is the
63 ;;;     number of beats, each containing 3 base-moments, if the time
64 ;;;     signature numerator is greater than 3 and divisible by 3, and
65 ;;;     a list of (1 1 1 ...), where the number of entries is the
66 ;;;     number of base moments in a measure otherwise.
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/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, so no entries necessary
129
130     ;; in 6 16 time:
131     ;;   use defaults, so no entries necessary
132
133     ;; in 9 4 time:
134     ;;   use defaults, but end beams with 32nd or finer each 1 4 beat
135     ((9 . 4) .
136      ((beamExceptions . ((end . ((1/32 . (8 8 8 8 8 8 8 8))))))))
137
138     ;; in 9 8 time
139     ;;   use defaults, so no entries necessary
140
141     ;; in 9 16 time
142     ;;   use defaults, so no entries necessary
143
144     ;; in 12 4 time:
145     ;;   use defaults, but end beams with 32nd or finer notes each 1 4 beat
146     ((12 . 4) .
147      ((beamExceptions . ((end . ((1/32 . (8 8 8 8 8 8 8 8 8 8 8 8))))))))
148
149     ;; in 12 8 time:
150     ;;   use defaults, so no entries necessary
151
152     ;; in 12 16 time:
153     ;;   use defaults, so no entries necessary
154
155     ;; in 5 8 time:
156     ;;   default: group (3 2)
157     ((5 . 8) .
158      ((beatStructure . (3 2))))
159
160     ;; in 8 8 time:
161     ;;   default: group (3 3 2)
162     ((8 . 8) .
163      ((beatStructure . (3 3 2))))
164
165     ))  ; end of alist definition
166
167 ;;;
168 ;;;  Accessor and constructor functions
169 ;;;
170
171 (define (get-setting my-symbol time-signature time-signature-settings)
172   "Get setting @code{my-symbol} for @code{time-signature} from
173 @code{time-signature-settings}."
174   (let ((my-time-signature-settings
175          (assoc-get time-signature time-signature-settings '())))
176     (assoc-get my-symbol my-time-signature-settings '())))
177
178 (define-public (make-setting base-fraction
179                              beat-structure
180                              beam-exceptions)
181   (list
182    (cons 'baseMoment (if (pair? base-fraction)
183                          (/ (car base-fraction) (cdr base-fraction))
184                          base-fraction))
185    (cons 'beatStructure beat-structure)
186    (cons 'beamExceptions beam-exceptions)))
187
188 (define-public (base-length time-signature time-signature-settings)
189   "Get @code{baseMoment} rational 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         (/ (cdr time-signature))
196         return-value)))
197
198 (define-public (beat-structure base-length time-signature time-signature-settings)
199   "Get @code{beatStructure} value in @var{base-length} units
200 for @var{time-signature} from @var{time-signature-settings}."
201
202   (let ((return-value (get-setting 'beatStructure
203                                    time-signature
204                                    time-signature-settings)))
205     (if (null? return-value)
206         ;; calculate default beatStructure
207         (let* ((numerator (car time-signature))
208                (group-size (if (and (> numerator 3)
209                                     (zero? (remainder numerator 3)))
210                                3
211                                1))
212                (beat-count (/ (car time-signature)
213                               (cdr time-signature)
214                               base-length
215                               group-size)))
216           (if (integer? beat-count)
217               (make-list beat-count group-size)
218               '()))
219         ;; use value obtained from time-signature-settings
220         return-value)))
221
222 (define-public (beam-exceptions time-signature time-signature-settings)
223   "Get @code{beamExceptions} value for @var{time-signature} from
224 @var{time-signature-settings}."
225   (get-setting 'beamExceptions time-signature time-signature-settings))
226
227
228 ;;; Functions for overriding time-signature settings
229 ;;;
230
231 (define (override-property-setting context property setting value)
232   "Like the C++ code that executes \\override, but without type
233 checking."
234   (begin
235     (ly:context-set-property!
236      context
237      property
238      (cons (cons setting value) (ly:context-property context property)))))
239
240 (define (revert-property-setting context property setting)
241   "Like the C++ code that executes \revert, but without type
242 checking."
243
244   (define (entry-count alist entry-key)
245     "Count the number of entries in alist with a key of
246 ENTRY-KEY."
247     (cond
248      ((null? alist) 0)
249      ((equal? (caar alist) entry-key)
250       (+ 1 (entry-count (cdr alist) entry-key)))
251      (else (entry-count (cdr alist) entry-key))))
252
253   (define (revert-member alist entry-key)
254     "Return ALIST, with the first entry having a key of
255 ENTRY-KEY removed.  ALIST is not modified, instead
256 a fresh copy of the list-head is made."
257     (cond
258      ((null? alist) '())
259      ((equal? (caar alist) entry-key) (cdr alist))
260      (else (cons (car alist)
261                  (revert-member (cdr alist) entry-key)))))
262
263   ;; body of revert-property-setting
264   (let ((current-value (ly:context-property context property)))
265     (if (> (entry-count current-value setting) 0)
266         (ly:context-set-property!
267          context
268          property
269          (revert-member current-value setting)))))
270
271 (define-public (override-time-signature-setting time-signature setting)
272   "Override the time signature settings for the context in
273 @var{time-signature}, with the new setting alist @var{setting}."
274   (context-spec-music
275    (make-apply-context
276     (lambda (c) (override-property-setting
277                  c
278                  'timeSignatureSettings
279                  time-signature
280                  setting)))
281    'Timing))
282
283 (define-public (revert-time-signature-setting time-signature)
284   (context-spec-music
285    (make-apply-context
286     (lambda (c)
287       (revert-property-setting
288        c
289        'timeSignatureSettings
290        time-signature)))
291    'Timing))
292
293
294
295
296 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 ;;; Formatting of complex/compound time signatures
298
299 ; There ought to be a \join-line sep {...} command
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-left-column-markup
316                             (list (make-center-column-markup
317                                    (list (make-line-markup (insert-markups nums "+"))
318                                          den)))))))
319
320 (define (format-time-numerator time-sig)
321   (make-vcenter-markup (number->string (car time-sig))))
322
323 (define (format-time-element time-sig)
324   (cond ((number-pair? time-sig)
325          (format-time-fraction (list (car time-sig) (cdr time-sig))))
326         ((pair? (cdr time-sig))
327          (format-time-fraction time-sig))
328         (else
329          (format-time-numerator time-sig))))
330
331 (define (format-time-list time-sig)
332   (make-override-markup '(baseline-skip . 0)
333                         (make-line-markup
334                          (insert-markups (map format-time-element time-sig)
335                                          (make-vcenter-markup "+")))))
336
337 (define (format-compound-time time-sig)
338   (make-number-markup
339    (cond
340     ((number? time-sig) (format-time-element (list time-sig)))
341     ((number-pair? time-sig)
342      (format-time-element (list (car time-sig) (cdr time-sig))))
343     ((pair? (car time-sig)) (format-time-list time-sig))
344     (else (format-time-element time-sig)))))
345
346 (define-markup-command (compound-meter layout props time-sig)
347   (number-or-pair?)
348   #:category music
349   "Draw a numeric time signature.
350
351 @lilypond[verbatim,quote]
352 \\markup {
353   \\column {
354     \\line { Single number: \\compound-meter #3 }
355     \\line { Conventional: \\compound-meter #'(4 . 4)
356                        or \\compound-meter #'(4 4) }
357     \\line { Compound: \\compound-meter #'(2 3 8) }
358     \\line { Single-number compound: \\compound-meter #'((2) (3)) }
359     \\line { Complex compound: \\compound-meter #'((2 3 8) (3 4)) }
360   }
361 }
362 @end lilypond
363 "
364   (interpret-markup layout props (format-compound-time time-sig)))
365
366 (add-simple-time-signature-style 'numbered make-compound-meter-markup)
367
368 (add-simple-time-signature-style 'single-digit
369   (lambda (fraction) (make-compound-meter-markup (car fraction))))
370
371 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 ;;; Formatting of symbolic time signatures
373
374 (define-public (make-glyph-time-signature-markup style fraction)
375   "Make markup for a symbolic time signature.  If the music font does not have a glyph for the requested style and fraction, issue a warning and make a numbered time signature instead."
376   (make-first-visible-markup
377    (list (make-musicglyph-markup (string-append
378                                   "timesig."
379                                   (symbol->string style)
380                                   (number->string (car fraction))
381                                   (number->string (cdr fraction))))
382          (make-compound-meter-markup fraction))))
383
384 (define-public (make-c-time-signature-markup fraction)
385   "Make markup for the `C' time signature style."
386   (let ((n (car fraction))
387         (d (cdr fraction)))
388     ; check specific fractions to avoid warnings when no glyph exists
389     (if (or (and (= n 2) (= d 2))
390             (and (= n 4) (= d 4)))
391         (make-glyph-time-signature-markup 'C fraction)
392         (make-compound-meter-markup fraction))))
393
394 (add-simple-time-signature-style 'C make-c-time-signature-markup)
395 (add-simple-time-signature-style 'default make-c-time-signature-markup)
396
397 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 ;;; Measure length calculation of (possibly complex) compound time signatures
399
400 (define (calculate-time-fraction time-sig-fraction)
401   (let* ((revargs (reverse time-sig-fraction))
402          (den (car revargs))
403          (num (apply + (cdr revargs))))
404     (ly:make-moment num den)))
405
406 (define (calculate-complex-compound-time time-sig)
407   (let add-moment ((moment ZERO-MOMENT)
408                    (remaining (map calculate-time-fraction time-sig)))
409     (if (pair? remaining)
410         (add-moment (ly:moment-add moment (car remaining)) (cdr remaining))
411         moment)))
412
413 (define-public (calculate-compound-measure-length time-sig)
414   (cond
415    ((not (pair? time-sig)) (ly:make-moment 4 4))
416    ((pair? (car time-sig)) (calculate-complex-compound-time time-sig))
417    (else (calculate-time-fraction time-sig))))
418
419
420 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 ;;; Base beat length: Use the smallest denominator from all fraction
422
423 (define (calculate-compound-base-beat-full time-sig)
424   (apply max (map last time-sig)))
425
426 (define-public (calculate-compound-base-beat time-sig)
427   (ly:make-moment 1
428                   (cond
429                    ((not (pair? time-sig)) 4)
430                    ((pair? (car time-sig)) (calculate-compound-base-beat-full time-sig))
431                    (else (calculate-compound-base-beat-full (list time-sig))))))
432
433
434 ;;;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 ;;; Beat Grouping
436
437 (define (normalize-fraction frac beat)
438   (let* ((thisbeat (car (reverse frac)))
439          (factor (/ beat thisbeat)))
440     (map (lambda (f) (* factor f)) frac)))
441
442 (define (beat-grouping-internal time-sig)
443   ;; Normalize to given beat, extract the beats and join them to one list
444   (let* ((beat (calculate-compound-base-beat-full time-sig))
445          (normalized (map (lambda (f) (normalize-fraction f beat)) time-sig))
446          (beats (map (lambda (f) (drop-right f 1)) normalized)))
447     (concatenate beats)))
448
449 (define-public (calculate-compound-beat-grouping time-sig)
450   (cond
451    ((not (pair? time-sig)) '(2 . 2))
452    ((pair? (car time-sig)) (beat-grouping-internal time-sig))
453    (else (beat-grouping-internal (list time-sig)))))