annotate data/fileio/test/EncodingTest.h @ 1596:81989085997b bqaudiostream

Argh, that fix for Mac broke Win
author Chris Cannam
date Tue, 22 Jan 2019 20:07:46 +0000
parents afa75922fe0f
children ae9849c0815f
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@1596 82 const char *strOf(QString s) {
Chris@1596 83 return strdup(s.toLocal8Bit().data());
Chris@1596 84 }
Chris@1596 85
Chris@1359 86 void addAudioFiles() {
Chris@1596 87 QTest::addColumn<QString>("audiofile");
Chris@1596 88 #ifndef Q_OS_MAC
Chris@1596 89 // The normal case - populate the file list from the files
Chris@1596 90 // actually present in the encodings directory
Chris@1596 91 QStringList files = QDir(encodingDir).entryList(QDir::Files);
Chris@1596 92 foreach (QString filename, files) {
Chris@1596 93 QTest::newRow(strOf(filename)) << filename;
Chris@1596 94 }
Chris@1596 95 #else
Chris@1596 96 // Deviant case for Mac - populate the file list from the
Chris@1596 97 // hard-coded list of expected files in testFiles. This is
Chris@1596 98 // because QDir::entryList is currently broken on APFS (as of
Chris@1596 99 // Qt 5.12) because of variant Unicode normalisations.
Chris@1596 100 for (int i = 0; i < testFileCount; ++i) {
Chris@1596 101 std::string s = testFiles[i][0];
Chris@1596 102 s += ".";
Chris@1596 103 s += testFiles[i][1];
Chris@1596 104 QTest::newRow(strdup(s.c_str())) << QString::fromStdString(s);
Chris@1596 105 }
Chris@1596 106 #endif
Chris@1359 107 }
Chris@1359 108
Chris@1345 109 private slots:
Chris@1345 110 void init()
Chris@1345 111 {
Chris@1345 112 if (!QDir(encodingDir).exists()) {
Chris@1428 113 SVCERR << "ERROR: Audio encoding file directory \"" << encodingDir << "\" does not exist" << endl;
Chris@1345 114 QVERIFY2(QDir(encodingDir).exists(), "Audio encoding file directory not found");
Chris@1593 115 }
Chris@1359 116 if (!QDir(outDir).exists() && !QDir().mkpath(outDir)) {
Chris@1428 117 SVCERR << "ERROR: Audio out directory \"" << outDir << "\" does not exist and could not be created" << endl;
Chris@1359 118 QVERIFY2(QDir(outDir).exists(), "Audio out directory not found and could not be created");
Chris@1345 119 }
Chris@1345 120 }
Chris@1345 121
Chris@1359 122 void readAudio_data() {
Chris@1359 123 addAudioFiles();
Chris@1359 124 }
Chris@1359 125
Chris@1359 126 void readAudio() {
Chris@1359 127
Chris@1359 128 // Ensure that we can open all the files
Chris@1359 129
Chris@1359 130 QFETCH(QString, audiofile);
Chris@1359 131
Chris@1592 132 if (!AudioFileReaderFactory::isSupported(encodingDir + "/" +
Chris@1592 133 audiofile)) {
Chris@1592 134 #if ( QT_VERSION >= 0x050000 )
Chris@1592 135 QSKIP("Known unsupported file, skipping");
Chris@1592 136 #else
Chris@1592 137 QSKIP("Known unsupported file, skipping", SkipSingle);
Chris@1592 138 #endif
Chris@1592 139 }
Chris@1592 140
Chris@1359 141 AudioFileReaderFactory::Parameters params;
Chris@1359 142 AudioFileReader *reader =
Chris@1359 143 AudioFileReaderFactory::createReader
Chris@1359 144 (encodingDir + "/" + audiofile, params);
Chris@1359 145
Chris@1359 146 QVERIFY(reader != nullptr);
Chris@1402 147
Chris@1402 148 delete reader;
Chris@1359 149 }
Chris@1359 150
Chris@1359 151 void readMetadata_data() {
Chris@1359 152 addAudioFiles();
Chris@1359 153 }
Chris@1359 154
Chris@1359 155 void readMetadata() {
Chris@1359 156
Chris@1359 157 // All files other than WAVs should have title metadata; check
Chris@1359 158 // that the title matches whatever is in our mapping structure
Chris@1359 159 // defined at the top
Chris@1359 160
Chris@1345 161 QFETCH(QString, audiofile);
Chris@1345 162
Chris@1345 163 AudioFileReaderFactory::Parameters params;
Chris@1346 164 AudioFileReader *reader =
Chris@1346 165 AudioFileReaderFactory::createReader
Chris@1346 166 (encodingDir + "/" + audiofile, params);
Chris@1345 167
Chris@1592 168 if (!reader) {
Chris@1592 169 #if ( QT_VERSION >= 0x050000 )
Chris@1592 170 QSKIP("Unsupported file, skipping");
Chris@1592 171 #else
Chris@1592 172 QSKIP("Unsupported file, skipping", SkipSingle);
Chris@1592 173 #endif
Chris@1592 174 }
Chris@1345 175
Chris@1345 176 QStringList fileAndExt = audiofile.split(".");
Chris@1345 177 QString file = fileAndExt[0];
Chris@1345 178 QString extension = fileAndExt[1];
Chris@1345 179
Chris@1402 180 if (extension == "wav") {
Chris@1402 181
Chris@1402 182 // Nothing
Chris@1402 183
Chris@1402 184 delete reader;
Chris@1402 185
Chris@1402 186 } else {
Chris@1345 187
Chris@1588 188 //#if (!defined (HAVE_OGGZ) || !defined(HAVE_FISHSOUND))
Chris@1588 189 // if (extension == "ogg") {
Chris@1588 190 // QSKIP("Lack native Ogg Vorbis reader, so won't be getting metadata");
Chris@1588 191 // }
Chris@1588 192 //#endif
cannam@1360 193
Chris@1359 194 auto blah = reader->getInterleavedFrames(0, 10);
Chris@1359 195
Chris@1345 196 QString title = reader->getTitle();
Chris@1345 197 QVERIFY(title != QString());
Chris@1345 198
Chris@1402 199 delete reader;
Chris@1402 200
Chris@1345 201 bool found = false;
Chris@1345 202 for (int m = 0; m < mappingCount; ++m) {
Chris@1345 203 if (file == QString::fromUtf8(mapping[m][0])) {
Chris@1345 204 found = true;
Chris@1346 205 QString expected = QString::fromUtf8(mapping[m][1]);
Chris@1346 206 if (title != expected) {
Chris@1428 207 SVCERR << "Title does not match expected: codepoints are" << endl;
Chris@1428 208 SVCERR << "Title (" << title.length() << "ch): ";
Chris@1346 209 for (int i = 0; i < title.length(); ++i) {
Chris@1428 210 SVCERR << title[i].unicode() << " ";
Chris@1346 211 }
Chris@1428 212 SVCERR << endl;
Chris@1428 213 SVCERR << "Expected (" << expected.length() << "ch): ";
Chris@1346 214 for (int i = 0; i < expected.length(); ++i) {
Chris@1428 215 SVCERR << expected[i].unicode() << " ";
Chris@1346 216 }
Chris@1428 217 SVCERR << endl;
Chris@1346 218 }
Chris@1346 219 QCOMPARE(title, expected);
Chris@1345 220 break;
Chris@1345 221 }
Chris@1345 222 }
Chris@1345 223
Chris@1345 224 if (!found) {
Chris@1359 225 // Note that this can happen legitimately on Windows,
Chris@1359 226 // where (for annoying VCS-related reasons) the test
Chris@1359 227 // files may have a different filename encoding from
Chris@1359 228 // the expected UTF-16. We check this properly in
Chris@1359 229 // readWriteAudio below, by saving out the file to a
Chris@1359 230 // name matching the metadata
Chris@1428 231 SVCERR << "Couldn't find filename \""
Chris@1345 232 << file << "\" in title mapping array" << endl;
Chris@1346 233 QSKIP("Couldn't find filename in title mapping array");
Chris@1345 234 }
Chris@1345 235 }
Chris@1345 236 }
Chris@1359 237
Chris@1359 238 void readWriteAudio_data() {
Chris@1359 239 addAudioFiles();
Chris@1359 240 }
Chris@1359 241
Chris@1359 242 void readWriteAudio()
Chris@1359 243 {
Chris@1359 244 // For those files that have title metadata (i.e. all of them
Chris@1359 245 // except the WAVs), read the title metadata and write a wav
Chris@1359 246 // file (of arbitrary content) whose name matches that. Then
Chris@1359 247 // check that we can re-read it. This is intended to exercise
Chris@1359 248 // systems on which the original test filename is miscoded (as
Chris@1359 249 // can happen on Windows).
Chris@1359 250
Chris@1359 251 QFETCH(QString, audiofile);
Chris@1359 252
Chris@1359 253 QStringList fileAndExt = audiofile.split(".");
Chris@1359 254 QString file = fileAndExt[0];
Chris@1359 255 QString extension = fileAndExt[1];
Chris@1359 256
Chris@1359 257 if (extension == "wav") {
Chris@1359 258 return;
Chris@1359 259 }
Chris@1359 260
Chris@1588 261 //#if (!defined (HAVE_OGGZ) || !defined(HAVE_FISHSOUND))
Chris@1588 262 // if (extension == "ogg") {
Chris@1588 263 // QSKIP("Lack native Ogg Vorbis reader, so won't be getting metadata");
Chris@1588 264 // }
Chris@1588 265 //#endif
cannam@1360 266
Chris@1359 267 AudioFileReaderFactory::Parameters params;
Chris@1359 268 AudioFileReader *reader =
Chris@1359 269 AudioFileReaderFactory::createReader
Chris@1359 270 (encodingDir + "/" + audiofile, params);
Chris@1592 271
Chris@1592 272 if (!reader) {
Chris@1592 273 #if ( QT_VERSION >= 0x050000 )
Chris@1592 274 QSKIP("Unsupported file, skipping");
Chris@1592 275 #else
Chris@1592 276 QSKIP("Unsupported file, skipping", SkipSingle);
Chris@1592 277 #endif
Chris@1592 278 }
Chris@1359 279
Chris@1359 280 QString title = reader->getTitle();
Chris@1359 281 QVERIFY(title != QString());
Chris@1359 282
Chris@1359 283 for (int useTemporary = 0; useTemporary <= 1; ++useTemporary) {
Chris@1359 284
Chris@1359 285 QString outfile = outDir + "/" + file + ".wav";
Chris@1359 286 WavFileWriter writer(outfile,
Chris@1359 287 reader->getSampleRate(),
Chris@1359 288 1,
Chris@1359 289 useTemporary ?
Chris@1359 290 WavFileWriter::WriteToTemporary :
Chris@1359 291 WavFileWriter::WriteToTarget);
Chris@1359 292
Chris@1359 293 QVERIFY(writer.isOK());
Chris@1359 294
Chris@1359 295 floatvec_t data { 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0 };
Chris@1359 296 const float *samples = data.data();
Chris@1359 297 bool ok = writer.writeSamples(&samples, 8);
Chris@1359 298 QVERIFY(ok);
Chris@1359 299
Chris@1359 300 ok = writer.close();
Chris@1359 301 QVERIFY(ok);
Chris@1359 302
Chris@1359 303 AudioFileReader *rereader =
Chris@1359 304 AudioFileReaderFactory::createReader(outfile, params);
Chris@1359 305 QVERIFY(rereader != nullptr);
Chris@1359 306
Chris@1359 307 floatvec_t readFrames = rereader->getInterleavedFrames(0, 8);
Chris@1359 308 QCOMPARE(readFrames, data);
Chris@1359 309
Chris@1359 310 delete rereader;
Chris@1359 311 }
Chris@1359 312
Chris@1359 313 delete reader;
Chris@1359 314 }
Chris@1345 315 };
Chris@1345 316
Chris@1345 317 #endif