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;
72 for (vsize i = 1; i < infos_.size (); i++)
74 Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
77 This is a kludge, for the most common case of 16th, 32nds
78 etc. What should really happen is that \times x/y should
79 locally introduce a voice-specific beat duration. (or
80 perhaps: a list of beat durations for nested tuplets.)
84 dt /= infos_[i].beat_length_;
86 if (dt.den () < min_den)
97 Beaming_pattern::beam_extend_count (Direction d) const
99 if (infos_.size () == 1)
100 return infos_[0].beam_count_drul_[d];
102 Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
103 Beam_rhythmic_element next = boundary (infos_, d, 1);
105 return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
109 Beaming_pattern::de_grace ()
111 for (vsize i = 0; i < infos_.size (); i ++)
113 infos_[i].de_grace ();
118 Beaming_pattern::beamify (Beaming_options const &options)
120 if (infos_.size () <= 1)
123 if (infos_[0].start_moment_.grace_part_)
126 if (infos_[0].start_moment_ < Moment (0))
127 for (vsize i = 0; i < infos_.size (); i++)
128 infos_[i].start_moment_ += options.measure_length_;
130 Moment measure_pos (0);
132 vector<Moment> group_starts;
133 vector<Moment> beat_starts;
135 SCM grouping = options.grouping_;
136 while (measure_pos <= infos_.back ().start_moment_)
139 if (scm_is_pair (grouping))
141 count = scm_to_int (scm_car (grouping));
142 grouping = scm_cdr (grouping);
145 group_starts.push_back (measure_pos);
146 for (int i = 0; i < count; i++)
148 beat_starts.push_back (measure_pos + options.beat_length_ * i);
150 measure_pos += options.beat_length_ * count;
155 for (vsize i = 0; i < infos_.size (); i++)
157 while (j + 1 < group_starts.size ()
158 && group_starts[j+1] <= infos_[i].start_moment_)
161 if (j < group_starts.size ())
162 infos_[i].group_start_ = group_starts[j];
164 infos_[i].beat_length_ = options.beat_length_;
165 while (k + 1 < beat_starts.size ()
166 && beat_starts[k+1] <= infos_[i].start_moment_)
169 if (k < beat_starts.size ())
170 infos_[i].beat_start_ = beat_starts[k];
173 beamify (options.subdivide_beams_);
178 Beaming_pattern::beamify (bool subdivide_beams)
180 if (infos_.size () <= 1)
183 Drul_array<Beaming_pattern> splits;
185 bool at_boundary = false;
186 int m = best_splitpoint_index (&at_boundary);
188 splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
189 infos_.begin () + m);
190 splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
197 splits[d].beamify (subdivide_beams);
199 while (flip (&d) != LEFT);
201 int middle_beams = ((at_boundary && subdivide_beams)
203 : min (splits[RIGHT].beam_extend_count (LEFT),
204 splits[LEFT].beam_extend_count (RIGHT)));
208 if (splits[d].infos_.size () != 1)
209 boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
211 while (flip (&d) != LEFT);
213 infos_ = splits[LEFT].infos_;
214 infos_.insert (infos_.end (),
215 splits[RIGHT].infos_.begin (),
216 splits[RIGHT].infos_.end ());
221 Beaming_pattern::add_stem (Moment m, int b)
223 infos_.push_back (Beam_rhythmic_element (m, b));
226 Beaming_pattern::Beaming_pattern ()
231 Beaming_pattern::beamlet_count (int i, Direction d) const
233 return infos_.at (i).beam_count_drul_[d];
237 Beaming_options::from_context (Context *context)
239 grouping_ = context->get_property ("beatGrouping");
240 subdivide_beams_ = to_boolean (context->get_property ("subdivideBeams"));
241 beat_length_ = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
242 measure_length_ = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
245 Beaming_options::Beaming_options ()
248 subdivide_beams_ = false;