]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beam.cc
release: 1.2.13
[lilypond.git] / lily / beam.cc
index 53a5eb34e9176694d0d73f4326c0bb704144c394..92a92050a2fc019dcc24bce56191e284d71070c3 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--1999, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
     Jan Nieuwenhuizen <janneke@gnu.org>
 
 */
@@ -24,6 +24,8 @@ needs what, and what information should be available when.
 
 #include <math.h>
 
+#include "chord-tremolo.hh"
+#include "beaming.hh"
 #include "proto.hh"
 #include "dimensions.hh"
 #include "beam.hh"
@@ -34,7 +36,6 @@ needs what, and what information should be available when.
 #include "stem.hh"
 #include "paper-def.hh"
 #include "lookup.hh"
-#include "rhythmic-grouping.hh"
 
 Beam::Beam ()
 {
@@ -48,13 +49,20 @@ void
 Beam::add_stem (Stem*s)
 {
 #if 0
+  /*
+    should figure out why this didn't work.
+
+    --hwn.
+   */
   if (!stems_.size ())
     {
-      dim_cache_[Y_AXIS].parent_l_ = &s->dim_cache_[Y_AXIS];
+      set_parent (s, Y_AXIS);
     }
 #endif
   stems_.push (s);
   s->add_dependency (this);
+
+  assert (!s->beam_l_);
   s->beam_l_ = this;
 
   if (!spanned_drul_[LEFT])
@@ -97,13 +105,7 @@ Beam::do_brew_molecule_p () const
       mol_p->add_molecule (sb);
     }
   mol_p->translate_axis (x0 
-    - spanned_drul_[LEFT]->absolute_coordinate (X_AXIS), X_AXIS);
-
-  // correct if last note (and therefore reference point of beam)
-  // is on different staff
-  Stem_info si = sinfo_.top ();
-  mol_p->translate_axis (-si.interstaff_f_ * si.stem_l_->staff_line_leading_f ()/2,
-                        Y_AXIS);
+    - spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS), X_AXIS);
 
   return mol_p;
 }
@@ -118,18 +120,81 @@ Beam::center () const
                 si.stem_l_->staff_line_leading_f ()/2);
 }
 
+/*
+  Simplistic auto-knees; only consider vertical gap between two
+  adjacent chords
+ */
+bool
+Beam::auto_knee (SCM gap, bool interstaff_b)
+{
+  bool knee = false;
+  int knee_y = 0;
+  Real internote_f = stems_[0]->staff_line_leading_f ()/2;
+  if (gap != SCM_BOOL_F)
+    {
+      int auto_gap_i = gh_scm2int (SCM_CDR (gap));
+      for (int i=1; i < stems_.size (); i++)
+        {
+         bool is_b = (bool)(sinfo_[i].interstaff_f_ - sinfo_[i-1].interstaff_f_);
+         int l_y = (int)(stems_[i-1]->chord_start_f () / internote_f)
+           + (int)sinfo_[i-1].interstaff_f_;
+         int r_y = (int)(stems_[i]->chord_start_f () / internote_f)
+           + (int)sinfo_[i].interstaff_f_;
+         int gap_i = r_y - l_y;
+
+         /*
+           Forced stem directions are ignored.  If you don't want auto-knees,
+           don't set, or unset autoKneeGap/autoInterstaffKneeGap.
+          */
+         if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
+           {
+             knee_y = (r_y + l_y) / 2;
+             knee = true;
+             break;
+           }
+       }
+    }
+  if (knee)
+    {
+      for (int i=0; i < stems_.size (); i++)
+        {
+         int y = (int)(stems_[i]->chord_start_f () / internote_f)
+           + (int)sinfo_[i].interstaff_f_;
+         stems_[i]->dir_ = y < knee_y ? UP : DOWN;
+         stems_[i]->set_elt_property (dir_forced_scm_sym, SCM_BOOL_T);
+       }
+    }
+  return knee;
+}
+
+bool
+Beam::auto_knees ()
+{
+  if (auto_knee (get_elt_property (auto_interstaff_knee_gap_scm_sym), true))
+    return true;
+  
+  return auto_knee (get_elt_property (auto_knee_gap_scm_sym), false);
+}
+
+
 void
 Beam::do_pre_processing ()
 {
+  /*
+    urg: it seems that info on whether beam (voice) dir was forced
+         is being junked here?
+  */
   if (!dir_)
-    set_default_dir ();
+    dir_ = get_default_dir ();
+  
+  set_direction (dir_);
 }
 
 void
 Beam::do_print () const
 {
 #ifndef NPRINT
-  DOUT << "slope_f_ " << slope_f_ << "left ypos " << left_y_;
+  DEBUG_OUT << "slope_f_ " << slope_f_ << "left ypos " << left_y_;
   Spanner::do_print ();
 #endif
 }
