]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration-scheme.cc
* lily/duration-scheme.cc: bugfix: correct parameters to
[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--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9 */
10
11 #include "duration.hh"
12 #include "misc.hh"
13
14 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
15 SCM
16 Duration::less_p (SCM p1, SCM p2)
17 {
18   Duration *a = unsmob_duration (p1);
19   Duration *b = unsmob_duration (p2);
20
21   if (compare (*a, *b) < 0)
22     return SCM_BOOL_T;
23   else
24     return SCM_BOOL_F;
25 }
26
27 LY_DEFINE (ly_duration_less_p, "ly:duration<?",
28            2, 0, 0, (SCM p1, SCM p2),
29           "Is @var{p1} shorter than @var{p2}?")
30 {
31   Duration *a = unsmob_duration (p1);
32   Duration *b = unsmob_duration (p2);
33
34   SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Duration");
35   SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Duration");
36
37   if (Duration::compare (*a, *b) < 0)
38     return SCM_BOOL_T;
39   else
40     return SCM_BOOL_F;
41 }
42
43 LY_DEFINE (ly_make_duration, "ly:make-duration",
44            1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
45            "@var{length} is the negative logarithm (base 2) of the duration:\n"
46            "1 is a half note, 2 is a quarter note, 3 is an eighth\n"
47            "note, etc.  The number of dots after the note is given by\n"
48            "the optional argument @var{dotcount}.\n"
49            "\n"
50            "The duration factor is optionally given by @var{num}\n"
51            "and @var{den}.\n\n"
52            "A duration is a musical duration, "
53            "i.e. a length of time described by a power of two "
54            "(whole, half, quarter, etc.) and a number of augmentation\n"
55            "dots. \n")
56 {
57   SCM_ASSERT_TYPE (scm_integer_p (length) == SCM_BOOL_T,
58                    length, SCM_ARG1, __FUNCTION__, "integer");
59
60   int dots = 0;
61   if (dotcount != SCM_UNDEFINED)
62     {
63       SCM_ASSERT_TYPE (scm_integer_p (dotcount) == SCM_BOOL_T,
64                        dotcount, SCM_ARG2, __FUNCTION__, "integer");
65       dots = scm_to_int (dotcount);
66     }
67
68   bool compress = false;
69   if (num != SCM_UNDEFINED)
70     {
71       SCM_ASSERT_TYPE (scm_is_number (num), num, SCM_ARG3, __FUNCTION__, "integer");
72       compress = true;
73     }
74   else
75     num = scm_int2num (1);
76
77   if (den != SCM_UNDEFINED)
78     {
79       SCM_ASSERT_TYPE (scm_is_number (den), den, SCM_ARG4, __FUNCTION__, "integer");
80       compress = true;
81     }
82   else
83     den = scm_int2num (1);
84
85   Duration p (scm_to_int (length), dots);
86   if (compress)
87     p = p.compressed (Rational (scm_to_int (num), scm_to_int (den)));
88
89   return p.smobbed_copy ();
90 }
91
92 LY_DEFINE (ly_duration_log, "ly:duration-log",
93            1, 0, 0, (SCM dur),
94           "Extract the duration log from @var{dur}")
95 {
96   SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
97   return scm_int2num (unsmob_duration (dur)->duration_log ());
98 }
99
100 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
101            1, 0, 0, (SCM dur),
102           "Extract the dot count from @var{dur}")
103 {
104   SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
105   return scm_int2num (unsmob_duration (dur)->dot_count ());
106 }
107
108 LY_DEFINE (ly_intlog2, "ly:intlog2",
109            1, 0, 0, (SCM d),
110           "The 2-logarithm of 1/@var{d}.")
111 {
112   SCM_ASSERT_TYPE (scm_is_number (d), d, SCM_ARG1, __FUNCTION__, "integer");
113   int log = intlog2 (scm_to_int (d));
114   return scm_int2num (log);
115 }
116
117 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
118            1, 0, 0, (SCM dur),
119           "Extract the compression factor from @var{dur}. Return as a pair.")
120 {
121   SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
122   Rational r = unsmob_duration (dur)->factor ();
123   return scm_cons (scm_int2num (r.num ()), scm_int2num (r.den ()));
124 }