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 (Beaming_options const &options)
121 if (infos_.size () <= 1)
124 if (infos_[0].start_moment_.grace_part_)
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_;
131 Moment measure_pos (0);
133 vector<Moment> group_starts;
134 vector<Moment> beat_starts;
136 SCM grouping = options.grouping_;
137 while (measure_pos <= infos_.back ().start_moment_)
140 if (scm_is_pair (grouping))
142 count = scm_to_int (scm_car (grouping));
143 grouping = scm_cdr (grouping);
146 group_starts.push_back (measure_pos);
147 for (int i = 0; i < count; i++)
149 beat_starts.push_back (measure_pos + options.beat_length_ * i);
151 measure_pos += options.beat_length_ * count;
156 for (vsize i = 0; i < infos_.size (); i++)
158 while (j + 1 < group_starts.size ()
159 && group_starts[j+1] <= infos_[i].start_moment_)
162 if (j < group_starts.size ())
163 infos_[i].group_start_ = group_starts[j];
165 infos_[i].beat_length_ = options.beat_length_;
166 while (k + 1 < beat_starts.size ()
167 && beat_starts[k+1] <= infos_[i].start_moment_)
170 if (k < beat_starts.size ())
171 infos_[i].beat_start_ = beat_starts[k];
174 beamify (options.subdivide_beams_);
179 Beaming_pattern::beamify (bool subdivide_beams)
181 if (infos_.size () <= 1)
184 Drul_array<Beaming_pattern> splits;
186 bool at_boundary = false;
187 int m = best_splitpoint_index (&at_boundary);
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,
198 splits[d].beamify (subdivide_beams);
200 while (flip (&d) != LEFT);
202 int middle_beams = ((at_boundary && subdivide_beams)
204 : min (splits[RIGHT].beam_extend_count (LEFT),
205 splits[LEFT].beam_extend_count (RIGHT)));
209 if (splits[d].infos_.size () != 1)
210 boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
212 while (flip (&d) != LEFT);
214 infos_ = splits[LEFT].infos_;
215 infos_.insert (infos_.end (),
216 splits[RIGHT].infos_.begin (),
217 splits[RIGHT].infos_.end ());
222 Beaming_pattern::add_stem (Moment m, int b)
224 infos_.push_back (Beam_rhythmic_element (m, b));
227 Beaming_pattern::Beaming_pattern ()
232 Beaming_pattern::beamlet_count (int i, Direction d) const
234 return infos_.at (i).beam_count_drul_[d];
238 Beaming_options::from_context (Context *context)
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));
246 Beaming_options::Beaming_options ()
249 subdivide_beams_ = false;