2 beaming-info.cc -- implement Beam_rhythmic_element, Beaming_pattern
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "beaming-pattern.hh"
12 Beam_rhythmic_element::Beam_rhythmic_element ()
15 beam_count_drul_[LEFT] = 0;
16 beam_count_drul_[RIGHT] = 0;
20 Beam_rhythmic_element::Beam_rhythmic_element (Moment m, int i)
23 beam_count_drul_[LEFT] = i;
24 beam_count_drul_[RIGHT] = i;
29 Beam_rhythmic_element::de_grace ()
31 if (start_moment_.grace_part_)
33 start_moment_.main_part_ = start_moment_.grace_part_;
34 start_moment_.grace_part_ = 0;
39 count_factor_twos (int x)
42 while (x && x % 2 == 0)
52 Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
55 for (vsize i = 1; i < infos_.size (); i++)
57 if (infos_[i].group_start_ == infos_[i].start_moment_)
61 for (vsize i = 1; i < infos_.size (); i++)
63 if (infos_[i].beat_start_ == infos_[i].start_moment_)
69 int min_den = INT_MAX;
73 for (vsize i = 1; i < infos_.size (); i++)
75 Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
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.)
85 dt /= infos_[i].beat_length_;
87 if (dt.den () < min_den)
98 Beaming_pattern::beam_extend_count (Direction d) const
100 if (infos_.size () == 1)
101 return infos_[0].beam_count_drul_[d];
103 Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
104 Beam_rhythmic_element next = boundary (infos_, d, 1);
106 return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
110 Beaming_pattern::de_grace ()
112 for (vsize i = 0; i < infos_.size (); i ++)
114 infos_[i].de_grace ();
119 Beaming_pattern::beamify (Context *context)
121 if (infos_.size () <= 1)
124 if (infos_[0].start_moment_.grace_part_)
127 bool subdivide_beams = to_boolean (context->get_property ("subdivideBeams"));
128 Moment beat_length = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
129 Moment measure_length = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
131 if (infos_[0].start_moment_ < Moment (0))
132 for (vsize i = 0; i < infos_.size(); i++)
133 infos_[i].start_moment_ += measure_length;
135 SCM grouping = context->get_property ("beatGrouping");
136 Moment measure_pos (0);
138 vector<Moment> group_starts;
139 vector<Moment> beat_starts;
141 while (measure_pos <= infos_.back().start_moment_)
144 if (scm_is_pair (grouping))
146 count = scm_to_int (scm_car (grouping));
147 grouping = scm_cdr (grouping);
150 group_starts.push_back (measure_pos);
151 for (int i = 0; i < count; i++)
153 beat_starts.push_back (measure_pos + beat_length * i);
155 measure_pos += beat_length * count;
160 for (vsize i = 0; i < infos_.size(); i++)
162 while (j + 1 < group_starts.size()
163 && group_starts[j+1] <= infos_[i].start_moment_)
166 if (j < group_starts.size ())
167 infos_[i].group_start_ = group_starts[j];
169 infos_[i].beat_length_ = beat_length;
170 while (k + 1 < beat_starts.size()
171 && beat_starts[k+1] <= infos_[i].start_moment_)
174 if (k < beat_starts.size())
175 infos_[i].beat_start_ = beat_starts[k];
178 beamify (subdivide_beams);
183 Beaming_pattern::beamify (bool subdivide_beams)
185 if (infos_.size () <= 1)
188 Drul_array<Beaming_pattern> splits;
190 bool at_boundary = false;
191 int m = best_splitpoint_index (&at_boundary);
193 splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
194 infos_.begin () + m);
195 splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
202 splits[d].beamify (subdivide_beams);
204 while (flip (&d) != LEFT);
206 int middle_beams = ((at_boundary && subdivide_beams)
208 : min (splits[RIGHT].beam_extend_count (LEFT),
209 splits[LEFT].beam_extend_count (RIGHT)));
213 if (splits[d].infos_.size () != 1)
214 boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
216 while (flip (&d) != LEFT);
218 infos_ = splits[LEFT].infos_;
219 infos_.insert (infos_.end (),
220 splits[RIGHT].infos_.begin (),
221 splits[RIGHT].infos_.end ());
226 Beaming_pattern::add_stem (Moment m, int b)
228 infos_.push_back (Beam_rhythmic_element (m, b));
231 Beaming_pattern::Beaming_pattern ()
236 Beaming_pattern::beamlet_count (int i, Direction d) const
238 return infos_.at (i).beam_count_drul_[d];