1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6
An API for audio analysis and feature extraction plugins.
8
Centre for Digital Music, Queen Mary, University of London.
9
Copyright 2006 Chris Cannam.
11
Permission is hereby granted, free of charge, to any person
12
obtaining a copy of this software and associated documentation
13
files (the "Software"), to deal in the Software without
14
restriction, including without limitation the rights to use, copy,
15
modify, merge, publish, distribute, sublicense, and/or sell copies
16
of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions:
19
The above copyright notice and this permission notice shall be
20
included in all copies or substantial portions of the Software.
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
26
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
Except as contained in this notice, the names of the Centre for
31
Digital Music; Queen Mary, University of London; and Chris Cannam
32
shall not be used in advertising or otherwise to promote the sale,
33
use or other dealings in this Software without prior written
38
This is a modified version of a source file from the
39
Rosegarden MIDI and audio sequencer and notation editor.
40
This file copyright 2000-2006 Chris Cannam.
41
Relicensed by the author as detailed above.
48
#define stringstream strstream
60
#include "vamp-sdk/RealTime.h"
62
_VAMP_SDK_PLUGSPACE_BEGIN(RealTime.cpp)
66
// A RealTime consists of two ints that must be at least 32 bits each.
67
// A signed 32-bit int can store values exceeding +/- 2 billion. This
68
// means we can safely use our lower int for nanoseconds, as there are
69
// 1 billion nanoseconds in a second and we need to handle double that
70
// because of the implementations of addition etc that we use.
72
// The maximum valid RealTime on a 32-bit system is somewhere around
73
// 68 years: 999999999 nanoseconds longer than the classic Unix epoch.
75
#define ONE_BILLION 1000000000
77
RealTime::RealTime(int s, int n) :
81
while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
82
while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
84
while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
85
while (nsec > 0) { nsec -= ONE_BILLION; ++sec; }
87
while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
88
while (nsec < 0) { nsec += ONE_BILLION; --sec; }
93
RealTime::fromSeconds(double sec)
95
return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION + 0.5));
99
RealTime::fromMilliseconds(int msec)
101
return RealTime(msec / 1000, (msec % 1000) * 1000000);
106
RealTime::fromTimeval(const struct timeval &tv)
108
return RealTime(tv.tv_sec, tv.tv_usec * 1000);
112
std::ostream &operator<<(std::ostream &out, const RealTime &rt)
114
if (rt < RealTime::zeroTime) {
120
int s = (rt.sec < 0 ? -rt.sec : rt.sec);
121
int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec);
126
if (nn == 0) out << "00000000";
127
else while (nn < (ONE_BILLION / 10)) {
137
RealTime::toString() const
139
std::stringstream out;
146
std::string s = out.str();
149
return s.substr(0, s.length() - 1);
153
RealTime::toText(bool fixedDp) const
155
if (*this < RealTime::zeroTime) return "-" + (-*this).toText();
157
std::stringstream out;
160
out << (sec / 3600) << ":";
164
out << (sec % 3600) / 60 << ":";
168
out << ((sec % 60) / 10);
182
} else if (fixedDp) {
187
} else if (fixedDp) {
190
} else if (fixedDp) {
198
std::string s = out.str();
205
RealTime::operator/(int d) const
207
int secdiv = sec / d;
208
int secrem = sec % d;
210
double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d;
212
return RealTime(secdiv, int(nsecdiv + 0.5));
216
RealTime::operator/(const RealTime &r) const
218
double lTotal = double(sec) * ONE_BILLION + double(nsec);
219
double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec);
221
if (rTotal == 0) return 0.0;
222
else return lTotal/rTotal;
226
RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
228
if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
229
double s = time.sec + double(time.nsec + 1) / 1000000000.0;
230
return long(s * sampleRate);
234
RealTime::frame2RealTime(long frame, unsigned int sampleRate)
236
if (frame < 0) return -frame2RealTime(-frame, sampleRate);
239
rt.sec = frame / long(sampleRate);
240
frame -= rt.sec * long(sampleRate);
241
rt.nsec = (int)(((double(frame) * 1000000.0) / sampleRate) * 1000.0);
245
const RealTime RealTime::zeroTime(0,0);
249
_VAMP_SDK_PLUGSPACE_END(RealTime.cpp)