From: Joe Neeman Date: Tue, 12 Aug 2008 12:18:52 +0000 (+1000) Subject: Cleanups for pitches and scales. X-Git-Tag: release/2.11.58-1~38 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=bdbcd520701951f81c4e04dd36591086c4c23da5;p=lilypond.git Cleanups for pitches and scales. Much of this code was merged from dev/rune. --- diff --git a/lily/include/pitch.hh b/lily/include/pitch.hh index e711fc0d57..4ab7cb5ea5 100644 --- a/lily/include/pitch.hh +++ b/lily/include/pitch.hh @@ -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: diff --git a/lily/include/scale.hh b/lily/include/scale.hh index ff8e6b2b7f..e74317effe 100644 --- a/lily/include/scale.hh +++ b/lily/include/scale.hh @@ -16,10 +16,19 @@ struct Scale { - vector step_tones_; - Scale (); +public: + Scale (vector 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 step_tones_; }; extern Scale *default_global_scale; diff --git a/lily/pitch.cc b/lily/pitch.cc index bc8a9e45a6..fa1a45089c 100644 --- a/lily/pitch.cc +++ b/lily/pitch.cc @@ -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))); diff --git a/lily/scale.cc b/lily/scale.cc index 5d8ccc9a38..24abf335bc 100644 --- a/lily/scale.cc +++ b/lily/scale.cc @@ -4,7 +4,8 @@ source file of the GNU LilyPond music typesetter (c) 2006--2007 Han-Wen Nienhuys - + 2007--2008 Rune Zedeler + 2008 Joe Neeman */ #include "scale.hh" @@ -17,34 +18,35 @@ */ 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 semitones; + vector 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 const &tones) { + step_tones_ = tones; + smobify_self (); }