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