]> git.donarmstrong.com Git - lilypond.git/blob - lily/beaming-pattern.cc
8966657e4813f37b1e55343cf9efcb64ef0fd29f
[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--2006 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_ = 
34         start_moment_.grace_part_; 
35       start_moment_.grace_part_ = 0;
36     }
37 }
38
39 int
40 count_factor_twos (int x)
41 {
42   int c = 0;
43   while (x && x % 2 == 0)
44     {
45       x /= 2;
46       c ++;
47     }
48          
49   return c;
50 }
51
52 int
53 Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
54 {
55   *at_boundary = true;
56   for (vsize i = 1; i < infos_.size (); i++)  
57     {
58       if (infos_[i].group_start_  == infos_[i].start_moment_)
59         return i;
60     }
61
62   for (vsize i = 1; i < infos_.size (); i++)  
63     {
64       if (infos_[i].beat_start_  == infos_[i].start_moment_)
65         return i;
66     }
67
68   *at_boundary = false;
69   
70   int min_den = INT_MAX;
71   int min_index = -1;
72   
73   Moment beat_pos;
74   for (vsize i = 1; i < infos_.size (); i++)  
75     {
76       Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
77
78       /*
79         This is a kludge, for the most common case of 16th, 32nds
80         etc. What should really happen is that \times x/y should
81         locally introduce a voice-specific beat duration.  (or
82         perhaps: a list of beat durations for nested tuplets.)
83         
84        */
85       
86       dt /= infos_[i].beat_length_;
87       
88       if (dt.den () < min_den)
89         {
90           min_den = dt.den ();
91           min_index = i;
92         }
93     }
94
95   return min_index;
96 }
97
98 int
99 Beaming_pattern::beam_extend_count (Direction d) const
100 {
101   if (infos_.size () == 1)
102     return infos_[0].beam_count_drul_[d];
103
104   Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
105   Beam_rhythmic_element next = boundary (infos_, d, 1);
106
107   return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
108 }
109
110 void
111 Beaming_pattern::de_grace ()
112 {
113   for (vsize i = 0; i < infos_.size (); i ++)
114     {
115       infos_[i].de_grace ();
116     }
117 }
118
119 void
120 Beaming_pattern::beamify (Context *context)
121 {
122   if (infos_.size () <= 1)
123     return;
124
125   if (infos_[0].start_moment_.grace_part_)
126     de_grace ();
127   
128   bool subdivide_beams = to_boolean (context->get_property ("subdivideBeams"));
129   Moment beat_length = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
130   Moment measure_length = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
131
132   if (infos_[0].start_moment_ < Moment (0))
133     for (vsize i = 0; i < infos_.size(); i++)
134       infos_[i].start_moment_ += measure_length;
135   
136   SCM grouping = context->get_property ("beatGrouping");
137   Moment measure_pos (0);
138   
139   vector<Moment> group_starts;
140   vector<Moment> beat_starts;
141   
142   while (measure_pos <= infos_.back().start_moment_)
143     {
144       int count = 2;
145       if (scm_is_pair (grouping))
146         {
147           count = scm_to_int (scm_car (grouping));
148           grouping = scm_cdr (grouping);
149         }
150
151       group_starts.push_back (measure_pos);
152       for (int i = 0; i < count; i++)
153         {
154           beat_starts.push_back (measure_pos + beat_length * i);
155         }
156       measure_pos += beat_length * count;
157     }
158    
159   vsize j = 0;
160   vsize k = 0;
161   for (vsize i = 0; i  < infos_.size(); i++)
162     {
163       while (j < group_starts.size() - 1
164              && group_starts[j+1] <= infos_[i].start_moment_)
165         j++;
166
167       infos_[i].group_start_ = group_starts[j];
168       infos_[i].beat_length_ = beat_length;  
169       while (k < beat_starts.size() - 1
170              && beat_starts[k+1] <= infos_[i].start_moment_)
171         k++;
172
173       infos_[i].beat_start_ = beat_starts[k];
174     }
175   
176   beamify (subdivide_beams);
177 }
178
179
180 void
181 Beaming_pattern::beamify (bool subdivide_beams)
182 {
183   if (infos_.size () <= 1)
184     return;
185   
186   Drul_array<Beaming_pattern> splits;
187
188   bool at_boundary = false;
189   int m = best_splitpoint_index (&at_boundary);
190
191   splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
192                                               infos_.begin () + m);
193   splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
194                                                infos_.end ());
195
196   Direction d = LEFT;
197
198   do
199     {
200       splits[d].beamify (subdivide_beams);
201     }
202   while (flip (&d) != LEFT);
203
204   int middle_beams = ((at_boundary && subdivide_beams)
205                       ? 1       
206                       : min (splits[RIGHT].beam_extend_count (LEFT),
207                              splits[LEFT].beam_extend_count (RIGHT)));
208
209   do
210     {
211       if (splits[d].infos_.size () != 1)
212         boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
213     }
214   while (flip (&d) != LEFT);
215
216   infos_ = splits[LEFT].infos_;
217   infos_.insert (infos_.end (),
218                  splits[RIGHT].infos_.begin (),
219                  splits[RIGHT].infos_.end ());
220 }
221
222
223 void
224 Beaming_pattern::add_stem (Moment m, int b)
225 {
226   infos_.push_back (Beam_rhythmic_element (m, b));
227 }
228
229 Beaming_pattern::Beaming_pattern ()
230 {
231 }
232
233 int
234 Beaming_pattern::beamlet_count (int i, Direction d) const
235 {
236   return infos_.at (i).beam_count_drul_[d];
237 }