]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
release: 1.3.94
[lilypond.git] / lily / auto-beam-engraver.cc
1
2 /*   
3   auto-beam-engraver.cc --  implement Auto_beam_engraver
4   
5   source file of the GNU LilyPond music typesetter
6   
7   (c) 1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
8   
9  */
10 #include "beaming.hh"
11 #include "musical-request.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "debug.hh"
15 #include "engraver-group-engraver.hh"
16 #include "bar.hh"
17 #include "rest.hh"
18 #include "engraver.hh"
19 #include "item.hh"
20 #include "spanner.hh"
21
22 class Auto_beam_engraver : public Engraver
23 {
24 public:
25   Auto_beam_engraver ();
26   VIRTUAL_COPY_CONS (Translator);
27
28 protected:
29   virtual bool do_try_music (Music*);
30   virtual void do_pre_move_processing ();
31   virtual void do_post_move_processing ();
32   virtual void do_removal_processing ();
33   virtual void acknowledge_element (Score_element_info);
34   virtual void do_process_music ();
35   virtual void process_acknowledged ();
36 private:
37   void begin_beam ();
38   void consider_end_and_begin (Moment test_mom);
39   Spanner* create_beam_p ();
40   void end_beam ();
41   void junk_beam ();
42   bool same_grace_state_b (Score_element* e);
43   void typeset_beam ();
44
45   Moment shortest_mom_;
46   Spanner *finished_beam_p_;
47   Link_array<Item>* stem_l_arr_p_;
48   
49   Moment last_add_mom_;
50   Moment extend_mom_;
51   Moment beam_start_moment_;
52   Moment beam_start_location_;
53   
54   // We act as if beam were created, and start a grouping anyway.
55   Beaming_info_list*grouping_p_;  
56   Beaming_info_list*finished_grouping_p_;
57 };
58
59
60
61
62 ADD_THIS_TRANSLATOR (Auto_beam_engraver);
63
64
65 /*
66   TODO: remove all references to Timing_engraver; should read properties.
67   
68  */
69 Auto_beam_engraver::Auto_beam_engraver ()
70 {
71   stem_l_arr_p_ = 0;
72   shortest_mom_ = Moment (1, 8);
73   finished_beam_p_ = 0;
74   finished_grouping_p_ = 0;
75   grouping_p_ = 0;
76 }
77
78
79 bool
80 Auto_beam_engraver::do_try_music (Music*) 
81 {
82   return false;
83
84
85 void
86 Auto_beam_engraver::do_process_music ()
87 {
88   consider_end_and_begin (shortest_mom_);
89 }
90
91 void
92 Auto_beam_engraver::consider_end_and_begin (Moment test_mom)
93 {
94   Moment one_beat = *unsmob_moment( get_property ("beatLength"));
95
96   int num = *unsmob_moment (get_property("measureLength")) / one_beat;
97   int den = one_beat.den_i ();
98
99   
100   String time_str = String ("time") + to_str (num) + "_" + to_str (den);
101
102   String type_str;
103   if (test_mom.num () != 1)
104     type_str = to_str (test_mom.num ());
105   if (test_mom.den () != 1)
106     type_str = type_str + "_" + to_str (test_mom.den ());
107
108   /*
109     URG
110     
111     FIXME: SHOULD USE ALIST
112     
113    */
114
115   /*
116     Determine end moment for auto beaming (and begin, mostly 0==anywhere) 
117     In order of increasing priority:
118
119     i.   every beat <den>
120     ii.  time<num>_<den>beamAutoEnd
121     iii. time<num>_<den>beamAutoEnd<type>
122     iv.  beamAutoEnd
123     v.   beamAutoEnd<type>
124
125
126     Rationale:
127
128     [to be defined in config file]
129     i.   easy catch-all rule
130     ii.  exceptions for time signature
131     iii. exceptions for time signature, for specific duration type
132
133     [user override]
134     iv.  generic override
135     v.   override for specific duration type
136
137     The user overrides should be required for common cases.
138    */
139   
140   /*
141     first guess: begin beam at any position
142   */
143   Moment begin_mom (0);
144   /*
145     first guess: end beam at end of beat
146   */
147   SCM one (get_property ("beatLength"));
148
149   Moment end_mom;
150   if (unsmob_moment (one))
151     end_mom = *unsmob_moment (one);
152
153   /*
154     second guess: property generic time exception
155   */
156   SCM begin = get_property ((time_str + "beamAutoBegin").ch_C());
157   if (unsmob_moment (begin))
158     begin_mom = * unsmob_moment (begin);
159
160   SCM end = get_property ((time_str + "beamAutoEnd").ch_C());
161   if (unsmob_moment (end))
162     end_mom = * unsmob_moment (end);
163
164   /*
165     third guess: property time exception, specific for duration type
166   */
167   if (type_str.length_i ())
168     {
169       SCM end_mult = get_property ((time_str + "beamAutoEnd" + type_str).ch_C());
170       if (unsmob_moment (end_mult))
171         end_mom = * unsmob_moment (end_mult);
172
173       SCM begin_mult = get_property ((time_str + "beamAutoBegin" + type_str).ch_C());
174       if (unsmob_moment (begin_mult))
175         begin_mom = * unsmob_moment (begin_mult);
176     }
177
178   /*
179     fourth guess [user override]: property plain generic
180   */
181   begin = get_property ("beamAutoBegin");
182   if (unsmob_moment (begin))
183     begin_mom = * unsmob_moment (begin);
184
185
186   
187   end = get_property ("beamAutoEnd");
188   if (unsmob_moment (end))
189     end_mom = * unsmob_moment (end);
190
191   /*
192     fifth guess [user override]: property plain, specific for duration type
193   */
194   if (type_str.length_i ())
195     {
196       SCM end_mult = get_property ((String ("beamAutoEnd") + type_str).ch_C());
197       if (unsmob_moment (end_mult))
198         end_mom = * unsmob_moment (end_mult);
199
200       SCM begin_mult = get_property ((String ("beamAutoBegin") + type_str).ch_C());
201       if (unsmob_moment (begin_mult))
202         begin_mom = * unsmob_moment (begin_mult);
203     }
204
205   Rational r;
206   if (end_mom)
207     r = unsmob_moment (get_property ("measurePosition"))->mod_rat (end_mom);
208   else
209     r = Moment (1);
210
211   if (stem_l_arr_p_ && !r)
212     end_beam ();
213      
214   /*
215     Allow already started autobeam to end
216    */
217   SCM on = get_property ("noAutoBeaming");
218   if (to_boolean (on))
219     return;
220
221   if (begin_mom)
222     r =     unsmob_moment (get_property ("measurePosition"))->mod_rat (begin_mom);
223   if (!stem_l_arr_p_ && (!begin_mom || !r))
224     begin_beam ();
225 }
226
227       
228 void
229 Auto_beam_engraver::begin_beam ()
230 {
231   assert (!stem_l_arr_p_);
232   stem_l_arr_p_ = new Link_array<Item>;
233   assert (!grouping_p_);
234   grouping_p_ = new Beaming_info_list;
235   beam_start_moment_ = now_mom ();
236   beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
237 }
238
239 Spanner*
240 Auto_beam_engraver::create_beam_p ()
241 {
242   Spanner* beam_p = new Spanner (get_property ("Beam"));
243   Beam::set_interface (beam_p);
244
245   for (int i = 0; i < stem_l_arr_p_->size (); i++)
246     {
247       /*
248         watch out for stem tremolos and abbreviation beams
249        */
250       if (Stem::beam_l ((*stem_l_arr_p_)[i]))
251         {
252           return 0;
253         }
254       Beam::add_stem (beam_p,(*stem_l_arr_p_)[i]);
255     }
256   
257   announce_element (beam_p, 0);
258
259   return beam_p;
260 }
261
262 void
263 Auto_beam_engraver::end_beam ()
264 {
265   if (stem_l_arr_p_->size () < 2)
266     {
267       junk_beam ();
268     }
269   else
270     {
271       finished_beam_p_ = create_beam_p ();
272       if (finished_beam_p_)
273         finished_grouping_p_ = grouping_p_;
274       delete stem_l_arr_p_;
275       stem_l_arr_p_ = 0;
276       grouping_p_ = 0;
277       shortest_mom_ = Moment (1, 8);
278     }
279 }
280  
281 void
282 Auto_beam_engraver::typeset_beam ()
283 {
284   if (finished_beam_p_)
285     {
286       finished_grouping_p_->beamify ();
287       Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
288       typeset_element (finished_beam_p_);
289       finished_beam_p_ = 0;
290     
291       delete finished_grouping_p_;
292       finished_grouping_p_= 0;
293     }
294 }
295
296 void
297 Auto_beam_engraver::do_post_move_processing ()
298 {
299   /*
300     don't beam over skips
301    */
302   if (stem_l_arr_p_)
303     {
304       Moment now = now_mom ();
305       if (extend_mom_ < now)
306         {
307           end_beam ();
308         }
309     }
310 }
311
312 void
313 Auto_beam_engraver::do_pre_move_processing ()
314 {
315   typeset_beam ();
316 }
317
318 void
319 Auto_beam_engraver::do_removal_processing ()
320 {
321   /* finished beams may be typeset */
322   typeset_beam ();
323   /* but unfinished may need another announce/acknowledge pass */
324   if (stem_l_arr_p_)
325     junk_beam ();
326 }
327
328 bool
329 Auto_beam_engraver::same_grace_state_b (Score_element* e)
330 {
331   bool gr = e->get_elt_property ("grace") == SCM_BOOL_T;
332   SCM wg =get_property ("weAreGraceContext");
333   return (to_boolean (wg)) == gr;
334 }
335
336 void
337 Auto_beam_engraver::acknowledge_element (Score_element_info info)
338 {
339   if (!same_grace_state_b (info.elem_l_))
340     return;
341   
342   if (stem_l_arr_p_)
343     {
344       if (Beam::has_interface (info.elem_l_))
345         {
346           end_beam ();
347         }
348       else if (Bar::has_interface (info.elem_l_))
349         {
350           end_beam ();
351         }
352       else if (Rest::has_interface (info.elem_l_))
353         {
354           end_beam ();
355         }
356     }
357   
358   if (Stem::has_interface (info.elem_l_))
359     {
360       Item* stem_l = dynamic_cast<Item *> (info.elem_l_);
361                                        
362       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
363       if (!rhythmic_req)
364         {
365           programming_error ("Stem must have rhythmic structure");
366           return;
367         }
368       
369       /*
370         Don't (start) auto-beam over empty stems; skips or rests
371         */
372       if (!Stem::heads_i (stem_l))
373         {
374           if (stem_l_arr_p_)
375             end_beam ();
376           return;
377         }
378
379       if (Stem::beam_l (stem_l))
380         {
381           if (stem_l_arr_p_)
382             junk_beam ();
383           return ;
384         }
385               
386       int durlog  =rhythmic_req->duration_.durlog_i_;
387       if (durlog <= 2)
388         {
389           if (stem_l_arr_p_)
390             end_beam ();
391           return;
392         }
393
394       /*
395         if shortest duration would change
396         reconsider ending/starting beam first.
397       */
398       Moment mom = rhythmic_req->duration_.length_mom ();
399       consider_end_and_begin (mom);
400       if (!stem_l_arr_p_)
401         return;
402       if (mom < shortest_mom_)
403         {
404           if (stem_l_arr_p_->size ())
405             {
406               shortest_mom_ = mom;
407               consider_end_and_begin (shortest_mom_);
408               if (!stem_l_arr_p_)
409                 return;
410             }
411           shortest_mom_ = mom;
412         }
413       Moment now = now_mom ();
414       
415       grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
416                              durlog - 2);
417       stem_l_arr_p_->push (stem_l);
418       last_add_mom_ = now;
419       extend_mom_ = extend_mom_ >? now + rhythmic_req->length_mom ();
420     }
421 }
422
423 void
424 Auto_beam_engraver::junk_beam () 
425 {
426   assert (stem_l_arr_p_);
427   
428   delete stem_l_arr_p_;
429   stem_l_arr_p_ = 0;
430   delete grouping_p_;
431   grouping_p_ = 0;
432   shortest_mom_ = Moment (1, 8);
433 }
434
435 void
436 Auto_beam_engraver::process_acknowledged ()
437 {
438   if (stem_l_arr_p_)
439     {
440       Moment now = now_mom ();
441       if ((extend_mom_ < now)
442           || ((extend_mom_ == now) && (last_add_mom_ != now )))
443         {
444           end_beam ();
445         }
446       else if (!stem_l_arr_p_->size ())
447         {
448           junk_beam ();
449         }
450     }
451 }
452