1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1997--2021 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6 
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.
11 
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.
16 
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/>.
19 */
20 
21 #include "duration.hh"
22 
23 #include "misc.hh"
24 #include "moment.hh"
25 
26 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
27 SCM
less_p(SCM p1,SCM p2)28 Duration::less_p (SCM p1, SCM p2)
29 {
30   Duration *a = unsmob<Duration> (p1);
31   Duration *b = unsmob<Duration> (p2);
32 
33   if (compare (*a, *b) < 0)
34     return SCM_BOOL_T;
35   else
36     return SCM_BOOL_F;
37 }
38 
39 LY_DEFINE (ly_duration_less_p, "ly:duration<?",
40            2, 0, 0, (SCM p1, SCM p2),
41            "Is @var{p1} shorter than @var{p2}?")
42 {
43   auto *const a = LY_ASSERT_SMOB (Duration, p1, 1);
44   auto *const b = LY_ASSERT_SMOB (Duration, p2, 2);
45 
46   if (Duration::compare (*a, *b) < 0)
47     return SCM_BOOL_T;
48   else
49     return SCM_BOOL_F;
50 }
51 
52 LY_DEFINE (ly_make_duration, "ly:make-duration",
53            1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
54            "Make a duration.  @var{length} is the negative logarithm"
55            " (base@tie{}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"
59            "\n"
60            "The duration factor is optionally given by integers @var{num} and"
61            " @var{den}, alternatively by a single rational number.\n"
62            "\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.")
66 {
67   LY_ASSERT_TYPE (scm_is_integer, length, 1);
68 
69   int dots = 0;
70   if (!SCM_UNBNDP (dotcount))
71     {
72       LY_ASSERT_TYPE (scm_is_integer, dotcount, 2);
73       dots = scm_to_int (dotcount);
74     }
75 
76   bool compress = false;
77   if (!SCM_UNBNDP (num))
78     {
79       LY_ASSERT_TYPE (is_scm<Rational>, num, 3);
80       compress = true;
81     }
82   else
83     num = to_scm (1);
84 
85   if (!SCM_UNBNDP (den))
86     {
87       LY_ASSERT_TYPE (scm_is_integer, den, 4);
88       compress = true;
89     }
90   else
91     den = to_scm (1);
92 
93   Duration p (scm_to_int (length), dots);
94   if (compress)
95     p = p.compressed (from_scm<Rational> (scm_divide (num, den)));
96 
97   return p.smobbed_copy ();
98 }
99 
100 LY_DEFINE (ly_duration_log, "ly:duration-log",
101            1, 0, 0, (SCM dur),
102            "Extract the duration log from @var{dur}.")
103 {
104   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
105   return to_scm (a->duration_log ());
106 }
107 
108 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
109            1, 0, 0, (SCM dur),
110            "Extract the dot count from @var{dur}.")
111 {
112   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
113   return to_scm (a->dot_count ());
114 }
115 
116 LY_DEFINE (ly_intlog2, "ly:intlog2",
117            1, 0, 0, (SCM d),
118            "The 2-logarithm of 1/@var{d}.")
119 {
120   LY_ASSERT_TYPE (scm_is_number, d, 1);
121   int log = intlog2 (scm_to_int (d));
122   return to_scm (log);
123 }
124 
125 LY_DEFINE (ly_duration_length, "ly:duration-length",
126            1, 0, 0, (SCM dur),
127            "The length of the duration as a @code{moment}.")
128 {
129   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
130   return Moment (a->get_length ()).smobbed_copy ();
131 }
132 
133 LY_DEFINE (ly_duration_2_string, "ly:duration->string",
134            1, 0, 0, (SCM dur),
135            "Convert @var{dur} to a string.")
136 {
137   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
138   return ly_string2scm (a->to_string ());
139 }
140 
141 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
142            1, 0, 0, (SCM dur),
143            "Extract the compression factor from @var{dur}."
144            "  Return it as a pair.")
145 {
146   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
147   Rational r = a->factor ();
148   return scm_cons (to_scm (r.num ()), to_scm (r.den ()));
149 }
150 
151 // This is likely what ly:duration-factor should have been in the
152 // first place.
153 LY_DEFINE (ly_duration_scale, "ly:duration-scale",
154            1, 0, 0, (SCM dur),
155            "Extract the compression factor from @var{dur}."
156            "  Return it as a rational.")
157 {
158   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
159   Rational r = a->factor ();
160 
161   return to_scm (r);
162 }
163 
164 LY_DEFINE (ly_duration_compress, "ly:duration-compress",
165            2, 0, 0, (SCM dur, SCM factor),
166            "Compress @var{dur} by rational @var{factor}.")
167 {
168   auto *const a = LY_ASSERT_SMOB (Duration, dur, 1);
169   LY_ASSERT_TYPE (is_scm<Rational>, factor, 2);
170 
171   return a->compressed (from_scm<Rational> (factor)).smobbed_copy ();
172 }
173