]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration.cc
*** empty log message ***
[lilypond.git] / lily / duration.cc
1 /*
2   duration.cc -- implement Duration
3   
4   source file of the LilyPond music typesetter
5
6   (c)  1997--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7            Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9 */
10
11 #include <assert.h>
12
13 #include "misc.hh"
14 #include "lily-proto.hh"
15 #include "string.hh"
16 #include "moment.hh"
17 #include "duration.hh"
18 #include "ly-smobs.icc"
19
20
21
22 int
23 Duration::compare (Duration const &left, Duration const &right)
24 {
25   return Rational::compare (left.get_length (), right.get_length ());
26 }
27
28 Duration::Duration ()
29 {
30   durlog_ = 0;
31   dots_ = 0;
32   factor_ = Rational (1,1);
33 }
34
35 Duration::Duration (int l, int d)
36 {
37   durlog_ = l;
38   dots_ = d;
39   factor_ = Rational (1,1);
40 }
41
42 Duration
43 Duration::compressed (Rational m) const
44 {
45   Duration d (*this);
46   d.factor_ *= m;
47   return d;
48 }
49
50 Rational
51 Duration::get_length () const
52 {
53   Rational mom (1 << abs (durlog_));
54
55   if (durlog_> 0)
56     mom = Rational (1)/mom;
57
58   Rational delta = mom;
59
60   for (int d = dots_; d; d--)
61     {
62       delta /= Rational (2);
63       mom += delta;
64     }
65
66   return mom * factor_;
67 }
68
69
70
71 String
72 Duration::string () const
73 {
74   String s;
75
76   if (durlog_ < 0  )
77     s = "log = "  + to_string (durlog_);
78   else
79     s = to_string (1 << durlog_);
80   
81   s += to_string ('.', dots_);
82   if (factor_ != Moment (Rational (1,1)))
83     {
84       s += "*" + factor_.string ();
85     }
86   return s;
87 }
88
89
90 IMPLEMENT_TYPE_P (Duration, "ly:duration?");
91
92 SCM
93 Duration::mark_smob (SCM)
94 {
95   return SCM_EOL;
96 }
97
98 IMPLEMENT_SIMPLE_SMOBS (Duration);
99 int
100 Duration::print_smob (SCM s, SCM port, scm_print_state *)
101 {
102   Duration  *r = (Duration *) ly_cdr (s);
103      
104   scm_puts ("#<Duration ", port);
105   scm_display (scm_makfrom0str (r->string ().to_str0 ()), port);
106   scm_puts (" >", port);
107   
108   return 1;
109 }
110
111 SCM
112 Duration::equal_p (SCM a , SCM b)
113 {
114   Duration  *p = (Duration *) ly_cdr (a);
115   Duration  *q = (Duration *) ly_cdr (b);  
116
117   bool eq = p->dots_ == q->dots_
118     && p->durlog_ == q->durlog_
119     && p->factor_ == q->factor_;
120
121   return eq ? SCM_BOOL_T : SCM_BOOL_F;
122 }
123   
124 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
125 SCM
126 Duration::less_p (SCM p1, SCM p2)
127 {
128   Duration *a = unsmob_duration (p1);
129   Duration *b = unsmob_duration (p2);
130
131   if (compare (*a, *b) < 0)
132     return SCM_BOOL_T;
133   else
134     return SCM_BOOL_F;
135 }
136
137 LY_DEFINE(make_duration,
138           "ly:make-duration", 2, 2, 0, (SCM length, SCM dotcount,
139                                      SCM num, SCM den),
140 "         \n"
141 "@var{length} is the negative logarithm (base 2) of the duration:\n"
142 "1 is a half note, 2 is a quarter note, 3 is an eighth\n"
143 "note, etc.  The number of dots after the note is given by\n"
144 "@var{dotcount}.\n"
145 "\n"
146 "The duration factor is optionally given by @var{num} and @var{den}.\n"
147 "\n"
148 "A duration is a musical duration, i.e. a length of time described by a\n"
149 "power of two (whole, half, quarter, etc.) and a number of augmentation\n"
150 "dots. \n"
151 "\n"
152 "")
153 {
154   SCM_ASSERT_TYPE(gh_number_p (length), length, SCM_ARG1, __FUNCTION__, "integer");
155   SCM_ASSERT_TYPE(gh_number_p (dotcount), dotcount, SCM_ARG2, __FUNCTION__, "integer");
156
157   bool compress = false;
158   if (num != SCM_UNDEFINED)
159     {
160       SCM_ASSERT_TYPE(gh_number_p (num), length, SCM_ARG3, __FUNCTION__, "integer");
161       compress = true;
162     }
163   else
164     num = gh_int2scm (1);
165   
166   if (den != SCM_UNDEFINED)
167     SCM_ASSERT_TYPE(gh_number_p (den), length, SCM_ARG4, __FUNCTION__, "integer");
168   else
169     den = gh_int2scm (1);
170   
171   Duration p (gh_scm2int (length), gh_scm2int (dotcount));
172   if (compress)
173     p = p.compressed (Rational (gh_scm2int (num), gh_scm2int (den)));
174
175   return p.smobbed_copy ();
176 }
177
178
179
180 LY_DEFINE(duration_log,
181           "ly:duration-log", 1, 0, 0, (SCM dur),
182           "Extract the duration log from @var{dur}"
183 )
184 {
185   SCM_ASSERT_TYPE(unsmob_duration(dur), dur, SCM_ARG1, __FUNCTION__, "duration");
186
187   return gh_int2scm (unsmob_duration (dur)->duration_log ());
188 }
189
190
191 LY_DEFINE(dot_count_log,
192           "ly:duration-dot-count", 1, 0, 0, (SCM dur),
193           "Extract the dot count from @var{dur}"
194 )
195 {
196   SCM_ASSERT_TYPE(unsmob_duration(dur), dur, SCM_ARG1, __FUNCTION__, "duration");
197
198   return gh_int2scm (unsmob_duration (dur)->dot_count ());
199 }
200
201
202 LY_DEFINE(ly_intlog2,
203           "ly:intlog2", 1, 0, 0, (SCM d),
204           "The 2-logarithm of 1/@var{d}."
205 )
206 {
207   SCM_ASSERT_TYPE(gh_number_p (d), d, SCM_ARG1, __FUNCTION__, "integer");
208
209   int l = intlog2 (gh_scm2int (d));
210
211   return gh_int2scm (l);
212 }
213
214 LY_DEFINE(compression_factor,
215           "ly:duration-factor", 1, 0, 0, (SCM dur),
216           "Extract the compression factor from @var{dur}. Return as a pair."
217 )
218 {
219   SCM_ASSERT_TYPE(unsmob_duration(dur), dur, SCM_ARG1, __FUNCTION__, "duration");
220   Rational r =unsmob_duration (dur)->factor ();
221
222   return gh_cons(gh_int2scm (r.num()),gh_int2scm (r.den ())); 
223 }
224
225 SCM
226 Duration::smobbed_copy ()const
227 {
228   Duration *  p = new Duration (*this);
229   return p->smobbed_self ();
230 }
231
232 int
233 Duration::duration_log () const
234 {
235   return durlog_;
236 }
237
238 int
239 Duration::dot_count () const
240 {
241   return dots_;
242 }
243