]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
release: 1.5.3
[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 public:
30   Auto_beam_engraver ();
31   VIRTUAL_COPY_CONS (Translator);
32
33 protected:
34   virtual void stop_translation_timestep ();
35   virtual void start_translation_timestep ();
36   virtual void finalize ();
37   virtual void acknowledge_grob (Grob_info);
38   virtual void create_grobs ();
39
40 private:
41   bool test_moment (Direction, Moment);
42   void consider_begin (Moment);
43   void consider_end (Moment);
44   Spanner* create_beam_p ();
45   void begin_beam ();
46   void end_beam ();
47   void junk_beam ();
48   bool same_grace_state_b (Grob* e);
49   void typeset_beam ();
50
51   /*
52     shortest_mom is the shortest note in the beam.
53    */
54   Moment shortest_mom_;
55   Spanner *finished_beam_p_;
56   Link_array<Item>* stem_l_arr_p_;
57
58
59   int count_i_;
60   Moment last_add_mom_;
61   /*
62     Projected ending of the  beam we're working on.
63    */
64   Moment extend_mom_;
65   Moment beam_start_moment_;
66   Moment beam_start_location_;
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 ADD_THIS_TRANSLATOR (Auto_beam_engraver);
74
75 Auto_beam_engraver::Auto_beam_engraver ()
76 {
77   count_i_ = 0;
78   stem_l_arr_p_ = 0;
79   shortest_mom_ = Moment (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   SCM wild = gh_list (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
113   SCM function;
114   if (dir == START)
115     function = gh_list (ly_symbol2scm ("begin"), SCM_UNDEFINED);
116   else
117     function = gh_list (ly_symbol2scm ("end"), SCM_UNDEFINED);
118
119   Moment one_beat = *unsmob_moment (get_property ("beatLength"));
120   int num = *unsmob_moment (get_property ("measureLength")) / one_beat;
121   int den = one_beat.den ();
122   SCM time = gh_list (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
123
124   SCM type = gh_list (gh_int2scm (test_mom.num ()),
125                       gh_int2scm (test_mom.den ()), SCM_UNDEFINED);
126
127   SCM settings = get_property ("autoBeamSettings");
128   
129   /* first guess */
130   
131   /* begin beam at any position
132  (and fallback for end) */
133   Moment moment (0);
134   
135   /* end beam at end of beat */
136   if (dir == STOP)
137     {
138       SCM beat (get_property ("beatLength"));
139       
140       if (unsmob_moment (beat))
141         moment = *unsmob_moment (beat);
142     }
143
144   /* second guess: property generic time exception */
145   SCM m = gh_assoc (gh_append3 (function, wild, time), settings);
146   
147   if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
148     moment = * unsmob_moment (gh_cdr (m));
149
150   /* third guess: property time exception, specific for duration type */
151   m = gh_assoc (gh_append3 (function, type, time), settings);
152   if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
153     moment = * unsmob_moment (gh_cdr (m));
154
155   /* fourth guess [user override]: property plain generic */
156   m = gh_assoc (gh_append3 (function, wild, wild), settings);
157   if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
158     moment = * unsmob_moment (gh_cdr (m));
159
160   /* fifth guess [user override]: property plain, specific for duration type */
161   m = gh_assoc (gh_append3 (function, type, wild), settings);
162   if (m != SCM_BOOL_F && unsmob_moment (gh_cdr (m)))
163     moment = * unsmob_moment (gh_cdr (m));
164   
165   Rational r;
166   if (moment)
167     {
168       /* Ugh? measurePosition can be negative, when \partial
169          We may have to fix this elsewhere (timing translator)
170         r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
171       */
172       Moment pos = * unsmob_moment (get_property ("measurePosition"));
173       if (pos < Moment (0))
174         {
175           Moment length = * unsmob_moment (get_property ("measureLength"));
176           pos = length - pos;
177         }
178       r = pos.main_part_.mod_rat (moment.main_part_);
179     }
180   else
181     {
182       if (dir == START)
183         /* if undefined, starting is ok */
184         r = 0;
185       else
186         /* but ending is not */
187         r = 1;
188     }
189   return !r;
190 }
191
192 void
193 Auto_beam_engraver::consider_begin (Moment test_mom)
194 {
195   bool off = to_boolean (get_property ("noAutoBeaming"));
196   if (!stem_l_arr_p_ && ! off)
197     {
198       bool b = test_moment (START, test_mom);
199       if (b)
200         begin_beam ();
201     }
202 }
203
204 void
205 Auto_beam_engraver::consider_end (Moment test_mom)
206 {
207   if (stem_l_arr_p_)
208     {
209       /* Allow already started autobeam to end:
210          don't check for noAutoBeaming */
211       bool b = test_moment (STOP, test_mom);
212       if (b)
213         end_beam ();
214     }
215 }
216
217 Spanner*
218 Auto_beam_engraver::create_beam_p ()
219 {
220   if (to_boolean (get_property ("skipTypesetting")))
221     {
222      return 0;
223     }
224   
225   Spanner* beam_p = new Spanner (get_property ("Beam"));
226   for (int i = 0; i < stem_l_arr_p_->size (); i++)
227     {
228       /*
229         watch out for stem tremolos and abbreviation beams
230        */
231       if (Stem::beam_l ((*stem_l_arr_p_)[i]))
232         {
233           scm_gc_unprotect_object (beam_p->self_scm ());
234           return 0;
235         }
236       Beam::add_stem (beam_p, (*stem_l_arr_p_)[i]);
237     }
238   
239   announce_grob (beam_p, 0);
240
241   return beam_p;
242 }
243
244 void
245 Auto_beam_engraver::begin_beam ()
246 {
247   assert (!stem_l_arr_p_);
248   stem_l_arr_p_ = new Link_array<Item>;
249   assert (!grouping_p_);
250   grouping_p_ = new Beaming_info_list;
251   beam_start_moment_ = now_mom ();
252   beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
253
254
255 }
256
257
258 void
259 Auto_beam_engraver::junk_beam () 
260 {
261   assert (stem_l_arr_p_);
262   
263   delete stem_l_arr_p_;
264   stem_l_arr_p_ = 0;
265   delete grouping_p_;
266   grouping_p_ = 0;
267
268   shortest_mom_ = Moment (1, 8);
269 }
270
271 void
272 Auto_beam_engraver::end_beam ()
273 {
274   if (stem_l_arr_p_->size () < 2)
275     {
276       junk_beam ();
277     }
278   else
279     
280     {
281       finished_beam_p_ = create_beam_p ();
282       if (finished_beam_p_)
283         finished_grouping_p_ = grouping_p_;
284       delete stem_l_arr_p_;
285       stem_l_arr_p_ = 0;
286       grouping_p_ = 0;
287     }
288
289   shortest_mom_ = Moment (1, 8);
290 }
291
292 void
293 Auto_beam_engraver::typeset_beam ()
294 {
295   if (finished_beam_p_)
296     {
297       finished_grouping_p_->beamify ();
298       Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
299       typeset_grob (finished_beam_p_);
300       finished_beam_p_ = 0;
301     
302       delete finished_grouping_p_;
303       finished_grouping_p_= 0;
304     }
305 }
306
307 void
308 Auto_beam_engraver::start_translation_timestep ()
309 {
310   count_i_ = 0;
311   /*
312     don't beam over skips
313    */
314   if (stem_l_arr_p_)
315     {
316       Moment now = now_mom ();
317       if (extend_mom_ < now)
318         {
319           end_beam ();
320         }
321     }
322 }
323
324 void
325 Auto_beam_engraver::stop_translation_timestep ()
326 {
327   
328   typeset_beam ();
329 }
330
331 void
332 Auto_beam_engraver::finalize ()
333 {
334   /* finished beams may be typeset */
335   typeset_beam ();
336   /* but unfinished may need another announce/acknowledge pass */
337   if (stem_l_arr_p_)
338     junk_beam ();
339 }
340
341
342 void
343 Auto_beam_engraver::acknowledge_grob (Grob_info info)
344 {
345   if (stem_l_arr_p_)
346     {
347       if (Beam::has_interface (info.elem_l_))
348         {
349           end_beam ();
350         }
351       else if (Bar::has_interface (info.elem_l_))
352         {
353           end_beam ();
354         }
355       else if (Rest::has_interface (info.elem_l_))
356         {
357           end_beam ();
358         }
359     }
360   
361   if (Stem::has_interface (info.elem_l_))
362     {
363       Item* stem_l = dynamic_cast<Item *> (info.elem_l_);
364                                        
365       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
366       if (!rhythmic_req)
367         {
368           programming_error ("Stem must have rhythmic structure");
369           return;
370         }
371       
372       /*
373         Don't (start) auto-beam over empty stems; skips or rests
374         */
375       if (!Stem::heads_i (stem_l))
376         {
377           if (stem_l_arr_p_)
378             end_beam ();
379           return;
380         }
381
382       if (Stem::beam_l (stem_l))
383         {
384           if (stem_l_arr_p_)
385             junk_beam ();
386           return ;
387         }
388               
389       int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
390       
391       if (durlog <= 2)
392         {
393           if (stem_l_arr_p_)
394             end_beam ();
395           return;
396         }
397
398
399       /*
400         ignore grace notes.
401        */
402       if (bool (beam_start_location_.grace_mom_) != bool (now_mom ().grace_mom_))
403         return ;
404         
405       
406       Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
407       /* FIXME:
408
409         This comment has been here since long:
410
411            if shortest duration would change
412             consider ending and beginning beam first. 
413
414         but the code didn't match: */
415 #if 1
416       consider_end (dur);
417       consider_begin (dur);
418
419       if (dur < shortest_mom_)
420         shortest_mom_ = dur;
421 #else
422       /* I very much suspect that we wanted: */
423
424       consider_end (shortest_mom_);
425       if (dur < shortest_mom_)
426         {
427           shortest_mom_ = dur;
428           consider_end (shortest_mom_);
429         }
430       consider_begin (shortest_mom_);
431 #endif
432
433       if (!stem_l_arr_p_)
434         return;
435       
436       Moment now = now_mom ();
437       
438       grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
439                              durlog - 2);
440       stem_l_arr_p_->push (stem_l);
441       last_add_mom_ = now;
442       extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
443     }
444 }
445
446 void
447 Auto_beam_engraver::create_grobs ()
448 {
449   if (!count_i_)
450     {
451       consider_end (shortest_mom_);
452       consider_begin (shortest_mom_);
453     }
454   else if (count_i_ > 1)
455     {
456       if (stem_l_arr_p_)
457         {
458           Moment now = now_mom ();
459           if ((extend_mom_ < now)
460               || ((extend_mom_ == now) && (last_add_mom_ != now)))
461             {
462               end_beam ();
463             }
464           else if (!stem_l_arr_p_->size ())
465             {
466               junk_beam ();
467             }
468         }    
469     }
470
471   /*
472     count_i_++ -> 
473
474         auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
475   count_i_ = count_i_ + 1;
476 }