X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fmoment.cc;h=54ee65c1842ec074cb034167bbe76c2b0636d8a6;hb=98ca84585c45f75503adff82fc0a8cec2a105769;hp=0e11977686cc0c144341dcfedb3c1c0a3aa94927;hpb=fd7e615444cf58b38283a59b56d3457c07778397;p=lilypond.git diff --git a/lily/moment.cc b/lily/moment.cc index 0e11977686..54ee65c184 100644 --- a/lily/moment.cc +++ b/lily/moment.cc @@ -1,78 +1,249 @@ /* moment.cc -- implement Moment - + source file of the GNU LilyPond music typesetter - - (c) 1999 Han-Wen Nienhuys - - */ + (c) 1999--2004 Han-Wen Nienhuys +*/ #include "lily-guile.hh" #include "moment.hh" #include "warn.hh" -SCM -Moment::mark_smob (SCM s) + +Moment::Moment () { - return SCM_EOL; } +Moment::Moment (int m) +{ + main_part_ = Rational (m); + grace_part_ = Rational ( 0); +} + +Moment::Moment (Rational m, Rational g) +{ + main_part_ = m; + grace_part_ = g; +} + +Moment::Moment (Rational m) +{ + main_part_ = m; + grace_part_ = Rational (0); +} + +#include "ly-smobs.icc" +IMPLEMENT_SIMPLE_SMOBS (Moment); +IMPLEMENT_TYPE_P (Moment, "ly:moment?"); -Moment::~Moment() +SCM +Moment::mark_smob (SCM) { - self_scm_ = SCM_EOL; + return SCM_EOL; } int Moment::print_smob (SCM s, SCM port, scm_print_state *) { - Moment *r = (Moment *) gh_cdr (s); + Moment *r = (Moment *) ly_cdr (s); scm_puts ("#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; } -void -Moment::do_smobify_self () -{} - -SCM -make_rational (SCM n, SCM d) +/* TODO: add optional factor argument. */ +LY_DEFINE (ly_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 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 notes, @var{g} is zero.\n") { - if (SCM_INUMP (n) && SCM_INUMP(d)) + SCM_ASSERT_TYPE (SCM_INUMP (n), n, SCM_ARG1, __FUNCTION__, "integer"); + SCM_ASSERT_TYPE (SCM_INUMP (d), d, SCM_ARG2, __FUNCTION__, "integer"); + + int grace_num = 0; + if (gn != SCM_UNDEFINED) { - Moment *r = new Moment (gh_scm2int (n), gh_scm2int (d)); - return r->smobify_self (); + SCM_ASSERT_TYPE (SCM_INUMP (gn), gn, SCM_ARG3, __FUNCTION__, "integer"); + grace_num = ly_scm2int (gn); } - else + + int grace_den = 1; + if (gd != SCM_UNDEFINED) { - ::error ("Not a number"); - assert(false); + SCM_ASSERT_TYPE (SCM_INUMP (gd), gd, SCM_ARG4, __FUNCTION__, "integer"); + grace_den = ly_scm2int (gd); } + + return Moment (Rational (ly_scm2int (n), ly_scm2int (d)), + Rational (grace_num, grace_den)).smobbed_copy (); } -#include "ly-smobs.icc" +LY_DEFINE (ly_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 (); +} -IMPLEMENT_SMOBS(Moment); +LY_DEFINE (ly_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 (); +} -void -init_moments () +LY_DEFINE (ly_div_moment,"ly:div-moment", + 2, 0, 0, (SCM a, SCM b), + "Divide two moments.") { - scm_make_gsubr ("make-moment", 2 , 0, 0, (SCM(*)(...)) make_rational); + 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 (); } -ADD_SCM_INIT_FUNC(moms,init_moments); +LY_DEFINE (ly_moment_less_p,"ly:moment