]> git.donarmstrong.com Git - lilypond.git/blob - lily/beaming-pattern.cc
Split WWW target in two stages WWW-1 and WWW-2
[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   for (vsize i = 1; i < infos_.size (); i++)  
73     {
74       Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
75
76       /*
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.)
81         
82        */
83       
84       dt /= infos_[i].beat_length_;
85       
86       if (dt.den () < min_den)
87         {
88           min_den = dt.den ();
89           min_index = i;
90         }
91     }
92
93   return min_index;
94 }
95
96 int
97 Beaming_pattern::beam_extend_count (Direction d) const
98 {
99   if (infos_.size () == 1)
100     return infos_[0].beam_count_drul_[d];
101
102   Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
103   Beam_rhythmic_element next = boundary (infos_, d, 1);
104
105   return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
106 }
107
108 void
109 Beaming_pattern::de_grace ()
110 {
111   for (vsize i = 0; i < infos_.size (); i ++)
112     {
113       infos_[i].de_grace ();
114     }
115 }
116
117 void
118 Beaming_pattern::beamify (Beaming_options const &options)
119 {
120   if (infos_.size () <= 1)
121     return;
122
123   if (infos_[0].start_moment_.grace_part_)
124     de_grace ();
125
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_;
129   
130   Moment measure_pos (0);
131   
132   vector<Moment> group_starts;
133   vector<Moment> beat_starts;
134
135   SCM grouping = options.grouping_;
136   while (measure_pos <= infos_.back ().start_moment_)
137     {
138       int count = 2;
139       if (scm_is_pair (grouping))
140         {
141           count = scm_to_int (scm_car (grouping));
142           grouping = scm_cdr (grouping);
143         }
144
145       group_starts.push_back (measure_pos);
146       for (int i = 0; i < count; i++)
147         {
148           beat_starts.push_back (measure_pos + options.beat_length_ * i);
149         }
150       measure_pos += options.beat_length_ * count;
151     }
152    
153   vsize j = 0;
154   vsize k = 0;
155   for (vsize i = 0; i  < infos_.size (); i++)
156     {
157       while (j + 1 < group_starts.size ()
158              && group_starts[j+1] <= infos_[i].start_moment_)
159         j++;
160
161       if (j < group_starts.size ())
162         infos_[i].group_start_ = group_starts[j];
163       
164       infos_[i].beat_length_ = options.beat_length_;  
165       while (k + 1 < beat_starts.size () 
166              && beat_starts[k+1] <= infos_[i].start_moment_)
167         k++;
168
169       if (k < beat_starts.size ())
170         infos_[i].beat_start_ = beat_starts[k];
171     }
172   
173   beamify (options.subdivide_beams_);
174 }
175
176
177 void
178 Beaming_pattern::beamify (bool subdivide_beams)
179 {
180   if (infos_.size () <= 1)
181     return;
182   
183   Drul_array<Beaming_pattern> splits;
184
185   bool at_boundary = false;
186   int m = best_splitpoint_index (&at_boundary);
187
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,
191                                                infos_.end ());
192
193   Direction d = LEFT;
194
195   do
196     {
197       splits[d].beamify (subdivide_beams);
198     }
199   while (flip (&d) != LEFT);
200
201   int middle_beams = ((at_boundary && subdivide_beams)
202                       ? 1       
203                       : min (splits[RIGHT].beam_extend_count (LEFT),
204                              splits[LEFT].beam_extend_count (RIGHT)));
205
206   do
207     {
208       if (splits[d].infos_.size () != 1)
209         boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
210     }
211   while (flip (&d) != LEFT);
212
213   infos_ = splits[LEFT].infos_;
214   infos_.insert (infos_.end (),
215                  splits[RIGHT].infos_.begin (),
216                  splits[RIGHT].infos_.end ());
217 }
218
219
220 void
221 Beaming_pattern::add_stem (Moment m, int b)
222 {
223   infos_.push_back (Beam_rhythmic_element (m, b));
224 }
225
226 Beaming_pattern::Beaming_pattern ()
227 {
228 }
229
230 int
231 Beaming_pattern::beamlet_count (int i, Direction d) const
232 {
233   return infos_.at (i).beam_count_drul_[d];
234 }
235
236 void
237 Beaming_options::from_context (Context *context)
238 {
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));
243 }
244
245 Beaming_options::Beaming_options ()
246 {
247   grouping_ = SCM_EOL;
248   subdivide_beams_ = false;
249 }