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