comparison base/RealTime.h @ 0:fc9323a41f5a

start base : Sonic Visualiser sv1-1.0rc1
author lbajardsilogic
date Fri, 11 May 2007 09:08:14 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:fc9323a41f5a
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 /*
16 This is a modified version of a source file from the
17 Rosegarden MIDI and audio sequencer and notation editor.
18 This file copyright 2000-2006 Chris Cannam.
19 */
20
21 #ifndef _REAL_TIME_H_
22 #define _REAL_TIME_H_
23
24 #include <iostream>
25 #include <string>
26
27 struct timeval;
28
29
30 /**
31 * RealTime represents time values to nanosecond precision
32 * with accurate arithmetic and frame-rate conversion functions.
33 */
34
35 struct RealTime
36 {
37 int sec;
38 int nsec;
39
40 int usec() const { return nsec / 1000; }
41 int msec() const { return nsec / 1000000; }
42
43 RealTime(): sec(0), nsec(0) {}
44 RealTime(int s, int n);
45
46 RealTime(const RealTime &r) :
47 sec(r.sec), nsec(r.nsec) { }
48
49 static RealTime fromSeconds(double sec);
50 static RealTime fromMilliseconds(int msec);
51 static RealTime fromTimeval(const struct timeval &);
52
53 RealTime &operator=(const RealTime &r) {
54 sec = r.sec; nsec = r.nsec; return *this;
55 }
56
57 RealTime operator+(const RealTime &r) const {
58 return RealTime(sec + r.sec, nsec + r.nsec);
59 }
60 RealTime operator-(const RealTime &r) const {
61 return RealTime(sec - r.sec, nsec - r.nsec);
62 }
63 RealTime operator-() const {
64 return RealTime(-sec, -nsec);
65 }
66
67 bool operator <(const RealTime &r) const {
68 if (sec == r.sec) return nsec < r.nsec;
69 else return sec < r.sec;
70 }
71
72 bool operator >(const RealTime &r) const {
73 if (sec == r.sec) return nsec > r.nsec;
74 else return sec > r.sec;
75 }
76
77 bool operator==(const RealTime &r) const {
78 return (sec == r.sec && nsec == r.nsec);
79 }
80
81 bool operator!=(const RealTime &r) const {
82 return !(r == *this);
83 }
84
85 bool operator>=(const RealTime &r) const {
86 if (sec == r.sec) return nsec >= r.nsec;
87 else return sec >= r.sec;
88 }
89
90 bool operator<=(const RealTime &r) const {
91 if (sec == r.sec) return nsec <= r.nsec;
92 else return sec <= r.sec;
93 }
94
95 RealTime operator*(int m) const;
96 RealTime operator/(int d) const;
97
98 // Find the fractional difference between times
99 //
100 double operator/(const RealTime &r) const;
101
102 // Return a human-readable debug-type string to full precision
103 // (probably not a format to show to a user directly). If align
104 // is true, prepend " " to the start of positive values so that
105 // they line up with negative ones (which start with "-").
106 //
107 std::string toString(bool align = false) const;
108
109 // Return a user-readable string to the nearest millisecond
110 // in a form like HH:MM:SS.mmm
111 //
112 std::string toText(bool fixedDp = false) const;
113
114 // Return a user-readable string to the nearest second in a form
115 // like "6s" (for less than a minute) or "2:21" (for more).
116 //
117 std::string toSecText() const;
118
119 // Convenience functions for handling sample frames
120 //
121 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
122 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
123
124 static const RealTime zeroTime;
125 };
126
127 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
128
129 #endif