2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "lily-guile.hh"
18 Moment::Moment (int m)
20 main_part_ = Rational (m);
21 grace_part_ = Rational ( 0);
24 Moment::Moment (Rational m, Rational g)
30 Moment::Moment (Rational m)
33 grace_part_ = Rational (0);
36 #include "ly-smobs.icc"
37 IMPLEMENT_SIMPLE_SMOBS (Moment);
38 IMPLEMENT_TYPE_P (Moment, "ly:moment?");
41 Moment::mark_smob (SCM)
47 Moment::print_smob (SCM s, SCM port, scm_print_state *)
49 Moment *r = (Moment *) SCM_CELL_WORD_1 (s);
51 scm_puts ("#<Mom ", port);
52 String str = r->to_string ();
53 scm_puts ((char *)str.to_str0 (), port);
59 /* TODO: add optional factor argument. */
60 LY_DEFINE (ly_make_moment, "ly:make-moment",
61 2, 2, 0, (SCM n, SCM d, SCM gn, SCM gd),
62 "Create the rational number with main timing @var{n}/@var{d}, "
63 "and optional grace timin @var{gn}/@var{gd}.\n"
66 "Moment is a point in musical time. "
67 "It is consists of a pair of rationals (@var{m},@var{g}), "
68 "where @var{m} is the timing for the main\n"
69 "notes, and @var{g} the timing for grace notes. "
70 "In absence of grace notes, @var{g} is zero.\n")
72 SCM_ASSERT_TYPE (scm_is_integer (n), n, SCM_ARG1, __FUNCTION__, "integer");
73 SCM_ASSERT_TYPE (scm_is_integer (d), d, SCM_ARG2, __FUNCTION__, "integer");
76 if (gn != SCM_UNDEFINED)
78 SCM_ASSERT_TYPE (scm_is_integer (gn), gn, SCM_ARG3, __FUNCTION__, "integer");
79 grace_num = scm_to_int (gn);
83 if (gd != SCM_UNDEFINED)
85 SCM_ASSERT_TYPE (scm_is_integer (gd), gd, SCM_ARG4, __FUNCTION__, "integer");
86 grace_den = scm_to_int (gd);
89 return Moment (Rational (scm_to_int (n), scm_to_int (d)),
90 Rational (grace_num, grace_den)).smobbed_copy ();
93 LY_DEFINE (ly_add_moment, "ly:add-moment",
94 2, 0, 0, (SCM a, SCM b),
97 Moment *ma = unsmob_moment (a);
98 Moment *mb = unsmob_moment (b);
99 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
100 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
101 return (*ma + *mb).smobbed_copy ();
104 LY_DEFINE (ly_mul_moment,"ly:mul-moment",
105 2, 0, 0, (SCM a, SCM b),
106 "Multiply two moments.")
108 Moment *ma = unsmob_moment (a);
109 Moment *mb = unsmob_moment (b);
110 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
111 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
112 return (*ma * *mb).smobbed_copy ();
115 LY_DEFINE (ly_div_moment,"ly:div-moment",
116 2, 0, 0, (SCM a, SCM b),
117 "Divide two moments.")
119 Moment *ma = unsmob_moment (a);
120 Moment *mb = unsmob_moment (b);
121 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
122 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
123 return (*ma / *mb).smobbed_copy ();
126 LY_DEFINE (ly_moment_less_p,"ly:moment<?",
127 2, 0, 0, (SCM a, SCM b),
128 "Compare two moments.")
130 Moment *ma = unsmob_moment (a);
131 Moment *mb = unsmob_moment (b);
132 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
133 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
134 return ly_bool2scm (*ma < *mb);
138 Moment::equal_p (SCM a, SCM b)
140 Moment *m1 = unsmob_moment (a);
141 Moment *m2 = unsmob_moment (b);
143 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
146 /****************************************************************/
149 compare (Moment const &a, Moment const &b)
151 return Moment::compare (a,b);
155 Moment::compare (Moment const &a, Moment const &b)
157 int c = Rational::compare (a.main_part_,b.main_part_);
161 return Rational::compare (a.grace_part_, b.grace_part_);
165 Moment::operator += (Moment const &src)
167 main_part_ +=src.main_part_ ;
168 grace_part_ += src.grace_part_;
171 Moment::operator -= (Moment const &src)
173 main_part_ -= src.main_part_ ;
174 grace_part_ -= src.grace_part_;
178 only take the main part of SRC for multiplication.
181 Moment::operator *= (Moment const &src)
183 main_part_ *= src.main_part_ ;
184 grace_part_ *= src.main_part_;
188 only take the main part of SRC for multiplication.
191 Moment::operator /= (Moment const &src)
193 main_part_ /= src.main_part_ ;
194 grace_part_ /= src.main_part_;
200 return main_part_.den ();
206 return main_part_.num ();
210 Moment::to_bool () const
212 return main_part_ || grace_part_;
216 Moment::set_infinite (int k)
218 main_part_.set_infinite (k);
222 Moment::to_string () const
224 String s = main_part_.to_string ();
227 s += "G" + grace_part_.to_string ();
233 Moment::operator - () const
236 m.grace_part_ = - grace_part_;
237 m. main_part_ = - main_part_ ;
242 #ifdef STREAM_SUPPORT
244 operator << (std::ostream &os, Moment const &m)
246 os << m.to_string ();
252 robust_scm2moment (SCM m, Moment d)
254 Moment * p = unsmob_moment (m);