2 moment.cc -- implement Moment bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 /* TODO: add optional factor argument. */
12 LY_DEFINE (ly_make_moment
, "ly:make-moment",
13 2, 2, 0, (SCM n
, SCM d
, SCM gn
, SCM gd
),
14 "Create the rational number with main timing @var{n}/@var{d}, "
15 "and optional grace timin @var{gn}/@var{gd}.\n"
18 "Moment is a point in musical time. "
19 "It is consists of a pair of rationals (@var{m}, @var{g}), "
20 "where @var{m} is the timing for the main\n"
21 "notes, and @var{g} the timing for grace notes. "
22 "In absence of grace notes, @var{g} is zero.\n")
24 LY_ASSERT_TYPE (scm_is_integer
, n
, 1);
25 LY_ASSERT_TYPE (scm_is_integer
, d
, 2);
28 if (gn
!= SCM_UNDEFINED
)
30 LY_ASSERT_TYPE (scm_is_integer
, gn
, 3);
31 grace_num
= scm_to_int (gn
);
35 if (gd
!= SCM_UNDEFINED
)
37 LY_ASSERT_TYPE (scm_is_integer
, gd
, 4);
38 grace_den
= scm_to_int (gd
);
41 return Moment (Rational (scm_to_int (n
), scm_to_int (d
)),
42 Rational (grace_num
, grace_den
)).smobbed_copy ();
45 LY_DEFINE (ly_moment_sub
, "ly:moment-sub",
46 2, 0, 0, (SCM a
, SCM b
),
47 "Subtract two moments.")
49 LY_ASSERT_SMOB (Moment
, a
, 1);
50 LY_ASSERT_SMOB (Moment
, b
, 2);
52 Moment
*ma
= unsmob_moment (a
);
53 Moment
*mb
= unsmob_moment (b
);
55 return (*ma
- *mb
).smobbed_copy ();
58 LY_DEFINE (ly_moment_add
, "ly:moment-add",
59 2, 0, 0, (SCM a
, SCM b
),
62 LY_ASSERT_SMOB (Moment
, a
, 1);
63 LY_ASSERT_SMOB (Moment
, b
, 2);
65 Moment
*ma
= unsmob_moment (a
);
66 Moment
*mb
= unsmob_moment (b
);
68 return (*ma
+ *mb
).smobbed_copy ();
71 LY_DEFINE (ly_moment_mul
, "ly:moment-mul",
72 2, 0, 0, (SCM a
, SCM b
),
73 "Multiply two moments.")
75 LY_ASSERT_SMOB (Moment
, a
, 1);
76 LY_ASSERT_SMOB (Moment
, b
, 2);
78 Moment
*ma
= unsmob_moment (a
);
79 Moment
*mb
= unsmob_moment (b
);
80 return (*ma
* * mb
).smobbed_copy ();
83 LY_DEFINE (ly_moment_div
, "ly:moment-div",
84 2, 0, 0, (SCM a
, SCM b
),
85 "Divide two moments.")
87 LY_ASSERT_SMOB (Moment
, a
, 1);
88 LY_ASSERT_SMOB (Moment
, b
, 2);
90 Moment
*ma
= unsmob_moment (a
);
91 Moment
*mb
= unsmob_moment (b
);
93 return (*ma
/ * mb
).smobbed_copy ();
96 LY_DEFINE (ly_moment_mod
, "ly:moment-mod",
97 2, 0, 0, (SCM a
, SCM b
),
98 "Modulo of two moments.")
100 LY_ASSERT_SMOB (Moment
, a
, 1);
101 LY_ASSERT_SMOB (Moment
, b
, 2);
103 Moment
*ma
= unsmob_moment (a
);
104 Moment
*mb
= unsmob_moment (b
);
105 return (*ma
% * mb
).smobbed_copy ();
108 LY_DEFINE (ly_moment_grace_numerator
, "ly:moment-grace-numerator",
110 "Extract numerator from grace timing.")
112 LY_ASSERT_SMOB (Moment
, mom
, 1);
114 Moment
*ma
= unsmob_moment (mom
);
116 return scm_from_int (ma
->grace_part_
.numerator ());
119 LY_DEFINE (ly_moment_grace_denominator
, "ly:moment-grace-denominator",
121 "Extract denominator from grace timing.")
123 LY_ASSERT_SMOB (Moment
, mom
, 1);
124 Moment
*ma
= unsmob_moment (mom
);
126 return scm_from_int (ma
->grace_part_
.denominator ());
128 LY_DEFINE (ly_moment_main_numerator
, "ly:moment-main-numerator",
130 "Extract numerator from main timing.")
132 LY_ASSERT_SMOB (Moment
, mom
, 1);
133 Moment
*ma
= unsmob_moment (mom
);
135 return scm_from_int (ma
->main_part_
.numerator ());
138 LY_DEFINE (ly_moment_main_denominator
, "ly:moment-main-denominator",
140 "Extract denominator from main timing.")
142 LY_ASSERT_SMOB (Moment
, mom
, 1);
143 Moment
*ma
= unsmob_moment (mom
);
145 return scm_from_int (ma
->main_part_
.denominator ());
148 LY_DEFINE (ly_moment_less_p
, "ly:moment<?",
149 2, 0, 0, (SCM a
, SCM b
),
150 "Compare two moments.")
152 LY_ASSERT_SMOB (Moment
, a
, 1);
153 LY_ASSERT_SMOB (Moment
, b
, 2);
155 Moment
*ma
= unsmob_moment (a
);
156 Moment
*mb
= unsmob_moment (b
);
158 return ly_bool2scm (*ma
< *mb
);