*** empty log message ***
[lilypond.git] / lily / moment.cc
blob43c3e0bf1893dba18473af75a923061c8ac8c6e5
1 /*
2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
11 #include "lily-guile.hh"
12 #include "moment.hh"
13 #include "warn.hh"
14 #include "ly-smobs.icc"
16 IMPLEMENT_SIMPLE_SMOBS (Moment);
17 IMPLEMENT_TYPE_P (Moment, "ly:moment?");
19 SCM
20 Moment::mark_smob (SCM)
22 return SCM_EOL;
26 SCM
27 Moment::smobbed_copy () const
29 Moment * m = new Moment (*this);
30 return m->smobbed_self ();
34 int
35 Moment::print_smob (SCM s, SCM port, scm_print_state *)
37 Moment *r = (Moment *) ly_cdr (s);
39 scm_puts ("#<Mom ", port);
40 String str = r->to_string ();
41 scm_puts ((char *)str.to_str0 (), port);
42 scm_puts (" >", port);
44 return 1;
48 TODO: add optional factor argument.
50 LY_DEFINE (make_moment,"ly:make-moment", 2,0,0, (SCM n, SCM d),
51 "create the rational number with main timing @var{n}/@var{d}. \n"
52 "\n"
53 "\n"
54 "Moment is a point in musical time. It is consists of a pair of\n"
55 "rationals (@var{m},@var{g}), where @var{m} is the timing for the main\n"
56 "notes, and @var{g} the timing for grace notes. In absence of grace\n"
57 "notes, @var{g} is zero.\n"
60 SCM_ASSERT_TYPE(SCM_INUMP (n), n, SCM_ARG1, __FUNCTION__, "integer");
61 SCM_ASSERT_TYPE(SCM_INUMP (d), d, SCM_ARG2, __FUNCTION__, "integer");
63 return Moment (Rational (gh_scm2int (n), gh_scm2int (d))).smobbed_copy();
66 LY_DEFINE (add_moment,"ly:add-moment", 2,0,0, (SCM a, SCM b),
67 "Add two moments."
70 Moment * ma = unsmob_moment (a);
71 Moment * mb = unsmob_moment (b);
72 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
73 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
75 return (*ma + *mb).smobbed_copy();
79 LY_DEFINE (mul_moment,"ly:mul-moment", 2,0,0, (SCM a, SCM b),
80 "Multiply two moments."
83 Moment * ma = unsmob_moment (a);
84 Moment * mb = unsmob_moment (b);
85 SCM_ASSERT_TYPE (ma, a, SCM_ARG1, __FUNCTION__, "moment");
86 SCM_ASSERT_TYPE (mb, b, SCM_ARG2, __FUNCTION__, "moment");
88 return (*ma * *mb).smobbed_copy();
93 LY_DEFINE (div_moment,"ly:div-moment", 2,0,0, (SCM a, SCM b),
94 "Divide two moments."
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");
102 return (*ma / *mb).smobbed_copy();
108 Moment::equal_p (SCM a, SCM b)
110 Moment *m1 = unsmob_moment (a);
111 Moment *m2 = unsmob_moment (b);
113 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
116 /****************************************************************/
119 compare (Moment const &a, Moment const &b)
121 return Moment::compare (a,b);
125 Moment::compare (Moment const &a, Moment const &b)
127 int c = Rational::compare (a.main_part_,b.main_part_);
128 if (c)
129 return c;
131 return Rational::compare (a.grace_part_, b.grace_part_);
134 Moment::Moment ()
139 Moment::Moment (int m)
141 main_part_ = Rational(m);
142 grace_part_ = Rational( 0);
145 Moment::Moment (Rational m, Rational g)
147 main_part_ = m;
148 grace_part_ = g;
151 Moment::Moment (Rational m)
153 main_part_ = m;
154 grace_part_ = Rational (0);
157 void
158 Moment::operator += (Moment const &src)
160 main_part_ +=src.main_part_ ;
161 grace_part_ += src.grace_part_;
163 void
164 Moment::operator -= (Moment const &src)
166 main_part_ -= src.main_part_ ;
167 grace_part_ -= src.grace_part_;
171 only take the main part of SRC for multiplication.
173 void
174 Moment::operator *= (Moment const &src)
176 main_part_ *= src.main_part_ ;
177 grace_part_ *= src.main_part_;
181 only take the main part of SRC for multiplication.
183 void
184 Moment::operator /= (Moment const &src)
186 main_part_ /= src.main_part_ ;
187 grace_part_ /= src.main_part_;
191 #if 0
192 Moment::operator Rational()
194 return main_part_;
196 #endif
199 Moment::den () const { return main_part_.den (); }
202 Moment::num () const { return main_part_.num (); }
204 bool
205 Moment::to_bool () const
207 return main_part_ || grace_part_;
210 void
211 Moment::set_infinite (int k)
213 main_part_.set_infinite (k);
217 String
218 Moment::to_string () const
220 String s = main_part_.to_string ();
221 if (grace_part_)
223 s += "G" + grace_part_.to_string ();
225 return s;
228 Moment
229 Moment::operator - () const
231 Moment m;
232 m.grace_part_ = - grace_part_;
233 m. main_part_ = - main_part_ ;
234 return m;
238 #ifdef STREAM_SUPPORT
239 std::ostream &
240 operator << (std::ostream &os, Moment const &m)
242 os << m.to_string ();
243 return os;
245 #endif