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