2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "duration.hh"
24 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
26 Duration::less_p (SCM p1, SCM p2)
28 Duration *a = Duration::unsmob (p1);
29 Duration *b = Duration::unsmob (p2);
31 if (compare (*a, *b) < 0)
37 LY_DEFINE (ly_duration_less_p, "ly:duration<?",
38 2, 0, 0, (SCM p1, SCM p2),
39 "Is @var{p1} shorter than @var{p2}?")
41 LY_ASSERT_SMOB (Duration, p1, 1);
42 LY_ASSERT_SMOB (Duration, p2, 2);
44 Duration *a = Duration::unsmob (p1);
45 Duration *b = Duration::unsmob (p2);
47 if (Duration::compare (*a, *b) < 0)
53 LY_DEFINE (ly_make_duration, "ly:make-duration",
54 1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
55 "@var{length} is the negative logarithm (base 2) of the duration:"
56 " 1@tie{}is a half note, 2@tie{}is a quarter note, 3@tie{}is an"
57 " eighth note, etc. The number of dots after the note is given by"
58 " the optional argument @var{dotcount}.\n"
60 "The duration factor is optionally given by integers @var{num} and"
61 " @var{den}, alternatively by a single rational number.\n"
63 "A duration is a musical duration, i.e., a length of time"
64 " described by a power of two (whole, half, quarter, etc.) and a"
65 " number of augmentation dots.")
67 LY_ASSERT_TYPE (scm_is_integer, length, 1);
70 if (!SCM_UNBNDP (dotcount))
72 LY_ASSERT_TYPE (scm_is_integer, dotcount, 2);
73 dots = scm_to_int (dotcount);
76 bool compress = false;
77 if (!SCM_UNBNDP (num))
79 LY_ASSERT_TYPE (ly_is_rational, num, 3);
83 num = scm_from_int (1);
85 if (!SCM_UNBNDP (den))
87 LY_ASSERT_TYPE (scm_is_integer, den, 4);
91 den = scm_from_int (1);
93 Duration p (scm_to_int (length), dots);
95 p = p.compressed (ly_scm2rational (scm_divide (num, den)));
97 return p.smobbed_copy ();
100 LY_DEFINE (ly_duration_log, "ly:duration-log",
102 "Extract the duration log from @var{dur}.")
104 LY_ASSERT_SMOB (Duration, dur, 1);
105 return scm_from_int (Duration::unsmob (dur)->duration_log ());
108 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
110 "Extract the dot count from @var{dur}.")
112 LY_ASSERT_SMOB (Duration, dur, 1);
113 return scm_from_int (Duration::unsmob (dur)->dot_count ());
116 LY_DEFINE (ly_intlog2, "ly:intlog2",
118 "The 2-logarithm of 1/@var{d}.")
120 LY_ASSERT_TYPE (scm_is_number, d, 1);
121 int log = intlog2 (scm_to_int (d));
122 return scm_from_int (log);
125 LY_DEFINE (ly_duration_length, "ly:duration-length",
127 "The length of the duration as a @code{moment}.")
129 LY_ASSERT_SMOB (Duration, dur, 1);
130 return Moment (Duration::unsmob (dur)->get_length ()).smobbed_copy ();
133 LY_DEFINE (ly_duration_2_string, "ly:duration->string",
135 "Convert @var{dur} to a string.")
137 LY_ASSERT_SMOB (Duration, dur, 1);
138 return ly_string2scm (Duration::unsmob (dur)->to_string ());
141 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
143 "Extract the compression factor from @var{dur}."
144 " Return it as a pair.")
146 LY_ASSERT_SMOB (Duration, dur, 1);
147 Rational r = Duration::unsmob (dur)->factor ();
148 return scm_cons (scm_from_int64 (r.num ()), scm_from_int64 (r.den ()));
151 // This is likely what ly:duration-factor should have been in the
153 LY_DEFINE (ly_duration_scale, "ly:duration-scale",
155 "Extract the compression factor from @var{dur}."
156 " Return it as a rational.")
158 LY_ASSERT_SMOB (Duration, dur, 1);
159 Rational r = Duration::unsmob (dur)->factor ();
161 return ly_rational2scm (r);