X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fpitch.cc;h=06c19f5473d3953570ea13d177cd61e8ab7b0ec1;hb=16f86be3db31bcf062a33fdd79c37c168e4883d4;hp=a6447b5f26a6869dfe74622c0b58de2f7a92fa88;hpb=4975901229a1b074f6c93d812e15d653aa8e2952;p=lilypond.git diff --git a/lily/pitch.cc b/lily/pitch.cc index a6447b5f26..06c19f5473 100644 --- a/lily/pitch.cc +++ b/lily/pitch.cc @@ -3,11 +3,11 @@ source file of the GNU LilyPond music typesetter - (c) 1998--2000 Han-Wen Nienhuys + (c) 1998--2002 Han-Wen Nienhuys */ #include "pitch.hh" -#include "debug.hh" +#include "warn.hh" #include "main.hh" #include "ly-smobs.icc" @@ -22,11 +22,12 @@ Pitch::Pitch (int o, int n, int a) if (n < 0 || n >= 7 || a < -2 || a > 2) { - String s = _("Pitch arguments out of range"); + String s = _ ("Pitch arguments out of range"); s += ": alteration = " + to_str (a); s += ", notename = " + to_str (n); warning (s); } + normalise (); } Pitch::Pitch () @@ -63,67 +64,109 @@ Pitch::steps () const */ static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 }; + +/* Calculate pitch height in 12th octave steps. Don't assume + normalised pitch as this function is used to normalise the pitch. */ int Pitch::semitone_pitch () const { - return pitch_byte_a[ notename_i_ % 7 ] + alteration_i_ + octave_i_ * 12; + int o = octave_i_; + int n = notename_i_; + while (n < 0) + { + n += 7; + o --; + } + return (o + n / 7) * 12 + pitch_byte_a[n % 7] + alteration_i_; +} + +void +Pitch::normalise () +{ + int pitch = semitone_pitch (); + while (notename_i_ >= 7) + { + notename_i_ -= 7; + octave_i_++; + alteration_i_ -= semitone_pitch () - pitch; + } + while (notename_i_ < 0) + { + notename_i_ += 7; + octave_i_--; + alteration_i_ -= semitone_pitch () - pitch; + } + while (alteration_i_ >= 3) + { + if (notename_i_ == 6) + { + notename_i_ = 0; + octave_i_++; + } + else + notename_i_++; + + alteration_i_ = 0; + alteration_i_ -= semitone_pitch () - pitch; + } + while (alteration_i_ <= -3) + { + if (notename_i_ == 0) + { + notename_i_ = 6; + octave_i_--; + } + else + notename_i_--; + + alteration_i_ = 0; + alteration_i_ -= semitone_pitch () - pitch; + } } /* WHugh, wat een intervaas */ void Pitch::transpose (Pitch delta) { - int old_pitch = semitone_pitch (); - int delta_pitch = delta.semitone_pitch (); + int old_semi = semitone_pitch (); + int delta_semi = delta.semitone_pitch (); octave_i_ += delta.octave_i_; notename_i_ += delta.notename_i_; - - while (notename_i_ >= 7) - { - notename_i_ -= 7; - octave_i_ ++; - } - - int new_pitch = semitone_pitch (); - int delta_acc = new_pitch - old_pitch - delta_pitch; + int new_semi = semitone_pitch (); + int delta_acc = new_semi - old_semi - delta_semi; alteration_i_ -= delta_acc; -} - + normalise (); +} -/* FIXME */ -#if 0 -// nice test for internationalisation strings -char const *accname[] = {"double flat", "flat", "natural", - "sharp" , "double sharp"}; -#else +/* FIXME + Merge with *pitch->text* funcs in chord-name.scm + */ char const *accname[] = {"eses", "es", "", "is" , "isis"}; -#endif String Pitch::str () const { int n = (notename_i_ + 2) % 7; - String s = to_str (char(n + 'a')); + String s = to_str (char (n + 'a')); if (alteration_i_) s += String (accname[alteration_i_ + 2]); - if (octave_i_ > 0) + if (octave_i_ >= 0) { int o = octave_i_ + 1; while (o--) s += "'"; } - else if (octave_i_ <0) + else if (octave_i_ < 0) { int o = (-octave_i_) - 1; while (o--) s += to_str (','); } - return s; } @@ -176,44 +219,43 @@ Pitch::down_to (int notename) } notename_i_ = notename; } - -///MAKE_SCHEME_CALLBACK (Pitch, transpose, 2); -///transpose_proc? -SCM -Pitch::transpose (SCM p, SCM delta) + +LY_DEFINE(ly_pitch_transpose, + "ly-transpose-pitch", 2, 0, 0, + (SCM p, SCM delta), + "Transpose @var{p} by the amount @var{delta}, where @var{delta} is the +pitch that central C is transposed to. +") { - Pitch t = *unsmob_pitch (p); - t.transpose (*unsmob_pitch (delta)); - return t.smobbed_copy (); -} - -static SCM -pitch_transpose (SCM p, SCM delta) -{ - return Pitch::transpose (p, delta); + Pitch* t = unsmob_pitch (p); + Pitch *d = unsmob_pitch (delta); + SCM_ASSERT_TYPE(t, p, SCM_ARG1, __FUNCTION__, "pitch") ; + SCM_ASSERT_TYPE(d, delta, SCM_ARG1, __FUNCTION__, "pitch") ; + + Pitch tp =*t; + tp.transpose (*d); + return tp.smobbed_copy (); } /****************************************************************/ -IMPLEMENT_TYPE_P(Pitch, "pitch?"); -IMPLEMENT_UNSMOB(Pitch, pitch); +IMPLEMENT_TYPE_P (Pitch, "pitch?"); + SCM -Pitch::mark_smob (SCM ) +Pitch::mark_smob (SCM) { return SCM_EOL; } -IMPLEMENT_SIMPLE_SMOBS(Pitch); - - +IMPLEMENT_SIMPLE_SMOBS (Pitch); int Pitch::print_smob (SCM s, SCM port, scm_print_state *) { - Pitch *r = (Pitch *) gh_cdr (s); + Pitch *r = (Pitch *) ly_cdr (s); scm_puts ("#str().ch_C()), port); + scm_display (ly_str02scm (r->str ().ch_C ()), port); scm_puts (" >", port); return 1; @@ -222,8 +264,8 @@ Pitch::print_smob (SCM s, SCM port, scm_print_state *) SCM Pitch::equal_p (SCM a , SCM b) { - Pitch *p = (Pitch *) gh_cdr (a); - Pitch *q = (Pitch *) gh_cdr (b); + Pitch *p = (Pitch *) ly_cdr (a); + Pitch *q = (Pitch *) ly_cdr (b); bool eq = p->notename_i_ == q->notename_i_ && p->octave_i_ == q->octave_i_ @@ -232,14 +274,14 @@ Pitch::equal_p (SCM a , SCM b) return eq ? SCM_BOOL_T : SCM_BOOL_F; } -MAKE_SCHEME_CALLBACK(Pitch, less_p, 2); +MAKE_SCHEME_CALLBACK (Pitch, less_p, 2); SCM Pitch::less_p (SCM p1, SCM p2) { Pitch *a = unsmob_pitch (p1); Pitch *b = unsmob_pitch (p2); - if (compare(*a, *b) < 0 ) + if (compare (*a, *b) < 0) return SCM_BOOL_T; else return SCM_BOOL_F; @@ -249,83 +291,83 @@ Pitch::less_p (SCM p1, SCM p2) should add optional args */ -static SCM -make_pitch (SCM o, SCM n, SCM a) +LY_DEFINE(make_pitch, "make-pitch", 3, 0, 0, + (SCM o, SCM n, SCM a), + " +@var{octave} is specified by an integer, zero for the octave containing +middle C. @var{note} is a number from 0 to 6, with 0 corresponding to C +and 6 corresponding to B. The shift is zero for a natural, negative for +flats, or positive for sharps. + +") { - Pitch p; - p.octave_i_ = gh_scm2int (o); - p.notename_i_ = gh_scm2int (n); - p.alteration_i_ = gh_scm2int (a); + SCM_ASSERT_TYPE(gh_number_p(o), o, SCM_ARG1, __FUNCTION__, "number"); + SCM_ASSERT_TYPE(gh_number_p(n), n, SCM_ARG2, __FUNCTION__, "number"); + SCM_ASSERT_TYPE(gh_number_p(a), a, SCM_ARG3, __FUNCTION__, "number"); + + Pitch p (gh_scm2int (o), gh_scm2int (n), gh_scm2int (a)); return p.smobbed_copy (); } -static SCM -pitch_octave (SCM pp) + +LY_DEFINE(pitch_octave, "pitch-octave", 1, 0, 0, + (SCM pp), + "extract the octave from pitch @var{p}.") { Pitch *p = unsmob_pitch (pp); - int q = 0; - if (!p) - warning ("Not a pitch"); - else - q = p->octave_i(); + SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch"); + int q = p->octave_i (); return gh_int2scm (q); } -static SCM -pitch_alteration (SCM pp) +LY_DEFINE(pitch_alteration, "pitch-alteration", 1, 0, 0, + (SCM pp), + "extract the alteration from pitch @var{p}.") { Pitch *p = unsmob_pitch (pp); - int q = 0; - if (!p) - warning ("Not a pitch"); - else - q = p->alteration_i(); + SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch"); + int q = p->alteration_i (); return gh_int2scm (q); } -static SCM -pitch_notename (SCM pp) +LY_DEFINE(pitch_notename, "pitch-notename", 1, 0, 0, + (SCM pp), + "extract the note name from pitch @var{pp}.") { Pitch *p = unsmob_pitch (pp); - int q = 0; - if (!p) - warning ("Not a pitch"); - else - q = p->notename_i(); + SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch"); + int q = p->notename_i (); return gh_int2scm (q); } -static SCM -pitch_semitones (SCM pp) +LY_DEFINE(pitch_semitones, "pitch-semitones", 1, 0, 0, + (SCM pp), + "calculate the number of semitones of @var{p} from central C.") { Pitch *p = unsmob_pitch (pp); - int q = 0; - if (!p) - warning ("Not a pitch"); - else - q = p->steps(); + SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch"); + + int q = p->semitone_pitch (); + + // Was : + // + //int q = p->steps (); + // + // As the function is called "pitch_semitones", I assume it was a mistake ! + // Jiba return gh_int2scm (q); } -static void -add_funcs() +LY_DEFINE(pitch_less, "pitch