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