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@1070
|
21 #ifndef SV_REAL_TIME_H
|
Chris@1070
|
22 #define SV_REAL_TIME_H
|
Chris@0
|
23
|
Chris@1038
|
24 #include "BaseTypes.h"
|
Chris@1038
|
25
|
Chris@0
|
26 #include <iostream>
|
Chris@0
|
27 #include <string>
|
Chris@0
|
28
|
Chris@1040
|
29 #include <vamp-hostsdk/RealTime.h>
|
Chris@1040
|
30
|
Chris@1216
|
31 #ifdef _MSC_VER
|
Chris@1216
|
32 #include "winsock.h" // struct timeval is in here
|
Chris@1216
|
33 #else
|
Chris@1216
|
34 #include "sys/time.h"
|
Chris@1216
|
35 #endif
|
Chris@26
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * RealTime represents time values to nanosecond precision
|
Chris@0
|
39 * with accurate arithmetic and frame-rate conversion functions.
|
Chris@0
|
40 */
|
Chris@0
|
41
|
Chris@0
|
42 struct RealTime
|
Chris@0
|
43 {
|
Chris@0
|
44 int sec;
|
Chris@0
|
45 int nsec;
|
Chris@0
|
46
|
Chris@0
|
47 int usec() const { return nsec / 1000; }
|
Chris@0
|
48 int msec() const { return nsec / 1000000; }
|
Chris@0
|
49
|
Chris@0
|
50 RealTime(): sec(0), nsec(0) {}
|
Chris@0
|
51 RealTime(int s, int n);
|
Chris@0
|
52
|
Chris@0
|
53 RealTime(const RealTime &r) :
|
Chris@1429
|
54 sec(r.sec), nsec(r.nsec) { }
|
Chris@0
|
55
|
Chris@1040
|
56 RealTime(const Vamp::RealTime &r) :
|
Chris@1429
|
57 sec(r.sec), nsec(r.nsec) { }
|
Chris@1040
|
58
|
Chris@26
|
59 static RealTime fromSeconds(double sec);
|
Chris@26
|
60 static RealTime fromMilliseconds(int msec);
|
Chris@1541
|
61 static RealTime fromMicroseconds(int usec);
|
Chris@26
|
62 static RealTime fromTimeval(const struct timeval &);
|
Chris@439
|
63 static RealTime fromXsdDuration(std::string xsdd);
|
Chris@439
|
64
|
Chris@439
|
65 double toDouble() const;
|
Chris@1040
|
66 Vamp::RealTime toVampRealTime() const { return Vamp::RealTime(sec, nsec); }
|
Chris@26
|
67
|
Chris@0
|
68 RealTime &operator=(const RealTime &r) {
|
Chris@1429
|
69 sec = r.sec; nsec = r.nsec; return *this;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 RealTime operator+(const RealTime &r) const {
|
Chris@1429
|
73 return RealTime(sec + r.sec, nsec + r.nsec);
|
Chris@0
|
74 }
|
Chris@0
|
75 RealTime operator-(const RealTime &r) const {
|
Chris@1429
|
76 return RealTime(sec - r.sec, nsec - r.nsec);
|
Chris@0
|
77 }
|
Chris@0
|
78 RealTime operator-() const {
|
Chris@1429
|
79 return RealTime(-sec, -nsec);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 bool operator <(const RealTime &r) const {
|
Chris@1429
|
83 if (sec == r.sec) return nsec < r.nsec;
|
Chris@1429
|
84 else return sec < r.sec;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 bool operator >(const RealTime &r) const {
|
Chris@1429
|
88 if (sec == r.sec) return nsec > r.nsec;
|
Chris@1429
|
89 else return sec > r.sec;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 bool operator==(const RealTime &r) const {
|
Chris@0
|
93 return (sec == r.sec && nsec == r.nsec);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 bool operator!=(const RealTime &r) const {
|
Chris@0
|
97 return !(r == *this);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 bool operator>=(const RealTime &r) const {
|
Chris@0
|
101 if (sec == r.sec) return nsec >= r.nsec;
|
Chris@0
|
102 else return sec >= r.sec;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 bool operator<=(const RealTime &r) const {
|
Chris@0
|
106 if (sec == r.sec) return nsec <= r.nsec;
|
Chris@0
|
107 else return sec <= r.sec;
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@183
|
110 RealTime operator*(int m) const;
|
Chris@0
|
111 RealTime operator/(int d) const;
|
Chris@0
|
112
|
Chris@378
|
113 RealTime operator*(double m) const;
|
Chris@378
|
114 RealTime operator/(double d) const;
|
Chris@378
|
115
|
Chris@350
|
116 /**
|
Chris@350
|
117 * Return the ratio of two times.
|
Chris@350
|
118 */
|
Chris@0
|
119 double operator/(const RealTime &r) const;
|
Chris@0
|
120
|
Chris@350
|
121 /**
|
Chris@350
|
122 * Return a human-readable debug-type string to full precision
|
Chris@350
|
123 * (probably not a format to show to a user directly). If align
|
Chris@350
|
124 * is true, prepend " " to the start of positive values so that
|
Chris@350
|
125 * they line up with negative ones (which start with "-").
|
Chris@350
|
126 */
|
Chris@121
|
127 std::string toString(bool align = false) const;
|
Chris@0
|
128
|
Chris@350
|
129 /**
|
Chris@350
|
130 * Convert a string as obtained from toString back to a RealTime
|
Chris@350
|
131 * object.
|
Chris@350
|
132 */
|
Chris@350
|
133 static RealTime fromString(std::string);
|
Chris@350
|
134
|
Chris@350
|
135 /**
|
Chris@1070
|
136 * Return a user-readable string to the nearest millisecond,
|
Chris@1070
|
137 * typically in a form like HH:MM:SS.mmm. The exact format will
|
Chris@1070
|
138 * depend on the application preferences for time display
|
Chris@1070
|
139 * precision and hours:minutes:seconds format -- this function
|
Chris@1070
|
140 * simply dispatches to toMSText or toFrameText with appropriate
|
Chris@1070
|
141 * arguments depending on the preferences.
|
Chris@1070
|
142 *
|
Chris@1070
|
143 * If fixedDp is true, the result will be padded to 3 dp,
|
Chris@1070
|
144 * i.e. millisecond resolution, even if the number of milliseconds
|
Chris@1070
|
145 * is a multiple of 10.
|
Chris@350
|
146 */
|
Chris@0
|
147 std::string toText(bool fixedDp = false) const;
|
Chris@0
|
148
|
Chris@1070
|
149 /**
|
Chris@1070
|
150 * Return a user-readable string to the nearest millisecond.
|
Chris@1070
|
151 *
|
Chris@1070
|
152 * If fixedDp is true, the result will be padded to 3 dp,
|
Chris@1070
|
153 * i.e. millisecond resolution, even if the number of milliseconds
|
Chris@1070
|
154 * is a multiple of 10.
|
Chris@1070
|
155 *
|
Chris@1070
|
156 * If hms is true, results may be returned in the form
|
Chris@1070
|
157 * HH:MM:SS.mmm (if the time is large enough). If hms is false,
|
Chris@1070
|
158 * the result will always be a (fractional) number of seconds.
|
Chris@1070
|
159 *
|
Chris@1070
|
160 * Unlike toText, this function does not depend on the application
|
Chris@1070
|
161 * preferences.
|
Chris@1070
|
162 */
|
Chris@1070
|
163 std::string toMSText(bool fixedDp, bool hms) const;
|
Chris@1070
|
164
|
Chris@350
|
165 /**
|
Chris@612
|
166 * Return a user-readable string in which seconds are divided into
|
Chris@612
|
167 * frames (presumably at a lower frame rate than audio rate,
|
Chris@612
|
168 * e.g. 24 or 25 video frames), in a form like HH:MM:SS:FF. fps
|
Chris@612
|
169 * gives the number of frames per second, and must be integral
|
Chris@612
|
170 * (29.97 not supported).
|
Chris@1070
|
171 *
|
Chris@1070
|
172 * Unlike toText, this function does not depend on the application
|
Chris@1070
|
173 * preferences.
|
Chris@612
|
174 */
|
Chris@1070
|
175 std::string toFrameText(int fps, bool hms) const;
|
Chris@612
|
176
|
Chris@612
|
177 /**
|
Chris@1070
|
178 * Return a user-readable string to the nearest second, in H:M:S
|
Chris@1070
|
179 * form. Does not include milliseconds or frames. The result will
|
Chris@1070
|
180 * be suffixed "s" if it contains only seconds (no hours or
|
Chris@1070
|
181 * minutes).
|
Chris@1070
|
182 *
|
Chris@1070
|
183 * Unlike toText, this function does not depend on the application
|
Chris@1070
|
184 * preferences.
|
Chris@350
|
185 */
|
Chris@247
|
186 std::string toSecText() const;
|
Chris@247
|
187
|
Chris@350
|
188 /**
|
Chris@494
|
189 * Return a string in xsd:duration format.
|
Chris@494
|
190 */
|
Chris@494
|
191 std::string toXsdDuration() const;
|
Chris@494
|
192
|
Chris@494
|
193 /**
|
Chris@350
|
194 * Convert a RealTime into a sample frame at the given sample rate.
|
Chris@350
|
195 */
|
Chris@1040
|
196 static sv_frame_t realTime2Frame(const RealTime &r, sv_samplerate_t sampleRate);
|
Chris@350
|
197
|
Chris@350
|
198 /**
|
Chris@350
|
199 * Convert a sample frame at the given sample rate into a RealTime.
|
Chris@350
|
200 */
|
Chris@1040
|
201 static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate);
|
Chris@0
|
202
|
Chris@0
|
203 static const RealTime zeroTime;
|
Chris@0
|
204 };
|
Chris@0
|
205
|
Chris@0
|
206 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
|
Chris@0
|
207
|
Chris@0
|
208 #endif
|