Much of this code was merged from dev/rune.
void transpose (Pitch);
void up_to (int);
void down_to (int);
+ void normalize_octave ();
+ void normalize_alteration ();
void normalize ();
public:
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;
alteration_ = a;
octave_ = o;
scale_ = default_global_scale;
- normalize ();
+ normalize_octave ();
}
/* FIXME: why is octave == 0 and default not middleC ? */
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
}
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)
{
notename_ += delta.notename_;
alteration_ += new_alter - tone_pitch ();
- normalize ();
+ normalize_octave ();
}
Pitch
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)));
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;
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 *)
return SCM_UNSPECIFIED;
}
-Scale::Scale ()
+Scale::Scale (vector<Rational> const &tones)
{
+ step_tones_ = tones;
+
smobify_self ();
}