]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
release: 1.1.52
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*   
2   auto-beam-engraver.cc --  implement Auto_beam_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "auto-beam-engraver.hh"
11 #include "musical-request.hh"
12 #include "bar.hh"
13 #include "beam.hh"
14 #include "rhythmic-grouping.hh"
15 #include "rest.hh"
16 #include "stem.hh"
17 #include "debug.hh"
18 #include "time-description.hh"
19
20 ADD_THIS_TRANSLATOR (Auto_beam_engraver);
21
22 Auto_beam_engraver::Auto_beam_engraver ()
23 {
24   stem_l_arr_p_ = 0;
25   shortest_mom_ = Moment (1, 8);
26   finished_beam_p_ = 0;
27   finished_grouping_p_ = 0;
28   grouping_p_ = 0;
29 }
30
31 void
32 Auto_beam_engraver::do_process_requests ()
33 {
34   consider_end_and_begin ();
35 }
36
37 void
38 Auto_beam_engraver::consider_end_and_begin ()
39 {
40   Time_description const *time = get_staff_info().time_C_;
41   int num = time->whole_per_measure_ / time->one_beat_;
42   int den = time->one_beat_.den_i ();
43   String time_str = String ("time") + to_str (num) + "_" + to_str (den);
44   String type_str;
45   if (shortest_mom_.num () != 1)
46     type_str = to_str (shortest_mom_.num ());
47   if (shortest_mom_.den () != 1)
48     type_str = type_str + "_" + to_str (shortest_mom_.den ());
49
50   /*
51     Determine end moment for auto beaming (and begin, mostly 0==anywhere) 
52     In order of increasing priority:
53
54     i.   every beat <den>
55     ii.  time<num>_<den>beamAutoEnd
56     iii. time<num>_<den>beamAutoEnd<type>
57     iv.  beamAutoEnd
58     v.   beamAutoEnd<type>
59
60
61     Rationale:
62
63     [to be defined in config file]
64     i.   easy catch-all rule
65     ii.  exceptions for time signature
66     iii. exceptions for time signature, for specific duration type
67
68     [user override]
69     iv.  generic override
70     v.   override for specific duration type
71
72     The user overrides should be required for common cases.
73    */
74   
75   /*
76     first guess: begin beam at any position
77   */
78   Moment begin_mom (0);
79   /*
80     first guess: end beam at end of beat
81   */
82   Moment end_mom = time->one_beat_;
83
84   /*
85     second guess: property generic time exception
86   */
87   Scalar begin = get_property (time_str + "beamAutoBegin", 0);
88   if (begin.length_i ())
89     begin_mom = begin.to_rat ();
90
91   Scalar end = get_property (time_str + "beamAutoEnd", 0);
92   if (end.length_i ())
93     end_mom = end.to_rat ();
94
95   /*
96     third guess: property time exception, specific for duration type
97   */
98   if (type_str.length_i ())
99     {
100       Scalar end_mult = get_property (time_str + "beamAutoEnd" + type_str, 0);
101       if (end_mult.length_i ())
102         end_mom = end_mult.to_rat ();
103       Scalar begin_mult = get_property (time_str + "beamAutoBegin" + type_str, 0);
104       if (begin_mult.length_i ())
105         begin_mom = begin_mult.to_rat ();
106     }
107
108   /*
109     fourth guess [user override]: property plain generic
110   */
111   begin = get_property ("beamAutoBegin", 0);
112   if (begin.length_i ())
113     begin_mom = begin.to_rat ();
114   
115   end = get_property ("beamAutoEnd", 0);
116   if (end.length_i ())
117     end_mom = end.to_rat ();
118
119   /*
120     fifth guess [user override]: property plain, specific for duration type
121   */
122   if (type_str.length_i ())
123     {
124       Scalar end_mult = get_property (String ("beamAutoEnd") + type_str, 0);
125       if (end_mult.length_i ())
126         end_mom = end_mult.to_rat ();
127       Scalar begin_mult = get_property (String ("beamAutoBegin") + type_str, 0);
128       if (begin_mult.length_i ())
129         begin_mom = begin_mult.to_rat ();
130     }
131
132   Rational r;
133   if (end_mom)
134     r = time->whole_in_measure_.mod_rat (end_mom);
135   else
136     r = Moment (1);
137
138   if (stem_l_arr_p_ && !r)
139     end_beam ();
140      
141   /*
142     Allow already started autobeam to end
143    */
144   Scalar on = get_property ("beamAuto", 0);
145   if (!on.to_bool ())
146     return;
147
148   if (begin_mom)
149     r = time->whole_in_measure_.mod_rat (begin_mom);
150   if (!stem_l_arr_p_ && (!begin_mom || !r))
151     begin_beam ();
152 }
153
154       
155 void
156 Auto_beam_engraver::begin_beam ()
157 {
158   assert (!stem_l_arr_p_);
159   stem_l_arr_p_ = new Array<Stem*>;
160   assert (!grouping_p_);
161   grouping_p_ = new Rhythmic_grouping;
162 }
163
164 Beam*
165 Auto_beam_engraver::create_beam_p ()
166 {
167   Beam* beam_p = new Beam;
168
169   for (int i = 0; i < stem_l_arr_p_->size (); i++)
170     beam_p->add_stem ((*stem_l_arr_p_)[i]);
171
172   /* urg, copied from Beam_engraver */
173   Scalar prop = get_property ("beamslopedamping", 0);
174   if (prop.isnum_b ()) 
175     beam_p->set_elt_property (damping_scm_sym, gh_int2scm( prop));
176
177   prop = get_property ("beamquantisation", 0);
178   if (prop.isnum_b ()) 
179     beam_p->quantisation_ = (Beam::Quantisation)(int)prop;
180  
181   announce_element (Score_element_info (beam_p, 0));
182   return beam_p;
183 }
184
185 void
186 Auto_beam_engraver::end_beam ()
187 {
188   if (stem_l_arr_p_->size () < 2)
189     {
190       junk_beam ();
191     }
192   else
193     {
194       finished_beam_p_ = create_beam_p ();
195       finished_grouping_p_ = grouping_p_;
196       delete stem_l_arr_p_;
197       stem_l_arr_p_ = 0;
198       grouping_p_ = 0;
199       shortest_mom_ = Moment (1, 8);
200     }
201 }
202  
203 void
204 Auto_beam_engraver::typeset_beam ()
205 {
206   if (finished_beam_p_)
207     {
208       Rhythmic_grouping const * rg_C = get_staff_info().rhythmic_C_;
209       rg_C->extend (finished_grouping_p_->interval());
210       finished_beam_p_->set_grouping (*rg_C, *finished_grouping_p_);
211       typeset_element (finished_beam_p_);
212       finished_beam_p_ = 0;
213     
214       delete finished_grouping_p_;
215       finished_grouping_p_= 0;
216     }
217 }
218
219 void
220 Auto_beam_engraver::do_post_move_processing ()
221 {
222 }
223
224 void
225 Auto_beam_engraver::do_pre_move_processing ()
226 {
227   typeset_beam ();
228 }
229
230 void
231 Auto_beam_engraver::do_removal_processing ()
232 {
233   if (stem_l_arr_p_)
234     end_beam ();
235   typeset_beam ();
236 }
237
238 bool
239 Auto_beam_engraver::same_grace_state_b (Score_element* e)
240 {
241   bool gr = (e->get_elt_property (grace_scm_sym) != SCM_BOOL_F) ;
242
243   return gr == get_property ("weAreGraceContext",0).to_bool ();
244 }
245
246 void
247 Auto_beam_engraver::acknowledge_element (Score_element_info info)
248 {
249   if (stem_l_arr_p_)
250     {
251       if (Beam *b = dynamic_cast<Beam *> (info.elem_l_))
252         {
253           if (same_grace_state_b (b))
254             junk_beam ();
255         }
256       else if (Bar *b = dynamic_cast<Bar *> (info.elem_l_))
257         {
258           if (same_grace_state_b (b))
259             junk_beam ();
260         }
261       else if (Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_))
262         {
263           if (Rest* rest_l = dynamic_cast<Rest *> (info.elem_l_))
264             {
265               if (same_grace_state_b (rest_l))
266                 end_beam ();
267             }
268           else if (Stem* stem_l = dynamic_cast<Stem *> (info.elem_l_))
269             {
270               /*
271                 if we're a nice grace beam, but the new note is regular,
272                 gracefully end beam, and consider starting a regular one.
273                */
274               /*
275                 When does that happen !? --hwn
276                */
277 #if 0
278               if (stem_l_arr_p_ && stem_l_arr_p_->size ()
279                   && grace_b (stem_l_arr_p_->top ())
280                   && !grace_b (stem_l))
281                 {
282                     end_beam ();
283                     consider_end_and_begin ();
284                     if (!stem_l_arr_p_)
285                       return;
286                 }
287 #endif
288               if (same_grace_state_b (stem_l))
289                 {
290                   if (stem_l->beam_l_)
291                     junk_beam ();
292                   /*
293                     now that we have last_add_mom_, perhaps we can (should) do away
294                     with these individual junk_beams
295                   */
296                   else if (rhythmic_req->duration_.durlog_i_ <= 2)
297                     end_beam ();
298                   else 
299                     {
300                       Moment start = get_staff_info().time_C_->whole_in_measure_;
301                       if (!grouping_p_->child_fit_b (start))
302                         end_beam ();
303                       else
304                         {
305                           /*
306                             if shortest duration would change
307                             reconsider ending/starting beam first.
308                           */
309                           Moment mom = rhythmic_req->duration_.length_mom ();
310                           if (mom < shortest_mom_)
311                             {
312                               if (stem_l_arr_p_->size ())
313                                 {
314                                   shortest_mom_ = mom;
315                                   consider_end_and_begin ();
316                                 }
317                               shortest_mom_ = mom;
318                             }
319                           grouping_p_->add_child (start, rhythmic_req->length_mom ());
320                           
321                           stem_l_arr_p_->push (stem_l);
322                           Moment now = now_mom ();
323                           last_add_mom_ = now;
324                           extend_mom_ = extend_mom_ >? now + rhythmic_req->length_mom ();
325                         }
326                     }
327                 }
328             }
329         }
330     }
331 }
332
333 void
334 Auto_beam_engraver::junk_beam () 
335 {
336   assert (stem_l_arr_p_);
337   
338   delete stem_l_arr_p_;
339   stem_l_arr_p_ = 0;
340   delete grouping_p_;
341   grouping_p_ = 0;
342   shortest_mom_ = Moment (1, 8);
343 }
344
345 void
346 Auto_beam_engraver::process_acknowledged ()
347 {
348   if (stem_l_arr_p_)
349     {
350       Moment now = now_mom ();
351       if ((extend_mom_ < now)
352           || ((extend_mom_ == now) && (last_add_mom_ != now )))
353         {
354           end_beam ();
355         }
356       else if (!stem_l_arr_p_->size ())
357         {
358           junk_beam ();
359         }
360     }
361 }