]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration-scheme.cc
Run `make grand-replace'.
[lilypond.git] / lily / duration-scheme.cc
1 /*
2   duration.cc -- implement Duration
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2008 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "duration.hh"
11 #include "misc.hh"
12
13 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
14 SCM
15 Duration::less_p (SCM p1, SCM p2)
16 {
17   Duration *a = unsmob_duration (p1);
18   Duration *b = unsmob_duration (p2);
19
20   if (compare (*a, *b) < 0)
21     return SCM_BOOL_T;
22   else
23     return SCM_BOOL_F;
24 }
25
26 LY_DEFINE (ly_duration_less_p, "ly:duration<?",
27            2, 0, 0, (SCM p1, SCM p2),
28            "Is @var{p1} shorter than @var{p2}?")
29 {
30   LY_ASSERT_SMOB (Duration, p1, 1);
31   LY_ASSERT_SMOB (Duration, p2, 2);
32
33   Duration *a = unsmob_duration (p1);
34   Duration *b = unsmob_duration (p2);
35
36   if (Duration::compare (*a, *b) < 0)
37     return SCM_BOOL_T;
38   else
39     return SCM_BOOL_F;
40 }
41
42 LY_DEFINE (ly_make_duration, "ly:make-duration",
43            1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
44            "@var{length} is the negative logarithm (base 2) of the duration:"
45            " 1@tie{}is a half note, 2@tie{}is a quarter note, 3@tie{}is an"
46            " eighth note, etc.  The number of dots after the note is given by"
47            " the optional argument @var{dotcount}.\n"
48            "\n"
49            "The duration factor is optionally given by @var{num} and"
50            " @var{den}.\n"
51            "\n"
52            "A duration is a musical duration, i.e., a length of time"
53            " described by a power of two (whole, half, quarter, etc.) and a"
54            " number of augmentation dots.")
55 {
56   LY_ASSERT_TYPE (scm_is_integer, length, 1);
57
58   int dots = 0;
59   if (dotcount != SCM_UNDEFINED)
60     {
61       LY_ASSERT_TYPE (scm_is_integer, dotcount,2);
62       dots = scm_to_int (dotcount);
63     }
64
65   bool compress = false;
66   if (num != SCM_UNDEFINED)
67     {
68       LY_ASSERT_TYPE (scm_is_number, num, 3);
69       compress = true;
70     }
71   else
72     num = scm_from_int (1);
73
74   if (den != SCM_UNDEFINED)
75     {
76       LY_ASSERT_TYPE (scm_is_number, den, 4);
77       compress = true;
78     }
79   else
80     den = scm_from_int (1);
81
82   Duration p (scm_to_int (length), dots);
83   if (compress)
84     p = p.compressed (Rational (scm_to_int (num), scm_to_int (den)));
85
86   return p.smobbed_copy ();
87 }
88
89 LY_DEFINE (ly_duration_log, "ly:duration-log",
90            1, 0, 0, (SCM dur),
91            "Extract the duration log from @var{dur}.")
92 {
93   LY_ASSERT_SMOB (Duration, dur, 1);
94   return scm_from_int (unsmob_duration (dur)->duration_log ());
95 }
96
97 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
98            1, 0, 0, (SCM dur),
99            "Extract the dot count from @var{dur}.")
100 {
101   LY_ASSERT_SMOB (Duration, dur, 1);
102   return scm_from_int (unsmob_duration (dur)->dot_count ());
103 }
104
105 LY_DEFINE (ly_intlog2, "ly:intlog2",
106            1, 0, 0, (SCM d),
107            "The 2-logarithm of 1/@var{d}.")
108 {
109   LY_ASSERT_TYPE (scm_is_number, d, 1);
110   int log = intlog2 (scm_to_int (d));
111   return scm_from_int (log);
112 }
113
114 LY_DEFINE (ly_duration_length, "ly:duration-length",
115            1, 0, 0, (SCM dur),
116            "The length of the duration as a @code{moment}.")
117 {
118   LY_ASSERT_SMOB (Duration, dur, 1);
119   return Moment (unsmob_duration (dur)->get_length ()).smobbed_copy ();
120 }
121
122 LY_DEFINE (ly_duration_2_string, "ly:duration->string",
123            1, 0, 0, (SCM dur),
124            "Convert @var{dur} to a string.")
125 {
126   LY_ASSERT_SMOB (Duration, dur, 1);
127   return ly_string2scm (unsmob_duration (dur)->to_string ());
128 }
129
130 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
131            1, 0, 0, (SCM dur),
132            "Extract the compression factor from @var{dur}."
133            "  Return it as a pair.")
134 {
135   LY_ASSERT_SMOB (Duration, dur, 1);
136   Rational r = unsmob_duration (dur)->factor ();
137   return scm_cons (scm_from_int64 (r.num ()), scm_from_int64 (r.den ()));
138 }