@@ -141,7 +206,25 @@ Beam::do_post_processing ()
     {
       warning (_ ("beam with less than two stems"));
       set_elt_property (transparent_scm_sym, SCM_BOOL_T);
-      return ;
+      return;
+    }
+  set_steminfo ();
+  if (auto_knees ())
+    {
+      /*
+       if auto-knee did its work, most probably stem directions
+       have changed, so we must recalculate all.
+       */
+      dir_ = get_default_dir ();
+      set_direction (dir_);
+
+      /* auto-knees used to only work for slope = 0
+        anyway, should be able to set slope per beam
+         set_elt_property (damping_scm_sym, gh_int2scm(1000));
+      */
+
+      sinfo_.clear ();
+      set_steminfo ();
     }
   calculate_slope ();
   set_stemlens ();
@@ -162,15 +245,15 @@ Beam::do_width () const
                   stems_.top ()->hpos_f ());
 }
 
-void
-Beam::set_default_dir ()
+Direction
+Beam::get_default_dir () const
 {
   Drul_array<int> total;
   total[UP]  = total[DOWN] = 0;
   Drul_array<int> count; 
   count[UP]  = count[DOWN] = 0;
   Direction d = DOWN;
-  
+
   for (int i=0; i <stems_.size (); i++)
     do {
       Stem *s = stems_[i];
@@ -194,38 +277,56 @@ Beam::set_default_dir ()
      wants to provide some real simple hands-on rules.
      
      We have our doubts, so we simply provide all sensible alternatives.
+
+     If dir is not determined: up (see stem::get_default_dir ())
   */
 
+  Direction beam_dir;
+  Direction neutral_dir = (Direction)(int)paper_l ()->get_var ("stem_default_neutral_direction");
+
   Dir_algorithm a = (Dir_algorithm)rint(paper_l ()->get_var ("beam_dir_algorithm"));
   switch (a)
     {
     case MAJORITY:
-      dir_ = (count[UP] > count[DOWN]) ? UP : DOWN;
+      beam_dir = (count[UP] == count[DOWN]) ? neutral_dir 
+        : (count[UP] > count[DOWN]) ? UP : DOWN;
       break;
     case MEAN:
-      // mean centre distance
-      dir_ = (total[UP] > total[DOWN]) ? UP : DOWN;
+      // mean center distance
+      beam_dir = (total[UP] == total[DOWN]) ? neutral_dir
+        : (total[UP] > total[DOWN]) ? UP : DOWN;
       break;
     default:
     case MEDIAN:
-      // median centre distance
-      if (!count[UP])
-       dir_ = DOWN;
-      else if (!count[DOWN])
-       dir_ = UP;
+      // median center distance
+      if (!count[DOWN] || !count[UP])
+        {
+         beam_dir = (count[UP] == count[DOWN]) ? neutral_dir 
+           : (count[UP] > count[DOWN]) ? UP : DOWN;
+       }
       else
-       dir_ = (total[UP] / count[UP] > total[DOWN] / count[DOWN]) ? UP : DOWN;
+        {
+         beam_dir = (total[UP] / count[UP] == total[DOWN] / count[DOWN]) 
+           ? neutral_dir 
+             : (total[UP] / count[UP] > total[DOWN] / count[DOWN]) ? UP : DOWN;
+       }
       break;
     }
+  return beam_dir;
+}
 
+void
+Beam::set_direction (Direction d)
+{
+  dir_ = d;
   for (int i=0; i <stems_.size (); i++)
     {
       Stem *s = stems_[i];
-      s->set_elt_property (beam_dir_scm_sym, gh_int2scm (dir_));
+      s->set_elt_property (beam_dir_scm_sym, gh_int2scm (d));
 
       SCM force = s->remove_elt_property (dir_forced_scm_sym);
       if (force == SCM_BOOL_F)
-       s->dir_ = dir_;
+       s->dir_ = d;
     }
 }
 
@@ -237,7 +338,7 @@ void
 Beam::solve_slope ()
 {
   assert (sinfo_.size () > 1);
-  DOUT << "Beam::solve_slope: \n";
+  DEBUG_OUT << "Beam::solve_slope: \n";
 
   Least_squares l;
   for (int i=0; i < sinfo_.size (); i++)
@@ -247,6 +348,10 @@ Beam::solve_slope ()
   l.minimise (slope_f_, left_y_);
 }
 
+/*
+  ugh. Naming: this doesn't check, but sets as well.
+ */
+  
 Real
 Beam::check_stemlengths_f (bool set_b)
 {
@@ -282,8 +387,8 @@ Beam::check_stemlengths_f (bool set_b)
        { 
          // when all too short, normal stems win..
          if (dy_f < -epsilon_f)
-           warning (_ ("weird beam shift, check your knees"));
-         dy_f = dy_f >? sinfo_[i].miny_f_ - y;
+           warning (_ ("weird beam vertical offset"));
+         dy_f = dy_f >? sinfo_[i].miny_f_ - y; 
        }
     }
   return dy_f;
@@ -296,6 +401,7 @@ Beam::set_steminfo ()
     return;
   
   assert (multiple_i_);
+
   int total_count_i = 0;
   int forced_count_i = 0;
   for (int i=0; i < stems_.size (); i++)
@@ -310,18 +416,24 @@ Beam::set_steminfo ()
       total_count_i++;
     }
 
