]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
doc fixes, rune pats
[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 "debug.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   SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
113   SCM function;
114   if (dir == START)
115     function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
116   else
117     function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
118
119   Moment one_beat = *unsmob_moment (get_property ("beatLength"));
120   int num = int ((*unsmob_moment (get_property ("measureLength")) / one_beat).main_part_);
121   int den = one_beat.den ();
122   SCM time = scm_list_n (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
123
124   SCM type = scm_list_n (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 (ly_cdr (m)))
148     moment = * unsmob_moment (ly_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 (ly_cdr (m)))
153     moment = * unsmob_moment (ly_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 (ly_cdr (m)))
158     moment = * unsmob_moment (ly_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 (ly_cdr (m)))
163     moment = * unsmob_moment (ly_cdr (m));
164   
165   Rational r;
166   if (moment.to_bool ())
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 on = to_boolean (get_property ("autoBeaming"));
196   if (!stem_l_arr_p_ && on)
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 autoBeaming */
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, SCM_EOL);
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   subdivide_beams_ = gh_scm2bool(get_property("subdivideBeams"));
254   beat_length_ = *unsmob_moment (get_property ("beatLength"));
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 (Rational (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 (Rational (1, 8));
290 }
291
292 void
293 Auto_beam_engraver::typeset_beam ()
294 {
295   if (finished_beam_p_)
296     {
297       finished_grouping_p_->beamify(beat_length_, subdivide_beams_);
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   typeset_beam ();
328 }
329
330 void
331 Auto_beam_engraver::finalize ()
332 {
333   /* finished beams may be typeset */
334   typeset_beam ();
335   /* but unfinished may need another announce/acknowledge pass */
336   if (stem_l_arr_p_)
337     junk_beam ();
338 }
339
340
341 void
342 Auto_beam_engraver::acknowledge_grob (Grob_info info)
343 {
344   if (stem_l_arr_p_)
345     {
346       if (Beam::has_interface (info.grob_l_))
347         {
348           end_beam ();
349         }
350       else if (Bar_line::has_interface (info.grob_l_))
351         {
352           end_beam ();
353         }
354       else if (Rest::has_interface (info.grob_l_))
355         {
356           end_beam ();
357         }
358     }
359   
360   if (Stem::has_interface (info.grob_l_))
361     {
362       Item* stem_l = dynamic_cast<Item *> (info.grob_l_);
363                                        
364       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.music_cause ());
365       if (!rhythmic_req)
366         {
367           programming_error ("Stem must have rhythmic structure");
368           return;
369         }
370       
371       /*
372         Don't (start) auto-beam over empty stems; skips or rests
373         */
374       if (!Stem::head_count (stem_l))
375         {
376           if (stem_l_arr_p_)
377             end_beam ();
378           return;
379         }
380
381       if (Stem::beam_l (stem_l))
382         {
383           if (stem_l_arr_p_)
384             junk_beam ();
385           return ;
386         }
387               
388       int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
389       
390       if (durlog <= 2)
391         {
392           if (stem_l_arr_p_)
393             end_beam ();
394           return;
395         }
396
397
398       /*
399         ignore grace notes.
400        */
401       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
402         return ;
403         
404       
405       Moment dur = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
406       /* FIXME:
407
408         This comment has been here since long:
409
410            if shortest duration would change
411             consider ending and beginning beam first. 
412
413         but the code didn't match: */
414 #if 1
415       consider_end (dur);
416       consider_begin (dur);
417
418       if (dur < shortest_mom_)
419         shortest_mom_ = dur;
420 #else
421       /* I very much suspect that we wanted: */
422
423       consider_end (shortest_mom_);
424       if (dur < shortest_mom_)
425         {
426           shortest_mom_ = dur;
427           consider_end (shortest_mom_);
428         }
429       consider_begin (shortest_mom_);
430 #endif
431
432       if (!stem_l_arr_p_)
433         return;
434       
435       Moment now = now_mom ();
436       
437       grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
438                              durlog - 2);
439       stem_l_arr_p_->push (stem_l);
440       last_add_mom_ = now;
441       extend_mom_ = (extend_mom_ >? now) + rhythmic_req->length_mom ();
442     }
443 }
444
445 void
446 Auto_beam_engraver::process_acknowledged_grobs ()
447 {
448   if (!count_i_)
449     {
450       consider_end (shortest_mom_);
451       consider_begin (shortest_mom_);
452     }
453   else if (count_i_ > 1)
454     {
455       if (stem_l_arr_p_)
456         {
457           Moment now = now_mom ();
458           if ((extend_mom_ < now)
459               || ((extend_mom_ == now) && (last_add_mom_ != now)))
460             {
461               end_beam ();
462             }
463           else if (!stem_l_arr_p_->size ())
464             {
465               junk_beam ();
466             }
467         }    
468     }
469
470   /*
471     count_i_++ -> 
472
473         auto-beam-engraver.cc:459: warning: value computed is not used (gcc: 2.96) */
474   count_i_ = count_i_ + 1;
475 }
476 ENTER_DESCRIPTION(Auto_beam_engraver,
477 /* descr */       "Generate beams based on measure characteristics and observed
478 Stems.  Uses beatLength, measureLength and measurePosition to decide
479 when to start and stop a beam.  Overriding beaming is done through
480 @ref{Stem_engraver} properties stemLeftBeamCount and
481 stemRightBeamCount.
482 ",
483 /* creats*/       "Beam",
484 /* acks  */       "stem-interface rest-interface beam-interface bar-line-interface",
485 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
486 /* write */       "");