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