annotate data/fileio/test/EncodingTest.h @ 1628:6db21df9f376 single-point

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