]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
release: 1.5.13
[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 ();
295       Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
296       typeset_grob (finished_beam_p_);
297       finished_beam_p_ = 0;
298     
299       delete finished_grouping_p_;
300       finished_grouping_p_= 0;
301     }
302 }
303
304 void
305 Auto_beam_engraver::start_translation_timestep ()
306 {
307   count_i_ = 0;
308   /*
309     don't beam over skips
310    */
311   if (stem_l_arr_p_)
312     {
313       Moment now = now_mom ();
314       if (extend_mom_ < now)
315         {
316           end_beam ();
317         }
318     }
319 }
320
321 void
322 Auto_beam_engraver::stop_translation_timestep ()
323 {
324   typeset_beam ();
325 }
326
327 void
328 Auto_beam_engraver::finalize ()
329 {
330   /* finished beams may be typeset */
331   typeset_beam ();
332   /* but unfinished may need another announce/acknowledge pass */
333   if (stem_l_arr_p_)
334     junk_beam ();
335 }
336
337
338 void
339 Auto_beam_engraver::acknowledge_grob (Grob_info info)
340 {
341   if (stem_l_arr_p_)
342     {
343       if (Beam::has_interface (info.grob_l_))
344         {
345           end_beam ();
346         }
347       else if (Bar::has_interface (info.grob_l_))
348         {
349           end_beam ();
350         }
351       else if (Rest::has_interface (info.grob_l_))
352         {
353           end_beam ();
354         }
355     }
356   
357   if (Stem::has_interface (info.grob_l_))
358     {
359       Item* stem_l = dynamic_cast<Item *> (info.grob_l_);
360                                        
361       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
362       if (!rhythmic_req)
363         {
364           programming_error ("Stem must have rhythmic structure");
365           return;
366         }
367       
368       /*
369         Don't (start) auto-beam over empty stems; skips or rests
370         */
371       if (!Stem::heads_i (stem_l))
372         {
373           if (stem_l_arr_p_)
374             end_beam ();
375           return;
376         }
377
378       if (Stem::beam_l (stem_l))
379         {
380           if (stem_l_arr_p_)
381             junk_beam ();
382           return ;
383         }
384               
385       int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
386       
387       if (durlog <= 2)
388         {
389           if (stem_l_arr_p_)
390             end_beam ();
391           return;
392         }
393
394
395       /*
396         ignore grace notes.
397        */
398       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
399         return ;
400         
401       
402       Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
403       /* FIXME:
404
405         This comment has been here since long:
406
407            if shortest duration would change
408             consider ending and beginning beam first. 
409
410         but the code didn't match: */
411 #if 1
412       consider_end (dur);
413       consider_begin (dur);
414
415       if (dur < shortest_mom_)
416         shortest_mom_ = dur;
417 #else
418       /* I very much suspect that we wanted: */
419
420       consider_end (shortest_mom_);
421       if (dur < shortest_mom_)
422         {
423           shortest_mom_ = dur;
424           consider_end (shortest_mom_);
425         }
426       consider_begin (shortest_mom_);
427 #endif
428
429       if (!stem_l_arr_p_)
430         return;
431       
432       Moment now = now_mom ();
433       
434       grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
435                              durlog - 2);
436       stem_l_arr_p_->push (stem_l);
437       last_add_mom_ = now;
438       extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
439     }
440 }
441
442 void
443 Auto_beam_engraver::create_grobs ()
444 {
445   if (!count_i_)
446     {
447       consider_end (shortest_mom_);
448       consider_begin (shortest_mom_);
449     }
450   else if (count_i_ > 1)
451     {
452       if (stem_l_arr_p_)
453         {
454           Moment now = now_mom ();
455           if ((extend_mom_ < now)
456               || ((extend_mom_ == now) && (last_add_mom_ != now)))
457             {
458               end_beam ();
459             }
460           else if (!stem_l_arr_p_->size ())
461             {
462               junk_beam ();
463             }
464         }    
465     }
466
467   /*
468     count_i_++ -> 
469
470         auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
471   count_i_ = count_i_ + 1;
472 }
473 ENTER_DESCRIPTION(Auto_beam_engraver,
474 /* descr */       "Generate beams based on measure characteristics and observed
475 Stems.  Uses beatLength, measureLength and measurePosition to decide
476 when to start and stop a beam.  Overriding beaming is done through
477 @ref{Stem_engraver} properties stemLeftBeamCount and
478 stemRightBeamCount.
479 ",
480 /* creats*/       "Beam",
481 /* acks  */       "stem-interface rest-interface beam-interface bar-line-interface",
482 /* reads */       "noAutoBeaming autoBeamSettings",
483 /* write */       "");