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