]> git.donarmstrong.com Git - lilypond.git/blob - lily/beaming-pattern.cc
codingstyle nits: space before ( and after ,
[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--2007 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 void
29 Beam_rhythmic_element::de_grace ()
30 {
31   if (start_moment_.grace_part_)
32     {
33       start_moment_.main_part_ = start_moment_.grace_part_; 
34       start_moment_.grace_part_ = 0;
35     }
36 }
37
38 int
39 count_factor_twos (int x)
40 {
41   int c = 0;
42   while (x && x % 2 == 0)
43     {
44       x /= 2;
45       c ++;
46     }
47          
48   return c;
49 }
50
51 int
52 Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
53 {
54   *at_boundary = true;
55   for (vsize i = 1; i < infos_.size (); i++)  
56     {
57       if (infos_[i].group_start_  == infos_[i].start_moment_)
58         return i;
59     }
60
61   for (vsize i = 1; i < infos_.size (); i++)  
62     {
63       if (infos_[i].beat_start_  == infos_[i].start_moment_)
64         return i;
65     }
66
67   *at_boundary = false;
68   
69   int min_den = INT_MAX;
70   int min_index = -1;
71   
72   Moment beat_pos;
73   for (vsize i = 1; i < infos_.size (); i++)  
74     {
75       Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
76
77       /*
78         This is a kludge, for the most common case of 16th, 32nds
79         etc. What should really happen is that \times x/y should
80         locally introduce a voice-specific beat duration.  (or
81         perhaps: a list of beat durations for nested tuplets.)
82         
83        */
84       
85       dt /= infos_[i].beat_length_;
86       
87       if (dt.den () < min_den)
88         {
89           min_den = dt.den ();
90           min_index = i;
91         }
92     }
93
94   return min_index;
95 }
96
97 int
98 Beaming_pattern::beam_extend_count (Direction d) const
99 {
100   if (infos_.size () == 1)
101     return infos_[0].beam_count_drul_[d];
102
103   Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
104   Beam_rhythmic_element next = boundary (infos_, d, 1);
105
106   return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
107 }
108
109 void
110 Beaming_pattern::de_grace ()
111 {
112   for (vsize i = 0; i < infos_.size (); i ++)
113     {
114       infos_[i].de_grace ();
115     }
116 }
117
118 void
119 Beaming_pattern::beamify (Beaming_options const &options)
120 {
121   if (infos_.size () <= 1)
122     return;
123
124   if (infos_[0].start_moment_.grace_part_)
125     de_grace ();
126
127   if (infos_[0].start_moment_ < Moment (0))
128     for (vsize i = 0; i < infos_.size (); i++)
129       infos_[i].start_moment_ += options.measure_length_;
130   
131   Moment measure_pos (0);
132   
133   vector<Moment> group_starts;
134   vector<Moment> beat_starts;
135
136   SCM grouping = options.grouping_;
137   while (measure_pos <= infos_.back ().start_moment_)
138     {
139       int count = 2;
140       if (scm_is_pair (grouping))
141         {
142           count = scm_to_int (scm_car (grouping));
143           grouping = scm_cdr (grouping);
144         }
145
146       group_starts.push_back (measure_pos);
147       for (int i = 0; i < count; i++)
148         {
149           beat_starts.push_back (measure_pos + options.beat_length_ * i);
150         }
151       measure_pos += options.beat_length_ * count;
152     }
153    
154   vsize j = 0;
155   vsize k = 0;
156   for (vsize i = 0; i  < infos_.size (); i++)
157     {
158       while (j + 1 < group_starts.size ()
159              && group_starts[j+1] <= infos_[i].start_moment_)
160         j++;
161
162       if (j < group_starts.size ())
163         infos_[i].group_start_ = group_starts[j];
164       
165       infos_[i].beat_length_ = options.beat_length_;  
166       while (k + 1 < beat_starts.size () 
167              && beat_starts[k+1] <= infos_[i].start_moment_)
168         k++;
169
170       if (k < beat_starts.size ())
171         infos_[i].beat_start_ = beat_starts[k];
172     }
173   
174   beamify (options.subdivide_beams_);
175 }
176
177
178 void
179 Beaming_pattern::beamify (bool subdivide_beams)
180 {
181   if (infos_.size () <= 1)
182     return;
183   
184   Drul_array<Beaming_pattern> splits;
185
186   bool at_boundary = false;
187   int m = best_splitpoint_index (&at_boundary);
188
189   splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
190                                               infos_.begin () + m);
191   splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
192                                                infos_.end ());
193
194   Direction d = LEFT;
195
196   do
197     {
198       splits[d].beamify (subdivide_beams);
199     }
200   while (flip (&d) != LEFT);
201
202   int middle_beams = ((at_boundary && subdivide_beams)
203                       ? 1       
204                       : min (splits[RIGHT].beam_extend_count (LEFT),
205                              splits[LEFT].beam_extend_count (RIGHT)));
206
207   do
208     {
209       if (splits[d].infos_.size () != 1)
210         boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
211     }
212   while (flip (&d) != LEFT);
213
214   infos_ = splits[LEFT].infos_;
215   infos_.insert (infos_.end (),
216                  splits[RIGHT].infos_.begin (),
217                  splits[RIGHT].infos_.end ());
218 }
219
220
221 void
222 Beaming_pattern::add_stem (Moment m, int b)
223 {
224   infos_.push_back (Beam_rhythmic_element (m, b));
225 }
226
227 Beaming_pattern::Beaming_pattern ()
228 {
229 }
230
231 int
232 Beaming_pattern::beamlet_count (int i, Direction d) const
233 {
234   return infos_.at (i).beam_count_drul_[d];
235 }
236
237 void
238 Beaming_options::from_context (Context *context)
239 {
240   grouping_ = context->get_property ("beatGrouping");
241   subdivide_beams_ = to_boolean (context->get_property ("subdivideBeams"));
242   beat_length_ = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
243   measure_length_ = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
244 }
245
246 Beaming_options::Beaming_options ()
247 {
248   grouping_ = SCM_EOL;
249   subdivide_beams_ = false;
250 }