]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/auto-beam-engraver.cc
release: 1.3.107
[lilypond.git] / lily / auto-beam-engraver.cc
index a29ec75e67f420e55d831cb50f37f5bd1887e916..1ef88f13cc547dd95785aa0da3033660ac496b7b 100644 (file)
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
   
  */
 
-#include "auto-beam-engraver.hh"
+#include "beaming.hh"
 #include "musical-request.hh"
-#include "bar.hh"
 #include "beam.hh"
-#include "rhythmic-grouping.hh"
-#include "rest.hh"
 #include "stem.hh"
 #include "debug.hh"
-#include "time-description.hh"
+#include "engraver-group-engraver.hh"
+#include "bar.hh"
+#include "rest.hh"
+#include "engraver.hh"
+#include "item.hh"
+#include "spanner.hh"
+
+/*
+  TODO: figure what to do in grace?
+ */
+class Auto_beam_engraver : public Engraver
+{
+public:
+  Auto_beam_engraver ();
+  VIRTUAL_COPY_CONS (Translator);
+
+protected:
+  virtual bool do_try_music (Music*);
+  virtual void do_pre_move_processing ();
+  virtual void do_post_move_processing ();
+  virtual void do_removal_processing ();
+  virtual void acknowledge_element (Score_element_info);
+  virtual void do_process_music ();
+  virtual void process_acknowledged ();
+private:
+  void begin_beam ();
+  void consider_end_and_begin (Moment test_mom);
+  Spanner* create_beam_p ();
+  void end_beam ();
+  void junk_beam ();
+  bool same_grace_state_b (Score_element* e);
+  void typeset_beam ();
+
+  Moment shortest_mom_;
+  Spanner *finished_beam_p_;
+  Link_array<Item>* stem_l_arr_p_;
+  
+  Moment last_add_mom_;
+  Moment extend_mom_;
+  Moment beam_start_moment_;
+  Moment beam_start_location_;
+  
+  // We act as if beam were created, and start a grouping anyway.
+  Beaming_info_list*grouping_p_;  
+  Beaming_info_list*finished_grouping_p_;
+};
 
 ADD_THIS_TRANSLATOR (Auto_beam_engraver);
 
 Auto_beam_engraver::Auto_beam_engraver ()
 {
-  beam_p_ = 0;
-  mult_i_ = 0;
+  stem_l_arr_p_ = 0;
+  shortest_mom_ = Moment (1, 8);
   finished_beam_p_ = 0;
   finished_grouping_p_ = 0;
   grouping_p_ = 0;
 }
 
+bool
+Auto_beam_engraver::do_try_music (Music*) 
+{
+  return false;
+} 
+
 void
-Auto_beam_engraver::do_process_requests ()
+Auto_beam_engraver::do_process_music ()
 {
-  consider_end_and_begin ();
+  consider_end_and_begin (shortest_mom_);
 }
 
 void
