]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/pitch.cc
duh.
[lilypond.git] / lily / pitch.cc
index 17f6b0f5008761a625d2c9634c5f306cdb9af904..8bcfcb4e3a5d53b3385e567cc8396c0724b3fda8 100644 (file)
@@ -3,7 +3,7 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   
  */
 #include "pitch.hh"
@@ -22,7 +22,7 @@ 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);
@@ -64,22 +64,20 @@ Pitch::steps () const
  */
 static Byte pitch_byte_a[  ] = { 0, 2, 4, 5, 7, 9, 11 };
 
-int
-sign_safe_div (int n, int d)
-{
-  if (n < 0)
-     return - (-n / d) - 1;
-  return n / d;
-}
 
 /* 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 (octave_i_ + sign_safe_div (notename_i_, 7)) * 12
-    + pitch_byte_a[notename_i_ % 7]
-    + alteration_i_;
+  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
@@ -152,17 +150,17 @@ 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--)
@@ -222,43 +220,49 @@ Pitch::down_to (int notename)
   notename_i_ = notename;
 }
 
-///MAKE_SCHEME_CALLBACK (Pitch, transpose, 2);
-///transpose_proc?
+/*
+  can't use macro MAKE_SCHEME_CALLBACK().
+   messy stuff since Pitch::transpose is overloaded.
+ */
+
 SCM
-Pitch::transpose (SCM p, SCM delta)
+pitch_transpose (SCM p, SCM delta)
 {
-  Pitch t = *unsmob_pitch (p);
-  t.transpose (*unsmob_pitch (delta));
-  return t.smobbed_copy ();
+  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 ();
 }
 
-static SCM
-pitch_transpose (SCM p, SCM delta)
+SCM
+Pitch::transpose (SCM p, SCM d)
 {
-  return Pitch::transpose (p, delta);
+  return pitch_transpose (p,d);
 }
 
 /****************************************************************/
 
 
-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 ("#<Pitch ", port);
-  scm_display (ly_str02scm (r->str().ch_C()), port);
+  scm_display (ly_str02scm (r->str ().ch_C ()), port);
   scm_puts (" >", port);
   
   return 1;
@@ -267,8 +271,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_
@@ -277,14 +281,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;
@@ -293,10 +297,13 @@ Pitch::less_p (SCM p1, SCM p2)
 /*
   should add optional args
  */
-
 static SCM
 make_pitch (SCM o, SCM n, SCM 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 ();
 }
@@ -305,11 +312,8 @@ static SCM
 pitch_octave (SCM pp)
 {
   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);
 }
@@ -318,11 +322,8 @@ static SCM
 pitch_alteration (SCM pp)
 {
   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);
 }
@@ -331,11 +332,8 @@ static SCM
 pitch_notename (SCM 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);
 }
@@ -344,29 +342,34 @@ static SCM
 pitch_semitones (SCM pp)
 {
   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->steps ();
 
   return gh_int2scm (q);
 }
+SCM pitch_less_proc;
+static SCM
+pitch_less (SCM p1, SCM p2)
+{
+  return Pitch::less_p (ly_car (p1),  ly_car (p2));
+}
+
 
 static void
-add_funcs()
+add_funcs ()
 {
-  // should take list?: (make-pitch '(octave name accidental))
-  scm_make_gsubr ("make-pitch", 3, 0, 0, (Scheme_function_unknown)make_pitch);
-
-  scm_make_gsubr ("pitch-octave", 1, 0, 0, (Scheme_function_unknown)pitch_octave);
-  scm_make_gsubr ("pitch-notename", 1, 0, 0, (Scheme_function_unknown)pitch_notename);
-  scm_make_gsubr ("pitch-alteration", 1, 0, 0, (Scheme_function_unknown)pitch_alteration);
-  scm_make_gsubr ("pitch-semitones", 1, 0, 0, (Scheme_function_unknown)pitch_semitones);
-  scm_make_gsubr ("Pitch::transpose", 2, 0, 0, (Scheme_function_unknown) pitch_transpose);
+  // should take list?: (make-pitch ' (octave name accidental))
+  scm_c_define_gsubr ("make-pitch", 3, 0, 0, (Scheme_function_unknown)make_pitch);
+  scm_c_define_gsubr ("pitch-octave", 1, 0, 0, (Scheme_function_unknown)pitch_octave);
+  scm_c_define_gsubr ("pitch-notename", 1, 0, 0, (Scheme_function_unknown)pitch_notename);
+  scm_c_define_gsubr ("pitch-alteration", 1, 0, 0, (Scheme_function_unknown)pitch_alteration);
+  scm_c_define_gsubr ("pitch-semitones", 1, 0, 0, (Scheme_function_unknown)pitch_semitones);
+  scm_c_define_gsubr ("Pitch::transpose", 2, 0, 0, (Scheme_function_unknown) pitch_transpose);
+  pitch_less_proc = gh_new_procedure2_0 ("pitch-less", &pitch_less);
 }
 
-ADD_SCM_INIT_FUNC(pitch, add_funcs);
+ADD_SCM_INIT_FUNC (pitch, add_funcs);
 
 SCM
 Pitch::smobbed_copy ()const
@@ -392,3 +395,4 @@ Pitch::alteration_i () const
 {
   return alteration_i_;
 }
+