annotate data/fileio/test/EncodingTest.h @ 1594:afa75922fe0f bqaudiostream

Add Opus tests
author Chris Cannam
date Tue, 22 Jan 2019 16:27:14 +0000
parents d3f068153546
children 81989085997b
rev   line source
Chris@1345 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1345 2
Chris@1345 3 /*
Chris@1345 4 Sonic Visualiser
Chris@1345 5 An audio file viewer and annotation editor.
Chris@1345 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1345 7
Chris@1345 8 This program is free software; you can redistribute it and/or
Chris@1345 9 modify it under the terms of the GNU General Public License as
Chris@1345 10 published by the Free Software Foundation; either version 2 of the
Chris@1345 11 License, or (at your option) any later version. See the file
Chris@1345 12 COPYING included with this distribution for more information.
Chris@1345 13 */
Chris@1345 14
Chris@1345 15 #ifndef TEST_AUDIO_ENCODINGS_H
Chris@1345 16 #define TEST_AUDIO_ENCODINGS_H
Chris@1345 17
Chris@1345 18 // Quick tests for filename encodings and encoding of ID3 data. Not a
Chris@1345 19 // test of audio codecs.
Chris@1345 20
Chris@1345 21 #include "../AudioFileReaderFactory.h"
Chris@1345 22 #include "../AudioFileReader.h"
Chris@1359 23 #include "../WavFileWriter.h"
Chris@1345 24
Chris@1345 25 #include <cmath>
Chris@1345 26
Chris@1345 27 #include <QObject>
Chris@1345 28 #include <QtTest>
Chris@1345 29 #include <QDir>
Chris@1345 30
Chris@1345 31 #include <iostream>
Chris@1345 32
Chris@1345 33 using namespace std;
Chris@1345 34
Chris@1346 35 const char utf8_name_cdp_1[] = "Caf\303\251 de Paris";
Chris@1346 36 const char utf8_name_cdp_2[] = "Caf\303\251 de \351\207\215\345\272\206";
Chris@1347 37 const char utf8_name_tsprk[] = "T\303\253mple of Sp\303\266rks";
Chris@1346 38 const char utf8_name_sprkt[] = "\343\202\271\343\203\235\343\203\274\343\202\257\343\201\256\345\257\272\351\231\242";
Chris@1345 39
Chris@1359 40 // Mapping between filename and expected title metadata field
Chris@1345 41 static const char *mapping[][2] = {
Chris@1346 42 { "id3v2-iso-8859-1", utf8_name_cdp_1 },
Chris@1346 43 { "id3v2-ucs-2", utf8_name_cdp_2 },
Chris@1346 44 { utf8_name_tsprk, utf8_name_tsprk },
Chris@1346 45 { utf8_name_sprkt, utf8_name_sprkt },
Chris@1345 46 };
Chris@1345 47 static const int mappingCount = 4;
Chris@1345 48
Chris@1593 49 static const char *testFiles[][2] = {
Chris@1593 50 { "id3v2-iso-8859-1", "mp3" },
Chris@1593 51 { "id3v2-ucs-2", "mp3" },
Chris@1593 52 { utf8_name_tsprk, "flac" },
Chris@1593 53 { utf8_name_tsprk, "m4a" },
Chris@1593 54 { utf8_name_tsprk, "mp3" },
Chris@1593 55 { utf8_name_tsprk, "ogg" },
Chris@1594 56 { utf8_name_tsprk, "opus" },
Chris@1593 57 { utf8_name_sprkt, "mp3" },
Chris@1593 58 { utf8_name_sprkt, "ogg" },
Chris@1593 59 };
Chris@1593 60 static const int testFileCount = 8;
Chris@1593 61
Chris@1345 62 class EncodingTest : public QObject
Chris@1345 63 {
Chris@1345 64 Q_OBJECT
Chris@1345 65
Chris@1346 66 private:
Chris@1346 67 QString testDirBase;
Chris@1346 68 QString encodingDir;
Chris@1359 69 QString outDir;
Chris@1346 70
Chris@1346 71 public:
Chris@1346 72 EncodingTest(QString base) {
Chris@1346 73 if (base == "") {
Chris@1346 74 base = "svcore/data/fileio/test";
Chris@1346 75 }
Chris@1346 76 testDirBase = base;
Chris@1346 77 encodingDir = base + "/encodings";
Chris@1359 78 outDir = base + "/outfiles";
Chris@1346 79 }
Chris@1346 80
Chris@1346 81 private:
Chris@1359 82 void addAudioFiles() {
Chris@1359 83 QTest::addColumn<QString>("audiofile");
Chris@1593 84 for (int i = 0; i < testFileCount; ++i) {
Chris@1593 85 std::string s = testFiles[i][0];
Chris@1593 86 s += ".";
Chris@1593 87 s += testFiles[i][1];
Chris@1593 88 QTest::newRow(strdup(s.c_str())) << QString::fromStdString(s);
Chris@1359 89 }
Chris@1359 90 }
Chris@1359 91
Chris@1345 92 private slots:
Chris@1345 93 void init()
Chris@1345 94 {
Chris@1345 95 if (!QDir(encodingDir).exists()) {
Chris@1428 96 SVCERR << "ERROR: Audio encoding file directory \"" << encodingDir << "\" does not exist" << endl;
Chris@1345 97 QVERIFY2(QDir(encodingDir).exists(), "Audio encoding file directory not found");
Chris@1593 98 }
Chris@1359 99 if (!QDir(outDir).exists() && !QDir().mkpath(outDir)) {
Chris@1428 100 SVCERR << "ERROR: Audio out directory \"" << outDir << "\" does not exist and could not be created" << endl;
Chris@1359 101 QVERIFY2(QDir(outDir).exists(), "Audio out directory not found and could not be created");
Chris@1345 102 }
Chris@1345 103 }
Chris@1345 104
Chris@1359 105 void readAudio_data() {
Chris@1359 106 addAudioFiles();
Chris@1359 107 }
Chris@1359 108
Chris@1359 109 void readAudio() {
Chris@1359 110
Chris@1359 111 // Ensure that we can open all the files
Chris@1359 112
Chris@1359 113 QFETCH(QString, audiofile);
Chris@1359 114
Chris@1592 115 if (!AudioFileReaderFactory::isSupported(encodingDir + "/" +
Chris@1592 116 audiofile)) {
Chris@1592 117 #if ( QT_VERSION >= 0x050000 )
Chris@1592 118 QSKIP("Known unsupported file, skipping");
Chris@1592 119 #else
Chris@1592 120 QSKIP("Known unsupported file, skipping", SkipSingle);
Chris@1592 121 #endif
Chris@1592 122 }
Chris@1592 123
Chris@1359 124 AudioFileReaderFactory::Parameters params;
Chris@1359 125 AudioFileReader *reader =
Chris@1359 126 AudioFileReaderFactory::createReader
Chris@1359 127 (encodingDir + "/" + audiofile, params);
Chris@1359 128
Chris@1359 129 QVERIFY(reader != nullptr);
Chris@1402 130
Chris@1402 131 delete reader;
Chris@1359 132 }
Chris@1359 133
Chris@1359 134 void readMetadata_data() {
Chris@1359 135 addAudioFiles();
Chris@1359 136 }
Chris@1359 137
Chris@1359 138 void readMetadata() {
Chris@1359 139
Chris@1359 140 // All files other than WAVs should have title metadata; check
Chris@1359 141 // that the title matches whatever is in our mapping structure
Chris@1359 142 // defined at the top
Chris@1359 143
Chris@1345 144 QFETCH(QString, audiofile);
Chris@1345 145
Chris@1345 146 AudioFileReaderFactory::Parameters params;
Chris@1346 147 AudioFileReader *reader =
Chris@1346 148 AudioFileReaderFactory::createReader
Chris@1346 149 (encodingDir + "/" + audiofile, params);
Chris@1345 150
Chris@1592 151 if (!reader) {
Chris@1592 152 #if ( QT_VERSION >= 0x050000 )
Chris@1592 153 QSKIP("Unsupported file, skipping");
Chris@1592 154 #else
Chris@1592 155 QSKIP("Unsupported file, skipping", SkipSingle);
Chris@1592 156 #endif
Chris@1592 157 }
Chris@1345 158
Chris@1345 159 QStringList fileAndExt = audiofile.split(".");
Chris@1345 160 QString file = fileAndExt[0];
Chris@1345 161 QString extension = fileAndExt[1];
Chris@1345 162
Chris@1402 163 if (extension == "wav") {
Chris@1402 164
Chris@1402 165 // Nothing
Chris@1402 166
Chris@1402 167 delete reader;
Chris@1402 168
Chris@1402 169 } else {
Chris@1345 170
Chris@1588 171 //#if (!defined (HAVE_OGGZ) || !defined(HAVE_FISHSOUND))
Chris@1588 172 // if (extension == "ogg") {
Chris@1588 173 // QSKIP("Lack native Ogg Vorbis reader, so won't be getting metadata");
Chris@1588 174 // }
Chris@1588 175 //#endif
cannam@1360 176
Chris@1359 177 auto blah = reader->getInterleavedFrames(0, 10);
Chris@1359 178
Chris@1345 179 QString title = reader->getTitle();
Chris@1345 180 QVERIFY(title != QString());
Chris@1345 181
Chris@1402 182 delete reader;
Chris@1402 183
Chris@1345 184 bool found = false;
Chris@1345 185 for (int m = 0; m < mappingCount; ++m) {
Chris@1345 186 if (file == QString::fromUtf8(mapping[m][0])) {
Chris@1345 187 found = true;
Chris@1346 188 QString expected = QString::fromUtf8(mapping[m][1]);
Chris@1346 189 if (title != expected) {
Chris@1428 190 SVCERR << "Title does not match expected: codepoints are" << endl;
Chris@1428 191 SVCERR << "Title (" << title.length() << "ch): ";
Chris@1346 192 for (int i = 0; i < title.length(); ++i) {
Chris@1428 193 SVCERR << title[i].unicode() << " ";
Chris@1346 194 }
Chris@1428 195 SVCERR << endl;
Chris@1428 196 SVCERR << "Expected (" << expected.length() << "ch): ";
Chris@1346 197 for (int i = 0; i < expected.length(); ++i) {
Chris@1428 198 SVCERR << expected[i].unicode() << " ";
Chris@1346 199 }
Chris@1428 200 SVCERR << endl;
Chris@1346 201 }
Chris@1346 202 QCOMPARE(title, expected);
Chris@1345 203 break;
Chris@1345 204 }
Chris@1345 205 }
Chris@1345 206
Chris@1345 207 if (!found) {
Chris@1359 208 // Note that this can happen legitimately on Windows,
Chris@1359 209 // where (for annoying VCS-related reasons) the test
Chris@1359 210 // files may have a different filename encoding from
Chris@1359 211 // the expected UTF-16. We check this properly in
Chris@1359 212 // readWriteAudio below, by saving out the file to a
Chris@1359 213 // name matching the metadata
Chris@1428 214 SVCERR << "Couldn't find filename \""
Chris@1345 215 << file << "\" in title mapping array" << endl;
Chris@1346 216 QSKIP("Couldn't find filename in title mapping array");
Chris@1345 217 }
Chris@1345 218 }
Chris@1345 219 }
Chris@1359 220
Chris@1359 221 void readWriteAudio_data() {
Chris@1359 222 addAudioFiles();
Chris@1359 223 }
Chris@1359 224
Chris@1359 225 void readWriteAudio()
Chris@1359 226 {
Chris@1359 227 // For those files that have title metadata (i.e. all of them
Chris@1359 228 // except the WAVs), read the title metadata and write a wav
Chris@1359 229 // file (of arbitrary content) whose name matches that. Then
Chris@1359 230 // check that we can re-read it. This is intended to exercise
Chris@1359 231 // systems on which the original test filename is miscoded (as
Chris@1359 232 // can happen on Windows).
Chris@1359 233
Chris@1359 234 QFETCH(QString, audiofile);
Chris@1359 235
Chris@1359 236 QStringList fileAndExt = audiofile.split(".");
Chris@1359 237 QString file = fileAndExt[0];
Chris@1359 238 QString extension = fileAndExt[1];
Chris@1359 239
Chris@1359 240 if (extension == "wav") {
Chris@1359 241 return;
Chris@1359 242 }
Chris@1359 243
Chris@1588 244 //#if (!defined (HAVE_OGGZ) || !defined(HAVE_FISHSOUND))
Chris@1588 245 // if (extension == "ogg") {
Chris@1588 246 // QSKIP("Lack native Ogg Vorbis reader, so won't be getting metadata");
Chris@1588 247 // }
Chris@1588 248 //#endif
cannam@1360 249
Chris@1359 250 AudioFileReaderFactory::Parameters params;
Chris@1359 251 AudioFileReader *reader =
Chris@1359 252 AudioFileReaderFactory::createReader
Chris@1359 253 (encodingDir + "/" + audiofile, params);
Chris@1592 254
Chris@1592 255 if (!reader) {
Chris@1592 256 #if ( QT_VERSION >= 0x050000 )
Chris@1592 257 QSKIP("Unsupported file, skipping");
Chris@1592 258 #else
Chris@1592 259 QSKIP("Unsupported file, skipping", SkipSingle);
Chris@1592 260 #endif
Chris@1592 261 }
Chris@1359 262
Chris@1359 263 QString title = reader->getTitle();
Chris@1359 264 QVERIFY(title != QString());
Chris@1359 265
Chris@1359 266 for (int useTemporary = 0; useTemporary <= 1; ++useTemporary) {
Chris@1359 267
Chris@1359 268 QString outfile = outDir + "/" + file + ".wav";
Chris@1359 269 WavFileWriter writer(outfile,
Chris@1359 270 reader->getSampleRate(),
Chris@1359 271 1,
Chris@1359 272 useTemporary ?
Chris@1359 273 WavFileWriter::WriteToTemporary :
Chris@1359 274 WavFileWriter::WriteToTarget);
Chris@1359 275
Chris@1359 276 QVERIFY(writer.isOK());
Chris@1359 277
Chris@1359 278 floatvec_t data { 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0 };
Chris@1359 279 const float *samples = data.data();
Chris@1359 280 bool ok = writer.writeSamples(&samples, 8);
Chris@1359 281 QVERIFY(ok);
Chris@1359 282
Chris@1359 283 ok = writer.close();
Chris@1359 284 QVERIFY(ok);
Chris@1359 285
Chris@1359 286 AudioFileReader *rereader =
Chris@1359 287 AudioFileReaderFactory::createReader(outfile, params);
Chris@1359 288 QVERIFY(rereader != nullptr);
Chris@1359 289
Chris@1359 290 floatvec_t readFrames = rereader->getInterleavedFrames(0, 8);
Chris@1359 291 QCOMPARE(readFrames, data);
Chris@1359 292
Chris@1359 293 delete rereader;
Chris@1359 294 }
Chris@1359 295
Chris@1359 296 delete reader;
Chris@1359 297 }
Chris@1345 298 };
Chris@1345 299
Chris@1345 300 #endif