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