]> git.donarmstrong.com Git - lilypond.git/commitdiff
Cleanups for pitches and scales.
authorJoe Neeman <joeneeman@gmail.com>
Tue, 12 Aug 2008 12:18:52 +0000 (22:18 +1000)
committerJoe Neeman <joeneeman@gmail.com>
Tue, 19 Aug 2008 17:49:43 +0000 (10:49 -0700)
Much of this code was merged from dev/rune.

lily/include/pitch.hh
lily/include/scale.hh
lily/pitch.cc
lily/scale.cc

index e711fc0d579b138b91355414bf3b91d3e8134d4c..4ab7cb5ea5734d41358ec341fe110367b4040f40 100644 (file)
@@ -33,6 +33,8 @@ private:
   void transpose (Pitch);
   void up_to (int);
   void down_to (int);
+  void normalize_octave ();
+  void normalize_alteration ();
   void normalize ();
 
 public:
index ff8e6b2b7f332986e8d1e0df526695de25692d85..e74317effebf77c5f63159a3591d2e38dd4c8151 100644 (file)
 
 struct Scale
 {
-  vector<Rational> step_tones_;
-  Scale ();
+public:
+  Scale (vector<Rational> const&);
   Scale (Scale const&);
+
+  Rational tones_at_step (int step, int octave = 0) const;
+  Rational step_size (int step) const;
+  int step_count () const;
+  int normalize_step (int step) const;
+
   DECLARE_SMOBS (Scale);
+
+private:
+  vector<Rational> step_tones_;
 };
 
 extern Scale *default_global_scale;
index bc8a9e45a6cd9b8467ecc3af1344deed84da3998..fa1a45089c1eee6a0a9578c372f073b1117423a4 100644 (file)
@@ -22,7 +22,7 @@ Pitch::Pitch (int o, int n, Rational a)
   alteration_ = a;
   octave_ = o;
   scale_ = default_global_scale; 
-  normalize ();
+  normalize_octave ();
 }
 
 /* FIXME: why is octave == 0 and default not middleC ? */
