]> git.donarmstrong.com Git - lilypond.git/blob - scm/auto-beam.scm
Merge branch 'master' of carldsorensen@git.sv.gnu.org:/srv/git/lilypond
[lilypond.git] / scm / auto-beam.scm
1 ;;;; auto-beam.scm -- Auto-beam-engraver settings
2 ;;;;
3 ;;;; source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2000--2008 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;;; specify generic beam begin and end times
8
9 ;;; format:
10 ;;;
11 ;;;   function shortest-duration-in-beam time-signature
12 ;;;
13 ;;; where
14 ;;;
15 ;;;     function = begin or end
16 ;;;     shortest-duration-in-beam = numerator denominator; e.g.: 1 16
17 ;;;     time-signature = numerator denominator, e.g.: 4 4
18 ;;;
19 ;;; unspecified or wildcard entries for duration or time-signature
20 ;;; are given by * *
21
22 ;;; maybe do:  '(end shortest-1 16 time-3 4) ?
23
24 (define-public default-auto-beam-settings
25   `(
26     ;; in 3 2 time:
27     ;;   end beams each 1 2 note
28     ;;   end beams with 16th notes each 1 4 note
29     ;;   end beams with 32nd notes each 1 8 note
30
31     ((end * * 3 2) . ,(ly:make-moment 1 2))
32     ((end * * 3 2) . ,(ly:make-moment 2 2))
33
34     ((end 1 16 3 2) . ,(ly:make-moment 1 4))
35     ((end 1 16 3 2) . ,(ly:make-moment 1 2))
36     ((end 1 16 3 2) . ,(ly:make-moment 3 4))
37     ((end 1 16 3 2) . ,(ly:make-moment 5 4))
38
39     ((end 1 32 3 2) . ,(ly:make-moment 1 8))
40     ((end 1 32 3 2) . ,(ly:make-moment 1 4))
41     ((end 1 32 3 2) . ,(ly:make-moment 3 8))
42     ((end 1 32 3 2) . ,(ly:make-moment 1 2))
43     ((end 1 32 3 2) . ,(ly:make-moment 5 8))
44     ((end 1 32 3 2) . ,(ly:make-moment 3 4))
45     ((end 1 32 3 2) . ,(ly:make-moment 7 8))
46     ((end 1 32 3 2) . ,(ly:make-moment 9 8))
47     ((end 1 32 3 2) . ,(ly:make-moment 5 4))
48     ((end 1 32 3 2) . ,(ly:make-moment 11 8))
49
50     ((end * * 3 4) . ,(ly:make-moment 3 4))
51
52     ((end 1 16 3 4) . ,(ly:make-moment 1 4))
53     ((end 1 16 3 4) . ,(ly:make-moment 1 2))
54
55     ((end 1 32 3 4) . ,(ly:make-moment 1 8))
56     ((end 1 32 3 4) . ,(ly:make-moment 1 4))
57     ((end 1 32 3 4) . ,(ly:make-moment 3 8))
58     ((end 1 32 3 4) . ,(ly:make-moment 1 2))
59     ((end 1 32 3 4) . ,(ly:make-moment 5 8))
60
61     ((end * * 3 8) . ,(ly:make-moment 3 8))
62
63     ;; in common time:
64     ;;   end beams each 1 2 note
65     ;;   end beams with 32nd notes each 1 8 note
66     ;;   end beams with 1 8 triplets each 1 4 note
67
68     ((end * * 4 4) . ,(ly:make-moment 1 2))
69     ((end 1 12 4 4) . ,(ly:make-moment 1 4))
70     ((end 1 12 4 4) . ,(ly:make-moment 3 4))
71
72     ((end 1 16 4 4) . ,(ly:make-moment 1 4))
73     ((end 1 16 4 4) . ,(ly:make-moment 3 4))
74
75     ((end 1 32 4 4) . ,(ly:make-moment 1 8))
76     ((end 1 32 4 4) . ,(ly:make-moment 1 4))
77     ((end 1 32 4 4) . ,(ly:make-moment 3 8))
78     ((end 1 32 4 4) . ,(ly:make-moment 5 8))
79     ((end 1 32 4 4) . ,(ly:make-moment 3 4))
80     ((end 1 32 4 4) . ,(ly:make-moment 7 8))
81
82     ((end * * 2 4) . ,(ly:make-moment 1 4))
83     ((end 1 32 2 4) . ,(ly:make-moment 1 8))
84     ((end 1 32 2 4) . ,(ly:make-moment 3 8))
85
86     ((end * * 4 8) . ,(ly:make-moment 1 4))
87     ((end 1 32 4 8) . ,(ly:make-moment 1 8))
88     ((end 1 32 4 8) . ,(ly:make-moment 3 8))
89
90     ((end * * 4 16) . ,(ly:make-moment 1 8))
91
92     ;; in 6 8, 9 8 and 12 8 time:
93     ;;   use beatGrouping for all except 1 32nd notes
94     ;;   end beams with 32nd notes every 1 8 note
95
96     ((end 1 32 6 8) . ,(ly:make-moment 1 8))
97     ((end 1 32 6 8) . ,(ly:make-moment 2 8))
98     ((end 1 32 6 8) . ,(ly:make-moment 3 8))
99     ((end 1 32 6 8) . ,(ly:make-moment 4 8))
100     ((end 1 32 6 8) . ,(ly:make-moment 5 8))
101
102     ((end 1 32 9 8) . ,(ly:make-moment 1 8))
103     ((end 1 32 9 8) . ,(ly:make-moment 2 8))
104     ((end 1 32 9 8) . ,(ly:make-moment 3 8))
105     ((end 1 32 9 8) . ,(ly:make-moment 4 8))
106     ((end 1 32 9 8) . ,(ly:make-moment 5 8))
107     ((end 1 32 9 8) . ,(ly:make-moment 6 8))
108     ((end 1 32 9 8) . ,(ly:make-moment 7 8))
109     ((end 1 32 9 8) . ,(ly:make-moment 8 8))
110
111     ((end 1 32 12 8) . ,(ly:make-moment 1 8))
112     ((end 1 32 12 8) . ,(ly:make-moment 2 8))
113     ((end 1 32 12 8) . ,(ly:make-moment 3 8))
114     ((end 1 32 12 8) . ,(ly:make-moment 4 8))
115     ((end 1 32 12 8) . ,(ly:make-moment 5 8))
116     ((end 1 32 12 8) . ,(ly:make-moment 6 8))
117     ((end 1 32 12 8) . ,(ly:make-moment 7 8))
118     ((end 1 32 12 8) . ,(ly:make-moment 8 8))
119     ((end 1 32 12 8) . ,(ly:make-moment 9 8))
120     ((end 1 32 12 8) . ,(ly:make-moment 10 8))
121     ((end 1 32 12 8) . ,(ly:make-moment 11 8))
122     ))
123
124 (define (override-property-setting context property setting value)
125   "Like the C++ code that executes \\override, but without type
126 checking. "
127   (ly:context-set-property!
128    context property
129    (cons (cons setting value) (ly:context-property context property))))
130
131 (define (revert-property-setting context property setting)
132   "Like the C++ code that executes \revert, but without type
133 checking. "
134
135   (define (revert-member alist entry new)
136     "Return ALIST, with ENTRY removed.  ALIST is not modified, instead
137 a fresh copy of the list-head is made."
138     (cond
139      ((null? alist) new)
140      ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
141      (else (revert-member (cdr alist) entry (cons (car alist) new)))))
142
143   (ly:context-set-property!
144    context property
145    (revert-member (ly:context-property context property) setting '())))
146
147 (define-public (override-auto-beam-setting setting num den . rest)
148   (ly:export
149    (context-spec-music
150     (make-apply-context (lambda (c)
151                           (override-property-setting
152                            c 'autoBeamSettings
153                            setting (ly:make-moment num den))))
154     (if (and (pair? rest) (symbol? (car rest)))
155         (car rest)
156         'Voice))))
157
158 (define-public (score-override-auto-beam-setting setting num den)
159   (override-auto-beam-setting setting num den 'Score))
160
161 (define-public (revert-auto-beam-setting setting num den . rest)
162   (ly:export
163    (context-spec-music
164     (make-apply-context (lambda (c)
165                           (revert-property-setting
166                            c 'autoBeamSettings
167                            (cons setting (ly:make-moment num den)))))
168     (if (and (pair? rest) (symbol? (car rest)))
169         (car rest)
170         'Voice))))
171
172 ;;  Determine end moment for auto beaming (or begin moment, but mostly
173 ;;  0== anywhere).  In order of decreasing priority:
174 ;;
175 ;;  1. end <type>   *     *
176 ;;  2. end   *      *     *
177 ;;  3. end <type> <num> <den>
178 ;;  4. end   *    <num> <den>
179 ;;  5. if 1-4 not specified, begin anywhere, end at time determined by
180 ;;          beatGrouping and beatLength:
181 ;;     if beatGrouping and beatLength are consistent with measureLength,
182 ;;        use beatGrouping to determine end of beams.
183 ;;     if beatGrouping and beatLength are inconsistent with measureLength,
184 ;;        use beatLength to determine end of beams.
185 ;;
186 ;;  Rationale:
187 ;;
188 ;;  [user override]
189 ;;  1. override for specific duration type
190 ;;  2. generic override
191 ;;
192 ;;  [to be defined in config file]
193 ;;  3. exceptions for specific time signature, for specific duration type
194 ;;  4. exceptions for specific time signature
195 ;;  5. easy catch-all rule for non-specified measure types
196
197
198 (define-public (default-auto-beam-check context dir test)
199   (define (get name default)
200     (let ((value (ly:context-property context name)))
201       (if (not (null? value)) value default)))
202
203   (define (ending-moments group-list start-beat beat-length)
204   (if (null? group-list)
205       '()
206       (let ((new-start (+ start-beat (car group-list))))
207         (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
208               (ending-moments (cdr group-list) new-start beat-length)))))
209
210   (define (make-end-settings time ending-list moment-den)
211     (if (null? ending-list)
212         '()
213         (cons (cons (append '(end * *) time)
214                     (ly:make-moment (car ending-list) moment-den))
215               (make-end-settings time (cdr ending-list) moment-den))))
216   
217   ;; Don't start auto beams on grace notes
218   (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
219                  (= dir START))
220       #f
221       (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
222                    (measure-length (get 'measureLength (ly:make-moment 1 1)))
223                    (measure-pos (get 'measurePosition ZERO-MOMENT))
224              (beat-grouping (get 'beatGrouping '()))
225                    (settings (get 'autoBeamSettings '()))
226                    (function (list (if (= dir START) 'begin 'end)))
227              ;; Calculate implied time signature based on measureLength
228              ;; and beatLength for default value in get
229                    (num-mom (ly:moment-div measure-length beat-length))
230                    (num (inexact->exact
231                                 (round (/ (ly:moment-main-numerator num-mom)
232                                                 (ly:moment-main-denominator num-mom)))))
233                    (den (ly:moment-main-denominator beat-length))
234              (time-signature-fraction 
235                (get 'timeSignatureFraction (cons num den)))
236                    (time (list (car time-signature-fraction)
237                          (cdr time-signature-fraction)))
238                    (type (list (ly:moment-main-numerator test)
239                                            (ly:moment-main-denominator test)))
240                    (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
241                                   measure-pos
242                                   (ly:moment-add measure-length measure-pos)))
243              (grouping-moments (ending-moments beat-grouping 0 beat-length))
244              ;; Calculate implied measure length from beatGrouping
245              ;; and beatLength
246                    (grouping-length (if (null? grouping-moments)
247                                   ZERO-MOMENT
248                                   (list-ref grouping-moments 
249                                             (1- (length grouping-moments)))))
250              (lst (list
251                                 ;; Hmm, should junk user-override feature,
252                                 ;; or split this in user-override and config section?
253                                 (append function type '(* *))
254                                 (append function '(* * * *))
255                                 (append function type time)
256                                 (append function '(* *) time)))
257              (predefined-setting (first-assoc lst settings)))
258          (if (or
259                       ;; always begin or end beams at beginning/ending of measure
260                       (= (ly:moment-main-numerator pos) 0)
261                       (first-member (map (lambda (x) (cons x pos)) lst) settings))
262                    #t
263                    (if (= dir START)
264                              ;; if no entry matches our function + time or type,
265                              ;; start anywhere
266                              (not predefined-setting)
267                              ;; if entry matches our function + time or type, check moment
268                              (if predefined-setting
269                     (equal? measure-pos (cdr predefined-setting))
270                     ;; if measure-length matches grouping-length, use
271                     ;; grouping moments, else use beat-length
272                     (if (equal? measure-length grouping-length)
273                                     (member measure-pos grouping-moments)
274                         (= (ly:moment-main-denominator
275                                              (ly:moment-div pos beat-length)) 1))))))))