]> git.donarmstrong.com Git - lilypond.git/blob - scm/auto-beam.scm
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / scm / auto-beam.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2010 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.  We start anywhere except on the last note
20 ;;  of a beat.  We end according to the follwing rules, in order
21 ;;  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   ;; Start of actual auto-beam test routine
65   ;;
66   ;;
67   ;; Don't start auto beams on grace notes
68   (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
69            (= dir START))
70       #f
71       (let* ((base-moment (get 'baseMoment (ly:make-moment 1 4)))
72              (measure-length (get 'measureLength (ly:make-moment 1 1)))
73              (time-signature-fraction
74                (get 'timeSignatureFraction '(4 . 4)))
75              (beat-structure (get 'beatStructure '(1 1 1 1)))
76              (beat-endings (ending-moments beat-structure 0 base-moment))
77              (exceptions (sort (assoc-get 'end
78                                           (get 'beamExceptions '())
79                                           '())
80                                beaming<?))
81              (function (if (= dir START) 'begin 'end))
82              (type (moment->fraction test-beam))
83              (non-grace (ly:make-moment
84                           (ly:moment-main-numerator measure-pos)
85                           (ly:moment-main-denominator measure-pos)))
86              (pos (if (ly:moment<? non-grace ZERO-MOMENT)
87                       (ly:moment-add measure-length non-grace)
88                       non-grace))
89              (type-grouping (assoc-get type exceptions '()))
90              (default-rule (if (null? type-grouping)
91                                (larger-setting test-beam exceptions)
92                                '()))
93              (default-grouping (if (pair? default-rule)
94                                    (cdr default-rule)
95                                    '()))
96              (default-beat-length (if (pair? default-rule)
97                                       (car default-rule)
98                                       '()))
99              (exception-grouping (if (null? type-grouping)
100                                 default-grouping
101                                 type-grouping))
102              (grouping-moment (if (null? type-grouping)
103                                   (fraction->moment default-beat-length)
104                                   test-beam))
105              (exception-moments (ending-moments
106                                   exception-grouping 0 grouping-moment)))
107
108         (if (= dir START)
109             ;; Start rules
110             (or (not (equal? time-signature-fraction '(3 . 4))) ;; start anywher if not 3/4
111                 (beat-end? pos beat-endings)  ;; are we at start of beat?
112                 (and (not (equal? test-beam base-moment)) ;; is beat split?
113                      (not (beat-end? (ly:moment-add pos test-beam)
114                                      beat-endings))))  ;; will this note end the beat
115             ;; End rules
116             (or (= (ly:moment-main-numerator pos) 0) ;; end at measure beginning
117                 (if (null? exception-grouping)
118                     (beat-end? pos beat-endings) ;; no exception, so check beat ending
119                     (member pos exception-moments))))))) ;; check exception rule
120