cannam@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@0
|
2
|
cannam@0
|
3 /*
|
cannam@0
|
4 Vamp
|
cannam@0
|
5
|
cannam@0
|
6 An API for audio analysis and feature extraction plugins.
|
cannam@0
|
7
|
cannam@0
|
8 Centre for Digital Music, Queen Mary, University of London.
|
cannam@0
|
9 Copyright 2006 Chris Cannam.
|
cannam@0
|
10
|
cannam@0
|
11 Permission is hereby granted, free of charge, to any person
|
cannam@0
|
12 obtaining a copy of this software and associated documentation
|
cannam@0
|
13 files (the "Software"), to deal in the Software without
|
cannam@0
|
14 restriction, including without limitation the rights to use, copy,
|
cannam@0
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
cannam@0
|
16 of the Software, and to permit persons to whom the Software is
|
cannam@0
|
17 furnished to do so, subject to the following conditions:
|
cannam@0
|
18
|
cannam@0
|
19 The above copyright notice and this permission notice shall be
|
cannam@0
|
20 included in all copies or substantial portions of the Software.
|
cannam@0
|
21
|
cannam@0
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@0
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@0
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
cannam@0
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR
|
cannam@0
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@0
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@0
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@0
|
29
|
cannam@0
|
30 Except as contained in this notice, the names of the Centre for
|
cannam@0
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
cannam@0
|
32 shall not be used in advertising or otherwise to promote the sale,
|
cannam@0
|
33 use or other dealings in this Software without prior written
|
cannam@0
|
34 authorization.
|
cannam@0
|
35 */
|
cannam@0
|
36
|
cannam@0
|
37 /*
|
cannam@0
|
38 This is a modified version of a source file from the
|
cannam@0
|
39 Rosegarden MIDI and audio sequencer and notation editor.
|
cannam@0
|
40 This file copyright 2000-2006 Chris Cannam; relicensed as detailed above
|
cannam@0
|
41 */
|
cannam@0
|
42
|
cannam@0
|
43 #include <iostream>
|
cannam@0
|
44
|
cannam@0
|
45 #if (__GNUC__ < 3)
|
cannam@0
|
46 #include <strstream>
|
cannam@0
|
47 #define stringstream strstream
|
cannam@0
|
48 #else
|
cannam@0
|
49 #include <sstream>
|
cannam@0
|
50 #endif
|
cannam@0
|
51
|
cannam@0
|
52 using std::cerr;
|
cannam@0
|
53 using std::endl;
|
cannam@0
|
54
|
cannam@0
|
55 #include "RealTime.h"
|
cannam@0
|
56 #include "sys/time.h"
|
cannam@0
|
57
|
cannam@0
|
58 namespace Vamp {
|
cannam@0
|
59
|
cannam@0
|
60 // A RealTime consists of two ints that must be at least 32 bits each.
|
cannam@0
|
61 // A signed 32-bit int can store values exceeding +/- 2 billion. This
|
cannam@0
|
62 // means we can safely use our lower int for nanoseconds, as there are
|
cannam@0
|
63 // 1 billion nanoseconds in a second and we need to handle double that
|
cannam@0
|
64 // because of the implementations of addition etc that we use.
|
cannam@0
|
65 //
|
cannam@0
|
66 // The maximum valid RealTime on a 32-bit system is somewhere around
|
cannam@0
|
67 // 68 years: 999999999 nanoseconds longer than the classic Unix epoch.
|
cannam@0
|
68
|
cannam@0
|
69 #define ONE_BILLION 1000000000
|
cannam@0
|
70
|
cannam@0
|
71 RealTime::RealTime(int s, int n) :
|
cannam@0
|
72 sec(s), nsec(n)
|
cannam@0
|
73 {
|
cannam@0
|
74 if (sec == 0) {
|
cannam@0
|
75 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
|
cannam@0
|
76 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
|
cannam@0
|
77 } else if (sec < 0) {
|
cannam@0
|
78 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
|
cannam@0
|
79 while (nsec > 0) { nsec -= ONE_BILLION; ++sec; }
|
cannam@0
|
80 } else {
|
cannam@0
|
81 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
|
cannam@0
|
82 while (nsec < 0) { nsec += ONE_BILLION; --sec; }
|
cannam@0
|
83 }
|
cannam@0
|
84 }
|
cannam@0
|
85
|
cannam@0
|
86 RealTime
|
cannam@0
|
87 RealTime::fromSeconds(double sec)
|
cannam@0
|
88 {
|
cannam@0
|
89 return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION));
|
cannam@0
|
90 }
|
cannam@0
|
91
|
cannam@0
|
92 RealTime
|
cannam@0
|
93 RealTime::fromMilliseconds(int msec)
|
cannam@0
|
94 {
|
cannam@0
|
95 return RealTime(msec / 1000, (msec % 1000) * 1000000);
|
cannam@0
|
96 }
|
cannam@0
|
97
|
cannam@0
|
98 RealTime
|
cannam@0
|
99 RealTime::fromTimeval(const struct timeval &tv)
|
cannam@0
|
100 {
|
cannam@0
|
101 return RealTime(tv.tv_sec, tv.tv_usec * 1000);
|
cannam@0
|
102 }
|
cannam@0
|
103
|
cannam@0
|
104 std::ostream &operator<<(std::ostream &out, const RealTime &rt)
|
cannam@0
|
105 {
|
cannam@0
|
106 if (rt < RealTime::zeroTime) {
|
cannam@0
|
107 out << "-";
|
cannam@0
|
108 } else {
|
cannam@0
|
109 out << " ";
|
cannam@0
|
110 }
|
cannam@0
|
111
|
cannam@0
|
112 int s = (rt.sec < 0 ? -rt.sec : rt.sec);
|
cannam@0
|
113 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec);
|
cannam@0
|
114
|
cannam@0
|
115 out << s << ".";
|
cannam@0
|
116
|
cannam@0
|
117 int nn(n);
|
cannam@0
|
118 if (nn == 0) out << "00000000";
|
cannam@0
|
119 else while (nn < (ONE_BILLION / 10)) {
|
cannam@0
|
120 out << "0";
|
cannam@0
|
121 nn *= 10;
|
cannam@0
|
122 }
|
cannam@0
|
123
|
cannam@0
|
124 out << n << "R";
|
cannam@0
|
125 return out;
|
cannam@0
|
126 }
|
cannam@0
|
127
|
cannam@0
|
128 std::string
|
cannam@0
|
129 RealTime::toString() const
|
cannam@0
|
130 {
|
cannam@0
|
131 std::stringstream out;
|
cannam@0
|
132 out << *this;
|
cannam@0
|
133
|
cannam@0
|
134 #if (__GNUC__ < 3)
|
cannam@0
|
135 out << std::ends;
|
cannam@0
|
136 #endif
|
cannam@0
|
137
|
cannam@0
|
138 std::string s = out.str();
|
cannam@0
|
139
|
cannam@0
|
140 // remove trailing R
|
cannam@0
|
141 return s.substr(0, s.length() - 1);
|
cannam@0
|
142 }
|
cannam@0
|
143
|
cannam@0
|
144 std::string
|
cannam@0
|
145 RealTime::toText(bool fixedDp) const
|
cannam@0
|
146 {
|
cannam@0
|
147 if (*this < RealTime::zeroTime) return "-" + (-*this).toText();
|
cannam@0
|
148
|
cannam@0
|
149 std::stringstream out;
|
cannam@0
|
150
|
cannam@0
|
151 if (sec >= 3600) {
|
cannam@0
|
152 out << (sec / 3600) << ":";
|
cannam@0
|
153 }
|
cannam@0
|
154
|
cannam@0
|
155 if (sec >= 60) {
|
cannam@0
|
156 out << (sec % 3600) / 60 << ":";
|
cannam@0
|
157 }
|
cannam@0
|
158
|
cannam@0
|
159 if (sec >= 10) {
|
cannam@0
|
160 out << ((sec % 60) / 10);
|
cannam@0
|
161 }
|
cannam@0
|
162
|
cannam@0
|
163 out << (sec % 10);
|
cannam@0
|
164
|
cannam@0
|
165 int ms = msec();
|
cannam@0
|
166
|
cannam@0
|
167 if (ms != 0) {
|
cannam@0
|
168 out << ".";
|
cannam@0
|
169 out << (ms / 100);
|
cannam@0
|
170 ms = ms % 100;
|
cannam@0
|
171 if (ms != 0) {
|
cannam@0
|
172 out << (ms / 10);
|
cannam@0
|
173 ms = ms % 10;
|
cannam@0
|
174 } else if (fixedDp) {
|
cannam@0
|
175 out << "0";
|
cannam@0
|
176 }
|
cannam@0
|
177 if (ms != 0) {
|
cannam@0
|
178 out << ms;
|
cannam@0
|
179 } else if (fixedDp) {
|
cannam@0
|
180 out << "0";
|
cannam@0
|
181 }
|
cannam@0
|
182 } else if (fixedDp) {
|
cannam@0
|
183 out << ".000";
|
cannam@0
|
184 }
|
cannam@0
|
185
|
cannam@0
|
186 #if (__GNUC__ < 3)
|
cannam@0
|
187 out << std::ends;
|
cannam@0
|
188 #endif
|
cannam@0
|
189
|
cannam@0
|
190 std::string s = out.str();
|
cannam@0
|
191
|
cannam@0
|
192 return s;
|
cannam@0
|
193 }
|
cannam@0
|
194
|
cannam@0
|
195
|
cannam@0
|
196 RealTime
|
cannam@0
|
197 RealTime::operator/(int d) const
|
cannam@0
|
198 {
|
cannam@0
|
199 int secdiv = sec / d;
|
cannam@0
|
200 int secrem = sec % d;
|
cannam@0
|
201
|
cannam@0
|
202 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d;
|
cannam@0
|
203
|
cannam@0
|
204 return RealTime(secdiv, int(nsecdiv + 0.5));
|
cannam@0
|
205 }
|
cannam@0
|
206
|
cannam@0
|
207 double
|
cannam@0
|
208 RealTime::operator/(const RealTime &r) const
|
cannam@0
|
209 {
|
cannam@0
|
210 double lTotal = double(sec) * ONE_BILLION + double(nsec);
|
cannam@0
|
211 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec);
|
cannam@0
|
212
|
cannam@0
|
213 if (rTotal == 0) return 0.0;
|
cannam@0
|
214 else return lTotal/rTotal;
|
cannam@0
|
215 }
|
cannam@0
|
216
|
cannam@0
|
217 long
|
cannam@0
|
218 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
|
cannam@0
|
219 {
|
cannam@0
|
220 if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
|
cannam@0
|
221
|
cannam@0
|
222 // We like integers. The last term is always zero unless the
|
cannam@0
|
223 // sample rate is greater than 1MHz, but hell, you never know...
|
cannam@0
|
224
|
cannam@0
|
225 long frame =
|
cannam@0
|
226 time.sec * sampleRate +
|
cannam@0
|
227 (time.msec() * sampleRate) / 1000 +
|
cannam@0
|
228 ((time.usec() - 1000 * time.msec()) * sampleRate) / 1000000 +
|
cannam@0
|
229 ((time.nsec - 1000 * time.usec()) * sampleRate) / 1000000000;
|
cannam@0
|
230
|
cannam@0
|
231 return frame;
|
cannam@0
|
232 }
|
cannam@0
|
233
|
cannam@0
|
234 RealTime
|
cannam@0
|
235 RealTime::frame2RealTime(long frame, unsigned int sampleRate)
|
cannam@0
|
236 {
|
cannam@0
|
237 if (frame < 0) return -frame2RealTime(-frame, sampleRate);
|
cannam@0
|
238
|
cannam@0
|
239 RealTime rt;
|
cannam@0
|
240 rt.sec = frame / long(sampleRate);
|
cannam@0
|
241 frame -= rt.sec * long(sampleRate);
|
cannam@0
|
242 rt.nsec = (int)(((float(frame) * 1000000) / long(sampleRate)) * 1000);
|
cannam@0
|
243 return rt;
|
cannam@0
|
244 }
|
cannam@0
|
245
|
cannam@0
|
246 const RealTime RealTime::zeroTime(0,0);
|
cannam@0
|
247
|
cannam@0
|
248 }
|