-Auto_beam_engraver::consider_end_and_begin ()
+Auto_beam_engraver::consider_end_and_begin (Moment test_mom)
 {
-  Time_description const *time = get_staff_info().time_C_;
-  int num = time->whole_per_measure_ / time->one_beat_;
-  int den = time->one_beat_.den_i ();
-  String time_str = String ("time") + to_str (num) + "_" + to_str (den);
-  int type = 1 << (mult_i_ + 2);
-  String type_str = to_str (type);
+  SCM wild = gh_list (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
+  SCM b = gh_list (ly_symbol2scm ("begin"), SCM_UNDEFINED);
+  SCM e = gh_list (ly_symbol2scm ("end"), SCM_UNDEFINED);
+
+  Moment one_beat = *unsmob_moment( get_property ("beatLength"));
+  int num = *unsmob_moment (get_property("measureLength")) / one_beat;
+  int den = one_beat.den_i ();
+  SCM time = gh_list (gh_int2scm (num), gh_int2scm (den), SCM_UNDEFINED);
 
+  SCM type = gh_list (gh_int2scm (test_mom.num_i ()),
+                     gh_int2scm (test_mom.den_i ()), SCM_UNDEFINED);
+
+  SCM settings = get_property ("autoBeamSettings");
+  
   /*
     Determine end moment for auto beaming (and begin, mostly 0==anywhere) 
     In order of increasing priority:
 
-    i.   every beat <den>
-    ii.  time<num>_<den>beamAutoEnd
-    iii. time<num>_<den>beamAutoEnd<type>
-    iv.  beamAutoEnd
-    v.   beamAutoEnd<type>
+    i.   every beat
+    ii.  end   *    <num> <den>
+    iii. end <type> <num> <den>
+
+    iv.  end   *      *     *
+    v.   end <type>   *     *
 
 
     Rationale:
@@ -66,9 +122,9 @@ Auto_beam_engraver::consider_end_and_begin ()
     iv.  generic override
     v.   override for specific duration type
 
-    The user overrides should be required for common cases.
-   */
+  */
   
+
   /*
     first guess: begin beam at any position
   */
@@ -76,75 +132,76 @@ Auto_beam_engraver::consider_end_and_begin ()
   /*
     first guess: end beam at end of beat
   */
-  Moment end_mom = time->one_beat_;
+  SCM one (get_property ("beatLength"));
+
+  Moment end_mom;
+  if (unsmob_moment (one))
+    end_mom = *unsmob_moment (one);
 
   /*
     second guess: property generic time exception
   */
-  Scalar begin = get_property (time_str + "beamAutoBegin", 0);
-  if (begin.length_i ())
-    begin_mom = begin.to_rat ();
+  SCM begin = gh_assoc (gh_append3 (b, wild, time), settings);
+  
+  if (begin != SCM_BOOL_F && unsmob_moment (gh_cdr (begin)))
+    begin_mom = * unsmob_moment (gh_cdr (begin));
 
-  Scalar end = get_property (time_str + "beamAutoEnd", 0);
-  if (end.length_i ())
-    end_mom = end.to_rat ();
+  SCM end = gh_assoc (gh_append3 (e, wild, time), settings);
+  if (end != SCM_BOOL_F && unsmob_moment (gh_cdr (end)))
+    end_mom = * unsmob_moment (gh_cdr (end));
 
   /*
     third guess: property time exception, specific for duration type
   */
-  if (mult_i_)
-    {
-      Scalar end_mult = get_property (time_str + "beamAutoEnd" + type_str, 0);
-      if (end_mult.length_i ())
-       end_mom = end_mult.to_rat ();
-      Scalar begin_mult = get_property (time_str + "beamAutoBegin" + type_str, 0);
-      if (begin_mult.length_i ())
-       begin_mom = begin_mult.to_rat ();
-    }
+  SCM begin_mult = gh_assoc (gh_append3 (b, type, time), settings);
+  if (begin_mult != SCM_BOOL_F && unsmob_moment (gh_cdr (begin_mult)))
+    begin_mom = * unsmob_moment (gh_cdr (begin_mult));
+  
+  SCM end_mult = gh_assoc (gh_append3 (e, type, time), settings);
+  if (end_mult != SCM_BOOL_F && unsmob_moment (gh_cdr (end_mult)))
+    end_mom = * unsmob_moment (gh_cdr (end_mult));
 
   /*
     fourth guess [user override]: property plain generic
   */
-  begin = get_property ("beamAutoBegin", 0);
-  if (begin.length_i ())
-    begin_mom = begin.to_rat ();
-  
-  end = get_property ("beamAutoEnd", 0);
-  if (end.length_i ())
-    end_mom = end.to_rat ();
+  begin = gh_assoc (gh_append3 (b, wild, wild), settings);
+  if (begin != SCM_BOOL_F && unsmob_moment (gh_cdr (begin)))
+    begin_mom = * unsmob_moment (gh_cdr (begin));
+
+  end = gh_assoc (gh_append3 (e, wild, wild), settings);
+  if (end != SCM_BOOL_F && unsmob_moment (gh_cdr (end)))
+    end_mom = * unsmob_moment (gh_cdr (end));
 
   /*
     fifth guess [user override]: property plain, specific for duration type
   */
-  if (mult_i_)
-    {
-      Scalar end_mult = get_property (String ("beamAutoEnd") + type_str, 0);
-      if (end_mult.length_i ())
-       end_mom = end_mult.to_rat ();
-      Scalar begin_mult = get_property (String ("beamAutoBegin") + type_str, 0);
-      if (begin_mult.length_i ())
-       begin_mom = begin_mult.to_rat ();
-    }
+  begin_mult = gh_assoc (gh_append3 (b, type, wild), settings);
+  if (begin_mult != SCM_BOOL_F && unsmob_moment (gh_cdr (begin_mult)))
+    begin_mom = * unsmob_moment (gh_cdr (begin_mult));
+  
+  end_mult = gh_assoc (gh_append3 (e, type, wild), settings);
+  if (end_mult != SCM_BOOL_F && unsmob_moment (gh_cdr (end_mult)))
+    end_mom = * unsmob_moment (gh_cdr (end_mult));
 
   Rational r;
   if (end_mom)
-    r = time->whole_in_measure_.mod_rat (end_mom);
+    r = unsmob_moment (get_property ("measurePosition"))->mod_rat (end_mom);
   else
     r = Moment (1);
 
-  if (beam_p_ && !r)
+  if (stem_l_arr_p_ && !r)
     end_beam ();
      
   /*
     Allow already started autobeam to end
    */
-  Scalar on = get_property ("beamAuto", 0);
-  if (!on.to_bool ())
+  SCM on = get_property ("noAutoBeaming");
+  if (to_boolean (on))
     return;
 
   if (begin_mom)
-    r = time->whole_in_measure_.mod_rat (begin_mom);
-  if (!beam_p_ && (!begin_mom || !r))
+    r = unsmob_moment (get_property ("measurePosition"))->mod_rat (begin_mom);
+  if (!stem_l_arr_p_ && (!begin_mom || !r))
     begin_beam ();
 }
 
@@ -152,60 +209,63 @@ Auto_beam_engraver::consider_end_and_begin ()
 void
 Auto_beam_engraver::begin_beam ()
 {
-  DOUT << String ("starting autobeam at: ") + now_mom ().str () + "\n";
-  assert (!beam_p_);
-  beam_p_ = new Beam;
-  grouping_p_ = new Rhythmic_grouping;
-
-      /* urg, copied from Beam_engraver */
-  Scalar prop = get_property ("beamslopedamping", 0);
-  if (prop.isnum_b ()) 
-    beam_p_->damping_i_ = prop;
-
-  prop = get_property ("beamquantisation", 0);
-  if (prop.isnum_b ()) 
-    beam_p_->quantisation_ = (Beam::Quantisation)(int)prop;
-      // must set minVerticalAlign = = maxVerticalAlign to get sane results
-      // see input/test/beam-interstaff.ly
-  prop = get_property ("minVerticalAlign", 0);
-  if (prop.isnum_b ())
-    beam_p_->vertical_align_drul_[MIN] = prop;
-
-  prop = get_property ("maxVerticalAlign", 0);
-  if (prop.isnum_b ())
-    beam_p_->vertical_align_drul_[MAX] = prop;
-
-  announce_element (Score_element_info (beam_p_, 0));
+  assert (!stem_l_arr_p_);
+  stem_l_arr_p_ = new Link_array<Item>;
+  assert (!grouping_p_);
+  grouping_p_ = new Beaming_info_list;
+  beam_start_moment_ = now_mom ();
+  beam_start_location_ = *unsmob_moment (get_property ("measurePosition"));
+}
+
+Spanner*
+Auto_beam_engraver::create_beam_p ()
+{
+  Spanner* beam_p = new Spanner (get_property ("Beam"));
+  Beam::set_interface (beam_p);
+
+  for (int i = 0; i < stem_l_arr_p_->size (); i++)
+    {
+      /*
+       watch out for stem tremolos and abbreviation beams
+       */
+      if (Stem::beam_l ((*stem_l_arr_p_)[i]))
+       {
+         return 0;
+       }
+      Beam::add_stem (beam_p,(*stem_l_arr_p_)[i]);
+    }
+  
+  announce_element (beam_p, 0);
+
+  return beam_p;
 }
 
 void
 Auto_beam_engraver::end_beam ()
 {
-  DOUT << String ("ending autobeam at: ") + now_mom ().str () + "\n";
-  if (beam_p_->stems_.size () < 2)
+  if (stem_l_arr_p_->size () < 2)
     {
-      DOUT << "junking autombeam: less than two stems\n";
       junk_beam ();
     }
   else
     {
-      finished_beam_p_ = beam_p_;
-      finished_grouping_p_ = grouping_p_;
-      beam_p_ = 0;
+      finished_beam_p_ = create_beam_p ();
+      if (finished_beam_p_)
+       finished_grouping_p_ = grouping_p_;
+      delete stem_l_arr_p_;
+      stem_l_arr_p_ = 0;
       grouping_p_ = 0;
-      mult_i_ = 0;
+      shortest_mom_ = Moment (1, 8);
     }
 }
+
 void
 Auto_beam_engraver::typeset_beam ()
 {
   if (finished_beam_p_)
     {
-      Rhythmic_grouping const * rg_C = get_staff_info().rhythmic_C_;
-      rg_C->extend (finished_grouping_p_->interval());
-      finished_beam_p_->set_grouping (*rg_C, *finished_grouping_p_);
+      finished_grouping_p_->beamify ();
+      Beam::set_beaming (finished_beam_p_, finished_grouping_p_);
       typeset_element (finished_beam_p_);
       finished_beam_p_ = 0;
     
@@ -217,6 +277,17 @@ Auto_beam_engraver::typeset_beam ()
 void
 Auto_beam_engraver::do_post_move_processing ()
 {
+  /*
+    don't beam over skips
+   */
+  if (stem_l_arr_p_)
+    {
+      Moment now = now_mom ();
+      if (extend_mom_ < now)
+       {
+         end_beam ();
+       }
+    }
 }
 
 void
@@ -228,137 +299,134 @@ Auto_beam_engraver::do_pre_move_processing ()
 void
 Auto_beam_engraver::do_removal_processing ()
 {
+  /* finished beams may be typeset */
   typeset_beam ();
-  if (beam_p_)
-    {
-      DOUT << "Unfinished beam\n";
-      junk_beam ();
-    }
+  /* but unfinished may need another announce/acknowledge pass */
+  if (stem_l_arr_p_)
+    junk_beam ();
+}
+
+bool
+Auto_beam_engraver::same_grace_state_b (Score_element* e)
+{
+  bool gr = e->get_elt_property ("grace") == SCM_BOOL_T;
+  SCM wg =get_property ("weAreGraceContext");
+  return (to_boolean (wg)) == gr;
 }
 
 void
 Auto_beam_engraver::acknowledge_element (Score_element_info info)
 {
-  if (Beam *b = dynamic_cast<Beam *> (info.elem_l_))
+  if (!same_grace_state_b (info.elem_l_))
+    return;
+  
+  if (stem_l_arr_p_)
     {
-      if (beam_p_)
+      if (Beam::has_interface (info.elem_l_))
        {
-         DOUT << "junking autobeam: beam encountered\n";
-         junk_beam ();
+         end_beam ();
        }
-    }
-  if (Bar *b = dynamic_cast<Bar *> (info.elem_l_))
-    {
-      if (beam_p_)
+      else if (Bar::has_interface (info.elem_l_))
        {
-         DOUT << "junking autobeam: bar encountered\n";
-         junk_beam ();
+         end_beam ();
+       }
+      else if (Rest::has_interface (info.elem_l_))
+       {
+         end_beam ();
        }
     }
-
-  if (beam_p_)
+  
+  if (Stem::has_interface (info.elem_l_))
     {
+      Item* stem_l = dynamic_cast<Item *> (info.elem_l_);
+                                      
       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
       if (!rhythmic_req)
-       return;
-
-      if (dynamic_cast<Rest *> (info.elem_l_))
        {
-         DOUT << "junking autobeam: rest encountered\n";
-         end_beam ();
+         programming_error ("Stem must have rhythmic structure");
          return;
        }
-
-      Stem* stem_l = dynamic_cast<Stem *> (info.elem_l_);
-      if (!stem_l)
-       return;
-
-      if (stem_l->beam_l_ && (stem_l->beam_l_ != beam_p_))
+      
+      /*
+       Don't (start) auto-beam over empty stems; skips or rests
+       */
+      if (!Stem::heads_i (stem_l))
        {
-         DOUT << "junking autobeam: beamed stem encountered\n";
-         junk_beam ();
+         if (stem_l_arr_p_)
+           end_beam ();
          return;
        }
-       
 
-      /*
-       now that we have last_add_mom_, perhaps we can (should) do away
-       with these individual junk_beams
-       */
-      if (rhythmic_req->duration_.durlog_i_<= 2)
+      if (Stem::beam_l (stem_l))
        {
-         DOUT << "ending autobeam: stem doesn't fit in beam\n";
-         end_beam ();
-         return;
+         if (stem_l_arr_p_)
+           junk_beam ();
+         return ;
        }
-
-      Moment start = get_staff_info().time_C_->whole_in_measure_;
-      if (!grouping_p_->child_fit_b (start))
+             
+      int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->duration_log ();
+      
+      if (durlog <= 2)
        {
-         DOUT << "ending autobeam: stem doesn't fit in group\n";
-         end_beam ();
+         if (stem_l_arr_p_)
+           end_beam ();
+         return;
        }
-      else
+
+      /*
+       if shortest duration would change
+       reconsider ending/starting beam first.
+      */
+      Moment mom = unsmob_duration (rhythmic_req->get_mus_property ("duration"))->length_mom ();
+      consider_end_and_begin (mom);
+      if (!stem_l_arr_p_)
+       return;
+      if (mom < shortest_mom_)
        {
-         int m = (rhythmic_req->duration_.durlog_i_ - 2);
-         /*
-           if multiplicity would become greater,
-           reconsider ending/starting beam first.
-          */
-         if (m > mult_i_)
+         if (stem_l_arr_p_->size ())
            {
-             mult_i_ = m;
-             consider_end_and_begin ();
+             shortest_mom_ = mom;
+             consider_end_and_begin (shortest_mom_);
+             if (!stem_l_arr_p_)
+               return;
            }
-         mult_i_ = m;
-         grouping_p_->add_child (start, rhythmic_req->length_mom ());
-         stem_l->flag_i_ = rhythmic_req->duration_.durlog_i_;
-         beam_p_->add_stem (stem_l);
-         Moment now = now_mom ();
-         last_add_mom_ = now;
-         extend_mom_ = extend_mom_ >? now + rhythmic_req->length_mom ();
+         shortest_mom_ = mom;
        }
+      Moment now = now_mom ();
+      
+      grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
+                            durlog - 2);
+      stem_l_arr_p_->push (stem_l);
+      last_add_mom_ = now;
+      extend_mom_ = extend_mom_ >? now + rhythmic_req->length_mom ();
     }
 }
 
 void
 Auto_beam_engraver::junk_beam () 
 {
-  assert (beam_p_);
-#if 0
-  for (int i=0; i < beam_p_->stems_.size (); i++)
-    {
-      Stem* s = beam_p_->stems_[i];
-      s->beams_i_drul_[LEFT] = 0;
-      s->beams_i_drul_[RIGHT] = 0;
-      s->mult_i_ = 0;
-      s->beam_l_ = 0;
-    }
-#endif
+  assert (stem_l_arr_p_);
   
-  beam_p_->unlink ();
-  beam_p_ = 0;
+  delete stem_l_arr_p_;
+  stem_l_arr_p_ = 0;
   delete grouping_p_;
   grouping_p_ = 0;
-  mult_i_ = 0;
+  shortest_mom_ = Moment (1, 8);
 }
 
 void
 Auto_beam_engraver::process_acknowledged ()
 {
-  if (beam_p_)
+  if (stem_l_arr_p_)
     {
       Moment now = now_mom ();
       if ((extend_mom_ < now)
          || ((extend_mom_ == now) && (last_add_mom_ != now )))
        {
-         DOUT << String ("junking autobeam: no stem added since: ")
-           + last_add_mom_.str () + "\n";
          end_beam ();
        }
-      else if (!beam_p_->stems_.size ())
+      else if (!stem_l_arr_p_->size ())
        {
-         DOUT << "junking started autobeam: no stems\n";
          junk_beam ();
        }
     }