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