]> git.donarmstrong.com Git - lilypond.git/blob - lily/beaming-pattern.cc
(best_splitpoint_index): fix beaming
[lilypond.git] / lily / beaming-pattern.cc
1 /*
2   beaming-info.cc -- implement Beam_rhythmic_element, Beaming_pattern
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "beaming-pattern.hh"
10 #include "context.hh"
11
12 Beam_rhythmic_element::Beam_rhythmic_element ()
13 {
14   start_moment_ = 0;
15   beam_count_drul_[LEFT] = 0;
16   beam_count_drul_[RIGHT] = 0;
17
18 }
19
20 Beam_rhythmic_element::Beam_rhythmic_element (Moment m, int i)
21 {
22   start_moment_ = m;
23   beam_count_drul_[LEFT] = i;
24   beam_count_drul_[RIGHT] = i;
25 }
26
27
28 int
29 count_factor_twos (int x)
30 {
31   int c = 0;
32   while (x && x % 2)
33     {
34       x /= 2;
35       c ++;
36     }
37          
38   return c;
39 }
40
41 int
42 Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
43 {
44   *at_boundary = true;
45   for (vsize i = 1; i < infos_.size (); i++)  
46     {
47       if (infos_[i].group_start_  == infos_[i].start_moment_)
48         return i;
49     }
50
51   for (vsize i = 1; i < infos_.size (); i++)  
52     {
53       if (infos_[i].beat_start_  == infos_[i].start_moment_)
54         return i;
55     }
56
57   *at_boundary = false;
58   
59   int min_factor_twos = INT_MAX;
60   int min_index = -1;
61   
62   Moment beat_pos;
63   for (vsize i = 1; i < infos_.size (); i++)  
64     {
65       Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
66
67       /*
68         This is a kludge, for the most common case of 16th, 32nds
69         etc. What should really happen is that \times x/y should
70         locally introduce a voice-specific beat duration.  (or
71         perhaps: a list of beat durations for nested tuplets.)
72         
73        */
74       
75       int factor_2s =  count_factor_twos (dt.den ());
76       
77       if (factor_2s < min_factor_twos)
78         {
79           min_factor_twos = factor_2s;
80           min_index = i;
81         }
82     }
83
84   return min_index;
85 }
86
87 int
88 Beaming_pattern::beam_extend_count (Direction d) const
89 {
90   if (infos_.size () == 1)
91     return infos_[0].beam_count_drul_[d];
92
93   Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
94   Beam_rhythmic_element next = boundary (infos_, d, 1);
95
96   return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
97 }
98
99 void
100 Beaming_pattern::beamify (Context *context)
101 {
102   if (infos_.size () <= 1)
103     return;
104   
105   bool subdivide_beams = to_boolean (context->get_property ("subdivideBeams"));
106   Moment beat_length = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
107   Moment measure_length = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
108
109   if (infos_[0].start_moment_ < Moment (0))
110     for (vsize i = 0; i < infos_.size(); i++)
111       infos_[i].start_moment_ += measure_length;
112   
113   SCM grouping = context->get_property ("beatGrouping");
114   Moment measure_pos (0);
115   
116   vector<Moment> group_starts;
117   vector<Moment> beat_starts;
118   
119   while (measure_pos <= infos_.back().start_moment_)
120     {
121       int count = 2;
122       if (scm_is_pair (grouping))
123         {
124           count = scm_to_int (scm_car (grouping));
125           grouping = scm_cdr (grouping);
126         }
127
128       group_starts.push_back (measure_pos);
129       for (int i = 0; i < count; i++)
130         {
131           beat_starts.push_back (measure_pos + beat_length * i);
132         }
133       measure_pos += beat_length * count;
134     }
135    
136   vsize j = 0;
137   vsize k = 0;
138   for (vsize i = 0; i  < infos_.size(); i++)
139     {
140       while (j < group_starts.size() - 1
141              && group_starts[j+1] <= infos_[i].start_moment_)
142         j++;
143
144       infos_[i].group_start_ = group_starts[j];
145
146       while (k < beat_starts.size() - 1
147              && beat_starts[k+1] <= infos_[i].start_moment_)
148         k++;
149
150       infos_[i].beat_start_ = beat_starts[k];
151     }
152   
153   beamify (subdivide_beams);
154 }
155
156
157 void
158 Beaming_pattern::beamify (bool subdivide_beams)
159 {
160   if (infos_.size () <= 1)
161     return;
162   
163   Drul_array<Beaming_pattern> splits;
164
165   bool at_boundary = false;
166   int m = best_splitpoint_index (&at_boundary);
167
168   splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
169                                               infos_.begin () + m);
170   splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
171                                                infos_.end ());
172
173   Direction d = LEFT;
174
175   do
176     {
177       splits[d].beamify (subdivide_beams);
178     }
179   while (flip (&d) != LEFT);
180
181   int middle_beams = ((at_boundary && subdivide_beams)
182                       ? 1       
183                       : min (splits[RIGHT].beam_extend_count (LEFT),
184                              splits[LEFT].beam_extend_count (RIGHT)));
185
186   do
187     {
188       if (splits[d].infos_.size () != 1)
189         boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
190     }
191   while (flip (&d) != LEFT);
192
193   infos_ = splits[LEFT].infos_;
194   infos_.insert (infos_.end (),
195                  splits[RIGHT].infos_.begin (),
196                  splits[RIGHT].infos_.end ());
197 }
198
199
200 void
201 Beaming_pattern::add_stem (Moment m, int b)
202 {
203   infos_.push_back (Beam_rhythmic_element (m, b));
204 }
205
206 Beaming_pattern::Beaming_pattern ()
207 {
208 }
209
210 int
211 Beaming_pattern::beamlet_count (int i, Direction d) const
212 {
213   return infos_.at (i).beam_count_drul_[d];
214 }