@@ -53,30 +53,13 @@ Pitch::compare (Pitch const &m1, Pitch const &m2)
 int
 Pitch::steps () const
 {
-  return notename_ + octave_ * scale_->step_tones_.size ();
+  return notename_ + octave_ * scale_->step_count ();
 }
 
 Rational
 Pitch::tone_pitch () const
 {
-  int o = octave_;
-  int n = notename_;
-  while (n < 0)
-    {
-      n += scale_->step_tones_.size ();
-      o--;
-    }
-
-  /*
-    we're effictively hardcoding the octave to 6 whole-tones,
-    which is as arbitrary as coding it to 1200 cents
-  */
-  Rational tones ((o + n / scale_->step_tones_.size ()) * 6, 1);
-  tones += scale_->step_tones_[n % scale_->step_tones_.size ()];
-
-  tones += alteration_;
-  
-  return tones;
+  return scale_->tones_at_step (notename_, octave_) + alteration_;
 }
 
 /* Calculate pitch height in 12th octave steps.  Don't assume
@@ -94,50 +77,38 @@ Pitch::rounded_quartertone_pitch () const
 }
 
 void
-Pitch::normalize ()
+Pitch::normalize_octave ()
 {
-  Rational pitch = tone_pitch ();
-  while (notename_ >= (int) scale_->step_tones_.size ())
-    {
-      notename_ -= scale_->step_tones_.size ();
-      octave_++;
-      alteration_ -= tone_pitch () - pitch;
-    }
-  while (notename_ < 0)
-    {
-      notename_ += scale_->step_tones_.size ();
-      octave_--;
-      alteration_ -= tone_pitch () - pitch;
-    }
+  int normalized_step = notename_ % scale_->step_count ();
+  if (normalized_step < 0)
+    normalized_step += scale_->step_count ();
 
+  octave_ += (notename_ - normalized_step) / scale_->step_count ();
+  notename_ = normalized_step;
+}
+
+void
+Pitch::normalize_alteration ()
+{
   while (alteration_ > Rational (1))
     {
-      if (notename_ == int (scale_->step_tones_.size ()))
-       {
-         notename_ = 0;
-         octave_++;
-       }
-      else
-       notename_++;
-
-      alteration_ = Rational (0);
-      alteration_ -= tone_pitch () - pitch;
+      alteration_ -= scale_->step_size (notename_);
+      notename_++;
     }
   while (alteration_ < Rational (-1))
     {
-      if (notename_ == 0)
-       {
-         notename_ = scale_->step_tones_.size ();
-         octave_--;
-       }
-      else
-       notename_--;
-
-      alteration_ = 0;
-      alteration_ -= tone_pitch () - pitch;
+      notename_--;
+      alteration_ += scale_->step_size (notename_);
     }
 }
 
+void
+Pitch::normalize ()
+{
+  normalize_alteration ();
+  normalize_octave ();
+}
+
 void
 Pitch::transpose (Pitch delta)
 {
@@ -147,7 +118,7 @@ Pitch::transpose (Pitch delta)
   notename_ += delta.notename_;
   alteration_ += new_alter - tone_pitch ();
 
-  normalize ();
+  normalize_octave ();
 }
 
 Pitch
@@ -170,7 +141,7 @@ char const *accname[] = {"eses", "eseh", "es", "eh", "",
 string
 Pitch::to_string () const
 {
-  int n = (notename_ + 2) % scale_->step_tones_.size ();
+  int n = (notename_ + 2) % scale_->step_count ();
   string s = ::to_string (char (n + 'a'));
   Rational qtones = alteration_ * Rational (4,1);
   int qt = int (rint (Real (qtones)));
index 5d8ccc9a38f9edb7467a9f53f9dc78c7705c6b77..24abf335bc211daa7b731381edaec58535065df9 100644 (file)
@@ -4,7 +4,8 @@
   source file of the GNU LilyPond music typesetter
   
   (c) 2006--2007 Han-Wen Nienhuys <hanwen@lilypond.org>
-  
+      2007--2008 Rune Zedeler
+      2008       Joe Neeman <joeneeman@gmail.com>
 */
 
 #include "scale.hh"
 */
 LY_DEFINE (ly_make_scale, "ly:make-scale",
           1, 0, 0, (SCM steps),
-          "Create a scale.  Takes a vector of integers as argument.")
+          "Create a scale. "
+          "The argument is a vector of natural numbers, each of which "
+          "represents the number of tones of a pitch above the tonic.")
 {
   bool type_ok = scm_is_vector (steps);
 
-  vector<Rational> semitones; 
+  vector<Rational> tones; 
   if (type_ok)
     {
       int len = scm_c_vector_length (steps);
       for (int i = 0 ; i < len; i++)
        {
-         SCM step = scm_c_vector_ref (steps, i);
+         SCM step = scm_c_vector_ref (steps, i);
          type_ok = type_ok && scm_is_rational (step);
          if (type_ok)
            {
              Rational from_c (scm_to_int (scm_numerator (step)),
                               scm_to_int (scm_denominator (step)));
-             semitones.push_back (from_c);
+             tones.push_back (from_c);
            }
        }
     }
   
-  SCM_ASSERT_TYPE (type_ok, steps, SCM_ARG1, __FUNCTION__, "vector of int");
+  
+  SCM_ASSERT_TYPE (type_ok, steps, SCM_ARG1, __FUNCTION__, "vector of rational");
 
-  Scale *s = new Scale;
-  s->step_tones_ = semitones;
+  Scale *s = new Scale (tones);
 
   SCM retval =  s->self_scm ();
-
   s->unprotect ();
   
   return retval;
@@ -77,6 +79,46 @@ LY_DEFINE (ly_set_default_scale, "ly:set-default-scale",
   return SCM_UNSPECIFIED;
 }
 
+int
+Scale::step_count () const
+{
+  return step_tones_.size ();
+}
+
+Rational
+Scale::tones_at_step (int step, int octave) const
+{
+  int normalized_step = normalize_step (step);
+
+  octave += (step - normalized_step) / step_count ();
+
+  // There are 6 tones in an octave.
+  return step_tones_[normalized_step] + Rational (octave*6);
+}
+
+Rational
+Scale::step_size (int step) const
+{
+  int normalized_step = normalize_step (step);
+
+  // Wrap around if we are asked for the final note of the
+  // scale (6 is the number of tones of the octave above the
+  // first note).
+  if (normalized_step + 1 == step_count ())
+    return Rational(6) - step_tones_[normalized_step];
+
+  return step_tones_[normalized_step + 1] - step_tones_[normalized_step];
+}
+
+int
+Scale::normalize_step (int step) const
+{
+  int ret = step % step_count ();
+  if (ret < 0)
+    ret += step_count ();
+
+  return ret;
+}
 
 int
 Scale::print_smob (SCM x, SCM port, scm_print_state *)
@@ -95,8 +137,10 @@ Scale::mark_smob (SCM x)
   return SCM_UNSPECIFIED;
 }
 
-Scale::Scale ()
+Scale::Scale (vector<Rational> const &tones)
 {
+  step_tones_ = tones;
+
   smobify_self ();
 }