Mercurial > hg > svcore
comparison base/test/TestVampRealTime.h @ 1262:c4f873749ab5 3.0-integration
RealTime fixes to comply with new, more demanding tests; also run same (failing) tests on Vamp SDK version
author | Chris Cannam |
---|---|
date | Tue, 15 Nov 2016 16:12:48 +0000 |
parents | |
children | abfc498c52bc |
comparison
equal
deleted
inserted
replaced
1261:968d54d893d2 | 1262:c4f873749ab5 |
---|---|
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 #ifndef TEST_VAMP_REALTIME_H | |
16 | |
17 #include <QObject> | |
18 #include <QtTest> | |
19 #include <QDir> | |
20 | |
21 #include <iostream> | |
22 | |
23 #include <vamp-hostsdk/RealTime.h> | |
24 | |
25 using namespace std; | |
26 | |
27 #define ONE_MILLION 1000000 | |
28 #define ONE_BILLION 1000000000 | |
29 | |
30 class TestVampRealTime : public QObject | |
31 { | |
32 Q_OBJECT | |
33 | |
34 void compareTexts(string s, const char *e) { | |
35 QCOMPARE(QString(s.c_str()), QString(e)); | |
36 } | |
37 | |
38 typedef Vamp::RealTime RealTime; | |
39 typedef long frame_type; | |
40 | |
41 private slots: | |
42 | |
43 void zero() | |
44 { | |
45 QCOMPARE(RealTime(0, 0), RealTime::zeroTime); | |
46 QCOMPARE(RealTime(0, 0).sec, 0); | |
47 QCOMPARE(RealTime(0, 0).nsec, 0); | |
48 QCOMPARE(RealTime(0, 0).msec(), 0); | |
49 QCOMPARE(RealTime(0, 0).usec(), 0); | |
50 } | |
51 | |
52 void ctor() | |
53 { | |
54 QCOMPARE(RealTime(0, 0), RealTime(0, 0)); | |
55 | |
56 // wraparounds | |
57 QCOMPARE(RealTime(0, ONE_BILLION/2), RealTime(1, -ONE_BILLION/2)); | |
58 QCOMPARE(RealTime(0, -ONE_BILLION/2), RealTime(-1, ONE_BILLION/2)); | |
59 | |
60 QCOMPARE(RealTime(1, ONE_BILLION), RealTime(2, 0)); | |
61 QCOMPARE(RealTime(1, -ONE_BILLION), RealTime(0, 0)); | |
62 QCOMPARE(RealTime(-1, ONE_BILLION), RealTime(0, 0)); | |
63 QCOMPARE(RealTime(-1, -ONE_BILLION), RealTime(-2, 0)); | |
64 | |
65 QCOMPARE(RealTime(2, -ONE_BILLION*2), RealTime(0, 0)); | |
66 QCOMPARE(RealTime(2, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); | |
67 | |
68 QCOMPARE(RealTime(-2, ONE_BILLION*2), RealTime(0, 0)); | |
69 QCOMPARE(RealTime(-2, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2)); | |
70 | |
71 QCOMPARE(RealTime(0, 1).sec, 0); | |
72 QCOMPARE(RealTime(0, 1).nsec, 1); | |
73 QCOMPARE(RealTime(0, -1).sec, 0); | |
74 QCOMPARE(RealTime(0, -1).nsec, -1); | |
75 QCOMPARE(RealTime(1, -1).sec, 0); | |
76 QCOMPARE(RealTime(1, -1).nsec, ONE_BILLION-1); | |
77 QCOMPARE(RealTime(-1, 1).sec, 0); | |
78 QCOMPARE(RealTime(-1, 1).nsec, -ONE_BILLION+1); | |
79 QCOMPARE(RealTime(-1, -1).sec, -1); | |
80 QCOMPARE(RealTime(-1, -1).nsec, -1); | |
81 | |
82 QCOMPARE(RealTime(2, -ONE_BILLION*2).sec, 0); | |
83 QCOMPARE(RealTime(2, -ONE_BILLION*2).nsec, 0); | |
84 QCOMPARE(RealTime(2, -ONE_BILLION/2).sec, 1); | |
85 QCOMPARE(RealTime(2, -ONE_BILLION/2).nsec, ONE_BILLION/2); | |
86 | |
87 QCOMPARE(RealTime(-2, ONE_BILLION*2).sec, 0); | |
88 QCOMPARE(RealTime(-2, ONE_BILLION*2).nsec, 0); | |
89 QCOMPARE(RealTime(-2, ONE_BILLION/2).sec, -1); | |
90 QCOMPARE(RealTime(-2, ONE_BILLION/2).nsec, -ONE_BILLION/2); | |
91 } | |
92 | |
93 void fromSeconds() | |
94 { | |
95 QCOMPARE(RealTime::fromSeconds(0), RealTime(0, 0)); | |
96 | |
97 QCOMPARE(RealTime::fromSeconds(0.5).sec, 0); | |
98 QCOMPARE(RealTime::fromSeconds(0.5).nsec, ONE_BILLION/2); | |
99 QCOMPARE(RealTime::fromSeconds(0.5).usec(), ONE_MILLION/2); | |
100 QCOMPARE(RealTime::fromSeconds(0.5).msec(), 500); | |
101 | |
102 QCOMPARE(RealTime::fromSeconds(0.5), RealTime(0, ONE_BILLION/2)); | |
103 QCOMPARE(RealTime::fromSeconds(1), RealTime(1, 0)); | |
104 QCOMPARE(RealTime::fromSeconds(1.5), RealTime(1, ONE_BILLION/2)); | |
105 | |
106 QCOMPARE(RealTime::fromSeconds(-0.5).sec, 0); | |
107 QCOMPARE(RealTime::fromSeconds(-0.5).nsec, -ONE_BILLION/2); | |
108 QCOMPARE(RealTime::fromSeconds(-0.5).usec(), -ONE_MILLION/2); | |
109 QCOMPARE(RealTime::fromSeconds(-0.5).msec(), -500); | |
110 | |
111 QCOMPARE(RealTime::fromSeconds(-1.5).sec, -1); | |
112 QCOMPARE(RealTime::fromSeconds(-1.5).nsec, -ONE_BILLION/2); | |
113 QCOMPARE(RealTime::fromSeconds(-1.5).usec(), -ONE_MILLION/2); | |
114 QCOMPARE(RealTime::fromSeconds(-1.5).msec(), -500); | |
115 | |
116 QCOMPARE(RealTime::fromSeconds(-0.5), RealTime(0, -ONE_BILLION/2)); | |
117 QCOMPARE(RealTime::fromSeconds(-1), RealTime(-1, 0)); | |
118 QCOMPARE(RealTime::fromSeconds(-1.5), RealTime(-1, -ONE_BILLION/2)); | |
119 } | |
120 | |
121 void fromMilliseconds() | |
122 { | |
123 QCOMPARE(RealTime::fromMilliseconds(0), RealTime(0, 0)); | |
124 QCOMPARE(RealTime::fromMilliseconds(500), RealTime(0, ONE_BILLION/2)); | |
125 QCOMPARE(RealTime::fromMilliseconds(1000), RealTime(1, 0)); | |
126 QCOMPARE(RealTime::fromMilliseconds(1500), RealTime(1, ONE_BILLION/2)); | |
127 | |
128 QCOMPARE(RealTime::fromMilliseconds(-0), RealTime(0, 0)); | |
129 QCOMPARE(RealTime::fromMilliseconds(-500), RealTime(0, -ONE_BILLION/2)); | |
130 QCOMPARE(RealTime::fromMilliseconds(-1000), RealTime(-1, 0)); | |
131 QCOMPARE(RealTime::fromMilliseconds(-1500), RealTime(-1, -ONE_BILLION/2)); | |
132 } | |
133 | |
134 void fromTimeval() | |
135 { | |
136 struct timeval tv; | |
137 | |
138 tv.tv_sec = 0; tv.tv_usec = 0; | |
139 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, 0)); | |
140 tv.tv_sec = 0; tv.tv_usec = ONE_MILLION/2; | |
141 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, ONE_BILLION/2)); | |
142 tv.tv_sec = 1; tv.tv_usec = 0; | |
143 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, 0)); | |
144 tv.tv_sec = 1; tv.tv_usec = ONE_MILLION/2; | |
145 QCOMPARE(RealTime::fromTimeval(tv), RealTime(1, ONE_BILLION/2)); | |
146 | |
147 tv.tv_sec = 0; tv.tv_usec = -ONE_MILLION/2; | |
148 QCOMPARE(RealTime::fromTimeval(tv), RealTime(0, -ONE_BILLION/2)); | |
149 tv.tv_sec = -1; tv.tv_usec = 0; | |
150 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, 0)); | |
151 tv.tv_sec = -1; tv.tv_usec = -ONE_MILLION/2; | |
152 QCOMPARE(RealTime::fromTimeval(tv), RealTime(-1, -ONE_BILLION/2)); | |
153 } | |
154 | |
155 void assign() | |
156 { | |
157 RealTime r; | |
158 r = RealTime(0, 0); | |
159 QCOMPARE(r, RealTime::zeroTime); | |
160 r = RealTime(0, ONE_BILLION/2); | |
161 QCOMPARE(r.sec, 0); | |
162 QCOMPARE(r.nsec, ONE_BILLION/2); | |
163 r = RealTime(-1, -ONE_BILLION/2); | |
164 QCOMPARE(r.sec, -1); | |
165 QCOMPARE(r.nsec, -ONE_BILLION/2); | |
166 } | |
167 | |
168 void plus() | |
169 { | |
170 QCOMPARE(RealTime(0, 0) + RealTime(0, 0), RealTime(0, 0)); | |
171 | |
172 QCOMPARE(RealTime(0, 0) + RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); | |
173 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(0, ONE_BILLION/2), RealTime(1, 0)); | |
174 QCOMPARE(RealTime(1, 0) + RealTime(0, ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); | |
175 | |
176 QCOMPARE(RealTime(0, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); | |
177 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(-1, 0)); | |
178 QCOMPARE(RealTime(-1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2)); | |
179 | |
180 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); | |
181 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, 0)); | |
182 QCOMPARE(RealTime(1, 0) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2) + RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); | |
183 | |
184 QCOMPARE(RealTime(0, ONE_BILLION/2) + RealTime(-1, 0), RealTime(0, -ONE_BILLION/2)); | |
185 QCOMPARE(RealTime(0, -ONE_BILLION/2) + RealTime(1, 0), RealTime(0, ONE_BILLION/2)); | |
186 } | |
187 | |
188 void minus() | |
189 { | |
190 QCOMPARE(RealTime(0, 0) - RealTime(0, 0), RealTime(0, 0)); | |
191 | |
192 QCOMPARE(RealTime(0, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); | |
193 QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(0, ONE_BILLION/2), RealTime(0, 0)); | |
194 QCOMPARE(RealTime(1, 0) - RealTime(0, ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); | |
195 | |
196 QCOMPARE(RealTime(0, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, ONE_BILLION/2)); | |
197 QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(0, 0)); | |
198 QCOMPARE(RealTime(-1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(0, -ONE_BILLION/2)); | |
199 | |
200 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); | |
201 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(2, 0)); | |
202 QCOMPARE(RealTime(1, 0) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2) - RealTime(0, -ONE_BILLION/2), RealTime(2, ONE_BILLION/2)); | |
203 | |
204 QCOMPARE(RealTime(0, ONE_BILLION/2) - RealTime(-1, 0), RealTime(1, ONE_BILLION/2)); | |
205 QCOMPARE(RealTime(0, -ONE_BILLION/2) - RealTime(1, 0), RealTime(-1, -ONE_BILLION/2)); | |
206 } | |
207 | |
208 void negate() | |
209 { | |
210 QCOMPARE(-RealTime(0, 0), RealTime(0, 0)); | |
211 QCOMPARE(-RealTime(1, 0), RealTime(-1, 0)); | |
212 QCOMPARE(-RealTime(1, ONE_BILLION/2), RealTime(-1, -ONE_BILLION/2)); | |
213 QCOMPARE(-RealTime(-1, -ONE_BILLION/2), RealTime(1, ONE_BILLION/2)); | |
214 } | |
215 | |
216 void compare() | |
217 { | |
218 int sec, nsec; | |
219 for (sec = -2; sec <= 2; sec += 2) { | |
220 for (nsec = -1; nsec <= 1; nsec += 1) { | |
221 QCOMPARE(RealTime(sec, nsec) < RealTime(sec, nsec), false); | |
222 QCOMPARE(RealTime(sec, nsec) > RealTime(sec, nsec), false); | |
223 QCOMPARE(RealTime(sec, nsec) == RealTime(sec, nsec), true); | |
224 QCOMPARE(RealTime(sec, nsec) != RealTime(sec, nsec), false); | |
225 QCOMPARE(RealTime(sec, nsec) <= RealTime(sec, nsec), true); | |
226 QCOMPARE(RealTime(sec, nsec) >= RealTime(sec, nsec), true); | |
227 } | |
228 } | |
229 RealTime prev(-3, 0); | |
230 for (sec = -2; sec <= 2; sec += 2) { | |
231 for (nsec = -1; nsec <= 1; nsec += 1) { | |
232 | |
233 RealTime curr(sec, nsec); | |
234 | |
235 QCOMPARE(prev < curr, true); | |
236 QCOMPARE(prev > curr, false); | |
237 QCOMPARE(prev == curr, false); | |
238 QCOMPARE(prev != curr, true); | |
239 QCOMPARE(prev <= curr, true); | |
240 QCOMPARE(prev >= curr, false); | |
241 | |
242 QCOMPARE(curr < prev, false); | |
243 QCOMPARE(curr > prev, true); | |
244 QCOMPARE(curr == prev, false); | |
245 QCOMPARE(curr != prev, true); | |
246 QCOMPARE(curr <= prev, false); | |
247 QCOMPARE(curr >= prev, true); | |
248 | |
249 prev = curr; | |
250 } | |
251 } | |
252 } | |
253 | |
254 void frame() | |
255 { | |
256 int frames[] = { | |
257 0, 1, 2047, 2048, 6656, | |
258 32767, 32768, 44100, 44101, | |
259 999999999, 2000000000 | |
260 }; | |
261 int n = sizeof(frames)/sizeof(frames[0]); | |
262 | |
263 int rates[] = { | |
264 1, 2, 8000, 22050, | |
265 44100, 44101, 192000, 2000000001 | |
266 }; | |
267 int m = sizeof(rates)/sizeof(rates[0]); | |
268 | |
269 vector<vector<RealTime>> realTimes = { | |
270 { { 0, 0 }, { 1, 0 }, { 2047, 0 }, { 2048, 0 }, | |
271 { 6656, 0 }, { 32767, 0 }, { 32768, 0 }, { 44100, 0 }, | |
272 { 44101, 0 }, { 999999999, 0 }, { 2000000000, 0 } }, | |
273 { { 0, 0 }, { 0, 500000000 }, { 1023, 500000000 }, { 1024, 0 }, | |
274 { 3328, 0 }, { 16383, 500000000 }, { 16384, 0 }, { 22050, 0 }, | |
275 { 22050, 500000000 }, { 499999999, 500000000 }, { 1000000000, 0 } }, | |
276 { { 0, 0 }, { 0, 125000 }, { 0, 255875000 }, { 0, 256000000 }, | |
277 { 0, 832000000 }, { 4, 95875000 }, { 4, 96000000 }, { 5, 512500000 }, | |
278 { 5, 512625000 }, { 124999, 999875000 }, { 250000, 0 } }, | |
279 { { 0, 0 }, { 0, 45351 }, { 0, 92834467 }, { 0, 92879819 }, | |
280 { 0, 301859410 }, { 1, 486031746 }, { 1, 486077098 }, { 2, 0 }, | |
281 { 2, 45351 }, { 45351, 473877551 }, { 90702, 947845805 } }, | |
282 { { 0, 0 }, { 0, 22676 }, { 0, 46417234 }, { 0, 46439909 }, | |
283 { 0, 150929705 }, { 0, 743015873 }, { 0, 743038549 }, { 1, 0 }, | |
284 { 1, 22676 }, { 22675, 736938776 }, { 45351, 473922902 } }, | |
285 { { 0, 0 }, { 0, 22675 }, { 0, 46416181 }, { 0, 46438856 }, | |
286 { 0, 150926283 }, { 0, 742999025 }, { 0, 743021700 }, { 0, 999977325 }, | |
287 { 1, 0 }, { 22675, 222761389 }, { 45350, 445568128 } }, | |
288 { { 0, 0 }, { 0, 5208 }, { 0, 10661458 }, { 0, 10666667 }, | |
289 { 0, 34666667 }, { 0, 170661458 }, { 0, 170666667 }, { 0, 229687500 }, | |
290 { 0, 229692708 }, { 5208, 333328125 }, { 10416, 666666667 } }, | |
291 { { 0, 0 }, { 0, 0 }, { 0, 1023 }, { 0, 1024 }, | |
292 { 0, 3328 }, { 0, 16383 }, { 0, 16384 }, { 0, 22050 }, | |
293 { 0, 22050 }, { 0, 499999999 }, { 1, 0 } } | |
294 }; | |
295 | |
296 for (int i = 0; i < n; ++i) { | |
297 frame_type frame = frames[i]; | |
298 for (int j = 0; j < m; ++j) { | |
299 int rate = rates[j]; | |
300 | |
301 cerr << "frame = " << frame << ", rate = " << rate << endl; | |
302 | |
303 RealTime rt = RealTime::frame2RealTime(frame, rate); | |
304 QCOMPARE(rt.sec, realTimes[j][i].sec); | |
305 QCOMPARE(rt.nsec, realTimes[j][i].nsec); | |
306 | |
307 cerr << "rt = " << rt << endl; | |
308 cerr << "rate = " << rate << endl; | |
309 | |
310 frame_type conv = RealTime::realTime2Frame(rt, rate); | |
311 cerr << "conv = " << conv << endl; | |
312 | |
313 rt = RealTime::frame2RealTime(-frame, rate); | |
314 frame_type negconv = RealTime::realTime2Frame(rt, rate); | |
315 cerr << "negconv = " << negconv << endl; | |
316 | |
317 if (rate > ONE_BILLION) { | |
318 // We no longer have enough precision in RealTime | |
319 // for this absurd sample rate, so a round trip | |
320 // conversion may round | |
321 QVERIFY(abs(frame - conv) < 2); | |
322 QVERIFY(abs(-frame - negconv) < 2); | |
323 } else { | |
324 QCOMPARE(conv, frame); | |
325 QCOMPARE(negconv, -frame); | |
326 } | |
327 } | |
328 } | |
329 } | |
330 | |
331 // Vamp SDK version just has toText, which is like our own | |
332 // toMSText with true for its second arg | |
333 | |
334 void toText() | |
335 { | |
336 // we want to use QStrings, because then the Qt test library | |
337 // will print out any conflicts. The compareTexts function | |
338 // does this for us | |
339 | |
340 int halfSec = ONE_BILLION/2; // nsec | |
341 | |
342 RealTime rt = RealTime(0, 0); | |
343 compareTexts(rt.toText(false), "0"); | |
344 compareTexts(rt.toText(true), "0.000"); | |
345 | |
346 rt = RealTime(1, halfSec); | |
347 compareTexts(rt.toText(false), "1.5"); | |
348 compareTexts(rt.toText(true), "1.500"); | |
349 | |
350 rt = RealTime::fromSeconds(-1.5); | |
351 compareTexts(rt.toText(false), "-1.5"); | |
352 compareTexts(rt.toText(true), "-1.500"); | |
353 | |
354 rt = RealTime::fromSeconds(60); | |
355 compareTexts(rt.toText(false), "1:00"); | |
356 compareTexts(rt.toText(true), "1:00.000"); | |
357 | |
358 rt = RealTime::fromSeconds(61.05); | |
359 compareTexts(rt.toText(false), "1:01.05"); | |
360 compareTexts(rt.toText(true), "1:01.050"); | |
361 | |
362 rt = RealTime::fromSeconds(601.05); | |
363 compareTexts(rt.toText(false), "10:01.05"); | |
364 compareTexts(rt.toText(true), "10:01.050"); | |
365 | |
366 rt = RealTime::fromSeconds(3600); | |
367 compareTexts(rt.toText(false), "1:00:00"); | |
368 compareTexts(rt.toText(true), "1:00:00.000"); | |
369 | |
370 // For practical reasons our time display always rounds down | |
371 rt = RealTime(3599, ONE_BILLION-1); | |
372 compareTexts(rt.toText(false), "59:59.999"); | |
373 compareTexts(rt.toText(true), "59:59.999"); | |
374 | |
375 rt = RealTime::fromSeconds(3600 * 4 + 60 * 5 + 3 + 0.01); | |
376 compareTexts(rt.toText(false), "4:05:03.01"); | |
377 compareTexts(rt.toText(true), "4:05:03.010"); | |
378 | |
379 rt = RealTime::fromSeconds(-(3600 * 4 + 60 * 5 + 3 + 0.01)); | |
380 compareTexts(rt.toText(false), "-4:05:03.01"); | |
381 compareTexts(rt.toText(true), "-4:05:03.010"); | |
382 } | |
383 }; | |
384 | |
385 #endif | |
386 |