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
64 // A RealTime consists of two ints that must be at least 32 bits each.
65 // A signed 32-bit int can store values exceeding +/- 2 billion. This
66 // means we can safely use our lower int for nanoseconds, as there are
67 // 1 billion nanoseconds in a second and we need to handle double that
68 // because of the implementations of addition etc that we use.
70 // The maximum valid RealTime on a 32-bit system is somewhere around
71 // 68 years: 999999999 nanoseconds longer than the classic Unix epoch.
73 #define ONE_BILLION 1000000000
75 RealTime::RealTime(int s, int n) :
79 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
80 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
82 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
83 while (nsec > 0) { nsec -= ONE_BILLION; ++sec; }
85 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
86 while (nsec < 0) { nsec += ONE_BILLION; --sec; }
91 RealTime::fromSeconds(double sec)
93 return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION + 0.5));
97 RealTime::fromMilliseconds(int msec)
99 return RealTime(msec / 1000, (msec % 1000) * 1000000);
104 RealTime::fromTimeval(const struct timeval &tv)
106 return RealTime(tv.tv_sec, tv.tv_usec * 1000);
110 std::ostream &operator<<(std::ostream &out, const RealTime &rt)
112 if (rt < RealTime::zeroTime) {
118 int s = (rt.sec < 0 ? -rt.sec : rt.sec);
119 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec);
124 if (nn == 0) out << "00000000";
125 else while (nn < (ONE_BILLION / 10)) {
135 RealTime::toString() const
137 std::stringstream out;
144 std::string s = out.str();
147 return s.substr(0, s.length() - 1);
151 RealTime::toText(bool fixedDp) const
153 if (*this < RealTime::zeroTime) return "-" + (-*this).toText();
155 std::stringstream out;
158 out << (sec / 3600) << ":";
162 out << (sec % 3600) / 60 << ":";
166 out << ((sec % 60) / 10);
180 } else if (fixedDp) {
185 } else if (fixedDp) {
188 } else if (fixedDp) {
196 std::string s = out.str();
203 RealTime::operator/(int d) const
205 int secdiv = sec / d;
206 int secrem = sec % d;
208 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d;
210 return RealTime(secdiv, int(nsecdiv + 0.5));
214 RealTime::operator/(const RealTime &r) const
216 double lTotal = double(sec) * ONE_BILLION + double(nsec);
217 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec);
219 if (rTotal == 0) return 0.0;
220 else return lTotal/rTotal;
224 RealTime::realTime2Frame(const RealTime &time, unsigned int sampleRate)
226 if (time < zeroTime) return -realTime2Frame(-time, sampleRate);
228 // We like integers. The last term is always zero unless the
229 // sample rate is greater than 1MHz, but hell, you never know...
232 time.sec * sampleRate +
233 (time.msec() * sampleRate) / 1000 +
234 ((time.usec() - 1000 * time.msec()) * sampleRate) / 1000000 +
235 ((time.nsec - 1000 * time.usec()) * sampleRate) / 1000000000;
241 RealTime::frame2RealTime(long frame, unsigned int sampleRate)
243 if (frame < 0) return -frame2RealTime(-frame, sampleRate);
246 rt.sec = frame / long(sampleRate);
247 frame -= rt.sec * long(sampleRate);
248 rt.nsec = (int)(((float(frame) * 1000000) / long(sampleRate)) * 1000);
252 const RealTime RealTime::zeroTime(0,0);