(readPostTable): ugh. Kludge: nglyphs in maxp
[lilypond.git] / lily / duration-scheme.cc
blob1d840a753e2e9649005ad69ec6cd9f5acdf3fa31
1 /*
2 duration.cc -- implement Duration
4 source file of the LilyPond music typesetter
6 (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "duration.hh"
11 #include "misc.hh"
13 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
14 SCM
15 Duration::less_p (SCM p1, SCM p2)
17 Duration *a = unsmob_duration (p1);
18 Duration *b = unsmob_duration (p2);
20 if (compare (*a, *b) < 0)
21 return SCM_BOOL_T;
22 else
23 return SCM_BOOL_F;
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}?")
30 Duration *a = unsmob_duration (p1);
31 Duration *b = unsmob_duration (p2);
33 SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Duration");
34 SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Duration");
36 if (Duration::compare (*a, *b) < 0)
37 return SCM_BOOL_T;
38 else
39 return SCM_BOOL_F;
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:\n"
45 "1 is a half note, 2 is a quarter note, 3 is an eighth\n"
46 "note, etc. The number of dots after the note is given by\n"
47 "the optional argument @var{dotcount}.\n"
48 "\n"
49 "The duration factor is optionally given by @var{num}\n"
50 "and @var{den}.\n\n"
51 "A duration is a musical duration, "
52 "i.e. a length of time described by a power of two "
53 "(whole, half, quarter, etc.) and a number of augmentation\n"
54 "dots. \n")
56 SCM_ASSERT_TYPE (scm_integer_p (length) == SCM_BOOL_T,
57 length, SCM_ARG1, __FUNCTION__, "integer");
59 int dots = 0;
60 if (dotcount != SCM_UNDEFINED)
62 SCM_ASSERT_TYPE (scm_integer_p (dotcount) == SCM_BOOL_T,
63 dotcount, SCM_ARG2, __FUNCTION__, "integer");
64 dots = scm_to_int (dotcount);
67 bool compress = false;
68 if (num != SCM_UNDEFINED)
70 SCM_ASSERT_TYPE (scm_is_number (num), num, SCM_ARG3, __FUNCTION__, "integer");
71 compress = true;
73 else
74 num = scm_int2num (1);
76 if (den != SCM_UNDEFINED)
78 SCM_ASSERT_TYPE (scm_is_number (den), den, SCM_ARG4, __FUNCTION__, "integer");
79 compress = true;
81 else
82 den = scm_int2num (1);
84 Duration p (scm_to_int (length), dots);
85 if (compress)
86 p = p.compressed (Rational (scm_to_int (num), scm_to_int (den)));
88 return p.smobbed_copy ();
91 LY_DEFINE (ly_duration_log, "ly:duration-log",
92 1, 0, 0, (SCM dur),
93 "Extract the duration log from @var{dur}")
95 SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
96 return scm_int2num (unsmob_duration (dur)->duration_log ());
99 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
100 1, 0, 0, (SCM dur),
101 "Extract the dot count from @var{dur}")
103 SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
104 return scm_int2num (unsmob_duration (dur)->dot_count ());
107 LY_DEFINE (ly_intlog2, "ly:intlog2",
108 1, 0, 0, (SCM d),
109 "The 2-logarithm of 1/@var{d}.")
111 SCM_ASSERT_TYPE (scm_is_number (d), d, SCM_ARG1, __FUNCTION__, "integer");
112 int log = intlog2 (scm_to_int (d));
113 return scm_int2num (log);
116 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
117 1, 0, 0, (SCM dur),
118 "Extract the compression factor from @var{dur}. Return as a pair.")
120 SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
121 Rational r = unsmob_duration (dur)->factor ();
122 return scm_cons (scm_int2num (r.num ()), scm_int2num (r.den ()));