2 duration.cc -- implement Duration
4 source file of the LilyPond music typesetter
6 (c) 1997--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include "lily-proto.hh"
17 #include "duration.hh"
18 #include "ly-smobs.icc"
22 Duration::compare (Duration const &left, Duration const &right)
24 return Rational::compare (left.get_length (), right.get_length ());
31 factor_ = Rational (1, 1);
34 Duration::Duration (int log, int d)
38 factor_ = Rational (1, 1);
42 Duration::compressed (Rational m) const
50 Duration::get_length () const
52 Rational mom (1 << abs (durlog_));
55 mom = Rational (1) / mom;
58 for (int i = 0; i < dots_; i++)
60 delta /= Rational (2);
68 Duration::to_string () const
73 s = "log = " + ::to_string (durlog_);
75 s = ::to_string (1 << durlog_);
77 s += ::to_string ('.', dots_);
78 if (factor_ != Moment (Rational (1, 1)))
79 s += "*" + factor_.to_string ();
84 IMPLEMENT_TYPE_P (Duration, "ly:duration?");
87 Duration::mark_smob (SCM)
92 IMPLEMENT_SIMPLE_SMOBS (Duration);
94 Duration::print_smob (SCM s, SCM port, scm_print_state *)
96 Duration *r = (Duration *) ly_cdr (s);
98 scm_puts ("#<Duration ", port);
99 scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
100 scm_puts (" >", port);
106 Duration::equal_p (SCM a , SCM b)
108 Duration *p = (Duration *) ly_cdr (a);
109 Duration *q = (Duration *) ly_cdr (b);
111 bool eq = p->dots_ == q->dots_
112 && p->durlog_ == q->durlog_
113 && p->factor_ == q->factor_;
115 return eq ? SCM_BOOL_T : SCM_BOOL_F;
118 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
120 Duration::less_p (SCM p1, SCM p2)
122 Duration *a = unsmob_duration (p1);
123 Duration *b = unsmob_duration (p2);
125 if (compare (*a, *b) < 0)
131 LY_DEFINE (ly_duration_less_p, "ly:duration<?",
132 2, 0, 0, (SCM p1, SCM p2),
133 "Is @var{p1} shorter than @var{p2}?")
135 Duration *a = unsmob_duration (p1);
136 Duration *b = unsmob_duration (p2);
138 SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Duration");
139 SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Duration");
141 if (Duration::compare (*a, *b) < 0)
147 LY_DEFINE (ly_make_duration, "ly:make-duration",
148 1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
149 "@var{length} is the negative logarithm (base 2) of the duration:\n"
150 "1 is a half note, 2 is a quarter note, 3 is an eighth\n"
151 "note, etc. The number of dots after the note is given by\n"
152 "the optional argument @var{dotcount}.\n"
154 "The duration factor is optionally given by @var{num}\n"
156 "A duration is a musical duration, "
157 "i.e. a length of time described by a power of two "
158 "(whole, half, quarter, etc.) and a number of augmentation\n"
161 SCM_ASSERT_TYPE (scm_integer_p (length) == SCM_BOOL_T,
162 length, SCM_ARG1, __FUNCTION__, "integer");
165 if (dotcount != SCM_UNDEFINED)
167 SCM_ASSERT_TYPE (scm_integer_p (dotcount) == SCM_BOOL_T,
168 dotcount, SCM_ARG2, __FUNCTION__, "integer");
169 dots = ly_scm2int (dotcount);
172 bool compress = false;
173 if (num != SCM_UNDEFINED)
175 SCM_ASSERT_TYPE (ly_c_number_p (num), length, SCM_ARG3, __FUNCTION__, "integer");
179 num = scm_int2num (1);
181 if (den != SCM_UNDEFINED)
183 SCM_ASSERT_TYPE (ly_c_number_p (den), length, SCM_ARG4, __FUNCTION__, "integer");
187 den = scm_int2num (1);
189 Duration p (ly_scm2int (length), dots);
191 p = p.compressed (Rational (ly_scm2int (num), ly_scm2int (den)));
193 return p.smobbed_copy ();
196 LY_DEFINE (ly_duration_log, "ly:duration-log",
198 "Extract the duration log from @var{dur}")
200 SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
201 return scm_int2num (unsmob_duration (dur)->duration_log ());
204 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
206 "Extract the dot count from @var{dur}")
208 SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
209 return scm_int2num (unsmob_duration (dur)->dot_count ());
212 LY_DEFINE (ly_intlog2, "ly:intlog2",
214 "The 2-logarithm of 1/@var{d}.")
216 SCM_ASSERT_TYPE (ly_c_number_p (d), d, SCM_ARG1, __FUNCTION__, "integer");
217 int log = intlog2 (ly_scm2int (d));
218 return scm_int2num (log);
221 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
223 "Extract the compression factor from @var{dur}. Return as a pair.")
225 SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
226 Rational r = unsmob_duration (dur)->factor ();
227 return scm_cons (scm_int2num (r.num ()), scm_int2num (r.den ()));
231 Duration::duration_log () const
237 Duration::dot_count () const