annotate data/fileio/test/EncodingTest.h @ 1592:f8e3dcbafb4d bqaudiostream

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