Chris@23: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@23: Chris@23: /* Chris@23: Vamp Chris@23: Chris@23: An API for audio analysis and feature extraction plugins. Chris@23: Chris@23: Centre for Digital Music, Queen Mary, University of London. Chris@23: Copyright 2006 Chris Cannam. Chris@23: Chris@23: Permission is hereby granted, free of charge, to any person Chris@23: obtaining a copy of this software and associated documentation Chris@23: files (the "Software"), to deal in the Software without Chris@23: restriction, including without limitation the rights to use, copy, Chris@23: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@23: of the Software, and to permit persons to whom the Software is Chris@23: furnished to do so, subject to the following conditions: Chris@23: Chris@23: The above copyright notice and this permission notice shall be Chris@23: included in all copies or substantial portions of the Software. Chris@23: Chris@23: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@23: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@23: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@23: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@23: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@23: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@23: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@23: Chris@23: Except as contained in this notice, the names of the Centre for Chris@23: Digital Music; Queen Mary, University of London; and Chris Cannam Chris@23: shall not be used in advertising or otherwise to promote the sale, Chris@23: use or other dealings in this Software without prior written Chris@23: authorization. Chris@23: */ Chris@23: Chris@23: /* Chris@23: This is a modified version of a source file from the Chris@23: Rosegarden MIDI and audio sequencer and notation editor. Chris@23: This file copyright 2000-2006 Chris Cannam. Chris@23: Relicensed by the author as detailed above. Chris@23: */ Chris@23: Chris@23: #ifndef _VAMP_REAL_TIME_H_ Chris@23: #define _VAMP_REAL_TIME_H_ Chris@23: Chris@23: #include Chris@23: #include Chris@23: Chris@23: #ifndef _WIN32 Chris@23: struct timeval; Chris@23: #endif Chris@23: Chris@23: #include "plugguard.h" Chris@23: _VAMP_SDK_PLUGSPACE_BEGIN(RealTime.h) Chris@23: Chris@23: namespace Vamp { Chris@23: Chris@23: /** Chris@23: * \class RealTime RealTime.h Chris@23: * Chris@23: * RealTime represents time values to nanosecond precision Chris@23: * with accurate arithmetic and frame-rate conversion functions. Chris@23: */ Chris@23: Chris@23: struct RealTime Chris@23: { Chris@23: int sec; Chris@23: int nsec; Chris@23: Chris@23: int usec() const { return nsec / 1000; } Chris@23: int msec() const { return nsec / 1000000; } Chris@23: Chris@23: RealTime(): sec(0), nsec(0) {} Chris@23: RealTime(int s, int n); Chris@23: Chris@23: RealTime(const RealTime &r) : Chris@23: sec(r.sec), nsec(r.nsec) { } Chris@23: Chris@23: static RealTime fromSeconds(double sec); Chris@23: static RealTime fromMilliseconds(int msec); Chris@23: Chris@23: #ifndef _WIN32 Chris@23: static RealTime fromTimeval(const struct timeval &); Chris@23: #endif Chris@23: Chris@23: RealTime &operator=(const RealTime &r) { Chris@23: sec = r.sec; nsec = r.nsec; return *this; Chris@23: } Chris@23: Chris@23: RealTime operator+(const RealTime &r) const { Chris@23: return RealTime(sec + r.sec, nsec + r.nsec); Chris@23: } Chris@23: RealTime operator-(const RealTime &r) const { Chris@23: return RealTime(sec - r.sec, nsec - r.nsec); Chris@23: } Chris@23: RealTime operator-() const { Chris@23: return RealTime(-sec, -nsec); Chris@23: } Chris@23: Chris@23: bool operator <(const RealTime &r) const { Chris@23: if (sec == r.sec) return nsec < r.nsec; Chris@23: else return sec < r.sec; Chris@23: } Chris@23: Chris@23: bool operator >(const RealTime &r) const { Chris@23: if (sec == r.sec) return nsec > r.nsec; Chris@23: else return sec > r.sec; Chris@23: } Chris@23: Chris@23: bool operator==(const RealTime &r) const { Chris@23: return (sec == r.sec && nsec == r.nsec); Chris@23: } Chris@23: Chris@23: bool operator!=(const RealTime &r) const { Chris@23: return !(r == *this); Chris@23: } Chris@23: Chris@23: bool operator>=(const RealTime &r) const { Chris@23: if (sec == r.sec) return nsec >= r.nsec; Chris@23: else return sec >= r.sec; Chris@23: } Chris@23: Chris@23: bool operator<=(const RealTime &r) const { Chris@23: if (sec == r.sec) return nsec <= r.nsec; Chris@23: else return sec <= r.sec; Chris@23: } Chris@23: Chris@23: RealTime operator/(int d) const; Chris@23: Chris@23: /** Chris@23: * Return the ratio of two times. Chris@23: */ Chris@23: double operator/(const RealTime &r) const; Chris@23: Chris@23: /** Chris@23: * Return a human-readable debug-type string to full precision Chris@23: * (probably not a format to show to a user directly) Chris@23: */ Chris@23: std::string toString() const; Chris@23: Chris@23: /** Chris@23: * Return a user-readable string to the nearest millisecond Chris@23: * in a form like HH:MM:SS.mmm Chris@23: */ Chris@23: std::string toText(bool fixedDp = false) const; Chris@23: Chris@23: /** Chris@23: * Convert a RealTime into a sample frame at the given sample rate. Chris@23: */ Chris@23: static long realTime2Frame(const RealTime &r, unsigned int sampleRate); Chris@23: Chris@23: /** Chris@23: * Convert a sample frame at the given sample rate into a RealTime. Chris@23: */ Chris@23: static RealTime frame2RealTime(long frame, unsigned int sampleRate); Chris@23: Chris@23: static const RealTime zeroTime; Chris@23: }; Chris@23: Chris@23: std::ostream &operator<<(std::ostream &out, const RealTime &rt); Chris@23: Chris@23: } Chris@23: Chris@23: _VAMP_SDK_PLUGSPACE_END(RealTime.h) Chris@23: Chris@23: #endif