annotate 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
rev   line source
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@756 66 AudioFileReader *reader =
Chris@756 67 AudioFileReaderFactory::createReader
Chris@756 68 (audioDir + "/" + audiofile, 48000);
Chris@756 69
Chris@756 70 if (!reader) {
Chris@756 71 QSKIP(strOf(QString("File format for \"%1\" not supported, skipping").arg(audiofile)), SkipSingle);
Chris@756 72 }
Chris@756 73
Chris@756 74 int channels = reader->getChannelCount();
Chris@756 75 AudioTestData tdata(48000, channels);
Chris@756 76
Chris@756 77 float *reference = tdata.getInterleavedData();
Chris@756 78 int refsize = tdata.getFrameCount() * channels;
Chris@756 79
Chris@756 80 vector<float> test;
Chris@756 81
Chris@756 82 // The reader should give us exactly the expected number of
Chris@756 83 // frames -- so we ask for one more, just to check we don't
Chris@756 84 // get it!
Chris@756 85 reader->getInterleavedFrames
Chris@756 86 (0, tdata.getFrameCount() + 1, test);
Chris@756 87 int read = test.size() / channels;
Chris@756 88
Chris@756 89 // need to resolve questions about frame count at end of
Chris@756 90 // resampled file before we can do this
Chris@756 91 //!!! QCOMPARE(read, tdata.getFrameCount());
Chris@756 92
Chris@756 93 float limit = 0.011;
Chris@756 94
Chris@756 95 for (int c = 0; c < channels; ++c) {
Chris@756 96 float maxdiff = 0.f;
Chris@756 97 int maxAt = 0;
Chris@756 98 float totdiff = 0.f;
Chris@756 99 for (int i = 0; i < read; ++i) {
Chris@756 100 float diff = fabsf(test[i * channels + c] -
Chris@756 101 reference[i * channels + c]);
Chris@756 102 totdiff += diff;
Chris@756 103 if (diff > maxdiff) {
Chris@756 104 maxdiff = diff;
Chris@756 105 maxAt = i;
Chris@756 106 }
Chris@756 107 }
Chris@756 108 float meandiff = totdiff / read;
Chris@756 109 // cerr << "meandiff on channel " << c << ": " << meandiff << endl;
Chris@756 110 // cerr << "maxdiff on channel " << c << ": " << maxdiff << " at " << maxAt << endl;
Chris@756 111 if (maxdiff >= limit) {
Chris@756 112 cerr << "ERROR: for audiofile " << audiofile << ": maxdiff = " << maxdiff << " at frame " << maxAt << " of " << read << " on channel " << c << " (mean diff = " << meandiff << ")" << endl;
Chris@756 113 QVERIFY(maxdiff < limit);
Chris@756 114 }
Chris@756 115 }
Chris@756 116 }
Chris@756 117 };
Chris@756 118
Chris@756 119 #endif