]> git.donarmstrong.com Git - lilypond.git/blob - scm/auto-beam.scm
Remove beam ending rules in 6/8, 9/8 and 12/8 time
[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--2007 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
110     ((end 1 32 12 8) . ,(ly:make-moment 1 8))
111     ((end 1 32 12 8) . ,(ly:make-moment 2 8))
112     ((end 1 32 12 8) . ,(ly:make-moment 3 8))
113     ((end 1 32 12 8) . ,(ly:make-moment 4 8))
114     ((end 1 32 12 8) . ,(ly:make-moment 5 8))
115     ((end 1 32 12 8) . ,(ly:make-moment 6 8))
116     ((end 1 32 12 8) . ,(ly:make-moment 7 8))
117     ((end 1 32 12 8) . ,(ly:make-moment 9 8))
118     ((end 1 32 12 8) . ,(ly:make-moment 10 8))
119     ((end 1 32 12 8) . ,(ly:make-moment 11 8))
120     ))
121
122 (define (override-property-setting context property setting value)
123   "Like the C++ code that executes \\override, but without type
124 checking. "
125   (ly:context-set-property!
126    context property
127    (cons (cons setting value) (ly:context-property context property))))
128
129 (define (revert-property-setting context property setting)
130   "Like the C++ code that executes \revert, but without type
131 checking. "
132
133   (define (revert-member alist entry new)
134     "Return ALIST, with ENTRY removed.  ALIST is not modified, instead
135 a fresh copy of the list-head is made."
136     (cond
137      ((null? alist) new)
138      ((equal? (car alist) entry) (revert-member (cdr alist) entry new))
139      (else (revert-member (cdr alist) entry (cons (car alist) new)))))
140
141   (ly:context-set-property!
142    context property
143    (revert-member (ly:context-property context property) setting '())))
144
145 (define-public (override-auto-beam-setting setting num den . rest)
146   (ly:export
147    (context-spec-music
148     (make-apply-context (lambda (c)
149                           (override-property-setting
150                            c 'autoBeamSettings
151                            setting (ly:make-moment num den))))
152     (if (and (pair? rest) (symbol? (car rest)))
153         (car rest)
154         'Voice))))
155
156 (define-public (score-override-auto-beam-setting setting num den)
157   (override-auto-beam-setting setting num den 'Score))
158
159 (define-public (revert-auto-beam-setting setting num den . rest)
160   (ly:export
161    (context-spec-music
162     (make-apply-context (lambda (c)
163                           (revert-property-setting
164                            c 'autoBeamSettings
165                            (cons setting (ly:make-moment num den)))))
166     (if (and (pair? rest) (symbol? (car rest)))
167         (car rest)
168         'Voice))))
169
170 ;;  Determine end moment for auto beaming (or begin moment, but mostly
171 ;;  0== anywhere).  In order of decreasing priority:
172 ;;
173 ;;  1. end <type>   *     *
174 ;;  2. end   *      *     *
175 ;;  3. end <type> <num> <den>
176 ;;  4. end   *    <num> <den>
177 ;;  5. if 1-4 not specified, begin anywhere, end at time determined by
178 ;;          beatGrouping and beatLength:
179 ;;     if beatGrouping and beatLength are consistent with measureLength,
180 ;;        use beatGrouping to determine end of beams.
181 ;;     if beatGrouping and beatLength are inconsistent with measureLength,
182 ;;        use beatLength to determine end of beams.
183 ;;
184 ;;  Rationale:
185 ;;
186 ;;  [user override]
187 ;;  1. override for specific duration type
188 ;;  2. generic override
189 ;;
190 ;;  [to be defined in config file]
191 ;;  3. exceptions for specific time signature, for specific duration type
192 ;;  4. exceptions for specific time signature
193 ;;  5. easy catch-all rule for non-specified measure types
194
195
196 (define-public (default-auto-beam-check context dir test)
197   (define (get name default)
198     (let ((value (ly:context-property context name)))
199       (if (not (null? value)) value default)))
200
201   (define (ending-moments group-list start-beat beat-length)
202   (if (null? group-list)
203       '()
204       (let ((new-start (+ start-beat (car group-list))))
205         (cons (ly:moment-mul (ly:make-moment new-start 1) beat-length)
206               (ending-moments (cdr group-list) new-start beat-length)))))
207
208   (define (make-end-settings time ending-list moment-den)
209     (if (null? ending-list)
210         '()
211         (cons (cons (append '(end * *) time)
212                     (ly:make-moment (car ending-list) moment-den))
213               (make-end-settings time (cdr ending-list) moment-den))))
214   
215   ;; Don't start auto beams on grace notes
216   (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
217            (= dir START))
218       #f
219       (let* ((beat-length (get 'beatLength (ly:make-moment 1 4)))
220              (measure-length (get 'measureLength (ly:make-moment 1 1)))
221              (measure-pos (get 'measurePosition ZERO-MOMENT))
222              (beat-grouping (get 'beatGrouping '()))
223              (settings (get 'autoBeamSettings '()))
224              (function (list (if (= dir START) 'begin 'end)))
225              ;; Calculate implied time signature based on measureLength
226              ;; and beatLength for default value in get
227              (num-mom (ly:moment-div measure-length beat-length))
228              (num (inexact->exact
229                    (round (/ (ly:moment-main-numerator num-mom)
230                              (ly:moment-main-denominator num-mom)))))
231              (den (ly:moment-main-denominator beat-length))
232              (time-signature-fraction 
233                (get 'timeSignatureFraction (cons num den)))
234              (time (list (car time-signature-fraction)
235                          (cdr time-signature-fraction)))
236              (type (list (ly:moment-main-numerator test)
237                          (ly:moment-main-denominator test)))
238              (pos (if (>= (ly:moment-main-numerator measure-pos) 0)
239                       measure-pos
240                       (ly:moment-add measure-length measure-pos)))
241              (grouping-moments (ending-moments beat-grouping 0 beat-length))
242              ;; Calculate implied measure length from beatGrouping
243              ;; and beatLength
244              (grouping-length (if (null? grouping-moments)
245                                   ZERO-MOMENT
246                                   (list-ref grouping-moments 
247                                             (1- (length grouping-moments)))))
248              (lst (list
249                    ;; Hmm, should junk user-override feature,
250                    ;; or split this in user-override and config section?
251                    (append function type '(* *))
252                    (append function '(* * * *))
253                    (append function type time)
254                    (append function '(* *) time)))
255              (predefined-setting (first-assoc lst settings)))
256          (if (or
257              ;; always begin or end beams at beginning/ending of measure
258              (= (ly:moment-main-numerator pos) 0)
259              (first-member (map (lambda (x) (cons x pos)) lst) settings))
260             #t
261             (if (= dir START)
262                 ;; if no entry matches our function + time or type,
263                 ;; start anywhere
264                 (not predefined-setting)
265                 ;; if entry matches our function + time or type, check moment
266                 (if predefined-setting
267                     (equal? measure-pos (cdr predefined-setting))
268                     ;; if measure-length matches grouping-length, use
269                     ;; grouping moments, else use beat-length
270                     (if (equal? measure-length grouping-length)
271                         (member measure-pos grouping-moments)
272                         (= (ly:moment-main-denominator
273                              (ly:moment-div pos beat-length)) 1))))))))