annotate base/RealTime.h @ 650:f6ed1a7a920f

* Add QuickTime if on a Mac
author Chris Cannam
date Thu, 23 Sep 2010 11:34:07 +0100
parents 75f154085a4d
children cc27f35aa75c
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7
Chris@52 8 This program is free software; you can redistribute it and/or
Chris@52 9 modify it under the terms of the GNU General Public License as
Chris@52 10 published by the Free Software Foundation; either version 2 of the
Chris@52 11 License, or (at your option) any later version. See the file
Chris@52 12 COPYING included with this distribution for more information.
Chris@0 13 */
Chris@0 14
Chris@0 15 /*
Chris@0 16 This is a modified version of a source file from the
Chris@0 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 18 This file copyright 2000-2006 Chris Cannam.
Chris@0 19 */
Chris@0 20
Chris@0 21 #ifndef _REAL_TIME_H_
Chris@0 22 #define _REAL_TIME_H_
Chris@0 23
Chris@0 24 #include <iostream>
Chris@0 25 #include <string>
Chris@0 26
Chris@26 27 struct timeval;
Chris@26 28
Chris@26 29
Chris@0 30 /**
Chris@0 31 * RealTime represents time values to nanosecond precision
Chris@0 32 * with accurate arithmetic and frame-rate conversion functions.
Chris@0 33 */
Chris@0 34
Chris@0 35 struct RealTime
Chris@0 36 {
Chris@0 37 int sec;
Chris@0 38 int nsec;
Chris@0 39
Chris@0 40 int usec() const { return nsec / 1000; }
Chris@0 41 int msec() const { return nsec / 1000000; }
Chris@0 42
Chris@0 43 RealTime(): sec(0), nsec(0) {}
Chris@0 44 RealTime(int s, int n);
Chris@0 45
Chris@0 46 RealTime(const RealTime &r) :
Chris@0 47 sec(r.sec), nsec(r.nsec) { }
Chris@0 48
Chris@26 49 static RealTime fromSeconds(double sec);
Chris@26 50 static RealTime fromMilliseconds(int msec);
Chris@26 51 static RealTime fromTimeval(const struct timeval &);
Chris@439 52 static RealTime fromXsdDuration(std::string xsdd);
Chris@439 53
Chris@439 54 double toDouble() const;
Chris@26 55
Chris@0 56 RealTime &operator=(const RealTime &r) {
Chris@0 57 sec = r.sec; nsec = r.nsec; return *this;
Chris@0 58 }
Chris@0 59
Chris@0 60 RealTime operator+(const RealTime &r) const {
Chris@0 61 return RealTime(sec + r.sec, nsec + r.nsec);
Chris@0 62 }
Chris@0 63 RealTime operator-(const RealTime &r) const {
Chris@0 64 return RealTime(sec - r.sec, nsec - r.nsec);
Chris@0 65 }
Chris@0 66 RealTime operator-() const {
Chris@0 67 return RealTime(-sec, -nsec);
Chris@0 68 }
Chris@0 69
Chris@0 70 bool operator <(const RealTime &r) const {
Chris@0 71 if (sec == r.sec) return nsec < r.nsec;
Chris@0 72 else return sec < r.sec;
Chris@0 73 }
Chris@0 74
Chris@0 75 bool operator >(const RealTime &r) const {
Chris@0 76 if (sec == r.sec) return nsec > r.nsec;
Chris@0 77 else return sec > r.sec;
Chris@0 78 }
Chris@0 79
Chris@0 80 bool operator==(const RealTime &r) const {
Chris@0 81 return (sec == r.sec && nsec == r.nsec);
Chris@0 82 }
Chris@0 83
Chris@0 84 bool operator!=(const RealTime &r) const {
Chris@0 85 return !(r == *this);
Chris@0 86 }
Chris@0 87
Chris@0 88 bool operator>=(const RealTime &r) const {
Chris@0 89 if (sec == r.sec) return nsec >= r.nsec;
Chris@0 90 else return sec >= r.sec;
Chris@0 91 }
Chris@0 92
Chris@0 93 bool operator<=(const RealTime &r) const {
Chris@0 94 if (sec == r.sec) return nsec <= r.nsec;
Chris@0 95 else return sec <= r.sec;
Chris@0 96 }
Chris@0 97
Chris@183 98 RealTime operator*(int m) const;
Chris@0 99 RealTime operator/(int d) const;
Chris@0 100
Chris@378 101 RealTime operator*(double m) const;
Chris@378 102 RealTime operator/(double d) const;
Chris@378 103
Chris@350 104 /**
Chris@350 105 * Return the ratio of two times.
Chris@350 106 */
Chris@0 107 double operator/(const RealTime &r) const;
Chris@0 108
Chris@350 109 /**
Chris@350 110 * Return a human-readable debug-type string to full precision
Chris@350 111 * (probably not a format to show to a user directly). If align
Chris@350 112 * is true, prepend " " to the start of positive values so that
Chris@350 113 * they line up with negative ones (which start with "-").
Chris@350 114 */
Chris@121 115 std::string toString(bool align = false) const;
Chris@0 116
Chris@350 117 /**
Chris@350 118 * Convert a string as obtained from toString back to a RealTime
Chris@350 119 * object.
Chris@350 120 */
Chris@350 121 static RealTime fromString(std::string);
Chris@350 122
Chris@350 123 /**
Chris@350 124 * Return a user-readable string to the nearest millisecond, in a
Chris@350 125 * form like HH:MM:SS.mmm
Chris@350 126 */
Chris@0 127 std::string toText(bool fixedDp = false) const;
Chris@0 128
Chris@350 129 /**
Chris@612 130 * Return a user-readable string in which seconds are divided into
Chris@612 131 * frames (presumably at a lower frame rate than audio rate,
Chris@612 132 * e.g. 24 or 25 video frames), in a form like HH:MM:SS:FF. fps
Chris@612 133 * gives the number of frames per second, and must be integral
Chris@612 134 * (29.97 not supported).
Chris@612 135 */
Chris@612 136 std::string toFrameText(int fps) const;
Chris@612 137
Chris@612 138 /**
Chris@350 139 * Return a user-readable string to the nearest second, in a form
Chris@350 140 * like "6s" (for less than a minute) or "2:21" (for more).
Chris@350 141 */
Chris@247 142 std::string toSecText() const;
Chris@247 143
Chris@350 144 /**
Chris@494 145 * Return a string in xsd:duration format.
Chris@494 146 */
Chris@494 147 std::string toXsdDuration() const;
Chris@494 148
Chris@494 149 /**
Chris@350 150 * Convert a RealTime into a sample frame at the given sample rate.
Chris@350 151 */
Chris@0 152 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
Chris@350 153
Chris@350 154 /**
Chris@350 155 * Convert a sample frame at the given sample rate into a RealTime.
Chris@350 156 */
Chris@0 157 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
Chris@0 158
Chris@0 159 static const RealTime zeroTime;
Chris@0 160 };
Chris@0 161
Chris@0 162 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
Chris@0 163
Chris@0 164 #endif