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