1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2000--2011 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
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:
24 ;; 2. end <greater type>
25 ;; 3. if 1-2 not specified, end at beatStructure intervals
30 ;; 1. override for specific duration type
31 ;; 2. overrides apply to shorter durations
33 ;; defined in scm/time-signature-settings.scm:
34 ;; 1. Default grouping for common time signatures
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)))
41 (define (beaming<? a b)
42 (ly:moment<? (fraction->moment (car a))
43 (fraction->moment (car b))))
45 (define (ending-moments group-list start-beat base-moment)
46 (if (null? group-list)
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)))))
52 (define (larger-setting test-beam sorted-alist)
53 (if (null? sorted-alist)
55 (let* ((first-key (caar sorted-alist))
56 (first-moment (fraction->moment first-key)))
57 (if (moment<=? test-beam first-moment)
59 (larger-setting test-beam (cdr sorted-alist))))))
61 (define (beat-end? moment beat-structure)
62 (pair? (member moment beat-structure))) ;; member returns a list if found, not #t
64 ;; Start of actual auto-beam test routine
67 ;; Don't start auto beams on grace notes
68 (if (and (!= (ly:moment-grace-numerator (ly:context-now context)) 0)
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 '())
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)
89 (type-grouping (assoc-get type exceptions '()))
90 (default-rule (if (null? type-grouping)
91 (larger-setting test-beam exceptions)
93 (default-grouping (if (pair? default-rule)
96 (default-beat-length (if (pair? default-rule)
99 (exception-grouping (if (null? type-grouping)
102 (grouping-moment (if (null? type-grouping)
103 (fraction->moment default-beat-length)
105 (exception-moments (ending-moments
106 exception-grouping 0 grouping-moment)))
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
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