]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/moment.cc
*** empty log message ***
[lilypond.git] / lily / moment.cc
index 3c504ecc04030bbd0433c14918dd122d7e7f243a..a180bedd6cb26e02922a60c3312ac02a2225480a 100644 (file)
@@ -3,7 +3,7 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   
  */
 
@@ -13,9 +13,8 @@
 #include "warn.hh"
 #include "ly-smobs.icc"
 
-
 IMPLEMENT_SIMPLE_SMOBS (Moment);
-IMPLEMENT_TYPE_P (Moment, "moment?");
+IMPLEMENT_TYPE_P (Moment, "ly:moment?");
 
 SCM
 Moment::mark_smob (SCM)
@@ -24,12 +23,6 @@ Moment::mark_smob (SCM)
 }
 
 
-SCM
-Moment::smobbed_copy () const
-{
-  Moment * m = new Moment (*this);
-  return m->smobbed_self ();
-}
 
 
 int
@@ -38,40 +31,100 @@ Moment::print_smob (SCM s, SCM port, scm_print_state *)
   Moment  *r = (Moment *) ly_cdr (s);
      
   scm_puts ("#<Mom ", port);
-  String str (r->str ());
-  scm_puts ((char *)str.ch_C (), port);
-  scm_puts (" >", port);
+  String str = r->to_string ();
+  scm_puts ((char *)str.to_str0 (), port);
+  scm_puts (">", port);
   
   return 1;
 }
 
 /*
   TODO: add optional factor argument.
- */
-LY_DEFINE (make_moment,"make-moment", 2,0,0, (SCM n, SCM d),
-          "create the rational number with main timing @var{n}/@var{d}. 
-
-
-Moment is a point in musical time. It is consists of a pair of
-rationals (@var{m},@var{g}), where @var{m} is the timing for the  main
-notes, and @var{g} the timing for  grace notes. In absence of grace
-notes, @var{g} is zero.
-")
+*/
+LY_DEFINE (make_moment,"ly:make-moment", 2,2,0, (SCM n, SCM d, SCM gn, SCM gd),
+          "Create the rational number with main timing @var{n}/@var{d}, "
+          "and optional grace timin @var{gn}/@var{gd}.\n"
+          "\n"
+          "\n"
+          "Moment is a point in musical time. It is consists of a pair of\n"
+          "rationals (@var{m},@var{g}), where @var{m} is the timing for the  main\n"
+          "notes, and @var{g} the timing for  grace notes. In absence of grace\n"
+          "notes, @var{g} is zero.\n"
+          )
 {
-  Moment m (Rational (1,1));
+  SCM_ASSERT_TYPE (SCM_INUMP (n), n, SCM_ARG1, __FUNCTION__, "integer");
+  SCM_ASSERT_TYPE (SCM_INUMP (d), d, SCM_ARG2, __FUNCTION__, "integer");
 
-  if (SCM_INUMP (n) && SCM_INUMP (d))
+  int grace_num = 0;
+  if (gn != SCM_UNDEFINED)
     {
-      m =  Moment (Rational (gh_scm2int (n), gh_scm2int (d)));
+      SCM_ASSERT_TYPE (SCM_INUMP (gn), gn, SCM_ARG3, __FUNCTION__, "integer");
+      grace_num = gh_scm2int (gn);
     }
-  else
+
+  int grace_den = 1;
+  if (gd != SCM_UNDEFINED)
     {
-      ::error ("make-moment takes two integer arguments. Using 1/1");
+      SCM_ASSERT_TYPE (SCM_INUMP (gd), gd, SCM_ARG4, __FUNCTION__, "integer");
+      grace_den = gh_scm2int (gd);
     }
 
-  return m.smobbed_copy ();
+  return Moment (Rational (gh_scm2int (n), gh_scm2int (d)),
+                Rational (grace_num, grace_den)).smobbed_copy ();
+}
+
+LY_DEFINE (add_moment,"ly:add-moment", 2,0,0, (SCM a, SCM b),
+          "Add two moments."
+          )
+{
+  Moment * ma = unsmob_moment (a);
+  Moment * mb = unsmob_moment (b);
+  SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
+  SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
+
+  return (*ma + *mb).smobbed_copy ();
+}
+
+
+LY_DEFINE (mul_moment,"ly:mul-moment", 2,0,0, (SCM a, SCM b),
+          "Multiply two moments."
+          )
+{
+  Moment * ma = unsmob_moment (a);
+  Moment * mb = unsmob_moment (b);
+  SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
+  SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
+
+  return (*ma *  *mb).smobbed_copy ();
 }
 
+
+
+LY_DEFINE (div_moment,"ly:div-moment", 2,0,0, (SCM a, SCM b),
+          "Divide two moments."
+          )
+{
+  Moment * ma = unsmob_moment (a);
+  Moment * mb = unsmob_moment (b);
+  SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
+  SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
+
+  return (*ma /  *mb).smobbed_copy ();
+}
+
+LY_DEFINE (ly_moment_less_p,"ly:moment<?", 2,0,0, (SCM a, SCM b),
+          "Compare two moments.")
+{
+  Moment * ma = unsmob_moment (a);
+  Moment * mb = unsmob_moment (b);
+  SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
+  SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
+
+  return gh_bool2scm (*ma <  *mb);
+}
+
+
+
 SCM
 Moment::equal_p (SCM a, SCM b)
 {
@@ -106,8 +159,8 @@ Moment::Moment ()
 
 Moment::Moment (int m)
 {
-  main_part_ = Rational(m);
-  grace_part_  = Rational( 0);
+  main_part_ = Rational (m);
+  grace_part_  = Rational ( 0);
 }
 
 Moment::Moment (Rational m, Rational g)
@@ -137,7 +190,7 @@ Moment::operator -= (Moment const &src)
 
 /*
   only take the main part of SRC for multiplication.
- */
+*/
 void
 Moment::operator *= (Moment const &src)
 {
@@ -147,7 +200,7 @@ Moment::operator *= (Moment const &src)
 
 /*
   only take the main part of SRC for multiplication.
- */
+*/
 void
 Moment::operator /= (Moment const &src)
 {
@@ -156,12 +209,6 @@ Moment::operator /= (Moment const &src)
 }
 
 
-#if 0
-Moment::operator Rational()
-{
-  return main_part_;
-}
-#endif
 
 int
 Moment::den () const { return main_part_.den (); }
@@ -183,12 +230,12 @@ Moment::set_infinite (int k)
 
 
 String
-Moment::str () const
+Moment::to_string () const
 {
-  String s =  main_part_.str ();
+  String s =  main_part_.to_string ();
   if (grace_part_)
     {
-      s += "G" + grace_part_.str ();
+      s += "G" + grace_part_.to_string ();
     }
   return s;
 }
@@ -204,10 +251,10 @@ Moment::operator - () const
 
 
 #ifdef STREAM_SUPPORT
-ostream &
-operator << (ostream &os, Moment const &m)
+std::ostream &
+operator << (std::ostream &os, Moment const &m)
 {
-  os << m.str ();
+  os << m.to_string ();
   return os;
 }
 #endif