Merge branch 'fret-diagram-details'
[lilypond/csorensen.git] / lily / moment-scheme.cc
blob85155034c597bc6c3bd187d2960832a27f4b4387
1 /*
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>
7 */
9 #include "moment.hh"
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 timing @var{gn}/@var{gd}.\n"
16 "\n"
17 "A @dfn{moment} is a point in musical time. It consists of"
18 " a pair of rationals (@var{m},@tie{}@var{g}), where @var{m} is"
19 " the timing for the main notes, and @var{g} the timing for"
20 " grace notes. In absence of grace notes, @var{g}@tie{}is zero.")
22 LY_ASSERT_TYPE (scm_is_integer, n, 1);
23 LY_ASSERT_TYPE (scm_is_integer, d, 2);
25 int grace_num = 0;
26 if (gn != SCM_UNDEFINED)
28 LY_ASSERT_TYPE (scm_is_integer, gn, 3);
29 grace_num = scm_to_int (gn);
32 int grace_den = 1;
33 if (gd != SCM_UNDEFINED)
35 LY_ASSERT_TYPE (scm_is_integer, gd, 4);
36 grace_den = scm_to_int (gd);
39 return Moment (Rational (scm_to_int (n), scm_to_int (d)),
40 Rational (grace_num, grace_den)).smobbed_copy ();
43 LY_DEFINE (ly_moment_sub, "ly:moment-sub",
44 2, 0, 0, (SCM a, SCM b),
45 "Subtract two moments.")
47 LY_ASSERT_SMOB (Moment, a, 1);
48 LY_ASSERT_SMOB (Moment, b, 2);
50 Moment *ma = unsmob_moment (a);
51 Moment *mb = unsmob_moment (b);
53 return (*ma - *mb).smobbed_copy ();
56 LY_DEFINE (ly_moment_add, "ly:moment-add",
57 2, 0, 0, (SCM a, SCM b),
58 "Add two moments.")
60 LY_ASSERT_SMOB (Moment, a, 1);
61 LY_ASSERT_SMOB (Moment, b, 2);
63 Moment *ma = unsmob_moment (a);
64 Moment *mb = unsmob_moment (b);
66 return (*ma + *mb).smobbed_copy ();
69 LY_DEFINE (ly_moment_mul, "ly:moment-mul",
70 2, 0, 0, (SCM a, SCM b),
71 "Multiply two moments.")
73 LY_ASSERT_SMOB (Moment, a, 1);
74 LY_ASSERT_SMOB (Moment, b, 2);
76 Moment *ma = unsmob_moment (a);
77 Moment *mb = unsmob_moment (b);
78 return (*ma * * mb).smobbed_copy ();
81 LY_DEFINE (ly_moment_div, "ly:moment-div",
82 2, 0, 0, (SCM a, SCM b),
83 "Divide two moments.")
85 LY_ASSERT_SMOB (Moment, a, 1);
86 LY_ASSERT_SMOB (Moment, b, 2);
88 Moment *ma = unsmob_moment (a);
89 Moment *mb = unsmob_moment (b);
91 return (*ma / * mb).smobbed_copy ();
94 LY_DEFINE (ly_moment_mod, "ly:moment-mod",
95 2, 0, 0, (SCM a, SCM b),
96 "Modulo of two moments.")
98 LY_ASSERT_SMOB (Moment, a, 1);
99 LY_ASSERT_SMOB (Moment, b, 2);
101 Moment *ma = unsmob_moment (a);
102 Moment *mb = unsmob_moment (b);
103 return (*ma % * mb).smobbed_copy ();
106 LY_DEFINE (ly_moment_grace_numerator, "ly:moment-grace-numerator",
107 1, 0, 0, (SCM mom),
108 "Extract numerator from grace timing.")
110 LY_ASSERT_SMOB (Moment, mom, 1);
112 Moment *ma = unsmob_moment (mom);
114 return scm_from_int (ma->grace_part_.numerator ());
117 LY_DEFINE (ly_moment_grace_denominator, "ly:moment-grace-denominator",
118 1, 0, 0, (SCM mom),
119 "Extract denominator from grace timing.")
121 LY_ASSERT_SMOB (Moment, mom, 1);
122 Moment *ma = unsmob_moment (mom);
124 return scm_from_int (ma->grace_part_.denominator ());
126 LY_DEFINE (ly_moment_main_numerator, "ly:moment-main-numerator",
127 1, 0, 0, (SCM mom),
128 "Extract numerator from main timing.")
130 LY_ASSERT_SMOB (Moment, mom, 1);
131 Moment *ma = unsmob_moment (mom);
133 return scm_from_int (ma->main_part_.numerator ());
136 LY_DEFINE (ly_moment_main_denominator, "ly:moment-main-denominator",
137 1, 0, 0, (SCM mom),
138 "Extract denominator from main timing.")
140 LY_ASSERT_SMOB (Moment, mom, 1);
141 Moment *ma = unsmob_moment (mom);
143 return scm_from_int (ma->main_part_.denominator ());
146 LY_DEFINE (ly_moment_less_p, "ly:moment<?",
147 2, 0, 0, (SCM a, SCM b),
148 "Compare two moments.")
150 LY_ASSERT_SMOB (Moment, a, 1);
151 LY_ASSERT_SMOB (Moment, b, 2);
153 Moment *ma = unsmob_moment (a);
154 Moment *mb = unsmob_moment (b);
156 return ly_bool2scm (*ma < *mb);