]> git.donarmstrong.com Git - lilypond.git/blob - scm/auto-beam.scm
Merge branch 'fixedmerge' into HEAD
[lilypond.git] / scm / auto-beam.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2012 Jan Nieuwenhuizen <janneke@gnu.org>
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 ;;  Determine whether an auto beam should be extended to the right
19 ;;  of the current stem.   In general, we start anywhere except on
20 ;;  the last note of a beat. We end according to the follwing rules,
21 ;;  in order of decreasing priority:
22 ;;
23 ;;  1. end <type>
24 ;;  2. end <greater type>
25 ;;  3. if 1-2 not specified,  end at beatStructure intervals
26 ;;
27 ;;  Rationale:
28 ;;
29 ;;  [user override]
30 ;;  1. override for specific duration type
31 ;;  2. overrides apply to shorter durations
32 ;;
33 ;;  defined in scm/time-signature-settings.scm:
34 ;;  1. Default grouping for common time signatures
35
36 (define-public (default-auto-beam-check context dir measure-pos test-beam)
37   (define (get name default)
38     (let ((value (ly:context-property context name)))
39       (if (not (null? value)) value default)))
40
41   (define (beaming<? a b)
42     (ly:moment<? (fraction->moment (car a))
43                  (fraction->moment (car b))))
44
45   (define (ending-moments group-list start-beat base-moment)
46     (if (null? group-list)
47         '()
48         (let ((new-start (+ start-beat (car group-list))))
49           (cons (ly:moment-mul (ly:make-moment new-start 1) base-moment)
50                 (ending-moments (cdr group-list) new-start base-moment)))))
51
52   (define (larger-setting test-beam sorted-alist)
53    (if (null? sorted-alist)
54        '()
55        (let* ((first-key (caar sorted-alist))
56               (first-moment (fraction->moment first-key)))
57          (if (moment<=? test-beam first-moment)
58              (car sorted-alist)
59              (larger-setting test-beam (cdr sorted-alist))))))
60
61   (define (beat-end? moment beat-structure)
62     (pair? (member moment beat-structure)))  ;; member returns a list if found, not #t
63
64   (define (use-special-3-4-rules? fraction base-moment exceptions)
65     "Should we use special 3/4 time signature beaming rules?"
66      (and (equal? fraction '(3 . 4))
67           (equal? base-moment (ly:make-moment 1 4))
68           (null? (assoc-get '(1 . 8) exceptions '()))))
69
70   ;; Start of actual auto-beam test routine
71   ;;
72   ;;
73   ;; Don't start auto beams on grace notes
74   (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
75            (= dir START))
76       #f
77       (let* ((base-moment (get 'baseMoment (ly:make-moment 1 4)))
78              (measure-length (get 'measureLength (ly:make-moment 1 1)))
79              (time-signature-fraction
80                (get 'timeSignatureFraction '(4 . 4)))
81              (beat-structure (get 'beatStructure '(1 1 1 1)))
82              (beat-endings (ending-moments beat-structure 0 base-moment))
83              (exceptions (sort (assoc-get 'end
84                                           (get 'beamExceptions '())
85                                           '())
86                                beaming<?))
87              (function (if (= dir START) 'begin 'end))
88              (beam-whole-measure (get 'beamWholeMeasure #t))
89              (beam-half-measure (get 'beamHalfMeasure #f))
90              (type (moment->fraction test-beam))
91              (non-grace (ly:make-moment
92                           (ly:moment-main-numerator measure-pos)
93                           (ly:moment-main-denominator measure-pos)))
94              (pos (if (ly:moment<? non-grace ZERO-MOMENT)
95                       (ly:moment-add measure-length non-grace)
96                       non-grace))
97              (type-grouping (assoc-get type exceptions '()))
98              (default-rule (if (null? type-grouping)
99                                (larger-setting test-beam exceptions)
100                                '()))
101              (default-grouping (if (pair? default-rule)
102                                    (cdr default-rule)
103                                    '()))
104              (default-beat-length (if (pair? default-rule)
105                                       (car default-rule)
106                                       '()))
107              (exception-grouping (if (null? type-grouping)
108                                 default-grouping
109                                 type-grouping))
110              (grouping-moment (if (null? type-grouping)
111                                   (fraction->moment default-beat-length)
112                                   test-beam))
113              (exception-moments (ending-moments
114                                   exception-grouping 0 grouping-moment)))
115
116         (if (= dir START)
117             ;; Start rules -- start anywhere unless 3/4 with default rules
118             ;; #t if beam is to start
119             (or (not (use-special-3-4-rules?
120                        time-signature-fraction
121                        base-moment
122                        exceptions)) ;; start anywhere if not default 3/4
123                 (= (ly:moment-main-numerator pos) 0) ;; start at beginning of measure
124                 (and beam-half-measure
125                      (equal? type '(1 . 8))
126                      (equal? pos (ly:make-moment 3 8))) ;; start at mid-measure if 1/8 note beam
127                 (beat-end? pos beat-endings)  ;; start if at start of beat
128                 (and (not (equal? test-beam base-moment)) ;; is beat split?
129                      (not (beat-end? (ly:moment-add pos test-beam)
130                                      beat-endings))))  ;; will this note end the beat
131             ;; End rules -- #t if beam is to end
132             (or (= (ly:moment-main-numerator pos) 0) ;; end at measure beginning
133                 (if (use-special-3-4-rules?
134                       time-signature-fraction
135                       base-moment
136                       exceptions)
137                     ;; special rule for default 3/4 beaming
138                     (if (and (equal? type '(1 . 8))
139                              (or beam-whole-measure
140                                  (and beam-half-measure
141                                       (not (equal? pos (ly:make-moment 3 8))))))
142                         #f
143                         (beat-end? pos beat-endings))
144                     ;; rules for all other cases -- check for applicable exception
145                     (if (null? exception-grouping)
146                         (beat-end? pos beat-endings) ;; no exception, so check beat ending
147                         (member pos exception-moments)))))))) ;; check exception rule
148