Chris@49
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@52
|
4 Sonic Visualiser
|
Chris@52
|
5 An audio file viewer and annotation editor.
|
Chris@52
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@0
|
7
|
Chris@52
|
8 This program is free software; you can redistribute it and/or
|
Chris@52
|
9 modify it under the terms of the GNU General Public License as
|
Chris@52
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@52
|
11 License, or (at your option) any later version. See the file
|
Chris@52
|
12 COPYING included with this distribution for more information.
|
Chris@0
|
13 */
|
Chris@0
|
14
|
Chris@0
|
15 /*
|
Chris@0
|
16 This is a modified version of a source file from the
|
Chris@0
|
17 Rosegarden MIDI and audio sequencer and notation editor.
|
Chris@17
|
18 This file copyright 2000-2006 Chris Cannam.
|
Chris@0
|
19 */
|
Chris@0
|
20
|
Chris@0
|
21 #include <iostream>
|
Chris@0
|
22
|
Chris@405
|
23 #include <cstdlib>
|
Chris@405
|
24
|
Chris@0
|
25 #if (__GNUC__ < 3)
|
Chris@0
|
26 #include <strstream>
|
Chris@0
|
27 #define stringstream strstream
|
Chris@0
|
28 #else
|
Chris@0
|
29 #include <sstream>
|
Chris@0
|
30 #endif
|
Chris@0
|
31
|
Chris@0
|
32 using std::cerr;
|
Chris@0
|
33 using std::endl;
|
Chris@0
|
34
|
Chris@150
|
35 #include "RealTime.h"
|
Chris@26
|
36 #include "sys/time.h"
|
Chris@0
|
37
|
Chris@0
|
38 // A RealTime consists of two ints that must be at least 32 bits each.
|
Chris@0
|
39 // A signed 32-bit int can store values exceeding +/- 2 billion. This
|
Chris@0
|
40 // means we can safely use our lower int for nanoseconds, as there are
|
Chris@0
|
41 // 1 billion nanoseconds in a second and we need to handle double that
|
Chris@0
|
42 // because of the implementations of addition etc that we use.
|
Chris@0
|
43 //
|
Chris@0
|
44 // The maximum valid RealTime on a 32-bit system is somewhere around
|
Chris@0
|
45 // 68 years: 999999999 nanoseconds longer than the classic Unix epoch.
|
Chris@0
|
46
|
Chris@0
|
47 #define ONE_BILLION 1000000000
|
Chris@0
|
48
|
Chris@0
|
49 RealTime::RealTime(int s, int n) :
|
Chris@0
|
50 sec(s), nsec(n)
|
Chris@0
|
51 {
|
Chris@0
|
52 if (sec == 0) {
|
Chris@0
|
53 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
|
Chris@0
|
54 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
|
Chris@0
|
55 } else if (sec < 0) {
|
Chris@0
|
56 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
|
Chris@0
|
57 while (nsec > 0) { nsec -= ONE_BILLION; ++sec; }
|
Chris@0
|
58 } else {
|
Chris@0
|
59 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
|
Chris@0
|
60 while (nsec < 0) { nsec += ONE_BILLION; --sec; }
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@26
|
64 RealTime
|
Chris@26
|
65 RealTime::fromSeconds(double sec)
|
Chris@26
|
66 {
|
Chris@119
|
67 return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION + 0.5));
|
Chris@26
|
68 }
|
Chris@26
|
69
|
Chris@26
|
70 RealTime
|
Chris@26
|
71 RealTime::fromMilliseconds(int msec)
|
Chris@26
|
72 {
|
Chris@26
|
73 return RealTime(msec / 1000, (msec % 1000) * 1000000);
|
Chris@26
|
74 }
|
Chris@26
|
75
|
Chris@26
|
76 RealTime
|
Chris@26
|
77 RealTime::fromTimeval(const struct timeval &tv)
|
Chris@26
|
78 {
|
Chris@26
|
79 return RealTime(tv.tv_sec, tv.tv_usec * 1000);
|
Chris@26
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 std::ostream &operator<<(std::ostream &out, const RealTime &rt)
|
Chris@0
|
83 {
|
Chris@0
|
84 if (rt < RealTime::zeroTime) {
|
Chris@0
|
85 out << "-";
|
Chris@0
|
86 } else {
|
Chris@0
|
87 out << " ";
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 int s = (rt.sec < 0 ? -rt.sec : rt.sec);
|
Chris@0
|
91 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec);
|
Chris@0
|
92
|
Chris@0
|
93 out << s << ".";
|
Chris@0
|
94
|
Chris@0
|
95 int nn(n);
|
Chris@0
|
96 if (nn == 0) out << "00000000";
|
Chris@0
|
97 else while (nn < (ONE_BILLION / 10)) {
|
Chris@0
|
98 out << "0";
|
Chris@0
|
99 nn *= 10;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 out << n << "R";
|
Chris@0
|
103 return out;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 std::string
|
Chris@121
|
107 RealTime::toString(bool align) const
|
Chris@0
|
108 {
|
Chris@0
|
109 std::stringstream out;
|
Chris@0
|
110 out << *this;
|
Chris@0
|
111
|
Chris@0
|
112 #if (__GNUC__ < 3)
|
Chris@0
|
113 out << std::ends;
|
Chris@0
|
114 #endif
|
Chris@0
|
115
|
Chris@0
|
116 std::string s = out.str();
|
Chris@0
|
117
|
Chris@121
|
118 if (!align && *this >= RealTime::zeroTime) {
|
Chris@121
|
119 // remove leading " "
|
Chris@121
|
120 s = s.substr(1, s.length() - 1);
|
Chris@121
|
121 }
|
Chris@121
|
122
|
Chris@0
|
123 // remove trailing R
|
Chris@0
|
124 return s.substr(0, s.length() - 1);
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@350
|
127 RealTime
|
Chris@350
|
128 RealTime::fromString(std::string s)
|
Chris@350
|
129 {
|
Chris@350
|
130 bool negative = false;
|
Chris@350
|
131 bool faulty = false;
|
Chris@350
|
132 bool section = 0;
|
Chris@350
|
133 std::string ssec, snsec;
|
Chris@350
|
134
|
Chris@350
|
135 for (size_t i = 0; i < s.length(); ++i) {
|
Chris@350
|
136
|
Chris@350
|
137 char c = s[i];
|
Chris@350
|
138 if (isspace(c)) continue;
|
Chris@350
|
139
|
Chris@350
|
140 if (section == 0) {
|
Chris@350
|
141
|
Chris@350
|
142 if (c == '-') negative = true;
|
Chris@350
|
143 else if (isdigit(c)) { section = 1; ssec += c; }
|
Chris@350
|
144 else if (c == '.') section = 2;
|
Chris@350
|
145 else break;
|
Chris@350
|
146
|
Chris@350
|
147 } else if (section == 1) {
|
Chris@350
|
148
|
Chris@350
|
149 if (c == '.') section = 2;
|
Chris@350
|
150 else if (isdigit(c)) ssec += c;
|
Chris@350
|
151 else break;
|
Chris@350
|
152
|
Chris@350
|
153 } else if (section == 2) {
|
Chris@350
|
154
|
Chris@350
|
155 if (isdigit(c)) snsec += c;
|
Chris@350
|
156 else break;
|
Chris@350
|
157 }
|
Chris@350
|
158 }
|
Chris@350
|
159
|
Chris@350
|
160 while (snsec.length() < 8) snsec += '0';
|
Chris@350
|
161
|
Chris@350
|
162 int sec = atoi(ssec.c_str());
|
Chris@350
|
163 int nsec = atoi(snsec.c_str());
|
Chris@350
|
164 if (negative) sec = -sec;
|
Chris@350
|
165
|
Chris@399
|
166 // std::cerr << "RealTime::fromString: string " << s << " -> "
|
Chris@399
|
167 // << sec << " sec, " << nsec << " nsec" << std::endl;
|
Chris@350
|
168
|
Chris@350
|
169 return RealTime(sec, nsec);
|
Chris@350
|
170 }
|
Chris@350
|
171
|
Chris@0
|
172 std::string
|
Chris@0
|
173 RealTime::toText(bool fixedDp) const
|
Chris@0
|
174 {
|
Chris@247
|
175 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp);
|
Chris@0
|
176
|
Chris@0
|
177 std::stringstream out;
|
Chris@0
|
178
|
Chris@0
|
179 if (sec >= 3600) {
|
Chris@0
|
180 out << (sec / 3600) << ":";
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 if (sec >= 60) {
|
Chris@0
|
184 out << (sec % 3600) / 60 << ":";
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 if (sec >= 10) {
|
Chris@0
|
188 out << ((sec % 60) / 10);
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 out << (sec % 10);
|
Chris@0
|
192
|
Chris@0
|
193 int ms = msec();
|
Chris@0
|
194
|
Chris@0
|
195 if (ms != 0) {
|
Chris@0
|
196 out << ".";
|
Chris@0
|
197 out << (ms / 100);
|
Chris@0
|
198 ms = ms % 100;
|
Chris@0
|
199 if (ms != 0) {
|
Chris@0
|
200 out << (ms / 10);
|
Chris@0
|
201 ms = ms % 10;
|
Chris@0
|
202 } else if (fixedDp) {
|
Chris@0
|
203 out << "0";
|
Chris@0
|
204 }
|
Chris@0
|
205 if (ms != 0) {
|
Chris@0
|
206 out << ms;
|
Chris@0
|
207 } else if (fixedDp) {
|
Chris@0
|
208 out << "0";
|
Chris@0
|
209 }
|
Chris@0
|
210 } else if (fixedDp) {
|
Chris@0
|
211 out << ".000";
|
Chris@0
|
212 }
|
Chris@0
|
213
|
Chris@0
|
214 #if (__GNUC__ < 3)
|
Chris@0
|
215 out << std::ends;
|
Chris@0
|
216 #endif
|
Chris@0
|
217
|
Chris@0
|
218 std::string s = out.str();
|
Chris@0
|
219
|
Chris@0
|
220 return s;
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@247
|
223 std::string
|
Chris@247
|
224 RealTime::toSecText() const
|
Chris@247
|
225 {
|
Chris@247
|
226 if (*this < RealTime::zeroTime) return "-" + (-*this).toSecText();
|
Chris@247
|
227
|
Chris@247
|
228 std::stringstream out;
|
Chris@247
|
229
|
Chris@247
|
230 if (sec >= 3600) {
|
Chris@247
|
231 out << (sec / 3600) << ":";
|
Chris@247
|
232 }
|
Chris@247
|
233
|
Chris@247
|
234 if (sec >= 60) {
|
Chris@247
|
235 out << (sec % 3600) / 60 << ":";
|
Chris@247
|
236 }
|
Chris@247
|
237
|
Chris@247
|
238 if (sec >= 10) {
|
Chris@247
|
239 out << ((sec % 60) / 10);
|
Chris@247
|
240 }
|
Chris@247
|
241
|
Chris@247
|
242 out << (sec % 10);
|
Chris@247
|
243
|
Chris@247
|
244 if (sec < 60) {
|
Chris@247
|
245 out << "s";
|
Chris@247
|
246 }
|
Chris@247
|
247
|
Chris@247
|
248
|
Chris@247
|
249 #if (__GNUC__ < 3)
|
Chris@247
|
250 out << std::ends;
|
Chris@247
|
251 #endif
|
Chris@247
|
252
|
Chris@247
|
253 std::string s = out.str();
|
Chris@247
|
254
|
Chris@247
|
255 return s;
|
Chris@247
|
256 }
|
Chris@247
|
257
|
Chris@183
|
258 RealTime
|
Chris@183
|
259 RealTime::operator*(int m) const
|
Chris@183
|
260 {
|
Chris@183
|
261 double t = (double(nsec) / ONE_BILLION) * m;
|
Chris@183
|
262 t += sec * m;
|
Chris@183
|
263 return fromSeconds(t);
|
Chris@183
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 RealTime
|
Chris@0
|
267 RealTime::operator/(int d) const
|
Chris@0
|
268 {
|
Chris@0
|
269 int secdiv = sec / d;
|
Chris@0
|
270 int secrem = sec % d;
|
Chris@0
|
271
|
Chris@0
|
272 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d;
|
Chris@0
|
273
|
Chris@0
|
274 return RealTime(secdiv, int(nsecdiv + 0.5));
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@378
|
277 RealTime
|
Chris@378
|
278 RealTime::operator*(double m) const
|
Chris@378
|
279 {
|
Chris@378
|
280 double t = (double(nsec) / ONE_BILLION) * m;
|
Chris@378
|
281 t += sec * m;
|
Chris@378
|
282 return fromSeconds(t);
|
Chris@378
|
283 }
|
Chris@378
|
284
|
Chris@378
|
285 RealTime
|
Chris@378
|
286 RealTime::operator/(double d) const
|
Chris@378
|
287 {
|
Chris@378
|
288 double t = (double(nsec) / ONE_BILLION) / d;
|
Chris@378
|
289 t += sec / d;
|
Chris@378
|
290 return fromSeconds(t);
|
Chris@378
|
291 }
|
Chris@378
|
292
|
Chris@0
|
293 double
|
Chris@0
|
294 RealTime::operator/(const RealTime &r) const
|
Chris@0
|
295 {
|
Chris@0
|
296 double lTotal = double(sec) * ONE_BILLION + double(nsec);
|
Chris@0
|
297 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec);
|
Chris@0
|
298
|
Chris@0
|
299 if (rTotal == 0) return 0.0;
|
Chris@0
|
300 else return lTotal/rTotal;
|
Chris@0
|
301 }
|
Chris@0
|
302
|
Chris@0
|
303 long
|
Chris@0
|
304 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
|
Chris@0
|
305 {
|
Chris@0
|
306 if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
|
Chris@427
|
307 double s = time.sec + double(time.nsec + 1) / 1000000000.0;
|
Chris@427
|
308 return long(s * sampleRate);
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 RealTime
|
Chris@0
|
312 RealTime::frame2RealTime(long frame, unsigned int sampleRate)
|
Chris@0
|
313 {
|
Chris@0
|
314 if (frame < 0) return -frame2RealTime(-frame, sampleRate);
|
Chris@0
|
315
|
Chris@0
|
316 RealTime rt;
|
Chris@0
|
317 rt.sec = frame / long(sampleRate);
|
Chris@0
|
318 frame -= rt.sec * long(sampleRate);
|
Chris@427
|
319 rt.nsec = (int)(((double(frame) * 1000000.0) / long(sampleRate)) * 1000.0);
|
Chris@0
|
320 return rt;
|
Chris@0
|
321 }
|
Chris@0
|
322
|
Chris@0
|
323 const RealTime RealTime::zeroTime(0,0);
|
Chris@0
|
324
|