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>
17 Moment::Moment (int m)
19 main_part_ = Rational (m);
20 grace_part_ = Rational ( 0);
23 Moment::Moment (Rational m, Rational g)
29 Moment::Moment (Rational m)
32 grace_part_ = Rational (0);
35 #include "ly-smobs.icc"
36 IMPLEMENT_SIMPLE_SMOBS (Moment);
37 IMPLEMENT_TYPE_P (Moment, "ly:moment?");
40 Moment::mark_smob (SCM)
46 Moment::print_smob (SCM s, SCM port, scm_print_state *)
48 Moment *r = (Moment *) SCM_CELL_WORD_1 (s);
50 scm_puts ("#<Mom ", port);
51 String str = r->to_string ();
52 scm_puts ((char *)str.to_str0 (), port);
59 Moment::as_scheme () const
61 return scm_list_5 (ly_symbol2scm ("ly:make-moment"),
62 scm_from_int (main_part_.num()),
63 scm_from_int (main_part_.den()),
64 scm_from_int (grace_part_.num()),
65 scm_from_int (grace_part_.den()));
68 /* TODO: add optional factor argument. */
69 LY_DEFINE (ly_make_moment, "ly:make-moment",
70 2, 2, 0, (SCM n, SCM d, SCM gn, SCM gd),
71 "Create the rational number with main timing @var{n}/@var{d}, "
72 "and optional grace timin @var{gn}/@var{gd}.\n"
75 "Moment is a point in musical time. "
76 "It is consists of a pair of rationals (@var{m},@var{g}), "
77 "where @var{m} is the timing for the main\n"
78 "notes, and @var{g} the timing for grace notes. "
79 "In absence of grace notes, @var{g} is zero.\n")
81 SCM_ASSERT_TYPE (scm_is_integer (n), n, SCM_ARG1, __FUNCTION__, "integer");
82 SCM_ASSERT_TYPE (scm_is_integer (d), d, SCM_ARG2, __FUNCTION__, "integer");
85 if (gn != SCM_UNDEFINED)
87 SCM_ASSERT_TYPE (scm_is_integer (gn), gn, SCM_ARG3, __FUNCTION__, "integer");
88 grace_num = scm_to_int (gn);
92 if (gd != SCM_UNDEFINED)
94 SCM_ASSERT_TYPE (scm_is_integer (gd), gd, SCM_ARG4, __FUNCTION__, "integer");
95 grace_den = scm_to_int (gd);
98 return Moment (Rational (scm_to_int (n), scm_to_int (d)),
99 Rational (grace_num, grace_den)).smobbed_copy ();
102 LY_DEFINE (ly_add_moment, "ly:add-moment",
103 2, 0, 0, (SCM a, SCM b),
106 Moment *ma = unsmob_moment (a);
107 Moment *mb = unsmob_moment (b);
108 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
109 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
110 return (*ma + *mb).smobbed_copy ();
113 LY_DEFINE (ly_mul_moment,"ly:mul-moment",
114 2, 0, 0, (SCM a, SCM b),
115 "Multiply two moments.")
117 Moment *ma = unsmob_moment (a);
118 Moment *mb = unsmob_moment (b);
119 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
120 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
121 return (*ma * *mb).smobbed_copy ();
124 LY_DEFINE (ly_div_moment,"ly:div-moment",
125 2, 0, 0, (SCM a, SCM b),
126 "Divide two moments.")
128 Moment *ma = unsmob_moment (a);
129 Moment *mb = unsmob_moment (b);
130 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
131 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
132 return (*ma / *mb).smobbed_copy ();
135 LY_DEFINE (ly_moment_main_numerator,"ly:moment-main-numerator",
137 "Extract numerator from main timing.")
139 Moment *ma = unsmob_moment (mom);
140 SCM_ASSERT_TYPE (ma, mom, SCM_ARG1, __FUNCTION__, "moment");
142 return scm_from_int (ma->main_part_.numerator ());
145 LY_DEFINE (ly_moment_main_denominator,"ly:moment-main-denominator",
147 "Extract denominator from main timing.")
149 Moment *ma = unsmob_moment (mom);
150 SCM_ASSERT_TYPE (ma, mom, SCM_ARG1, __FUNCTION__, "moment");
152 return scm_from_int (ma->main_part_.denominator ());
155 LY_DEFINE (ly_moment_less_p,"ly:moment<?",
156 2, 0, 0, (SCM a, SCM b),
157 "Compare two moments.")
159 Moment *ma = unsmob_moment (a);
160 Moment *mb = unsmob_moment (b);
161 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
162 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
163 return ly_bool2scm (*ma < *mb);
167 Moment::equal_p (SCM a, SCM b)
169 Moment *m1 = unsmob_moment (a);
170 Moment *m2 = unsmob_moment (b);
172 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
175 /****************************************************************/
178 compare (Moment const &a, Moment const &b)
180 return Moment::compare (a,b);
184 Moment::compare (Moment const &a, Moment const &b)
186 int c = Rational::compare (a.main_part_,b.main_part_);
190 return Rational::compare (a.grace_part_, b.grace_part_);
194 Moment::operator += (Moment const &src)
196 main_part_ += src.main_part_ ;
197 grace_part_ += src.grace_part_;
200 Moment::operator -= (Moment const &src)
202 main_part_ -= src.main_part_ ;
203 grace_part_ -= src.grace_part_;
207 only take the main part of SRC for multiplication.
210 Moment::operator *= (Moment const &src)
212 main_part_ *= src.main_part_ ;
213 grace_part_ *= src.main_part_;
217 only take the main part of SRC for multiplication.
220 Moment::operator /= (Moment const &src)
222 main_part_ /= src.main_part_ ;
223 grace_part_ /= src.main_part_;
229 return main_part_.den ();
235 return main_part_.num ();
239 Moment::to_bool () const
241 return main_part_ || grace_part_;
245 Moment::set_infinite (int k)
247 main_part_.set_infinite (k);
251 Moment::to_string () const
253 String s = main_part_.to_string ();
256 s += "G" + grace_part_.to_string ();
262 Moment::operator - () const
265 m.grace_part_ = - grace_part_;
266 m. main_part_ = - main_part_ ;
271 #ifdef STREAM_SUPPORT
273 operator << (std::ostream &os, Moment const &m)
275 os << m.to_string ();
281 robust_scm2moment (SCM m, Moment d)
283 Moment *p = unsmob_moment (m);
292 moment_less (SCM a, SCM b)
294 return *unsmob_moment (a) < *unsmob_moment (b);