2 beaming-info.cc -- implement Beam_rhythmic_element, Beaming_pattern
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2006 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 count_factor_twos (int x)
32 while (x && x % 2 == 0)
42 Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
45 for (vsize i = 1; i < infos_.size (); i++)
47 if (infos_[i].group_start_ == infos_[i].start_moment_)
51 for (vsize i = 1; i < infos_.size (); i++)
53 if (infos_[i].beat_start_ == infos_[i].start_moment_)
59 int min_den = INT_MAX;
63 for (vsize i = 1; i < infos_.size (); i++)
65 Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
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.)
75 dt /= infos_[i].beat_length_;
77 if (dt.den () < min_den)
88 Beaming_pattern::beam_extend_count (Direction d) const
90 if (infos_.size () == 1)
91 return infos_[0].beam_count_drul_[d];
93 Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
94 Beam_rhythmic_element next = boundary (infos_, d, 1);
96 return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
100 Beaming_pattern::beamify (Context *context)
102 if (infos_.size () <= 1)
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));
109 if (infos_[0].start_moment_ < Moment (0))
110 for (vsize i = 0; i < infos_.size(); i++)
111 infos_[i].start_moment_ += measure_length;
113 SCM grouping = context->get_property ("beatGrouping");
114 Moment measure_pos (0);
116 vector<Moment> group_starts;
117 vector<Moment> beat_starts;
119 while (measure_pos <= infos_.back().start_moment_)
122 if (scm_is_pair (grouping))
124 count = scm_to_int (scm_car (grouping));
125 grouping = scm_cdr (grouping);
128 group_starts.push_back (measure_pos);
129 for (int i = 0; i < count; i++)
131 beat_starts.push_back (measure_pos + beat_length * i);
133 measure_pos += beat_length * count;
138 for (vsize i = 0; i < infos_.size(); i++)
140 while (j < group_starts.size() - 1
141 && group_starts[j+1] <= infos_[i].start_moment_)
144 infos_[i].group_start_ = group_starts[j];
145 infos_[i].beat_length_ = beat_length;
146 while (k < beat_starts.size() - 1
147 && beat_starts[k+1] <= infos_[i].start_moment_)
150 infos_[i].beat_start_ = beat_starts[k];
153 beamify (subdivide_beams);
158 Beaming_pattern::beamify (bool subdivide_beams)
160 if (infos_.size () <= 1)
163 Drul_array<Beaming_pattern> splits;
165 bool at_boundary = false;
166 int m = best_splitpoint_index (&at_boundary);
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,
177 splits[d].beamify (subdivide_beams);
179 while (flip (&d) != LEFT);
181 int middle_beams = ((at_boundary && subdivide_beams)
183 : min (splits[RIGHT].beam_extend_count (LEFT),
184 splits[LEFT].beam_extend_count (RIGHT)));
188 if (splits[d].infos_.size () != 1)
189 boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
191 while (flip (&d) != LEFT);
193 infos_ = splits[LEFT].infos_;
194 infos_.insert (infos_.end (),
195 splits[RIGHT].infos_.begin (),
196 splits[RIGHT].infos_.end ());
201 Beaming_pattern::add_stem (Moment m, int b)
203 infos_.push_back (Beam_rhythmic_element (m, b));
206 Beaming_pattern::Beaming_pattern ()
211 Beaming_pattern::beamlet_count (int i, Direction d) const
213 return infos_.at (i).beam_count_drul_[d];