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>
11 #include "lily-guile.hh"
14 #include "ly-smobs.icc"
16 IMPLEMENT_SIMPLE_SMOBS (Moment
);
17 IMPLEMENT_TYPE_P (Moment
, "ly:moment?");
20 Moment::mark_smob (SCM
)
27 Moment::smobbed_copy () const
29 Moment
* m
= new Moment (*this);
30 return m
->smobbed_self ();
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
);
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"
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
),
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
),
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_
);
131 return Rational::compare (a
.grace_part_
, b
.grace_part_
);
139 Moment::Moment (int m
)
141 main_part_
= Rational(m
);
142 grace_part_
= Rational( 0);
145 Moment::Moment (Rational m
, Rational g
)
151 Moment::Moment (Rational m
)
154 grace_part_
= Rational (0);
158 Moment::operator += (Moment
const &src
)
160 main_part_
+=src
.main_part_
;
161 grace_part_
+= src
.grace_part_
;
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.
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.
184 Moment::operator /= (Moment
const &src
)
186 main_part_
/= src
.main_part_
;
187 grace_part_
/= src
.main_part_
;
192 Moment::operator Rational()
199 Moment::den () const { return main_part_
.den (); }
202 Moment::num () const { return main_part_
.num (); }
205 Moment::to_bool () const
207 return main_part_
|| grace_part_
;
211 Moment::set_infinite (int k
)
213 main_part_
.set_infinite (k
);
218 Moment::to_string () const
220 String s
= main_part_
.to_string ();
223 s
+= "G" + grace_part_
.to_string ();
229 Moment::operator - () const
232 m
.grace_part_
= - grace_part_
;
233 m
. main_part_
= - main_part_
;
238 #ifdef STREAM_SUPPORT
240 operator << (std::ostream
&os
, Moment
const &m
)
242 os
<< m
.to_string ();