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@1427
|
22 #include <limits.h>
|
Chris@0
|
23
|
Chris@405
|
24 #include <cstdlib>
|
Chris@0
|
25 #include <sstream>
|
Chris@0
|
26
|
Chris@150
|
27 #include "RealTime.h"
|
Chris@0
|
28
|
Chris@843
|
29 #include "Debug.h"
|
Chris@843
|
30
|
Chris@612
|
31 #include "Preferences.h"
|
Chris@612
|
32
|
Chris@0
|
33 // A RealTime consists of two ints that must be at least 32 bits each.
|
Chris@0
|
34 // A signed 32-bit int can store values exceeding +/- 2 billion. This
|
Chris@0
|
35 // means we can safely use our lower int for nanoseconds, as there are
|
Chris@0
|
36 // 1 billion nanoseconds in a second and we need to handle double that
|
Chris@0
|
37 // because of the implementations of addition etc that we use.
|
Chris@0
|
38 //
|
Chris@0
|
39 // The maximum valid RealTime on a 32-bit system is somewhere around
|
Chris@0
|
40 // 68 years: 999999999 nanoseconds longer than the classic Unix epoch.
|
Chris@0
|
41
|
Chris@0
|
42 #define ONE_BILLION 1000000000
|
Chris@0
|
43
|
Chris@0
|
44 RealTime::RealTime(int s, int n) :
|
Chris@0
|
45 sec(s), nsec(n)
|
Chris@0
|
46 {
|
Chris@1427
|
47 while (nsec <= -ONE_BILLION && sec > INT_MIN) { nsec += ONE_BILLION; --sec; }
|
Chris@1427
|
48 while (nsec >= ONE_BILLION && sec < INT_MAX) { nsec -= ONE_BILLION; ++sec; }
|
Chris@1427
|
49 while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; }
|
Chris@1427
|
50 while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; }
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@26
|
53 RealTime
|
Chris@26
|
54 RealTime::fromSeconds(double sec)
|
Chris@26
|
55 {
|
Chris@1012
|
56 if (sec >= 0) {
|
Chris@1012
|
57 return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION + 0.5));
|
Chris@1012
|
58 } else {
|
Chris@1012
|
59 return -fromSeconds(-sec);
|
Chris@1012
|
60 }
|
Chris@26
|
61 }
|
Chris@26
|
62
|
Chris@26
|
63 RealTime
|
Chris@26
|
64 RealTime::fromMilliseconds(int msec)
|
Chris@26
|
65 {
|
Chris@26
|
66 return RealTime(msec / 1000, (msec % 1000) * 1000000);
|
Chris@26
|
67 }
|
Chris@26
|
68
|
Chris@26
|
69 RealTime
|
Chris@1541
|
70 RealTime::fromMicroseconds(int usec)
|
Chris@1541
|
71 {
|
Chris@1541
|
72 return RealTime(usec / 1000000, (usec % 1000000) * 1000);
|
Chris@1541
|
73 }
|
Chris@1541
|
74
|
Chris@1541
|
75 RealTime
|
Chris@26
|
76 RealTime::fromTimeval(const struct timeval &tv)
|
Chris@26
|
77 {
|
Chris@1038
|
78 return RealTime(int(tv.tv_sec), int(tv.tv_usec * 1000));
|
Chris@26
|
79 }
|
Chris@0
|
80
|
Chris@439
|
81 RealTime
|
Chris@439
|
82 RealTime::fromXsdDuration(std::string xsdd)
|
Chris@439
|
83 {
|
Chris@439
|
84 RealTime t;
|
Chris@439
|
85
|
Chris@439
|
86 int year = 0, month = 0, day = 0, hour = 0, minute = 0;
|
Chris@439
|
87 double second = 0.0;
|
Chris@439
|
88
|
Chris@1298
|
89 char *loc = setlocale(LC_NUMERIC, 0);
|
Chris@1298
|
90 (void)setlocale(LC_NUMERIC, "C"); // avoid strtod expecting ,-separator in DE
|
Chris@1298
|
91
|
Chris@439
|
92 int i = 0;
|
Chris@439
|
93
|
Chris@439
|
94 const char *s = xsdd.c_str();
|
Chris@1038
|
95 int len = int(xsdd.length());
|
Chris@439
|
96
|
Chris@439
|
97 bool negative = false, afterT = false;
|
Chris@439
|
98
|
Chris@439
|
99 while (i < len) {
|
Chris@439
|
100
|
Chris@439
|
101 if (s[i] == '-') {
|
Chris@439
|
102 if (i == 0) negative = true;
|
Chris@439
|
103 ++i;
|
Chris@439
|
104 continue;
|
Chris@439
|
105 }
|
Chris@439
|
106
|
Chris@439
|
107 double value = 0.0;
|
Chris@439
|
108 char *eptr = 0;
|
Chris@439
|
109
|
Chris@439
|
110 if (isdigit(s[i]) || s[i] == '.') {
|
Chris@439
|
111 value = strtod(&s[i], &eptr);
|
Chris@1038
|
112 i = int(eptr - s);
|
Chris@439
|
113 }
|
Chris@439
|
114
|
Chris@439
|
115 if (i == len) break;
|
Chris@439
|
116
|
Chris@439
|
117 switch (s[i]) {
|
Chris@439
|
118 case 'Y': year = int(value + 0.1); break;
|
Chris@439
|
119 case 'D': day = int(value + 0.1); break;
|
Chris@439
|
120 case 'H': hour = int(value + 0.1); break;
|
Chris@439
|
121 case 'M':
|
Chris@439
|
122 if (afterT) minute = int(value + 0.1);
|
Chris@439
|
123 else month = int(value + 0.1);
|
Chris@439
|
124 break;
|
Chris@439
|
125 case 'S':
|
Chris@439
|
126 second = value;
|
Chris@439
|
127 break;
|
Chris@439
|
128 case 'T': afterT = true; break;
|
Chris@439
|
129 };
|
Chris@439
|
130
|
Chris@439
|
131 ++i;
|
Chris@439
|
132 }
|
Chris@439
|
133
|
Chris@439
|
134 if (year > 0) {
|
Chris@843
|
135 cerr << "WARNING: This xsd:duration (\"" << xsdd << "\") contains a non-zero year.\nWith no origin and a limited data size, I will treat a year as exactly 31556952\nseconds and you should expect overflow and/or poor results." << endl;
|
Chris@439
|
136 t = t + RealTime(year * 31556952, 0);
|
Chris@439
|
137 }
|
Chris@439
|
138
|
Chris@439
|
139 if (month > 0) {
|
Chris@843
|
140 cerr << "WARNING: This xsd:duration (\"" << xsdd << "\") contains a non-zero month.\nWith no origin and a limited data size, I will treat a month as exactly 2629746\nseconds and you should expect overflow and/or poor results." << endl;
|
Chris@439
|
141 t = t + RealTime(month * 2629746, 0);
|
Chris@439
|
142 }
|
Chris@439
|
143
|
Chris@439
|
144 if (day > 0) {
|
Chris@439
|
145 t = t + RealTime(day * 86400, 0);
|
Chris@439
|
146 }
|
Chris@439
|
147
|
Chris@439
|
148 if (hour > 0) {
|
Chris@439
|
149 t = t + RealTime(hour * 3600, 0);
|
Chris@439
|
150 }
|
Chris@439
|
151
|
Chris@439
|
152 if (minute > 0) {
|
Chris@439
|
153 t = t + RealTime(minute * 60, 0);
|
Chris@439
|
154 }
|
Chris@439
|
155
|
Chris@439
|
156 t = t + fromSeconds(second);
|
Chris@439
|
157
|
Chris@1298
|
158 setlocale(LC_NUMERIC, loc);
|
Chris@1298
|
159
|
Chris@928
|
160 if (negative) {
|
Chris@928
|
161 return -t;
|
Chris@928
|
162 } else {
|
Chris@928
|
163 return t;
|
Chris@928
|
164 }
|
Chris@439
|
165 }
|
Chris@439
|
166
|
Chris@439
|
167 double
|
Chris@439
|
168 RealTime::toDouble() const
|
Chris@439
|
169 {
|
Chris@439
|
170 double d = sec;
|
Chris@439
|
171 d += double(nsec) / double(ONE_BILLION);
|
Chris@439
|
172 return d;
|
Chris@439
|
173 }
|
Chris@439
|
174
|
Chris@0
|
175 std::ostream &operator<<(std::ostream &out, const RealTime &rt)
|
Chris@0
|
176 {
|
Chris@0
|
177 if (rt < RealTime::zeroTime) {
|
Chris@1429
|
178 out << "-";
|
Chris@0
|
179 } else {
|
Chris@1429
|
180 out << " ";
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 int s = (rt.sec < 0 ? -rt.sec : rt.sec);
|
Chris@0
|
184 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec);
|
Chris@0
|
185
|
Chris@0
|
186 out << s << ".";
|
Chris@0
|
187
|
Chris@0
|
188 int nn(n);
|
Chris@0
|
189 if (nn == 0) out << "00000000";
|
Chris@0
|
190 else while (nn < (ONE_BILLION / 10)) {
|
Chris@1429
|
191 out << "0";
|
Chris@1429
|
192 nn *= 10;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 out << n << "R";
|
Chris@0
|
196 return out;
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 std::string
|
Chris@121
|
200 RealTime::toString(bool align) const
|
Chris@0
|
201 {
|
Chris@0
|
202 std::stringstream out;
|
Chris@0
|
203 out << *this;
|
Chris@0
|
204
|
Chris@0
|
205 std::string s = out.str();
|
Chris@0
|
206
|
Chris@121
|
207 if (!align && *this >= RealTime::zeroTime) {
|
Chris@121
|
208 // remove leading " "
|
Chris@121
|
209 s = s.substr(1, s.length() - 1);
|
Chris@121
|
210 }
|
Chris@121
|
211
|
Chris@0
|
212 // remove trailing R
|
Chris@0
|
213 return s.substr(0, s.length() - 1);
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@350
|
216 RealTime
|
Chris@350
|
217 RealTime::fromString(std::string s)
|
Chris@350
|
218 {
|
Chris@350
|
219 bool negative = false;
|
Chris@957
|
220 int section = 0;
|
Chris@350
|
221 std::string ssec, snsec;
|
Chris@350
|
222
|
Chris@350
|
223 for (size_t i = 0; i < s.length(); ++i) {
|
Chris@350
|
224
|
Chris@350
|
225 char c = s[i];
|
Chris@350
|
226 if (isspace(c)) continue;
|
Chris@350
|
227
|
Chris@350
|
228 if (section == 0) {
|
Chris@350
|
229
|
Chris@350
|
230 if (c == '-') negative = true;
|
Chris@350
|
231 else if (isdigit(c)) { section = 1; ssec += c; }
|
Chris@350
|
232 else if (c == '.') section = 2;
|
Chris@350
|
233 else break;
|
Chris@350
|
234
|
Chris@350
|
235 } else if (section == 1) {
|
Chris@350
|
236
|
Chris@350
|
237 if (c == '.') section = 2;
|
Chris@350
|
238 else if (isdigit(c)) ssec += c;
|
Chris@350
|
239 else break;
|
Chris@350
|
240
|
Chris@350
|
241 } else if (section == 2) {
|
Chris@350
|
242
|
Chris@350
|
243 if (isdigit(c)) snsec += c;
|
Chris@350
|
244 else break;
|
Chris@350
|
245 }
|
Chris@350
|
246 }
|
Chris@350
|
247
|
Chris@350
|
248 while (snsec.length() < 8) snsec += '0';
|
Chris@350
|
249
|
Chris@350
|
250 int sec = atoi(ssec.c_str());
|
Chris@350
|
251 int nsec = atoi(snsec.c_str());
|
Chris@350
|
252 if (negative) sec = -sec;
|
Chris@350
|
253
|
Chris@690
|
254 // SVDEBUG << "RealTime::fromString: string " << s << " -> "
|
Chris@687
|
255 // << sec << " sec, " << nsec << " nsec" << endl;
|
Chris@350
|
256
|
Chris@350
|
257 return RealTime(sec, nsec);
|
Chris@350
|
258 }
|
Chris@350
|
259
|
Chris@0
|
260 std::string
|
Chris@0
|
261 RealTime::toText(bool fixedDp) const
|
Chris@0
|
262 {
|
Chris@247
|
263 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp);
|
Chris@0
|
264
|
Chris@612
|
265 Preferences *p = Preferences::getInstance();
|
Chris@1070
|
266 bool hms = true;
|
Chris@1070
|
267
|
Chris@612
|
268 if (p) {
|
Chris@1070
|
269 hms = p->getShowHMS();
|
Chris@612
|
270 int fps = 0;
|
Chris@612
|
271 switch (p->getTimeToTextMode()) {
|
Chris@612
|
272 case Preferences::TimeToTextMs: break;
|
Chris@612
|
273 case Preferences::TimeToTextUs: fps = 1000000; break;
|
Chris@612
|
274 case Preferences::TimeToText24Frame: fps = 24; break;
|
Chris@612
|
275 case Preferences::TimeToText25Frame: fps = 25; break;
|
Chris@612
|
276 case Preferences::TimeToText30Frame: fps = 30; break;
|
Chris@612
|
277 case Preferences::TimeToText50Frame: fps = 50; break;
|
Chris@612
|
278 case Preferences::TimeToText60Frame: fps = 60; break;
|
Chris@612
|
279 }
|
Chris@1070
|
280 if (fps != 0) return toFrameText(fps, hms);
|
Chris@612
|
281 }
|
Chris@612
|
282
|
Chris@1070
|
283 return toMSText(fixedDp, hms);
|
Chris@1070
|
284 }
|
Chris@0
|
285
|
Chris@1070
|
286 static void
|
Chris@1070
|
287 writeSecPart(std::stringstream &out, bool hms, int sec)
|
Chris@1070
|
288 {
|
Chris@1070
|
289 if (hms) {
|
Chris@1031
|
290 if (sec >= 3600) {
|
Chris@1031
|
291 out << (sec / 3600) << ":";
|
Chris@1031
|
292 }
|
Chris@1031
|
293
|
Chris@1031
|
294 if (sec >= 60) {
|
Chris@1070
|
295 int minutes = (sec % 3600) / 60;
|
Chris@1070
|
296 if (sec >= 3600 && minutes < 10) out << "0";
|
Chris@1070
|
297 out << minutes << ":";
|
Chris@1031
|
298 }
|
Chris@1031
|
299
|
Chris@1031
|
300 if (sec >= 10) {
|
Chris@1031
|
301 out << ((sec % 60) / 10);
|
Chris@1031
|
302 }
|
Chris@1031
|
303
|
Chris@1031
|
304 out << (sec % 10);
|
Chris@1031
|
305
|
Chris@1031
|
306 } else {
|
Chris@1031
|
307 out << sec;
|
Chris@0
|
308 }
|
Chris@1070
|
309 }
|
Chris@1070
|
310
|
Chris@1070
|
311 std::string
|
Chris@1070
|
312 RealTime::toMSText(bool fixedDp, bool hms) const
|
Chris@1070
|
313 {
|
Chris@1070
|
314 if (*this < RealTime::zeroTime) return "-" + (-*this).toMSText(fixedDp, hms);
|
Chris@1070
|
315
|
Chris@1070
|
316 std::stringstream out;
|
Chris@1070
|
317
|
Chris@1070
|
318 writeSecPart(out, hms, sec);
|
Chris@0
|
319
|
Chris@0
|
320 int ms = msec();
|
Chris@0
|
321
|
Chris@0
|
322 if (ms != 0) {
|
Chris@1429
|
323 out << ".";
|
Chris@1429
|
324 out << (ms / 100);
|
Chris@1429
|
325 ms = ms % 100;
|
Chris@1429
|
326 if (ms != 0) {
|
Chris@1429
|
327 out << (ms / 10);
|
Chris@1429
|
328 ms = ms % 10;
|
Chris@1429
|
329 } else if (fixedDp) {
|
Chris@1429
|
330 out << "0";
|
Chris@1429
|
331 }
|
Chris@1429
|
332 if (ms != 0) {
|
Chris@1429
|
333 out << ms;
|
Chris@1429
|
334 } else if (fixedDp) {
|
Chris@1429
|
335 out << "0";
|
Chris@1429
|
336 }
|
Chris@0
|
337 } else if (fixedDp) {
|
Chris@1429
|
338 out << ".000";
|
Chris@0
|
339 }
|
Chris@1429
|
340
|
Chris@612
|
341 std::string s = out.str();
|
Chris@0
|
342
|
Chris@612
|
343 return s;
|
Chris@612
|
344 }
|
Chris@612
|
345
|
Chris@612
|
346 std::string
|
Chris@1070
|
347 RealTime::toFrameText(int fps, bool hms) const
|
Chris@612
|
348 {
|
Chris@1070
|
349 if (*this < RealTime::zeroTime) return "-" + (-*this).toFrameText(fps, hms);
|
Chris@1031
|
350
|
Chris@612
|
351 std::stringstream out;
|
Chris@612
|
352
|
Chris@1070
|
353 writeSecPart(out, hms, sec);
|
Chris@1031
|
354
|
Chris@1070
|
355 // avoid rounding error if fps does not divide into ONE_BILLION
|
Chris@1070
|
356 int64_t fbig = nsec;
|
Chris@1070
|
357 fbig *= fps;
|
Chris@1070
|
358 int f = int(fbig / ONE_BILLION);
|
Chris@612
|
359
|
Chris@612
|
360 int div = 1;
|
Chris@612
|
361 int n = fps - 1;
|
Chris@612
|
362 while ((n = n / 10)) {
|
Chris@612
|
363 div *= 10;
|
Chris@612
|
364 }
|
Chris@612
|
365
|
Chris@612
|
366 out << ":";
|
Chris@612
|
367
|
Chris@843
|
368 // cerr << "div = " << div << ", f = "<< f << endl;
|
Chris@612
|
369
|
Chris@612
|
370 while (div) {
|
Chris@612
|
371 int d = (f / div) % 10;
|
Chris@612
|
372 out << d;
|
Chris@612
|
373 div /= 10;
|
Chris@612
|
374 }
|
Chris@1429
|
375
|
Chris@0
|
376 std::string s = out.str();
|
Chris@0
|
377
|
Chris@843
|
378 // cerr << "converted " << toString() << " to " << s << endl;
|
Chris@612
|
379
|
Chris@0
|
380 return s;
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@247
|
383 std::string
|
Chris@247
|
384 RealTime::toSecText() const
|
Chris@247
|
385 {
|
Chris@247
|
386 if (*this < RealTime::zeroTime) return "-" + (-*this).toSecText();
|
Chris@247
|
387
|
Chris@247
|
388 std::stringstream out;
|
Chris@247
|
389
|
Chris@1070
|
390 writeSecPart(out, true, sec);
|
Chris@247
|
391
|
Chris@247
|
392 if (sec < 60) {
|
Chris@247
|
393 out << "s";
|
Chris@247
|
394 }
|
Chris@247
|
395
|
Chris@247
|
396 std::string s = out.str();
|
Chris@247
|
397
|
Chris@247
|
398 return s;
|
Chris@247
|
399 }
|
Chris@247
|
400
|
Chris@494
|
401 std::string
|
Chris@494
|
402 RealTime::toXsdDuration() const
|
Chris@494
|
403 {
|
Chris@494
|
404 std::string s = "PT" + toString(false) + "S";
|
Chris@494
|
405 return s;
|
Chris@494
|
406 }
|
Chris@494
|
407
|
Chris@183
|
408 RealTime
|
Chris@183
|
409 RealTime::operator*(int m) const
|
Chris@183
|
410 {
|
Chris@183
|
411 double t = (double(nsec) / ONE_BILLION) * m;
|
Chris@183
|
412 t += sec * m;
|
Chris@183
|
413 return fromSeconds(t);
|
Chris@183
|
414 }
|
Chris@0
|
415
|
Chris@0
|
416 RealTime
|
Chris@0
|
417 RealTime::operator/(int d) const
|
Chris@0
|
418 {
|
Chris@0
|
419 int secdiv = sec / d;
|
Chris@0
|
420 int secrem = sec % d;
|
Chris@0
|
421
|
Chris@0
|
422 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d;
|
Chris@0
|
423
|
Chris@0
|
424 return RealTime(secdiv, int(nsecdiv + 0.5));
|
Chris@0
|
425 }
|
Chris@0
|
426
|
Chris@378
|
427 RealTime
|
Chris@378
|
428 RealTime::operator*(double m) const
|
Chris@378
|
429 {
|
Chris@378
|
430 double t = (double(nsec) / ONE_BILLION) * m;
|
Chris@378
|
431 t += sec * m;
|
Chris@378
|
432 return fromSeconds(t);
|
Chris@378
|
433 }
|
Chris@378
|
434
|
Chris@378
|
435 RealTime
|
Chris@378
|
436 RealTime::operator/(double d) const
|
Chris@378
|
437 {
|
Chris@378
|
438 double t = (double(nsec) / ONE_BILLION) / d;
|
Chris@378
|
439 t += sec / d;
|
Chris@378
|
440 return fromSeconds(t);
|
Chris@378
|
441 }
|
Chris@378
|
442
|
Chris@0
|
443 double
|
Chris@0
|
444 RealTime::operator/(const RealTime &r) const
|
Chris@0
|
445 {
|
Chris@0
|
446 double lTotal = double(sec) * ONE_BILLION + double(nsec);
|
Chris@0
|
447 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec);
|
Chris@0
|
448
|
Chris@0
|
449 if (rTotal == 0) return 0.0;
|
Chris@0
|
450 else return lTotal/rTotal;
|
Chris@0
|
451 }
|
Chris@0
|
452
|
Chris@1040
|
453 static RealTime
|
Chris@1040
|
454 frame2RealTime_i(sv_frame_t frame, sv_frame_t iSampleRate)
|
Chris@1040
|
455 {
|
Chris@1040
|
456 if (frame < 0) return -frame2RealTime_i(-frame, iSampleRate);
|
Chris@1040
|
457
|
Chris@1262
|
458 int sec = int(frame / iSampleRate);
|
Chris@1040
|
459 frame -= sec * iSampleRate;
|
Chris@1262
|
460 int nsec = int((double(frame) / double(iSampleRate)) * ONE_BILLION + 0.5);
|
Chris@1262
|
461 // Use ctor here instead of setting data members directly to
|
Chris@1262
|
462 // ensure nsec > ONE_BILLION is handled properly. It's extremely
|
Chris@1262
|
463 // unlikely, but not impossible.
|
Chris@1262
|
464 return RealTime(sec, nsec);
|
Chris@1040
|
465 }
|
Chris@1040
|
466
|
Chris@1038
|
467 sv_frame_t
|
Chris@1040
|
468 RealTime::realTime2Frame(const RealTime &time, sv_samplerate_t sampleRate)
|
Chris@0
|
469 {
|
Chris@0
|
470 if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
|
Chris@1262
|
471 double s = time.sec + double(time.nsec) / 1000000000.0;
|
Chris@1262
|
472 return sv_frame_t(s * sampleRate + 0.5);
|
Chris@0
|
473 }
|
Chris@0
|
474
|
Chris@0
|
475 RealTime
|
Chris@1040
|
476 RealTime::frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate)
|
Chris@0
|
477 {
|
Chris@1040
|
478 if (sampleRate == double(int(sampleRate))) {
|
Chris@1040
|
479 return frame2RealTime_i(frame, int(sampleRate));
|
Chris@1040
|
480 }
|
Chris@0
|
481
|
Chris@1040
|
482 double sec = double(frame) / sampleRate;
|
Chris@1040
|
483 return fromSeconds(sec);
|
Chris@0
|
484 }
|
Chris@0
|
485
|
Chris@0
|
486 const RealTime RealTime::zeroTime(0,0);
|
Chris@0
|
487
|