]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration.cc
7a02633100b030fdfed5cd82c615888ed8fda718
[lilypond.git] / lily / duration.cc
1 /*
2   duration.cc -- implement Duration
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "duration.hh"
11
12 #include "misc.hh"
13 #include "lily-proto.hh"
14
15 #include "ly-smobs.icc"
16
17 int
18 Duration::compare (Duration const &left, Duration const &right)
19 {
20   return Rational::compare (left.get_length (), right.get_length ());
21 }
22
23 Duration::Duration ()
24 {
25   durlog_ = 0;
26   dots_ = 0;
27   factor_ = Rational (1, 1);
28 }
29
30 Duration::Duration (int log, int d)
31 {
32   durlog_ = log;
33   dots_ = d;
34   factor_ = Rational (1, 1);
35 }
36
37 Duration::Duration (Rational r, bool scale)
38 {
39   factor_ = Rational (1, 1);
40
41   if (r.num () == 0.0)
42     {
43       durlog_ = 0;
44       dots_ = 0;
45     }
46   else
47     {
48       /* we want to find the integer k for which 2q/p > 2^k >= q/p.
49          It's simple to check that k' = \floor \log q - \floor \log p
50          satisfies the left inequality and is within a factor of 2 of
51          satistying the right one. Therefore either k = k' or k = k'+1 */
52
53       int p = r.num ();
54       int q = r.den ();
55       int k = intlog2 (q) - intlog2 (p);
56       if ((p << k) < q)
57         k++;
58
59       assert ((p << k) >= q && (p << (k-1)) < q);
60
61       /* If we were to write out log (p/q) in base 2, then the position of the
62          first non-zero bit (ie. k in our notation) would be the durlog
63          and the number of consecutive 1s after that bit would be the number of
64          dots */
65       p = (p << k) - q;
66       dots_ = 0;
67       while ((p *= 2) >= q)
68         {
69           p -= q;
70           dots_++;
71         }
72
73       /* we only go up to 64th notes */
74       if (k > 6)
75         {
76           durlog_ = 6;
77           dots_ = 0;
78         }
79       else
80         durlog_ = k;
81
82       if (scale || k > 6)
83         factor_ = r / get_length ();
84     }
85 }
86
87 Duration
88 Duration::compressed (Rational m) const
89 {
90   Duration d (*this);
91   d.factor_ *= m;
92   return d;
93 }
94
95 Rational
96 Duration::get_length () const
97 {
98   Rational mom (1 << abs (durlog_));
99
100   if (durlog_ > 0)
101     mom = Rational (1) / mom;
102
103   Rational delta = mom;
104   for (int i = 0; i < dots_; i++)
105     {
106       delta /= Rational (2);
107       mom += delta;
108     }
109
110   return mom * factor_;
111 }
112
113 string
114 Duration::to_string () const
115 {
116   string s;
117
118   if (durlog_ < 0)
119     s = "log = " + ::to_string (durlog_);
120   else
121     s = ::to_string (1 << durlog_);
122
123   s += ::to_string ('.', dots_);
124   if (factor_ != Moment (Rational (1, 1)))
125     s += "*" + factor_.to_string ();
126   return s;
127 }
128
129 IMPLEMENT_TYPE_P (Duration, "ly:duration?");
130
131 SCM
132 Duration::mark_smob (SCM)
133 {
134   return SCM_EOL;
135 }
136
137 IMPLEMENT_SIMPLE_SMOBS (Duration);
138 int
139 Duration::print_smob (SCM s, SCM port, scm_print_state *)
140 {
141   Duration *r = (Duration *) SCM_CELL_WORD_1 (s);
142
143   scm_puts ("#<Duration ", port);
144   scm_display (ly_string2scm (r->to_string ()), port);
145   scm_puts (" >", port);
146
147   return 1;
148 }
149
150 SCM
151 Duration::equal_p (SCM a, SCM b)
152 {
153   Duration *p = (Duration *) SCM_CELL_WORD_1 (a);
154   Duration *q = (Duration *) SCM_CELL_WORD_1 (b);
155
156   bool eq = p->dots_ == q->dots_
157     && p->durlog_ == q->durlog_
158     && p->factor_ == q->factor_;
159
160   return eq ? SCM_BOOL_T : SCM_BOOL_F;
161 }
162
163 int
164 Duration::duration_log () const
165 {
166   return durlog_;
167 }
168
169 int
170 Duration::dot_count () const
171 {
172   return dots_;
173 }