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@756
|
21
|
Chris@756
|
22 #include "AudioTestData.h"
|
Chris@756
|
23
|
Chris@756
|
24 #include <cmath>
|
Chris@756
|
25
|
Chris@756
|
26 #include <QObject>
|
Chris@756
|
27 #include <QtTest>
|
Chris@756
|
28 #include <QDir>
|
Chris@756
|
29
|
Chris@756
|
30 #include <iostream>
|
Chris@756
|
31
|
Chris@756
|
32 using namespace std;
|
Chris@756
|
33
|
Chris@756
|
34 static QString audioDir = "testfiles";
|
Chris@756
|
35
|
Chris@756
|
36 class AudioFileReaderTest : public QObject
|
Chris@756
|
37 {
|
Chris@756
|
38 Q_OBJECT
|
Chris@756
|
39
|
Chris@756
|
40 const char *strOf(QString s) {
|
Chris@756
|
41 return strdup(s.toLocal8Bit().data());
|
Chris@756
|
42 }
|
Chris@756
|
43
|
Chris@756
|
44 private slots:
|
Chris@756
|
45 void init()
|
Chris@756
|
46 {
|
Chris@756
|
47 if (!QDir(audioDir).exists()) {
|
Chris@756
|
48 cerr << "ERROR: Audio test file directory \"" << audioDir << "\" does not exist" << endl;
|
Chris@756
|
49 QVERIFY2(QDir(audioDir).exists(), "Audio test file directory not found");
|
Chris@756
|
50 }
|
Chris@756
|
51 }
|
Chris@756
|
52
|
Chris@756
|
53 void read_data()
|
Chris@756
|
54 {
|
Chris@756
|
55 QTest::addColumn<QString>("audiofile");
|
Chris@756
|
56 QStringList files = QDir(audioDir).entryList(QDir::Files);
|
Chris@756
|
57 foreach (QString filename, files) {
|
Chris@756
|
58 QTest::newRow(strOf(filename)) << filename;
|
Chris@756
|
59 }
|
Chris@756
|
60 }
|
Chris@756
|
61
|
Chris@756
|
62 void read()
|
Chris@756
|
63 {
|
Chris@756
|
64 QFETCH(QString, audiofile);
|
Chris@756
|
65
|
Chris@757
|
66 int readRate = 48000;
|
Chris@757
|
67
|
Chris@756
|
68 AudioFileReader *reader =
|
Chris@756
|
69 AudioFileReaderFactory::createReader
|
Chris@757
|
70 (audioDir + "/" + audiofile, readRate);
|
Chris@757
|
71
|
Chris@757
|
72 QStringList fileAndExt = audiofile.split(".");
|
Chris@757
|
73 QStringList bits = fileAndExt[0].split("-");
|
Chris@757
|
74 QString extension = fileAndExt[1];
|
Chris@757
|
75 int nominalRate = bits[0].toInt();
|
Chris@757
|
76 int nominalChannels = bits[1].toInt();
|
Chris@757
|
77 int nominalDepth = 16;
|
Chris@757
|
78 if (bits.length() > 2) nominalDepth = bits[2].toInt();
|
Chris@756
|
79
|
Chris@756
|
80 if (!reader) {
|
Chris@757
|
81 QSKIP("Unsupported file, skipping", SkipSingle);
|
Chris@756
|
82 }
|
Chris@756
|
83
|
Chris@757
|
84 QCOMPARE((int)reader->getChannelCount(), nominalChannels);
|
Chris@757
|
85 QCOMPARE((int)reader->getNativeRate(), nominalRate);
|
Chris@757
|
86 QCOMPARE((int)reader->getSampleRate(), readRate);
|
Chris@757
|
87
|
Chris@756
|
88 int channels = reader->getChannelCount();
|
Chris@757
|
89 AudioTestData tdata(readRate, channels);
|
Chris@756
|
90
|
Chris@756
|
91 float *reference = tdata.getInterleavedData();
|
Chris@756
|
92 int refsize = tdata.getFrameCount() * channels;
|
Chris@756
|
93
|
Chris@756
|
94 vector<float> test;
|
Chris@756
|
95
|
Chris@756
|
96 // The reader should give us exactly the expected number of
|
Chris@757
|
97 // frames, except for mp3 files -- so we ask for one more,
|
Chris@757
|
98 // just to check we don't get it!
|
Chris@756
|
99 reader->getInterleavedFrames
|
Chris@756
|
100 (0, tdata.getFrameCount() + 1, test);
|
Chris@756
|
101 int read = test.size() / channels;
|
Chris@756
|
102
|
Chris@757
|
103 if (extension == "mp3") {
|
Chris@757
|
104 // mp3s round up
|
Chris@757
|
105 QVERIFY(read >= tdata.getFrameCount());
|
Chris@757
|
106 } else {
|
Chris@757
|
107 QCOMPARE(read, tdata.getFrameCount());
|
Chris@757
|
108 }
|
Chris@757
|
109
|
Chris@757
|
110 // Our limits are pretty relaxed -- we're not testing decoder
|
Chris@757
|
111 // or resampler quality here, just whether the results are
|
Chris@757
|
112 // plainly wrong (e.g. at wrong samplerate or with an offset)
|
Chris@757
|
113
|
Chris@757
|
114 float limit = 0.01;
|
Chris@757
|
115 if (nominalDepth < 16) {
|
Chris@757
|
116 limit = 0.02;
|
Chris@757
|
117 }
|
Chris@757
|
118 if (extension == "ogg" || extension == "mp3" || extension == "aac") {
|
Chris@757
|
119 limit = 0.04;
|
Chris@757
|
120 }
|
Chris@757
|
121
|
Chris@757
|
122 int edgeSize = 100;
|
Chris@757
|
123 float edgeLimit = limit * 10; // in first or final edgeSize frames
|
Chris@756
|
124
|
Chris@756
|
125 for (int c = 0; c < channels; ++c) {
|
Chris@756
|
126 float maxdiff = 0.f;
|
Chris@756
|
127 int maxAt = 0;
|
Chris@756
|
128 float totdiff = 0.f;
|
Chris@756
|
129 for (int i = 0; i < read; ++i) {
|
Chris@756
|
130 float diff = fabsf(test[i * channels + c] -
|
Chris@756
|
131 reference[i * channels + c]);
|
Chris@756
|
132 totdiff += diff;
|
Chris@757
|
133 // in edge areas, record this only if it exceeds edgeLimit
|
Chris@757
|
134 if (i < edgeSize || i + edgeSize >= read) {
|
Chris@757
|
135 if (diff > edgeLimit) {
|
Chris@757
|
136 maxdiff = diff;
|
Chris@757
|
137 maxAt = i;
|
Chris@757
|
138 }
|
Chris@757
|
139 } else {
|
Chris@757
|
140 if (diff > maxdiff) {
|
Chris@757
|
141 maxdiff = diff;
|
Chris@757
|
142 maxAt = i;
|
Chris@757
|
143 }
|
Chris@756
|
144 }
|
Chris@756
|
145 }
|
Chris@756
|
146 float meandiff = totdiff / read;
|
Chris@756
|
147 // cerr << "meandiff on channel " << c << ": " << meandiff << endl;
|
Chris@756
|
148 // cerr << "maxdiff on channel " << c << ": " << maxdiff << " at " << maxAt << endl;
|
Chris@756
|
149 if (maxdiff >= limit) {
|
Chris@756
|
150 cerr << "ERROR: for audiofile " << audiofile << ": maxdiff = " << maxdiff << " at frame " << maxAt << " of " << read << " on channel " << c << " (mean diff = " << meandiff << ")" << endl;
|
Chris@756
|
151 QVERIFY(maxdiff < limit);
|
Chris@756
|
152 }
|
Chris@756
|
153 }
|
Chris@756
|
154 }
|
Chris@756
|
155 };
|
Chris@756
|
156
|
Chris@756
|
157 #endif
|