comparison base/RealTime.h @ 0:da6937383da8

initial import
author Chris Cannam
date Tue, 10 Jan 2006 16:33:16 +0000
parents
children d86891498eef
comparison
equal deleted inserted replaced
-1:000000000000 0:da6937383da8
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 A waveform viewer and audio annotation editor.
5 Chris Cannam, Queen Mary University of London, 2005
6
7 This is experimental software. Not for distribution.
8 */
9
10 /*
11 This is a modified version of a source file from the
12 Rosegarden MIDI and audio sequencer and notation editor.
13 This file copyright 2000-2005 Chris Cannam.
14 */
15
16 #ifndef _REAL_TIME_H_
17 #define _REAL_TIME_H_
18
19 #include <iostream>
20 #include <string>
21
22 /**
23 * RealTime represents time values to nanosecond precision
24 * with accurate arithmetic and frame-rate conversion functions.
25 */
26
27 struct RealTime
28 {
29 int sec;
30 int nsec;
31
32 int usec() const { return nsec / 1000; }
33 int msec() const { return nsec / 1000000; }
34
35 RealTime(): sec(0), nsec(0) {}
36 RealTime(int s, int n);
37
38 RealTime(const RealTime &r) :
39 sec(r.sec), nsec(r.nsec) { }
40
41 RealTime &operator=(const RealTime &r) {
42 sec = r.sec; nsec = r.nsec; return *this;
43 }
44
45 RealTime operator+(const RealTime &r) const {
46 return RealTime(sec + r.sec, nsec + r.nsec);
47 }
48 RealTime operator-(const RealTime &r) const {
49 return RealTime(sec - r.sec, nsec - r.nsec);
50 }
51 RealTime operator-() const {
52 return RealTime(-sec, -nsec);
53 }
54
55 bool operator <(const RealTime &r) const {
56 if (sec == r.sec) return nsec < r.nsec;
57 else return sec < r.sec;
58 }
59
60 bool operator >(const RealTime &r) const {
61 if (sec == r.sec) return nsec > r.nsec;
62 else return sec > r.sec;
63 }
64
65 bool operator==(const RealTime &r) const {
66 return (sec == r.sec && nsec == r.nsec);
67 }
68
69 bool operator!=(const RealTime &r) const {
70 return !(r == *this);
71 }
72
73 bool operator>=(const RealTime &r) const {
74 if (sec == r.sec) return nsec >= r.nsec;
75 else return sec >= r.sec;
76 }
77
78 bool operator<=(const RealTime &r) const {
79 if (sec == r.sec) return nsec <= r.nsec;
80 else return sec <= r.sec;
81 }
82
83 RealTime operator/(int d) const;
84
85 // Find the fractional difference between times
86 //
87 double operator/(const RealTime &r) const;
88
89 // Return a human-readable debug-type string to full precision
90 // (probably not a format to show to a user directly)
91 //
92 std::string toString() const;
93
94 // Return a user-readable string to the nearest millisecond
95 // in a form like HH:MM:SS.mmm
96 //
97 std::string toText(bool fixedDp = false) const;
98
99 // Convenience functions for handling sample frames
100 //
101 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
102 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
103
104 static const RealTime zeroTime;
105 };
106
107 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
108
109 #endif