Chris@756
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@756
|
2
|
Chris@756
|
3 /*
|
Chris@756
|
4 Sonic Visualiser
|
Chris@756
|
5 An audio file viewer and annotation editor.
|
Chris@756
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@756
|
7 This file copyright 2013 Chris Cannam.
|
Chris@756
|
8
|
Chris@756
|
9 This program is free software; you can redistribute it and/or
|
Chris@756
|
10 modify it under the terms of the GNU General Public License as
|
Chris@756
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@756
|
12 License, or (at your option) any later version. See the file
|
Chris@756
|
13 COPYING included with this distribution for more information.
|
Chris@756
|
14 */
|
Chris@756
|
15
|
Chris@756
|
16 #ifndef TEST_AUDIO_FILE_READER_H
|
Chris@756
|
17 #define TEST_AUDIO_FILE_READER_H
|
Chris@756
|
18
|
Chris@756
|
19 #include "../AudioFileReaderFactory.h"
|
Chris@756
|
20 #include "../AudioFileReader.h"
|
Chris@1313
|
21 #include "../WavFileWriter.h"
|
Chris@756
|
22
|
Chris@756
|
23 #include "AudioTestData.h"
|
Chris@756
|
24
|
Chris@756
|
25 #include <cmath>
|
Chris@756
|
26
|
Chris@756
|
27 #include <QObject>
|
Chris@756
|
28 #include <QtTest>
|
Chris@756
|
29 #include <QDir>
|
Chris@756
|
30
|
Chris@756
|
31 #include <iostream>
|
Chris@756
|
32
|
Chris@756
|
33 using namespace std;
|
Chris@756
|
34
|
Chris@756
|
35 class AudioFileReaderTest : public QObject
|
Chris@756
|
36 {
|
Chris@756
|
37 Q_OBJECT
|
Chris@756
|
38
|
Chris@1346
|
39 private:
|
Chris@1346
|
40 QString testDirBase;
|
Chris@1346
|
41 QString audioDir;
|
Chris@1346
|
42 QString diffDir;
|
Chris@1346
|
43
|
Chris@1346
|
44 public:
|
Chris@1346
|
45 AudioFileReaderTest(QString base) {
|
Chris@1346
|
46 if (base == "") {
|
Chris@1346
|
47 base = "svcore/data/fileio/test";
|
Chris@1346
|
48 }
|
Chris@1346
|
49 testDirBase = base;
|
Chris@1359
|
50 audioDir = base + "/audio";
|
Chris@1346
|
51 diffDir = base + "/diffs";
|
Chris@1346
|
52 }
|
Chris@1346
|
53
|
Chris@1346
|
54 private:
|
Chris@756
|
55 const char *strOf(QString s) {
|
Chris@756
|
56 return strdup(s.toLocal8Bit().data());
|
Chris@756
|
57 }
|
Chris@756
|
58
|
Chris@1313
|
59 void getFileMetadata(QString filename,
|
Chris@1313
|
60 QString &extension,
|
Chris@1313
|
61 sv_samplerate_t &rate,
|
Chris@1313
|
62 int &channels,
|
Chris@1313
|
63 int &bitdepth) {
|
Chris@1313
|
64
|
Chris@1313
|
65 QStringList fileAndExt = filename.split(".");
|
Chris@1313
|
66 QStringList bits = fileAndExt[0].split("-");
|
Chris@1313
|
67
|
Chris@1313
|
68 extension = fileAndExt[1];
|
Chris@1313
|
69 rate = bits[0].toInt();
|
Chris@1313
|
70 channels = bits[1].toInt();
|
Chris@1313
|
71 bitdepth = 16;
|
Chris@1313
|
72 if (bits.length() > 2) {
|
Chris@1313
|
73 bitdepth = bits[2].toInt();
|
Chris@1313
|
74 }
|
Chris@1313
|
75 }
|
Chris@1313
|
76
|
cannam@1315
|
77 void getExpectedThresholds(QString format,
|
cannam@1315
|
78 QString filename,
|
Chris@1313
|
79 bool resampled,
|
Chris@1313
|
80 bool gapless,
|
Chris@1313
|
81 bool normalised,
|
Chris@1313
|
82 double &maxLimit,
|
Chris@1313
|
83 double &rmsLimit) {
|
Chris@1313
|
84
|
Chris@1313
|
85 QString extension;
|
Chris@1313
|
86 sv_samplerate_t fileRate;
|
Chris@1313
|
87 int channels;
|
Chris@1313
|
88 int bitdepth;
|
Chris@1313
|
89 getFileMetadata(filename, extension, fileRate, channels, bitdepth);
|
Chris@1313
|
90
|
Chris@1313
|
91 if (normalised) {
|
Chris@1313
|
92
|
cannam@1315
|
93 if (format == "ogg") {
|
Chris@1313
|
94
|
Chris@1313
|
95 // Our ogg is not especially high quality and is
|
Chris@1313
|
96 // actually further from the original if normalised
|
Chris@1313
|
97
|
Chris@1313
|
98 maxLimit = 0.1;
|
Chris@1313
|
99 rmsLimit = 0.03;
|
Chris@1313
|
100
|
Chris@1598
|
101 } else if (format == "opus") {
|
Chris@1598
|
102
|
Chris@1598
|
103 maxLimit = 0.06;
|
Chris@1598
|
104 rmsLimit = 0.015;
|
Chris@1598
|
105
|
cannam@1315
|
106 } else if (format == "aac") {
|
Chris@1313
|
107
|
cannam@1315
|
108 // Terrible performance for this test, load of spill
|
cannam@1315
|
109 // from one channel to the other. I guess they know
|
cannam@1315
|
110 // what they're doing, it's perceptual after all, but
|
cannam@1315
|
111 // it does make this check a bit superfluous, you
|
cannam@1315
|
112 // could probably pass it with a signal that sounds
|
cannam@1315
|
113 // nothing like the original
|
cannam@1315
|
114 maxLimit = 0.2;
|
cannam@1314
|
115 rmsLimit = 0.1;
|
Chris@1313
|
116
|
cannam@1315
|
117 } else if (format == "mp3") {
|
Chris@1313
|
118
|
Chris@1313
|
119 if (resampled && !gapless) {
|
Chris@1313
|
120
|
Chris@1313
|
121 // We expect worse figures here, because the
|
Chris@1313
|
122 // combination of uncompensated encoder delay +
|
Chris@1313
|
123 // resampling results in a fractional delay which
|
Chris@1313
|
124 // means the decoded signal is slightly out of
|
Chris@1313
|
125 // phase compared to the test signal
|
Chris@1313
|
126
|
Chris@1313
|
127 maxLimit = 0.1;
|
Chris@1313
|
128 rmsLimit = 0.05;
|
Chris@1313
|
129
|
Chris@1313
|
130 } else {
|
Chris@1313
|
131
|
Chris@1313
|
132 maxLimit = 0.05;
|
Chris@1313
|
133 rmsLimit = 0.01;
|
Chris@1313
|
134 }
|
Chris@1313
|
135
|
Chris@1313
|
136 } else {
|
Chris@1313
|
137
|
cannam@1315
|
138 // lossless formats (wav, aiff, flac, apple_lossless)
|
Chris@1313
|
139
|
Chris@1313
|
140 if (bitdepth >= 16 && !resampled) {
|
Chris@1313
|
141 maxLimit = 1e-3;
|
Chris@1313
|
142 rmsLimit = 3e-4;
|
Chris@1313
|
143 } else {
|
Chris@1313
|
144 maxLimit = 0.01;
|
Chris@1313
|
145 rmsLimit = 5e-3;
|
Chris@1313
|
146 }
|
Chris@1313
|
147 }
|
Chris@1313
|
148
|
Chris@1313
|
149 } else { // !normalised
|
Chris@1313
|
150
|
cannam@1315
|
151 if (format == "ogg") {
|
Chris@1313
|
152
|
Chris@1313
|
153 maxLimit = 0.06;
|
Chris@1313
|
154 rmsLimit = 0.03;
|
Chris@1313
|
155
|
Chris@1598
|
156 } else if (format == "opus") {
|
Chris@1598
|
157
|
Chris@1598
|
158 maxLimit = 0.06;
|
Chris@1598
|
159 rmsLimit = 0.015;
|
Chris@1598
|
160
|
cannam@1315
|
161 } else if (format == "aac") {
|
Chris@1313
|
162
|
cannam@1315
|
163 maxLimit = 0.1;
|
cannam@1315
|
164 rmsLimit = 0.1;
|
Chris@1313
|
165
|
cannam@1315
|
166 } else if (format == "mp3") {
|
Chris@1313
|
167
|
Chris@1313
|
168 // all mp3 figures are worse when not normalising
|
Chris@1313
|
169 maxLimit = 0.1;
|
Chris@1313
|
170 rmsLimit = 0.05;
|
Chris@1313
|
171
|
Chris@1313
|
172 } else {
|
Chris@1313
|
173
|
cannam@1315
|
174 // lossless formats (wav, aiff, flac, apple_lossless)
|
Chris@1313
|
175
|
Chris@1313
|
176 if (bitdepth >= 16 && !resampled) {
|
Chris@1313
|
177 maxLimit = 1e-3;
|
Chris@1313
|
178 rmsLimit = 3e-4;
|
Chris@1313
|
179 } else {
|
Chris@1313
|
180 maxLimit = 0.02;
|
Chris@1313
|
181 rmsLimit = 0.01;
|
Chris@1313
|
182 }
|
Chris@1313
|
183 }
|
Chris@1313
|
184 }
|
Chris@1313
|
185 }
|
Chris@1313
|
186
|
cannam@1315
|
187 QString testName(QString format, QString filename, int rate, bool norm, bool gapless) {
|
cannam@1315
|
188 return QString("%1/%2 at %3%4%5")
|
cannam@1315
|
189 .arg(format)
|
Chris@1313
|
190 .arg(filename)
|
Chris@1313
|
191 .arg(rate)
|
Chris@1313
|
192 .arg(norm ? " normalised": "")
|
Chris@1313
|
193 .arg(gapless ? "" : " non-gapless");
|
Chris@1313
|
194 }
|
Chris@1313
|
195
|
Chris@756
|
196 private slots:
|
Chris@756
|
197 void init()
|
Chris@756
|
198 {
|
Chris@756
|
199 if (!QDir(audioDir).exists()) {
|
Chris@1346
|
200 QString cwd = QDir::currentPath();
|
Chris@1428
|
201 SVCERR << "ERROR: Audio test file directory \"" << audioDir << "\" does not exist (cwd = " << cwd << ")" << endl;
|
Chris@756
|
202 QVERIFY2(QDir(audioDir).exists(), "Audio test file directory not found");
|
Chris@756
|
203 }
|
Chris@1313
|
204 if (!QDir(diffDir).exists() && !QDir().mkpath(diffDir)) {
|
Chris@1428
|
205 SVCERR << "ERROR: Audio diff directory \"" << diffDir << "\" does not exist and could not be created" << endl;
|
Chris@1313
|
206 QVERIFY2(QDir(diffDir).exists(), "Audio diff directory not found and could not be created");
|
Chris@1313
|
207 }
|
Chris@756
|
208 }
|
Chris@756
|
209
|
Chris@756
|
210 void read_data()
|
Chris@756
|
211 {
|
cannam@1315
|
212 QTest::addColumn<QString>("format");
|
Chris@756
|
213 QTest::addColumn<QString>("audiofile");
|
Chris@1313
|
214 QTest::addColumn<int>("rate");
|
Chris@1313
|
215 QTest::addColumn<bool>("normalised");
|
Chris@1313
|
216 QTest::addColumn<bool>("gapless");
|
cannam@1315
|
217 QStringList dirs = QDir(audioDir).entryList(QDir::Dirs |
|
cannam@1315
|
218 QDir::NoDotAndDotDot);
|
cannam@1315
|
219 for (QString format: dirs) {
|
cannam@1315
|
220 QStringList files = QDir(QDir(audioDir).filePath(format))
|
cannam@1315
|
221 .entryList(QDir::Files);
|
cannam@1315
|
222 int readRates[] = { 44100, 48000 };
|
cannam@1315
|
223 bool norms[] = { false, true };
|
cannam@1315
|
224 bool gaplesses[] = { true, false };
|
cannam@1315
|
225 foreach (QString filename, files) {
|
cannam@1315
|
226 for (int rate: readRates) {
|
cannam@1315
|
227 for (bool norm: norms) {
|
cannam@1315
|
228 for (bool gapless: gaplesses) {
|
Chris@1313
|
229
|
cannam@1315
|
230 if (format != "mp3" && !gapless) {
|
cannam@1315
|
231 continue;
|
cannam@1315
|
232 }
|
cannam@1315
|
233
|
cannam@1315
|
234 QString desc = testName
|
cannam@1315
|
235 (format, filename, rate, norm, gapless);
|
cannam@1315
|
236
|
cannam@1315
|
237 QTest::newRow(strOf(desc))
|
cannam@1315
|
238 << format << filename << rate << norm << gapless;
|
Chris@1313
|
239 }
|
Chris@1313
|
240 }
|
Chris@1313
|
241 }
|
Chris@1313
|
242 }
|
Chris@756
|
243 }
|
Chris@756
|
244 }
|
Chris@756
|
245
|
Chris@756
|
246 void read()
|
Chris@756
|
247 {
|
cannam@1315
|
248 QFETCH(QString, format);
|
Chris@756
|
249 QFETCH(QString, audiofile);
|
Chris@1313
|
250 QFETCH(int, rate);
|
Chris@1313
|
251 QFETCH(bool, normalised);
|
Chris@1313
|
252 QFETCH(bool, gapless);
|
Chris@756
|
253
|
Chris@1313
|
254 sv_samplerate_t readRate(rate);
|
Chris@1313
|
255
|
cannam@1315
|
256 // cerr << "\naudiofile = " << audiofile << endl;
|
Chris@1313
|
257
|
Chris@1313
|
258 AudioFileReaderFactory::Parameters params;
|
Chris@1313
|
259 params.targetRate = readRate;
|
Chris@1313
|
260 params.normalisation = (normalised ?
|
Chris@1313
|
261 AudioFileReaderFactory::Normalisation::Peak :
|
Chris@1313
|
262 AudioFileReaderFactory::Normalisation::None);
|
Chris@1313
|
263 params.gaplessMode = (gapless ?
|
Chris@1313
|
264 AudioFileReaderFactory::GaplessMode::Gapless :
|
Chris@1313
|
265 AudioFileReaderFactory::GaplessMode::Gappy);
|
Chris@757
|
266
|
Chris@1429
|
267 AudioFileReader *reader =
|
Chris@1429
|
268 AudioFileReaderFactory::createReader
|
Chris@1429
|
269 (audioDir + "/" + format + "/" + audiofile, params);
|
Chris@1313
|
270
|
Chris@1429
|
271 if (!reader) {
|
Chris@820
|
272 #if ( QT_VERSION >= 0x050000 )
|
Chris@1429
|
273 QSKIP("Unsupported file, skipping");
|
Chris@820
|
274 #else
|
Chris@1429
|
275 QSKIP("Unsupported file, skipping", SkipSingle);
|
Chris@820
|
276 #endif
|
Chris@1429
|
277 }
|
Chris@756
|
278
|
Chris@1313
|
279 QString extension;
|
Chris@1313
|
280 sv_samplerate_t fileRate;
|
Chris@1313
|
281 int channels;
|
Chris@1313
|
282 int fileBitdepth;
|
Chris@1313
|
283 getFileMetadata(audiofile, extension, fileRate, channels, fileBitdepth);
|
Chris@1313
|
284
|
Chris@1313
|
285 QCOMPARE((int)reader->getChannelCount(), channels);
|
Chris@1313
|
286 QCOMPARE(reader->getNativeRate(), fileRate);
|
Chris@1040
|
287 QCOMPARE(reader->getSampleRate(), readRate);
|
Chris@757
|
288
|
Chris@1429
|
289 AudioTestData tdata(readRate, channels);
|
Chris@1429
|
290
|
Chris@1429
|
291 float *reference = tdata.getInterleavedData();
|
Chris@1040
|
292 sv_frame_t refFrames = tdata.getFrameCount();
|
Chris@1429
|
293
|
Chris@1429
|
294 // The reader should give us exactly the expected number of
|
Chris@1429
|
295 // frames, except for mp3/aac files. We ask for quite a lot
|
Chris@1429
|
296 // more, though, so we can (a) check that we only get the
|
Chris@1429
|
297 // expected number back (if this is not mp3/aac) or (b) take
|
Chris@1429
|
298 // into account silence at beginning and end (if it is).
|
Chris@1429
|
299 floatvec_t test = reader->getInterleavedFrames(0, refFrames + 5000);
|
Chris@1402
|
300
|
Chris@1402
|
301 delete reader;
|
Chris@1402
|
302 reader = 0;
|
Chris@1402
|
303
|
Chris@1429
|
304 sv_frame_t read = test.size() / channels;
|
Chris@756
|
305
|
Chris@1313
|
306 bool perceptual = (extension == "mp3" ||
|
Chris@1313
|
307 extension == "aac" ||
|
Chris@1598
|
308 extension == "m4a" ||
|
Chris@1598
|
309 extension == "opus");
|
Chris@1313
|
310
|
Chris@1313
|
311 if (perceptual && !gapless) {
|
Chris@1313
|
312 // allow silence at start and end
|
Chris@759
|
313 QVERIFY(read >= refFrames);
|
Chris@757
|
314 } else {
|
Chris@759
|
315 QCOMPARE(read, refFrames);
|
Chris@757
|
316 }
|
Chris@757
|
317
|
Chris@1313
|
318 bool resampled = readRate != fileRate;
|
Chris@1313
|
319 double maxLimit, rmsLimit;
|
cannam@1315
|
320 getExpectedThresholds(format,
|
cannam@1315
|
321 audiofile,
|
Chris@1313
|
322 resampled,
|
Chris@1313
|
323 gapless,
|
Chris@1313
|
324 normalised,
|
Chris@1313
|
325 maxLimit, rmsLimit);
|
Chris@1313
|
326
|
Chris@1313
|
327 double edgeLimit = maxLimit * 3; // in first or final edgeSize frames
|
Chris@1313
|
328 if (resampled && edgeLimit < 0.1) edgeLimit = 0.1;
|
Chris@759
|
329 int edgeSize = 100;
|
Chris@759
|
330
|
Chris@759
|
331 // And we ignore completely the last few frames when upsampling
|
Chris@1313
|
332 int discard = 1 + int(round(readRate / fileRate));
|
Chris@759
|
333
|
Chris@759
|
334 int offset = 0;
|
Chris@759
|
335
|
Chris@1313
|
336 if (perceptual) {
|
Chris@759
|
337
|
cannam@1314
|
338 // Look for an initial offset.
|
cannam@1314
|
339 //
|
cannam@1314
|
340 // We know the first channel has a sinusoid in it. It
|
cannam@1314
|
341 // should have a peak at 0.4ms (see AudioTestData.h) but
|
cannam@1314
|
342 // that might have been clipped, which would make it
|
cannam@1314
|
343 // imprecise. We can tell if it's clipped, though, as
|
cannam@1314
|
344 // there will be samples having exactly identical
|
cannam@1314
|
345 // values. So what we look for is the peak if it's not
|
cannam@1314
|
346 // clipped and, if it is, the first zero crossing after
|
cannam@1314
|
347 // the peak, which should be at 0.8ms.
|
cannam@1314
|
348
|
Chris@1296
|
349 int expectedPeak = int(0.0004 * readRate);
|
cannam@1314
|
350 int expectedZC = int(0.0008 * readRate);
|
cannam@1314
|
351 bool foundPeak = false;
|
cannam@1314
|
352 for (int i = 1; i+1 < read; ++i) {
|
cannam@1314
|
353 float prevSample = test[(i-1) * channels];
|
cannam@1314
|
354 float thisSample = test[i * channels];
|
cannam@1314
|
355 float nextSample = test[(i+1) * channels];
|
cannam@1314
|
356 if (thisSample > 0.8 && nextSample < thisSample) {
|
cannam@1314
|
357 foundPeak = true;
|
cannam@1314
|
358 if (thisSample > prevSample) {
|
cannam@1314
|
359 // not clipped
|
cannam@1314
|
360 offset = i - expectedPeak - 1;
|
cannam@1314
|
361 break;
|
cannam@1314
|
362 }
|
cannam@1314
|
363 }
|
cannam@1314
|
364 if (foundPeak && (thisSample >= 0.0 && nextSample < 0.0)) {
|
cannam@1315
|
365 // cerr << "thisSample = " << thisSample << ", nextSample = "
|
cannam@1315
|
366 // << nextSample << endl;
|
cannam@1314
|
367 offset = i - expectedZC - 1;
|
Chris@759
|
368 break;
|
Chris@759
|
369 }
|
Chris@759
|
370 }
|
Chris@1313
|
371
|
cannam@1315
|
372 // int fileRateEquivalent = int((offset / readRate) * fileRate);
|
cannam@1315
|
373 // std::cerr << "offset = " << offset << std::endl;
|
cannam@1315
|
374 // std::cerr << "at file rate would be " << fileRateEquivalent << std::endl;
|
Chris@1313
|
375
|
Chris@1313
|
376 // Previously our m4a test file had a fixed offset of 1024
|
Chris@1313
|
377 // at the file sample rate -- this may be because it was
|
Chris@1313
|
378 // produced by FAAC which did not write in the delay as
|
Chris@1313
|
379 // metadata? We now have an m4a produced by Core Audio
|
Chris@1313
|
380 // which gives a 0 offset. What to do...
|
Chris@1313
|
381
|
Chris@1313
|
382 // Anyway, mp3s should have 0 offset in gapless mode and
|
Chris@1313
|
383 // "something else" otherwise.
|
Chris@1313
|
384
|
Chris@1313
|
385 if (gapless) {
|
cannam@1315
|
386 if (format == "aac") {
|
cannam@1315
|
387 // ouch!
|
cannam@1315
|
388 if (offset == -1) offset = 0;
|
cannam@1315
|
389 }
|
Chris@1313
|
390 QCOMPARE(offset, 0);
|
Chris@1313
|
391 }
|
Chris@759
|
392 }
|
Chris@756
|
393
|
cannam@1315
|
394 {
|
cannam@1315
|
395 // Write the diff file now, so that it's already been written
|
cannam@1315
|
396 // even if the comparison fails. We aren't checking anything
|
cannam@1315
|
397 // here except as necessary to avoid buffer overruns etc
|
cannam@1315
|
398
|
cannam@1315
|
399 QString diffFile =
|
cannam@1315
|
400 testName(format, audiofile, rate, normalised, gapless);
|
cannam@1315
|
401 diffFile.replace("/", "_");
|
cannam@1315
|
402 diffFile.replace(".", "_");
|
cannam@1315
|
403 diffFile.replace(" ", "_");
|
cannam@1315
|
404 diffFile += ".wav";
|
cannam@1315
|
405 diffFile = QDir(diffDir).filePath(diffFile);
|
cannam@1315
|
406 WavFileWriter diffWriter(diffFile, readRate, channels,
|
Chris@1359
|
407 WavFileWriter::WriteToTemporary);
|
cannam@1315
|
408 QVERIFY(diffWriter.isOK());
|
cannam@1315
|
409
|
cannam@1315
|
410 vector<vector<float>> diffs(channels);
|
cannam@1315
|
411 for (int c = 0; c < channels; ++c) {
|
cannam@1315
|
412 for (int i = 0; i < refFrames; ++i) {
|
cannam@1315
|
413 int ix = i + offset;
|
cannam@1315
|
414 if (ix < read) {
|
cannam@1315
|
415 float signeddiff =
|
cannam@1315
|
416 test[ix * channels + c] -
|
cannam@1315
|
417 reference[i * channels + c];
|
cannam@1315
|
418 diffs[c].push_back(signeddiff);
|
cannam@1315
|
419 }
|
cannam@1315
|
420 }
|
cannam@1315
|
421 }
|
cannam@1315
|
422 float **ptrs = new float*[channels];
|
cannam@1315
|
423 for (int c = 0; c < channels; ++c) {
|
cannam@1315
|
424 ptrs[c] = diffs[c].data();
|
cannam@1315
|
425 }
|
cannam@1315
|
426 diffWriter.writeSamples(ptrs, refFrames);
|
cannam@1315
|
427 delete[] ptrs;
|
cannam@1315
|
428 }
|
Chris@1313
|
429
|
Chris@1346
|
430 for (int c = 0; c < channels; ++c) {
|
Chris@1313
|
431
|
Chris@1313
|
432 double maxDiff = 0.0;
|
Chris@1313
|
433 double totalDiff = 0.0;
|
Chris@1313
|
434 double totalSqrDiff = 0.0;
|
Chris@1346
|
435 int maxIndex = 0;
|
Chris@1313
|
436
|
Chris@1346
|
437 for (int i = 0; i < refFrames; ++i) {
|
Chris@1296
|
438 int ix = i + offset;
|
Chris@1296
|
439 if (ix >= read) {
|
Chris@1428
|
440 SVCERR << "ERROR: audiofile " << audiofile << " reads truncated (read-rate reference frames " << i << " onward, of " << refFrames << ", are lost)" << endl;
|
Chris@1296
|
441 QVERIFY(ix < read);
|
Chris@1296
|
442 }
|
Chris@1313
|
443
|
Chris@1296
|
444 if (ix + discard >= read) {
|
Chris@1296
|
445 // we forgive the very edge samples when
|
Chris@1296
|
446 // resampling (discard > 0)
|
Chris@1296
|
447 continue;
|
Chris@1296
|
448 }
|
Chris@1313
|
449
|
Chris@1346
|
450 double diff = fabs(test[ix * channels + c] -
|
cannam@1315
|
451 reference[i * channels + c]);
|
Chris@1313
|
452
|
Chris@1346
|
453 totalDiff += diff;
|
Chris@1313
|
454 totalSqrDiff += diff * diff;
|
Chris@1313
|
455
|
Chris@757
|
456 // in edge areas, record this only if it exceeds edgeLimit
|
Chris@1313
|
457 if (i < edgeSize || i + edgeSize >= refFrames) {
|
Chris@1313
|
458 if (diff > edgeLimit && diff > maxDiff) {
|
Chris@1313
|
459 maxDiff = diff;
|
Chris@1313
|
460 maxIndex = i;
|
Chris@757
|
461 }
|
Chris@757
|
462 } else {
|
Chris@1313
|
463 if (diff > maxDiff) {
|
Chris@1313
|
464 maxDiff = diff;
|
Chris@1313
|
465 maxIndex = i;
|
Chris@757
|
466 }
|
Chris@1346
|
467 }
|
Chris@1346
|
468 }
|
Chris@1313
|
469
|
Chris@1346
|
470 double meanDiff = totalDiff / double(refFrames);
|
Chris@1313
|
471 double rmsDiff = sqrt(totalSqrDiff / double(refFrames));
|
cannam@1308
|
472
|
cannam@1314
|
473 /*
|
Chris@1346
|
474 cerr << "channel " << c << ": mean diff " << meanDiff << endl;
|
Chris@1429
|
475 cerr << "channel " << c << ": rms diff " << rmsDiff << endl;
|
Chris@1429
|
476 cerr << "channel " << c << ": max diff " << maxDiff << " at " << maxIndex << endl;
|
cannam@1314
|
477 */
|
Chris@1313
|
478 if (rmsDiff >= rmsLimit) {
|
Chris@1428
|
479 SVCERR << "ERROR: for audiofile " << audiofile << ": RMS diff = " << rmsDiff << " for channel " << c << " (limit = " << rmsLimit << ")" << endl;
|
Chris@1313
|
480 QVERIFY(rmsDiff < rmsLimit);
|
Chris@1313
|
481 }
|
Chris@1346
|
482 if (maxDiff >= maxLimit) {
|
Chris@1428
|
483 SVCERR << "ERROR: for audiofile " << audiofile << ": max diff = " << maxDiff << " at frame " << maxIndex << " of " << read << " on channel " << c << " (limit = " << maxLimit << ", edge limit = " << edgeLimit << ", mean diff = " << meanDiff << ", rms = " << rmsDiff << ")" << endl;
|
Chris@1346
|
484 QVERIFY(maxDiff < maxLimit);
|
Chris@1346
|
485 }
|
Chris@1313
|
486
|
Chris@1313
|
487 // and check for spurious material at end
|
Chris@1313
|
488
|
Chris@1309
|
489 for (sv_frame_t i = refFrames; i + offset < read; ++i) {
|
Chris@1309
|
490 sv_frame_t ix = i + offset;
|
Chris@1323
|
491 float quiet = 0.1f; //!!! allow some ringing - but let's come back to this, it should tail off
|
cannam@1308
|
492 float mag = fabsf(test[ix * channels + c]);
|
cannam@1308
|
493 if (mag > quiet) {
|
Chris@1428
|
494 SVCERR << "ERROR: audiofile " << audiofile << " contains spurious data after end of reference (found sample " << test[ix * channels + c] << " at index " << ix << " of channel " << c << " after reference+offset ended at " << refFrames+offset << ")" << endl;
|
cannam@1308
|
495 QVERIFY(mag < quiet);
|
cannam@1308
|
496 }
|
cannam@1308
|
497 }
|
Chris@1429
|
498 }
|
Chris@756
|
499 }
|
Chris@756
|
500 };
|
Chris@756
|
501
|
Chris@756
|
502 #endif
|