2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "lily-guile.hh"
14 #include "ly-smobs.icc"
17 IMPLEMENT_SIMPLE_SMOBS (Moment);
18 IMPLEMENT_TYPE_P (Moment, "moment?");
21 Moment::mark_smob (SCM)
28 Moment::smobbed_copy () const
30 Moment * m = new Moment (*this);
31 return m->smobbed_self ();
36 Moment::print_smob (SCM s, SCM port, scm_print_state *)
38 Moment *r = (Moment *) ly_cdr (s);
40 scm_puts ("#<Mom ", port);
41 String str (r->str ());
42 scm_puts ((char *)str.ch_C (), port);
43 scm_puts (" >", port);
49 TODO: add optional factor argument.
51 LY_DEFINE (make_moment,"make-moment", 2,0,0, (SCM n, SCM d),
52 "create the rational number with main timing @var{n}/@var{d}.
55 Moment is a point in musical time. It is consists of a pair of
56 rationals (@var{m},@var{g}), where @var{m} is the timing for the main
57 notes, and @var{g} the timing for grace notes. In absence of grace
58 notes, @var{g} is zero.
61 Moment m (Rational (1,1));
63 if (SCM_INUMP (n) && SCM_INUMP (d))
65 m = Moment (Rational (gh_scm2int (n), gh_scm2int (d)));
69 ::error ("make-moment takes two integer arguments. Using 1/1");
72 return m.smobbed_copy ();
76 Moment::equal_p (SCM a, SCM b)
78 Moment *m1 = unsmob_moment (a);
79 Moment *m2 = unsmob_moment (b);
81 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
84 /****************************************************************/
87 compare (Moment const &a, Moment const &b)
89 return Moment::compare (a,b);
93 Moment::compare (Moment const &a, Moment const &b)
95 int c = Rational::compare (a.main_part_,b.main_part_);
99 return Rational::compare (a.grace_part_, b.grace_part_);
107 Moment::Moment (int m)
109 main_part_ = Rational(m);
110 grace_part_ = Rational( 0);
113 Moment::Moment (Rational m, Rational g)
119 Moment::Moment (Rational m)
122 grace_part_ = Rational (0);
126 Moment::operator += (Moment const &src)
128 main_part_ +=src.main_part_ ;
129 grace_part_ += src.grace_part_;
132 Moment::operator -= (Moment const &src)
134 main_part_ -= src.main_part_ ;
135 grace_part_ -= src.grace_part_;
139 only take the main part of SRC for multiplication.
142 Moment::operator *= (Moment const &src)
144 main_part_ *= src.main_part_ ;
145 grace_part_ *= src.main_part_;
149 only take the main part of SRC for multiplication.
152 Moment::operator /= (Moment const &src)
154 main_part_ /= src.main_part_ ;
155 grace_part_ /= src.main_part_;
160 Moment::operator Rational()
167 Moment::den () const { return main_part_.den (); }
170 Moment::num () const { return main_part_.num (); }
173 Moment::to_bool () const
175 return main_part_ || grace_part_;
179 Moment::set_infinite (int k)
181 main_part_.set_infinite (k);
188 String s = main_part_.str ();
191 s += "G" + grace_part_.str ();
197 Moment::operator - () const
200 m.grace_part_ = - grace_part_;
201 m. main_part_ = - main_part_ ;
206 #ifdef STREAM_SUPPORT
208 operator << (std::ostream &os, Moment const &m)