]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/beaming-pattern.cc
Run `make grand-replace'.
[lilypond.git] / lily / beaming-pattern.cc
index ca9970dd8901324dbfc02d0ca21dfbb8f489ce91..c32666133ba84f9b5fbb244d4fd7a9d018070326 100644 (file)
 /*
   beaming-info.cc -- implement Beam_rhythmic_element, Beaming_pattern
 
+  A Beaming_pattern object takes a set of stems at given moments and calculates
+  the pattern of their beam. That is, it works out, for each stem, how many
+  beams should be connected to the right and left sides of that stem. In
+  calculating this, Beaming_pattern takes into account
+   - the rhythmic position of the stems
+   - the options that are defined in Beaming_options
+
   source file of the GNU LilyPond music typesetter
 
-  (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 1999--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include "beaming-pattern.hh"
 #include "context.hh"
 
+/*
+  Represents a stem belonging to a beam. Sometimes (for example, if the stem
+  belongs to a rest and stemlets aren't used) the stem will be invisible.
+
+  The rhythmic_importance_ of an element tells us the significance of the
+  moment at which this element occurs. For example, an element that occurs at
+  a beat is more significant than one that doesn't. Smaller number are
+  more important. The rhythmic_importance_ is decided and filled in by
+  Beaming_pattern. A rhythmic_importance_ smaller than zero has extra
+  significance: it represents the start of a beat and therefore beams may
+  need to be subdivided.
+*/
 Beam_rhythmic_element::Beam_rhythmic_element ()
 {
   start_moment_ = 0;
+  rhythmic_importance_ = 0;
   beam_count_drul_[LEFT] = 0;
   beam_count_drul_[RIGHT] = 0;
+  invisible_ = false;
 
 }
 
-Beam_rhythmic_element::Beam_rhythmic_element (Moment m, int i)
+Beam_rhythmic_element::Beam_rhythmic_element (Moment m, int i, bool inv)
 {
   start_moment_ = m;
+  rhythmic_importance_ = 0;
   beam_count_drul_[LEFT] = i;
   beam_count_drul_[RIGHT] = i;
+  invisible_ = inv;
 }
 
-
-int
-count_factor_twos (int x)
+void
+Beam_rhythmic_element::de_grace ()
 {
-  int c = 0;
-  while (x && x % 2 == 0)
+  if (start_moment_.grace_part_)
     {
-      x /= 2;
-      c ++;
+      start_moment_.main_part_ = start_moment_.grace_part_; 
+      start_moment_.grace_part_ = 0;
     }
-        
-  return c;
 }
 
 int
-Beaming_pattern::best_splitpoint_index (bool *at_boundary) const
+Beam_rhythmic_element::count (Direction d) const
 {
-  *at_boundary = true;
-  for (vsize i = 1; i < infos_.size (); i++)  
-    {
-      if (infos_[i].group_start_  == infos_[i].start_moment_)
-       return i;
-    }
+  return beam_count_drul_[d];
+}
 
-  for (vsize i = 1; i < infos_.size (); i++)  
+/*
+  Finds the appropriate direction for the flags at the given index that
+  hang below the neighbouring flags. If
+  the stem has no more flags than either of its neighbours, this returns
+  CENTER.
+*/
+Direction
+Beaming_pattern::flag_direction (Beaming_options const &options, vsize i) const
+{
+  // The extremal stems shouldn't be messed with, so it's appropriate to
+  // return CENTER here also.
+  if (i == 0 || i == infos_.size () - 1)
+    return CENTER;
+
+  int count = infos_[i].count (LEFT); // Both directions should still be the same
+  int left_count = infos_[i-1].count (RIGHT);
+  int right_count = infos_[i+1].count (LEFT);
+
+  // If we are told to subdivide beams and we are next to a beat, point the
+  // beamlet away from the beat.
+  if (options.subdivide_beams_)
     {
-      if (infos_[i].beat_start_  == infos_[i].start_moment_)
-       return i;
+      if (infos_[i].rhythmic_importance_ < 0)
+       return RIGHT;
+      else if (infos_[i+1].rhythmic_importance_ < 0)
+       return LEFT;
     }
 
-  *at_boundary = false;
-  
-  int min_factor_twos = INT_MAX;
-  int min_index = -1;
-  
-  Moment beat_pos;
-  for (vsize i = 1; i < infos_.size (); i++)  
-    {
-      Moment dt = infos_[i].start_moment_ - infos_[i].beat_start_;
-
-      /*
-       This is a kludge, for the most common case of 16th, 32nds
-       etc. What should really happen is that \times x/y should
-       locally introduce a voice-specific beat duration.  (or
-       perhaps: a list of beat durations for nested tuplets.)
-       
-       */
-      
-      int factor_2s =  count_factor_twos (dt.den ());
-      
-      if (factor_2s < min_factor_twos)
-       {
-         min_factor_twos = factor_2s;
-         min_index = i;
-       }
-    }
+  if (count <= left_count && count <= right_count)
+    return CENTER;
+
+  // Try to avoid sticking-out flags as much as possible by pointing my flags
+  // at the neighbour with the most flags.
+  else if (right_count > left_count)
+    return RIGHT;
+  else if (left_count > right_count)
+    return LEFT;
 
-  return min_index;
+  // If all else fails, point the beamlet away from the important moment.
+  return (infos_[i].rhythmic_importance_ <= infos_[i+1].rhythmic_importance_) ? RIGHT : LEFT;
 }
 
-int
-Beaming_pattern::beam_extend_count (Direction d) const
+void
+Beaming_pattern::de_grace ()
 {
-  if (infos_.size () == 1)
-    return infos_[0].beam_count_drul_[d];
-
-  Beam_rhythmic_element thisbeam = boundary (infos_, d, 0);
-  Beam_rhythmic_element next = boundary (infos_, d, 1);
-
-  return min (thisbeam.beam_count_drul_[-d], next.beam_count_drul_[d]);
+  for (vsize i = 0; i < infos_.size (); i ++)
+    {
+      infos_[i].de_grace ();
+    }
 }
 
 void
-Beaming_pattern::beamify (Context *context)
+Beaming_pattern::beamify (Beaming_options const &options)
 {
+  unbeam_invisible_stems ();
+
   if (infos_.size () <= 1)
     return;
-  
-  bool subdivide_beams = to_boolean (context->get_property ("subdivideBeams"));
-  Moment beat_length = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
-  Moment measure_length = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
+
+  if (infos_[0].start_moment_.grace_part_)
+    de_grace ();
 
   if (infos_[0].start_moment_ < Moment (0))
-    for (vsize i = 0; i < infos_.size(); i++)
-      infos_[i].start_moment_ += measure_length;
-  
-  SCM grouping = context->get_property ("beatGrouping");
-  Moment measure_pos (0);
-  
-  vector<Moment> group_starts;
-  vector<Moment> beat_starts;
-  
-  while (measure_pos <= infos_.back().start_moment_)
+    for (vsize i = 0; i < infos_.size (); i++)
+      infos_[i].start_moment_ += options.measure_length_;
+
+  find_rhythmic_importance (options);
+
+  for (vsize i = 1; i < infos_.size () - 1; i++)
     {
+      Direction non_flag_dir = other_dir (flag_direction (options, i));
+      if (non_flag_dir)
+       {
+         int importance = (non_flag_dir == LEFT)
+           ? infos_[i].rhythmic_importance_ : infos_[i+1].rhythmic_importance_;
+         int count = (importance < 0 && options.subdivide_beams_)
+           ? 1 : min (infos_[i].count (non_flag_dir),
+                      infos_[i+non_flag_dir].count (-non_flag_dir));
+
+         infos_[i].beam_count_drul_[non_flag_dir] = count;
+       }
+    }
+}
+
+void
+Beaming_pattern::find_rhythmic_importance (Beaming_options const &options)
+{
+  Moment measure_pos (0);  
+  SCM grouping = options.grouping_;
+  vsize i = 0;
+
+  // Mark the importance of stems that start at a beat or a beat group.
+  while (i < infos_.size ())
+    {
+      // If a beat grouping is not specified, default to 2 beats per group.
       int count = 2;
       if (scm_is_pair (grouping))
        {
@@ -125,82 +165,69 @@ Beaming_pattern::beamify (Context *context)
          grouping = scm_cdr (grouping);
        }
 
-      group_starts.push_back (measure_pos);
-      for (int i = 0; i < count; i++)
+      // Mark the start of this beat group
+      if (infos_[i].start_moment_ == measure_pos)
+       infos_[i].rhythmic_importance_ = -2;
+
+      // Mark the start of each beat up to the end of this beat group.
+      for (int beat = 1; beat <= count; beat++)
        {
-         beat_starts.push_back (measure_pos + beat_length * i);
+         Moment next_measure_pos = measure_pos + options.beat_length_;
+
+         while (i < infos_.size () && infos_[i].start_moment_ < next_measure_pos)
+           {
+             Moment dt = infos_[i].start_moment_ - measure_pos;
+
+             // The rhythmic importance of a stem between beats depends on its fraction
+             // of a beat: those stems with a lower denominator are deemed more
+             // important.
+             // FIXME: This is not the right way to do things for tuplets. For example,
+             // in an 8th-note triplet with a quarter-note beat, 1/3 of a beat should be
+             // more important than 1/2.
+             if (infos_[i].rhythmic_importance_ >= 0)
+               infos_[i].rhythmic_importance_ = (dt / options.beat_length_).den ();
+
+             i++;
+           }
+
+         measure_pos = next_measure_pos;
+         if (i < infos_.size () && infos_[i].start_moment_ == measure_pos)
+           infos_[i].rhythmic_importance_ = -1;
        }
-      measure_pos += beat_length * count;
-    }
-   
-  vsize j = 0;
-  vsize k = 0;
-  for (vsize i = 0; i  < infos_.size(); i++)
-    {
-      while (j < group_starts.size() - 1
-            && group_starts[j+1] <= infos_[i].start_moment_)
-       j++;
-
-      infos_[i].group_start_ = group_starts[j];
-
-      while (k < beat_starts.size() - 1
-            && beat_starts[k+1] <= infos_[i].start_moment_)
-       k++;
-
-      infos_[i].beat_start_ = beat_starts[k];
     }
-  
-  beamify (subdivide_beams);
 }
 
 
+/*
+  Invisible stems should be treated as though they have the same number of
+  beams as their least-beamed neighbour. Here we go through the stems and
+  modify the invisible stems to satisfy this requirement.
+*/
 void
-Beaming_pattern::beamify (bool subdivide_beams)
+Beaming_pattern::unbeam_invisible_stems ()
 {
-  if (infos_.size () <= 1)
-    return;
-  
-  Drul_array<Beaming_pattern> splits;
-
-  bool at_boundary = false;
-  int m = best_splitpoint_index (&at_boundary);
-
-  splits[LEFT].infos_ = vector<Beam_rhythmic_element> (infos_.begin (),
-                                             infos_.begin () + m);
-  splits[RIGHT].infos_ = vector<Beam_rhythmic_element> (infos_.begin () + m,
-                                              infos_.end ());
-
-  Direction d = LEFT;
-
-  do
-    {
-      splits[d].beamify (subdivide_beams);
-    }
-  while (flip (&d) != LEFT);
-
-  int middle_beams = ((at_boundary && subdivide_beams)
-                     ? 1       
-                     : min (splits[RIGHT].beam_extend_count (LEFT),
-                            splits[LEFT].beam_extend_count (RIGHT)));
-
-  do
-    {
-      if (splits[d].infos_.size () != 1)
-       boundary (splits[d].infos_, -d, 0).beam_count_drul_[-d] = middle_beams;
-    }
-  while (flip (&d) != LEFT);
-
-  infos_ = splits[LEFT].infos_;
-  infos_.insert (infos_.end (),
-                splits[RIGHT].infos_.begin (),
-                splits[RIGHT].infos_.end ());
+  for (vsize i = 1; i < infos_.size (); i++)
+    if (infos_[i].invisible_)
+      {
+       int b = min (infos_[i].count (LEFT), infos_[i-1].count (LEFT));
+       infos_[i].beam_count_drul_[LEFT] = b;
+       infos_[i].beam_count_drul_[RIGHT] = b;
+      }
+
+  for (vsize i = infos_.size (); i--;)
+    if (infos_[i].invisible_)
+      {
+       int b = min (infos_[i].count (LEFT), infos_[i+1].count (LEFT));
+       infos_[i].beam_count_drul_[LEFT] = b;
+       infos_[i].beam_count_drul_[RIGHT] = b;
+      }
 }
 
 
 void
-Beaming_pattern::add_stem (Moment m, int b)
+Beaming_pattern::add_stem (Moment m, int b, bool invisible)
 {
-  infos_.push_back (Beam_rhythmic_element (m, b));
+  infos_.push_back (Beam_rhythmic_element (m, b, invisible));
 }
 
 Beaming_pattern::Beaming_pattern ()
@@ -212,3 +239,18 @@ Beaming_pattern::beamlet_count (int i, Direction d) const
 {
   return infos_.at (i).beam_count_drul_[d];
 }
+
+void
+Beaming_options::from_context (Context *context)
+{
+  grouping_ = context->get_property ("beatGrouping");
+  subdivide_beams_ = to_boolean (context->get_property ("subdivideBeams"));
+  beat_length_ = robust_scm2moment (context->get_property ("beatLength"), Moment (1, 4));
+  measure_length_ = robust_scm2moment (context->get_property ("measureLength"), Moment (1, 4));
+}
+
+Beaming_options::Beaming_options ()
+{
+  grouping_ = SCM_EOL;
+  subdivide_beams_ = false;
+}