-  Real internote_f = stems_[0]->staff_line_leading_f ()/2;
+  bool grace_b = get_elt_property (grace_scm_sym) != SCM_BOOL_F;
+  String type_str = grace_b ? "grace_" : "";
   int stem_max = (int)rint(paper_l ()->get_var ("stem_max"));
-  Real shorten_f = paper_l ()->get_var (String ("forced_stem_shorten"
-                                             + to_str (multiple_i_ <? stem_max)))
-    / internote_f;
+  Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten"
+                                       + to_str (multiple_i_ <? stem_max));
     
   Real leftx = 0;
   for (int i=0; i < stems_.size (); i++)
     {
       Stem *s = stems_[i];
-      if (s->invisible_b ())
-       continue;
+      /*
+       Chord tremolo needs to beam over invisible stems of wholes
+      */
+      if (!dynamic_cast<Chord_tremolo*> (this))
+       {
+         if (s->invisible_b ())
+           continue;
+       }
 
       Stem_info info (s, multiple_i_);
       if (leftx == 0)
@@ -341,7 +453,6 @@ Beam::set_steminfo ()
 void
 Beam::calculate_slope ()
 {
-  set_steminfo ();
   if (!sinfo_.size ())
     slope_f_ = left_y_ = 0;
   else if (sinfo_[0].idealy_f_ == sinfo_.top ().idealy_f_)
@@ -431,9 +542,9 @@ Beam::quantise_dy ()
 
 
   Interval iv = quantise_iv (allowed_fraction, interline_f, dy_f);
-  quanty_f = (dy_f - iv.min () <= iv.max () - dy_f)
-    ? iv.min ()
-    : iv.max ();
+  quanty_f = (dy_f - iv[SMALLER] <= iv[BIGGER] - dy_f)
+    ? iv[SMALLER]
+    : iv[BIGGER];
 
 
   slope_f_ = (quanty_f / dx_f) / internote_f * sign (slope_f_);
@@ -548,9 +659,9 @@ Beam::quantise_left_y (bool extend_b)
 
   Interval iv = quantise_iv (allowed_position, space, dy_f);
 
-  Real quanty_f = dy_f - iv.min () <= iv.max () - dy_f ? iv.min () : iv.max ();
+  Real quanty_f = dy_f - iv[SMALLER] <= iv[BIGGER] - dy_f ? iv[SMALLER] : iv[BIGGER];
   if (extend_b)
-    quanty_f = iv.max ();
+    quanty_f = iv[BIGGER];
 
   // dim(left_y_) = internote
   left_y_ = dir_ * quanty_f / internote_f;
@@ -563,7 +674,8 @@ Beam::set_stemlens ()
   // enge floots
   Real epsilon_f = staffline_f / 8;
 
-  DOUT << "Beam::set_stemlens: \n";
+
+  // je bent zelf eng --hwn.
   Real dy_f = check_stemlengths_f (false);
   for (int i = 0; i < 2; i++)
     { 
@@ -572,7 +684,6 @@ Beam::set_stemlens ()
       dy_f = check_stemlengths_f (true);
       if (abs (dy_f) <= epsilon_f)
         {
-         DOUT << "Beam::set_stemlens: " << i << " iterations\n";
          break;
        }
     }
@@ -581,60 +692,51 @@ Beam::set_stemlens ()
   test_pos %= 4;
 }
 
-/*
- FIXME
- ugh.  this is broken and should be rewritten.
-  - [c8. c32 c32]
- */
 void
-Beam::set_grouping (Rhythmic_grouping def, Rhythmic_grouping cur)
+Beam::set_beaming (Beaming_info_list *beaming)
+{
+  Direction d = LEFT;
+  for (int i=0; i  < stems_.size (); i++)
+    {
+      do
+       {
+         if (stems_[i]->beams_i_drul_[d] < 0)
+           stems_[i]->beams_i_drul_[d] = beaming->infos_.elem (i).beams_i_drul_[d];
+       }
+      while (flip (&d) != LEFT);
+    }
+}
+
+
+void
+Beam::do_add_processing ()
 {
-  def.OK ();
-  cur.OK ();
-  assert (cur.children.size () == stems_.size ());
-
-  cur.split (def);
-
-  Array<int> b;
-  {
-    Array<int> flags;
-    for (int j=0; j <stems_.size (); j++)
-      {
-       Stem *s = stems_[j];
-
-       int f = s->flag_i_ - 2;
-       assert (f>0);
-       flags.push (f);
-      }
-    int fi =0;
-    b= cur.generate_beams (flags, fi);
-    b.insert (0,0);
-    b.push (0);
-    assert (stems_.size () == b.size ()/2);
-  }
-
-  for (int j=0, i=0; i < b.size () && j <stems_.size (); j++)
+  for (int i=0; i < stems_.size () ; i++) 
     {
-      Stem *s = stems_[j];
       Direction d = LEFT;
       do {
-       if (s->beams_i_drul_[d] < 0)
-         s->beams_i_drul_[d] = b[i];
-
-       multiple_i_ = multiple_i_ >? s->beams_i_drul_[d];
-       i++;
+       multiple_i_ = multiple_i_ >? stems_[i]->beams_i_drul_[d];
       } while ((flip (&d)) != LEFT);
     }
+
+  if (stems_.size ())
+    {
+      stems_[0]->beams_i_drul_[LEFT] =0;
+      stems_.top()->beams_i_drul_[RIGHT] =0;
+    }
 }
 
+
+
 /*
   beams to go with one stem.
   */
 Molecule
 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
 {
-  assert (!next || next->hpos_f () > here->hpos_f ());
-  assert (!prev || prev->hpos_f () < here->hpos_f ());
+  if ((next && !(next->hpos_f () > here->hpos_f ())) ||
+      (prev && !(prev->hpos_f () < here->hpos_f ())))
+      programming_error ("Beams are not left-to-right");
 
   Real staffline_f = paper_l ()->rule_thickness ();
   Real interbeam_f = paper_l ()->interbeam_f (multiple_i_);
@@ -651,7 +753,15 @@ Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
   Molecule rightbeams;
 
   // UGH
-  Real nw_f = paper_l ()->note_width () * 0.8;
+  Real nw_f;
+  if (!here->head_l_arr_.size ())
+    nw_f = 0;
+  else if (here->type_i ()== 1)
+    nw_f = paper_l ()->get_var ("wholewidth");
+  else if (here->type_i () == 2)
+    nw_f = paper_l ()->note_width () * 0.8;
+  else
+    nw_f = paper_l ()->get_var ("quartwidth");
 
   /* half beams extending to the left. */
   if (prev)
@@ -690,7 +800,7 @@ Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
       SCM gap = get_elt_property (beam_gap_scm_sym);
       if (gap != SCM_BOOL_F)
        {
-         int gap_i = gh_scm2int (gap);
+         int gap_i = gh_scm2int (SCM_CDR (gap));
          int nogap = rwholebeams - gap_i;
          
          for (; j  < nogap; j++)
@@ -708,7 +818,10 @@ Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
       for (; j  < rwholebeams; j++)
        {
          Molecule b (a);
-         b.translate (Offset (gap_f, -dir_ * dy * j));
+         if (!here->invisible_b ())
+           b.translate (Offset (gap_f, -dir_ * dy * j));
+         else
+           b.translate (Offset (0, -dir_ * dy * j));
          rightbeams.add_molecule (b);
        }