]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
* input/regression/grace-auto-beam.ly: new file
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "beaming.hh"
11 #include "musical-request.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "engraver-group-engraver.hh"
16 #include "bar-line.hh"
17 #include "rest.hh"
18 #include "engraver.hh"
19 #include "item.hh"
20 #include "spanner.hh"
21
22 /*
23   TODO: figure what to do in grace?
24
25   TODO: documentme.
26  */
27 class Auto_beam_engraver : public Engraver
28 {
29   TRANSLATOR_DECLARATIONS(Auto_beam_engraver);
30 protected:
31   virtual void stop_translation_timestep ();
32   virtual void start_translation_timestep ();
33   virtual void finalize ();
34   virtual void acknowledge_grob (Grob_info);
35   virtual void process_acknowledged_grobs ();
36
37 private:
38   bool test_moment (Direction, Moment);
39   void consider_begin (Moment);
40   void consider_end (Moment);
41   Spanner* create_beam_p ();
42   void begin_beam ();
43   void end_beam ();
44   void junk_beam ();
45   bool same_grace_state_b (Grob* e);
46   void typeset_beam ();
47
48   /*
49     shortest_mom is the shortest note in the beam.
50    */
51   Moment shortest_mom_;
52   Spanner *finished_beam_p_;
53   Link_array<Item>* stem_l_arr_p_;
54
55
56   int count_i_;
57   Moment last_add_mom_;
58   /*
59     Projected ending of the  beam we're working on.
60    */
61   Moment extend_mom_;
62   Moment beam_start_moment_;
63   Moment beam_start_location_;
64
65   bool subdivide_beams_;
66   Moment beat_length_;
67   
68   // We act as if beam were created, and start a grouping anyway.
69   Beaming_info_list*grouping_p_;  
70   Beaming_info_list*finished_grouping_p_;
71 };
72
73
74
75 Auto_beam_engraver::Auto_beam_engraver ()
76 {
77   count_i_ = 0;
78   stem_l_arr_p_ = 0;
79   shortest_mom_ = Moment (Rational (1, 8));
80   finished_beam_p_ = 0;
81   finished_grouping_p_ = 0;
82   grouping_p_ = 0;
83 }
84
85 /*
86   Determine end moment for auto beaming (or begin moment, but mostly
87   0==anywhere) In order of increasing priority:
88   
89   i.   begin anywhere, end at every beat
90   ii.  end   *    <num> <den>
91   iii. end <type> <num> <den>
92   
93   iv.  end   *      *     *
94   v.   end <type>   *     *
95   
96   
97   Rationale:
98   
99   [to be defined in config file]
100   i.   easy catch-all rule
101   ii.  exceptions for time signature
102   iii. exceptions for time signature, for specific duration type
103   
104   [user override]
105   iv.  generic override
106   v.   override for specific duration type
107   
108 */
109 bool
110 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
111 {
112   Moment now = now_mom();
113   if (dir == START
114       && now.grace_part_)
115     {
116       return false;
117     }
118   
119   SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
120   SCM function;
121   if (dir == START)
122     function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
123   else
124     function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
125
126   Moment one_beat = *unsmob_moment (get_property ("beatLength"));
127   int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
128   int den = one_beat.den ();
129   SCM time = scm_list_n (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
130
131   SCM type = scm_list_n (gh_int2scm (test_mom.num ()),
132                       gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
133
134   SCM settings = get_property ("autoBeamSettings");
135   
136   /* first guess */
137   
138   /* begin beam at any position
139  (and fallback for end) */
140   Moment moment (0);
141   
142   /* end beam at end of beat */
143   if (dir == STOP)
144     {
145       SCM beat (get_property ("beatLength"));
146       
147       if (unsmob_moment (beat))
148         moment = *unsmob_moment (beat);
149     }
150
151   /* second guess: property generic time exception */
152   SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
153   
154   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
155     moment = * unsmob_moment (ly_cdr (m));
156
157   /* third guess: property time exception, specific for duration type */
158   m = gh_assoc (gh_append3 (function, type, time), settings);
159   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
160     moment = * unsmob_moment (ly_cdr (m));
161
162   /* fourth guess [user override]: property plain generic */
163   m = gh_assoc (gh_append3 (function, wild, wild), settings);
164   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
165     moment = * unsmob_moment (ly_cdr (m));
166
167   /* fifth guess [user override]: property plain, specific for duration type */
168   m = gh_assoc (gh_append3 (function, type, wild), settings);
169   if (m != SCM_BOOL_F && unsmob_moment (ly_cdr (m)))
170     moment = * unsmob_moment (ly_cdr (m));
171   
172   Rational r;
173   if (moment.to_bool ())
174     {
175       /* Ugh? measurePosition can be negative, when \partial
176          We may have to fix this elsewhere (timing translator)
177         r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
178       */
179       Moment pos = * unsmob_moment (get_property ("measurePosition"));
180       if (pos < Moment (0))
181         {
182           Moment length = * unsmob_moment (get_property ("measureLength"));
183           pos = length - pos;
184         }
185       r = pos.main_part_.mod_rat (moment.main_part_);
186     }
187   else
188     {
189       if (dir == START)
190         /* if undefined, starting is ok */
191         r = 0;
192       else
193         /* but ending is not */
194         r = 1;
195     }
196
197   return !r;
198 }
199
200 void
201 Auto_beam_engraver::consider_begin (Moment test_mom)
202 {
203   bool on = to_boolean (get_property ("autoBeaming"));
204   if (!stem_l_arr_p_ && on)
205     {
206       bool b = test_moment (START, test_mom);
207       if (b)
208         begin_beam ();
209     }
210 }
211
212 void
213 Auto_beam_engraver::consider_end (Moment test_mom)
214 {
215   if (stem_l_arr_p_)
216     {
217       /* Allow already started autobeam to end:
218          don't check for autoBeaming */
219       bool b = test_moment (STOP, test_mom);
220       if (b)
221         end_beam ();
222     }
223 }
224
225 Spanner*
226 Auto_beam_engraver::create_beam_p ()
227 {
228   if (to_boolean (get_property ("skipTypesetting")))
229     {
230      return 0;
231     }
232   
233   Spanner* beam_p = new Spanner (get_property ("Beam"));
234   for (int i = 0; i < stem_l_arr_p_->size (); i++)
235     {
236       /*
237         watch out for stem tremolos and abbreviation beams
238        */
239       if (Stem::beam_l ((*stem_l_arr_p_)[i]))
240         {
241           scm_gc_unprotect_object (beam_p->self_scm ());
242           return 0;
243         }
244       Beam::add_stem (beam_p, (*stem_l_arr_p_)[i]);
245     }
246   
247   announce_grob(beam_p, SCM_EOL);
248
249   return beam_p;
250 }
251
252 void
253 Auto_beam_engraver::begin_beam ()
254 {
255   assert (!stem_l_arr_p_);
256   stem_l_arr_p_ = new Link_array<Item>;
257   assert (!grouping_p_);
258   grouping_p_ = new Beaming_info_list;
259   beam_start_moment_ = now_mom ();
260   beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
261   subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
262   beat_length_ = *unsmob_moment (get_property ("beatLength"));
263 }
264
265
266 void
267 Auto_beam_engraver::junk_beam () 
268 {
269   assert (stem_l_arr_p_);
270   
271   delete stem_l_arr_p_;
272   stem_l_arr_p_ = 0;
273   delete grouping_p_;
274   grouping_p_ = 0;
275
276   shortest_mom_ = Moment (Rational (1, 8));
277 }
278
279 void
280 Auto_beam_engraver::end_beam ()
281 {
282   if (stem_l_arr_p_->size () < 2)
283     {
284       junk_beam ();
285     }
286   else
287     
288     {
289       finished_beam_p_ = create_beam_p ();
290       if (finished_beam_p_)
291         finished_grouping_p_ = grouping_p_;
292       delete stem_l_arr_p_;
293       stem_l_arr_p_ = 0;
294       grouping_p_ = 0;
295     }
296
297   shortest_mom_ = Moment (Rational (1, 8));
298 }
299
300 void
301 Auto_beam_engraver::typeset_beam ()
302 {
303   if (finished_beam_p_)
304     {
305       finished_grouping_p_->beamify(beat_length_, subdivide_beams_);
306       Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
307       typeset_grob (finished_beam_p_);
308       finished_beam_p_ = 0;
309     
310       delete finished_grouping_p_;
311       finished_grouping_p_= 0;
312     }
313 }
314
315 void
316 Auto_beam_engraver::start_translation_timestep ()
317 {
318   count_i_ = 0;
319   /*
320     don't beam over skips
321    */
322   if (stem_l_arr_p_)
323     {
324       Moment now = now_mom ();
325       if (extend_mom_ < now)
326         {
327           end_beam ();
328         }
329     }
330 }
331
332 void
333 Auto_beam_engraver::stop_translation_timestep ()
334 {
335   typeset_beam ();
336 }
337
338 void
339 Auto_beam_engraver::finalize ()
340 {
341   /* finished beams may be typeset */
342   typeset_beam ();
343   /* but unfinished may need another announce/acknowledge pass */
344   if (stem_l_arr_p_)
345     junk_beam ();
346 }
347
348
349 void
350 Auto_beam_engraver::acknowledge_grob (Grob_info info)
351 {
352   if (stem_l_arr_p_)
353     {
354       if (Beam::has_interface (info.grob_l_))
355         {
356           end_beam ();
357         }
358       else if (Bar_line::has_interface (info.grob_l_))
359         {
360           end_beam ();
361         }
362       else if (Rest::has_interface (info.grob_l_))
363         {
364           end_beam ();
365         }
366     }
367   
368   if (Stem::has_interface (info.grob_l_))
369     {
370       Item* stem_l = dynamic_cast<Item *> (info.grob_l_);
371                                        
372       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.music_cause ());
373       if (!rhythmic_req)
374         {
375           programming_error ("Stem must have rhythmic structure");
376           return;
377         }
378       
379       /*
380         Don't (start) auto-beam over empty stems; skips or rests
381         */
382       if (!Stem::head_count (stem_l))
383         {
384           if (stem_l_arr_p_)
385             end_beam ();
386           return;
387         }
388
389       if (Stem::beam_l (stem_l))
390         {
391           if (stem_l_arr_p_)
392             junk_beam ();
393           return ;
394         }
395               
396       int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
397       
398       if (durlog <= 2)
399         {
400           if (stem_l_arr_p_)
401             end_beam ();
402           return;
403         }
404
405
406       /*
407         ignore grace notes.
408        */
409       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
410         return ;
411         
412       
413       Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
414       /* FIXME:
415
416         This comment has been here since long:
417
418            if shortest duration would change
419             consider ending and beginning beam first. 
420
421         but the code didn't match: */
422 #if 1
423       consider_end (dur);
424       consider_begin (dur);
425
426       if (dur < shortest_mom_)
427         shortest_mom_ = dur;
428 #else
429       /* I very much suspect that we wanted: */
430
431       consider_end (shortest_mom_);
432       if (dur < shortest_mom_)
433         {
434           shortest_mom_ = dur;
435           consider_end (shortest_mom_);
436         }
437       consider_begin (shortest_mom_);
438 #endif
439
440       if (!stem_l_arr_p_)
441         return;
442       
443       Moment now = now_mom ();
444       
445       grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
446                              durlog - 2);
447       stem_l_arr_p_->push (stem_l);
448       last_add_mom_ = now;
449       extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
450     }
451 }
452
453 void
454 Auto_beam_engraver::process_acknowledged_grobs ()
455 {
456   if (!count_i_)
457     {
458       consider_end (shortest_mom_);
459       consider_begin (shortest_mom_);
460     }
461   else if (count_i_ > 1)
462     {
463       if (stem_l_arr_p_)
464         {
465           Moment now = now_mom ();
466           if ((extend_mom_ < now)
467               || ((extend_mom_ == now) && (last_add_mom_ != now)))
468             {
469               end_beam ();
470             }
471           else if (!stem_l_arr_p_->size ())
472             {
473               junk_beam ();
474             }
475         }    
476     }
477
478   /*
479     count_i_++ -> 
480
481         auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
482   count_i_ = count_i_ + 1;
483 }
484 ENTER_DESCRIPTION(Auto_beam_engraver,
485 /* descr */       "Generate beams based on measure characteristics and observed
486 Stems.  Uses beatLength, measureLength and measurePosition to decide
487 when to start and stop a beam.  Overriding beaming is done through
488 @ref{Stem_engraver} properties stemLeftBeamCount and
489 stemRightBeamCount.
490 ",
491 /* creats*/       "Beam",
492 /* acks  */       "stem-interface rest-interface beam-interface bar-line-interface",
493 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
494 /* write */       "");