]> git.donarmstrong.com Git - lilypond.git/blob - lily/beaming-pattern.cc
* lily/beaming-info.cc (beamify): new function: read beatLength
[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 int
28 Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
29 {
30   *at_boundary = true;
31   for (vsize i = 1; i < infos_.size (); i++)  
32     {
33       if (infos_[i].group_start_  == infos_[i].start_moment_)
34         return i;
35     }
36
37   for (vsize i = 1; i < infos_.size (); i++)  
38     {
39       if (infos_[i].beat_start_  == infos_[i].start_moment_)
40         return i;
41     }
42
43   *at_boundary = false;
44   
45   int min_denominator = INT_MAX;
46   int min_index = -1;
47   
48   Moment beat_pos;
49   for (vsize i = 1; i < infos_.size (); i++)  
50     {
51       Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_; 
52       if (dt.den () < min_denominator)
53         {
54           min_denominator = dt.den ();
55           min_index = i;
56         }
57     }
58
59   return min_index;
60 }
61
62 int
63 Beaming_pattern::beam_extend_count (Direction d) const
64 {
65   if (infos_.size () == 1)
66     return infos_[0].beam_count_drul_[d];
67
68   Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
69   Beam_rhythmic_element next = boundary (infos_, d, 1);
70
71   return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
72 }
73
74 void
75 Beaming_pattern::beamify (Context *context)
76 {
77   if (infos_.size () <= 1)
78     return;
79   
80   bool subdivide_beams = to_boolean (context->get_property ("subdivideBeams"));
81   Moment beat_length = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
82   
83   SCM grouping = context->get_property ("beatGrouping");
84   Moment measure_pos (0);
85   
86   vector<Moment> group_starts;
87   vector<Moment> beat_starts;
88   
89   while (measure_pos <= infos_.back().start_moment_)
90     {
91       int count = 2;
92       if (scm_is_pair (grouping))
93         {
94           count = scm_to_int (scm_car (grouping));
95           grouping = scm_cdr (grouping);
96         }
97
98       group_starts.push_back (measure_pos);
99       for (int i = 0; i < count; i++)
100         {
101           beat_starts.push_back (measure_pos + beat_length * i);
102         }
103       measure_pos += beat_length * count;
104     }
105    
106   vsize j = 0;
107   vsize k = 0;
108   for (vsize i = 0; i  < infos_.size(); i++)
109     {
110       while (j < group_starts.size()-1
111              && group_starts[j+1] <= infos_[i].start_moment_)
112         j++;
113
114       infos_[i].group_start_ = group_starts[j];
115
116       while (k < beat_starts.size()-1
117              && beat_starts[k+1] <= infos_[i].start_moment_)
118         k++;
119
120       infos_[i].beat_start_ = beat_starts[k];
121     }
122   
123   beamify (subdivide_beams);
124 }
125
126
127 void
128 Beaming_pattern::beamify (bool subdivide_beams)
129 {
130   if (infos_.size () <= 1)
131     return;
132   
133   Drul_array<Beaming_pattern> splits;
134
135   bool at_boundary = false;
136   int m = best_splitpoint_index (&at_boundary);
137
138   splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
139                                               infos_.begin () + m);
140   splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
141                                                infos_.end ());
142
143   Direction d = LEFT;
144
145   do
146     {
147       splits[d].beamify (subdivide_beams);
148     }
149   while (flip (&d) != LEFT);
150
151   int middle_beams = ((at_boundary && subdivide_beams)
152                       ? 1       
153                       : min (splits[RIGHT].beam_extend_count (LEFT),
154                              splits[LEFT].beam_extend_count (RIGHT)));
155
156   do
157     {
158       if (splits[d].infos_.size () != 1)
159         boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
160     }
161   while (flip (&d) != LEFT);
162
163   infos_ = splits[LEFT].infos_;
164   infos_.insert (infos_.end (),
165                  splits[RIGHT].infos_.begin (),
166                  splits[RIGHT].infos_.end ());
167 }
168
169
170 void
171 Beaming_pattern::add_stem (Moment m, int b)
172 {
173   infos_.push_back (Beam_rhythmic_element (m, b));
174 }
175
176 Beaming_pattern::Beaming_pattern ()
177 {
178 }
179
180 int
181 Beaming_pattern::beamlet_count (int i, Direction d) const
182 {
183   return infos_.at (i).beam_count_drul_[d];
184 }