2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 /* TODO: add optional factor argument. */
23 LY_DEFINE (ly_make_moment
, "ly:make-moment",
24 2, 2, 0, (SCM n
, SCM d
, SCM gn
, SCM gd
),
25 "Create the rational number with main timing @var{n}/@var{d},"
26 " and optional grace timing @var{gn}/@var{gd}.\n"
28 "A @dfn{moment} is a point in musical time. It consists of"
29 " a pair of rationals (@var{m},@tie{}@var{g}), where @var{m} is"
30 " the timing for the main notes, and @var{g} the timing for"
31 " grace notes. In absence of grace notes, @var{g}@tie{}is zero.")
33 LY_ASSERT_TYPE (scm_is_integer
, n
, 1);
34 LY_ASSERT_TYPE (scm_is_integer
, d
, 2);
37 if (gn
!= SCM_UNDEFINED
)
39 LY_ASSERT_TYPE (scm_is_integer
, gn
, 3);
40 grace_num
= scm_to_int (gn
);
44 if (gd
!= SCM_UNDEFINED
)
46 LY_ASSERT_TYPE (scm_is_integer
, gd
, 4);
47 grace_den
= scm_to_int (gd
);
50 return Moment (Rational (scm_to_int (n
), scm_to_int (d
)),
51 Rational (grace_num
, grace_den
)).smobbed_copy ();
54 LY_DEFINE (ly_moment_sub
, "ly:moment-sub",
55 2, 0, 0, (SCM a
, SCM b
),
56 "Subtract two moments.")
58 LY_ASSERT_SMOB (Moment
, a
, 1);
59 LY_ASSERT_SMOB (Moment
, b
, 2);
61 Moment
*ma
= unsmob_moment (a
);
62 Moment
*mb
= unsmob_moment (b
);
64 return (*ma
- *mb
).smobbed_copy ();
67 LY_DEFINE (ly_moment_add
, "ly:moment-add",
68 2, 0, 0, (SCM a
, SCM b
),
71 LY_ASSERT_SMOB (Moment
, a
, 1);
72 LY_ASSERT_SMOB (Moment
, b
, 2);
74 Moment
*ma
= unsmob_moment (a
);
75 Moment
*mb
= unsmob_moment (b
);
77 return (*ma
+ *mb
).smobbed_copy ();
80 LY_DEFINE (ly_moment_mul
, "ly:moment-mul",
81 2, 0, 0, (SCM a
, SCM b
),
82 "Multiply two moments.")
84 LY_ASSERT_SMOB (Moment
, a
, 1);
85 LY_ASSERT_SMOB (Moment
, b
, 2);
87 Moment
*ma
= unsmob_moment (a
);
88 Moment
*mb
= unsmob_moment (b
);
89 return (*ma
* * mb
).smobbed_copy ();
92 LY_DEFINE (ly_moment_div
, "ly:moment-div",
93 2, 0, 0, (SCM a
, SCM b
),
94 "Divide two moments.")
96 LY_ASSERT_SMOB (Moment
, a
, 1);
97 LY_ASSERT_SMOB (Moment
, b
, 2);
99 Moment
*ma
= unsmob_moment (a
);
100 Moment
*mb
= unsmob_moment (b
);
102 return (*ma
/ * mb
).smobbed_copy ();
105 LY_DEFINE (ly_moment_mod
, "ly:moment-mod",
106 2, 0, 0, (SCM a
, SCM b
),
107 "Modulo of two moments.")
109 LY_ASSERT_SMOB (Moment
, a
, 1);
110 LY_ASSERT_SMOB (Moment
, b
, 2);
112 Moment
*ma
= unsmob_moment (a
);
113 Moment
*mb
= unsmob_moment (b
);
114 return (*ma
% * mb
).smobbed_copy ();
117 LY_DEFINE (ly_moment_grace_numerator
, "ly:moment-grace-numerator",
119 "Extract numerator from grace timing.")
121 LY_ASSERT_SMOB (Moment
, mom
, 1);
123 Moment
*ma
= unsmob_moment (mom
);
125 return scm_from_int64 (ma
->grace_part_
.numerator ());
128 LY_DEFINE (ly_moment_grace_denominator
, "ly:moment-grace-denominator",
130 "Extract denominator from grace timing.")
132 LY_ASSERT_SMOB (Moment
, mom
, 1);
133 Moment
*ma
= unsmob_moment (mom
);
135 return scm_from_int64 (ma
->grace_part_
.denominator ());
137 LY_DEFINE (ly_moment_main_numerator
, "ly:moment-main-numerator",
139 "Extract numerator from main timing.")
141 LY_ASSERT_SMOB (Moment
, mom
, 1);
142 Moment
*ma
= unsmob_moment (mom
);
144 return scm_from_int64 (ma
->main_part_
.numerator ());
147 LY_DEFINE (ly_moment_main_denominator
, "ly:moment-main-denominator",
149 "Extract denominator from main timing.")
151 LY_ASSERT_SMOB (Moment
, mom
, 1);
152 Moment
*ma
= unsmob_moment (mom
);
154 return scm_from_int64 (ma
->main_part_
.denominator ());
157 LY_DEFINE (ly_moment_less_p
, "ly:moment<?",
158 2, 0, 0, (SCM a
, SCM b
),
159 "Compare two moments.")
161 LY_ASSERT_SMOB (Moment
, a
, 1);
162 LY_ASSERT_SMOB (Moment
, b
, 2);
164 Moment
*ma
= unsmob_moment (a
);
165 Moment
*mb
= unsmob_moment (b
);
167 return ly_bool2scm (*ma
< *mb
);