Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: /* Chris@0: This is a modified version of a source file from the Chris@0: Rosegarden MIDI and audio sequencer and notation editor. Chris@17: This file copyright 2000-2006 Chris Cannam. Chris@0: */ Chris@0: Chris@1070: #ifndef SV_REAL_TIME_H Chris@1070: #define SV_REAL_TIME_H Chris@0: Chris@1038: #include "BaseTypes.h" Chris@1038: Chris@0: #include Chris@0: #include Chris@0: Chris@1040: #include Chris@1040: Chris@1216: #ifdef _MSC_VER Chris@1216: #include "winsock.h" // struct timeval is in here Chris@1216: #else Chris@1216: #include "sys/time.h" Chris@1216: #endif Chris@26: Chris@0: /** Chris@0: * RealTime represents time values to nanosecond precision Chris@0: * with accurate arithmetic and frame-rate conversion functions. Chris@0: */ Chris@0: Chris@0: struct RealTime Chris@0: { Chris@0: int sec; Chris@0: int nsec; Chris@0: Chris@0: int usec() const { return nsec / 1000; } Chris@0: int msec() const { return nsec / 1000000; } Chris@0: Chris@0: RealTime(): sec(0), nsec(0) {} Chris@0: RealTime(int s, int n); Chris@0: Chris@0: RealTime(const RealTime &r) : Chris@1429: sec(r.sec), nsec(r.nsec) { } Chris@0: Chris@1040: RealTime(const Vamp::RealTime &r) : Chris@1429: sec(r.sec), nsec(r.nsec) { } Chris@1040: Chris@26: static RealTime fromSeconds(double sec); Chris@26: static RealTime fromMilliseconds(int msec); Chris@1541: static RealTime fromMicroseconds(int usec); Chris@26: static RealTime fromTimeval(const struct timeval &); Chris@439: static RealTime fromXsdDuration(std::string xsdd); Chris@439: Chris@439: double toDouble() const; Chris@1040: Vamp::RealTime toVampRealTime() const { return Vamp::RealTime(sec, nsec); } Chris@26: Chris@0: RealTime &operator=(const RealTime &r) { Chris@1429: sec = r.sec; nsec = r.nsec; return *this; Chris@0: } Chris@0: Chris@0: RealTime operator+(const RealTime &r) const { Chris@1429: return RealTime(sec + r.sec, nsec + r.nsec); Chris@0: } Chris@0: RealTime operator-(const RealTime &r) const { Chris@1429: return RealTime(sec - r.sec, nsec - r.nsec); Chris@0: } Chris@0: RealTime operator-() const { Chris@1429: return RealTime(-sec, -nsec); Chris@0: } Chris@0: Chris@0: bool operator <(const RealTime &r) const { Chris@1429: if (sec == r.sec) return nsec < r.nsec; Chris@1429: else return sec < r.sec; Chris@0: } Chris@0: Chris@0: bool operator >(const RealTime &r) const { Chris@1429: if (sec == r.sec) return nsec > r.nsec; Chris@1429: else return sec > r.sec; Chris@0: } Chris@0: Chris@0: bool operator==(const RealTime &r) const { Chris@0: return (sec == r.sec && nsec == r.nsec); Chris@0: } Chris@0: Chris@0: bool operator!=(const RealTime &r) const { Chris@0: return !(r == *this); Chris@0: } Chris@0: Chris@0: bool operator>=(const RealTime &r) const { Chris@0: if (sec == r.sec) return nsec >= r.nsec; Chris@0: else return sec >= r.sec; Chris@0: } Chris@0: Chris@0: bool operator<=(const RealTime &r) const { Chris@0: if (sec == r.sec) return nsec <= r.nsec; Chris@0: else return sec <= r.sec; Chris@0: } Chris@0: Chris@183: RealTime operator*(int m) const; Chris@0: RealTime operator/(int d) const; Chris@0: Chris@378: RealTime operator*(double m) const; Chris@378: RealTime operator/(double d) const; Chris@378: Chris@350: /** Chris@350: * Return the ratio of two times. Chris@350: */ Chris@0: double operator/(const RealTime &r) const; Chris@0: Chris@350: /** Chris@350: * Return a human-readable debug-type string to full precision Chris@350: * (probably not a format to show to a user directly). If align Chris@350: * is true, prepend " " to the start of positive values so that Chris@350: * they line up with negative ones (which start with "-"). Chris@350: */ Chris@121: std::string toString(bool align = false) const; Chris@0: Chris@350: /** Chris@350: * Convert a string as obtained from toString back to a RealTime Chris@350: * object. Chris@350: */ Chris@350: static RealTime fromString(std::string); Chris@350: Chris@350: /** Chris@1070: * Return a user-readable string to the nearest millisecond, Chris@1070: * typically in a form like HH:MM:SS.mmm. The exact format will Chris@1070: * depend on the application preferences for time display Chris@1070: * precision and hours:minutes:seconds format -- this function Chris@1070: * simply dispatches to toMSText or toFrameText with appropriate Chris@1070: * arguments depending on the preferences. Chris@1070: * Chris@1070: * If fixedDp is true, the result will be padded to 3 dp, Chris@1070: * i.e. millisecond resolution, even if the number of milliseconds Chris@1070: * is a multiple of 10. Chris@350: */ Chris@0: std::string toText(bool fixedDp = false) const; Chris@0: Chris@1070: /** Chris@1070: * Return a user-readable string to the nearest millisecond. Chris@1070: * Chris@1070: * If fixedDp is true, the result will be padded to 3 dp, Chris@1070: * i.e. millisecond resolution, even if the number of milliseconds Chris@1070: * is a multiple of 10. Chris@1070: * Chris@1070: * If hms is true, results may be returned in the form Chris@1070: * HH:MM:SS.mmm (if the time is large enough). If hms is false, Chris@1070: * the result will always be a (fractional) number of seconds. Chris@1070: * Chris@1070: * Unlike toText, this function does not depend on the application Chris@1070: * preferences. Chris@1070: */ Chris@1070: std::string toMSText(bool fixedDp, bool hms) const; Chris@1070: Chris@350: /** Chris@612: * Return a user-readable string in which seconds are divided into Chris@612: * frames (presumably at a lower frame rate than audio rate, Chris@612: * e.g. 24 or 25 video frames), in a form like HH:MM:SS:FF. fps Chris@612: * gives the number of frames per second, and must be integral Chris@612: * (29.97 not supported). Chris@1070: * Chris@1070: * Unlike toText, this function does not depend on the application Chris@1070: * preferences. Chris@612: */ Chris@1070: std::string toFrameText(int fps, bool hms) const; Chris@612: Chris@612: /** Chris@1070: * Return a user-readable string to the nearest second, in H:M:S Chris@1070: * form. Does not include milliseconds or frames. The result will Chris@1070: * be suffixed "s" if it contains only seconds (no hours or Chris@1070: * minutes). Chris@1070: * Chris@1070: * Unlike toText, this function does not depend on the application Chris@1070: * preferences. Chris@350: */ Chris@247: std::string toSecText() const; Chris@247: Chris@350: /** Chris@494: * Return a string in xsd:duration format. Chris@494: */ Chris@494: std::string toXsdDuration() const; Chris@494: Chris@494: /** Chris@350: * Convert a RealTime into a sample frame at the given sample rate. Chris@350: */ Chris@1040: static sv_frame_t realTime2Frame(const RealTime &r, sv_samplerate_t sampleRate); Chris@350: Chris@350: /** Chris@350: * Convert a sample frame at the given sample rate into a RealTime. Chris@350: */ Chris@1040: static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate); Chris@0: Chris@0: static const RealTime zeroTime; Chris@0: }; Chris@0: Chris@0: std::ostream &operator<<(std::ostream &out, const RealTime &rt); Chris@0: Chris@0: #endif