comparison data/fileio/test/AudioFileReaderTest.h @ 756:02390a4c2abe

Toward audio read tests
author Chris Cannam
date Fri, 08 Mar 2013 16:14:21 +0000
parents
children b98f5daab19e
comparison
equal deleted inserted replaced
755:dc6c0e50724c 756:02390a4c2abe
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 This file copyright 2013 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifndef TEST_AUDIO_FILE_READER_H
17 #define TEST_AUDIO_FILE_READER_H
18
19 #include "../AudioFileReaderFactory.h"
20 #include "../AudioFileReader.h"
21
22 #include "AudioTestData.h"
23
24 #include <cmath>
25
26 #include <QObject>
27 #include <QtTest>
28 #include <QDir>
29
30 #include <iostream>
31
32 using namespace std;
33
34 static QString audioDir = "testfiles";
35
36 class AudioFileReaderTest : public QObject
37 {
38 Q_OBJECT
39
40 const char *strOf(QString s) {
41 return strdup(s.toLocal8Bit().data());
42 }
43
44 private slots:
45 void init()
46 {
47 if (!QDir(audioDir).exists()) {
48 cerr << "ERROR: Audio test file directory \"" << audioDir << "\" does not exist" << endl;
49 QVERIFY2(QDir(audioDir).exists(), "Audio test file directory not found");
50 }
51 }
52
53 void read_data()
54 {
55 QTest::addColumn<QString>("audiofile");
56 QStringList files = QDir(audioDir).entryList(QDir::Files);
57 foreach (QString filename, files) {
58 QTest::newRow(strOf(filename)) << filename;
59 }
60 }
61
62 void read()
63 {
64 QFETCH(QString, audiofile);
65
66 AudioFileReader *reader =
67 AudioFileReaderFactory::createReader
68 (audioDir + "/" + audiofile, 48000);
69
70 if (!reader) {
71 QSKIP(strOf(QString("File format for \"%1\" not supported, skipping").arg(audiofile)), SkipSingle);
72 }
73
74 int channels = reader->getChannelCount();
75 AudioTestData tdata(48000, channels);
76
77 float *reference = tdata.getInterleavedData();
78 int refsize = tdata.getFrameCount() * channels;
79
80 vector<float> test;
81
82 // The reader should give us exactly the expected number of
83 // frames -- so we ask for one more, just to check we don't
84 // get it!
85 reader->getInterleavedFrames
86 (0, tdata.getFrameCount() + 1, test);
87 int read = test.size() / channels;
88
89 // need to resolve questions about frame count at end of
90 // resampled file before we can do this
91 //!!! QCOMPARE(read, tdata.getFrameCount());
92
93 float limit = 0.011;
94
95 for (int c = 0; c < channels; ++c) {
96 float maxdiff = 0.f;
97 int maxAt = 0;
98 float totdiff = 0.f;
99 for (int i = 0; i < read; ++i) {
100 float diff = fabsf(test[i * channels + c] -
101 reference[i * channels + c]);
102 totdiff += diff;
103 if (diff > maxdiff) {
104 maxdiff = diff;
105 maxAt = i;
106 }
107 }
108 float meandiff = totdiff / read;
109 // cerr << "meandiff on channel " << c << ": " << meandiff << endl;
110 // cerr << "maxdiff on channel " << c << ": " << maxdiff << " at " << maxAt << endl;
111 if (maxdiff >= limit) {
112 cerr << "ERROR: for audiofile " << audiofile << ": maxdiff = " << maxdiff << " at frame " << maxAt << " of " << read << " on channel " << c << " (mean diff = " << meandiff << ")" << endl;
113 QVERIFY(maxdiff < limit);
114 }
115 }
116 }
117 };
118
119 #endif