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