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