Chris@1345: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1345: Chris@1345: /* Chris@1345: Sonic Visualiser Chris@1345: An audio file viewer and annotation editor. Chris@1345: Centre for Digital Music, Queen Mary, University of London. Chris@1345: Chris@1345: This program is free software; you can redistribute it and/or Chris@1345: modify it under the terms of the GNU General Public License as Chris@1345: published by the Free Software Foundation; either version 2 of the Chris@1345: License, or (at your option) any later version. See the file Chris@1345: COPYING included with this distribution for more information. Chris@1345: */ Chris@1345: Chris@1345: #ifndef TEST_AUDIO_ENCODINGS_H Chris@1345: #define TEST_AUDIO_ENCODINGS_H Chris@1345: Chris@1345: // Quick tests for filename encodings and encoding of ID3 data. Not a Chris@1345: // test of audio codecs. Chris@1345: Chris@1345: #include "../AudioFileReaderFactory.h" Chris@1345: #include "../AudioFileReader.h" Chris@1359: #include "../WavFileWriter.h" Chris@1345: Chris@1345: #include Chris@1345: Chris@1345: #include Chris@1345: #include Chris@1345: #include Chris@1345: Chris@1345: #include Chris@1345: Chris@1345: using namespace std; Chris@1345: Chris@1346: const char utf8_name_cdp_1[] = "Caf\303\251 de Paris"; Chris@1346: const char utf8_name_cdp_2[] = "Caf\303\251 de \351\207\215\345\272\206"; Chris@1347: const char utf8_name_tsprk[] = "T\303\253mple of Sp\303\266rks"; Chris@1346: 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: Chris@1359: // Mapping between filename and expected title metadata field Chris@1345: static const char *mapping[][2] = { Chris@1346: { "id3v2-iso-8859-1", utf8_name_cdp_1 }, Chris@1346: { "id3v2-ucs-2", utf8_name_cdp_2 }, Chris@1346: { utf8_name_tsprk, utf8_name_tsprk }, Chris@1346: { utf8_name_sprkt, utf8_name_sprkt }, Chris@1345: }; Chris@1345: static const int mappingCount = 4; Chris@1345: Chris@1345: class EncodingTest : public QObject Chris@1345: { Chris@1345: Q_OBJECT Chris@1345: Chris@1346: private: Chris@1346: QString testDirBase; Chris@1346: QString encodingDir; Chris@1359: QString outDir; Chris@1346: Chris@1346: public: Chris@1346: EncodingTest(QString base) { Chris@1346: if (base == "") { Chris@1346: base = "svcore/data/fileio/test"; Chris@1346: } Chris@1346: testDirBase = base; Chris@1346: encodingDir = base + "/encodings"; Chris@1359: outDir = base + "/outfiles"; Chris@1346: } Chris@1346: Chris@1346: private: Chris@1345: const char *strOf(QString s) { Chris@1345: return strdup(s.toLocal8Bit().data()); Chris@1345: } Chris@1345: Chris@1359: void addAudioFiles() { Chris@1359: QTest::addColumn("audiofile"); Chris@1359: QStringList files = QDir(encodingDir).entryList(QDir::Files); Chris@1359: foreach (QString filename, files) { Chris@1359: QTest::newRow(strOf(filename)) << filename; Chris@1359: } Chris@1359: } Chris@1359: Chris@1345: private slots: Chris@1345: void init() Chris@1345: { Chris@1345: if (!QDir(encodingDir).exists()) { Chris@1345: cerr << "ERROR: Audio encoding file directory \"" << encodingDir << "\" does not exist" << endl; Chris@1345: QVERIFY2(QDir(encodingDir).exists(), "Audio encoding file directory not found"); Chris@1345: } Chris@1359: if (!QDir(outDir).exists() && !QDir().mkpath(outDir)) { Chris@1359: cerr << "ERROR: Audio out directory \"" << outDir << "\" does not exist and could not be created" << endl; Chris@1359: QVERIFY2(QDir(outDir).exists(), "Audio out directory not found and could not be created"); Chris@1345: } Chris@1345: } Chris@1345: Chris@1359: void readAudio_data() { Chris@1359: addAudioFiles(); Chris@1359: } Chris@1359: Chris@1359: void readAudio() { Chris@1359: Chris@1359: // Ensure that we can open all the files Chris@1359: Chris@1359: QFETCH(QString, audiofile); Chris@1359: Chris@1359: AudioFileReaderFactory::Parameters params; Chris@1359: AudioFileReader *reader = Chris@1359: AudioFileReaderFactory::createReader Chris@1359: (encodingDir + "/" + audiofile, params); Chris@1359: Chris@1359: QVERIFY(reader != nullptr); Chris@1359: } Chris@1359: Chris@1359: void readMetadata_data() { Chris@1359: addAudioFiles(); Chris@1359: } Chris@1359: Chris@1359: void readMetadata() { Chris@1359: Chris@1359: // All files other than WAVs should have title metadata; check Chris@1359: // that the title matches whatever is in our mapping structure Chris@1359: // defined at the top Chris@1359: Chris@1345: QFETCH(QString, audiofile); Chris@1345: Chris@1345: AudioFileReaderFactory::Parameters params; Chris@1346: AudioFileReader *reader = Chris@1346: AudioFileReaderFactory::createReader Chris@1346: (encodingDir + "/" + audiofile, params); Chris@1345: Chris@1345: QVERIFY(reader != nullptr); Chris@1345: Chris@1345: QStringList fileAndExt = audiofile.split("."); Chris@1345: QString file = fileAndExt[0]; Chris@1345: QString extension = fileAndExt[1]; Chris@1345: Chris@1359: if (extension != "wav") { Chris@1345: cannam@1360: #if (!defined (HAVE_OGGZ) || !defined(HAVE_FISHSOUND)) cannam@1360: if (extension == "ogg") { cannam@1360: QSKIP("Lack native Ogg Vorbis reader, so won't be getting metadata"); cannam@1360: } cannam@1360: #endif cannam@1360: Chris@1359: auto blah = reader->getInterleavedFrames(0, 10); Chris@1359: Chris@1345: QString title = reader->getTitle(); Chris@1345: QVERIFY(title != QString()); Chris@1345: Chris@1345: bool found = false; Chris@1345: for (int m = 0; m < mappingCount; ++m) { Chris@1345: if (file == QString::fromUtf8(mapping[m][0])) { Chris@1345: found = true; Chris@1346: QString expected = QString::fromUtf8(mapping[m][1]); Chris@1346: if (title != expected) { Chris@1346: cerr << "Title does not match expected: codepoints are" << endl; Chris@1346: cerr << "Title (" << title.length() << "ch): "; Chris@1346: for (int i = 0; i < title.length(); ++i) { Chris@1346: cerr << title[i].unicode() << " "; Chris@1346: } Chris@1346: cerr << endl; Chris@1346: cerr << "Expected (" << expected.length() << "ch): "; Chris@1346: for (int i = 0; i < expected.length(); ++i) { Chris@1346: cerr << expected[i].unicode() << " "; Chris@1346: } Chris@1346: cerr << endl; Chris@1346: } Chris@1346: QCOMPARE(title, expected); Chris@1345: break; Chris@1345: } Chris@1345: } Chris@1345: Chris@1345: if (!found) { Chris@1359: // Note that this can happen legitimately on Windows, Chris@1359: // where (for annoying VCS-related reasons) the test Chris@1359: // files may have a different filename encoding from Chris@1359: // the expected UTF-16. We check this properly in Chris@1359: // readWriteAudio below, by saving out the file to a Chris@1359: // name matching the metadata Chris@1346: cerr << "Couldn't find filename \"" Chris@1345: << file << "\" in title mapping array" << endl; Chris@1346: QSKIP("Couldn't find filename in title mapping array"); Chris@1345: } Chris@1345: } Chris@1345: } Chris@1359: Chris@1359: void readWriteAudio_data() { Chris@1359: addAudioFiles(); Chris@1359: } Chris@1359: Chris@1359: void readWriteAudio() Chris@1359: { Chris@1359: // For those files that have title metadata (i.e. all of them Chris@1359: // except the WAVs), read the title metadata and write a wav Chris@1359: // file (of arbitrary content) whose name matches that. Then Chris@1359: // check that we can re-read it. This is intended to exercise Chris@1359: // systems on which the original test filename is miscoded (as Chris@1359: // can happen on Windows). Chris@1359: Chris@1359: QFETCH(QString, audiofile); Chris@1359: Chris@1359: QStringList fileAndExt = audiofile.split("."); Chris@1359: QString file = fileAndExt[0]; Chris@1359: QString extension = fileAndExt[1]; Chris@1359: Chris@1359: if (extension == "wav") { Chris@1359: return; Chris@1359: } Chris@1359: cannam@1360: #if (!defined (HAVE_OGGZ) || !defined(HAVE_FISHSOUND)) cannam@1360: if (extension == "ogg") { cannam@1360: QSKIP("Lack native Ogg Vorbis reader, so won't be getting metadata"); cannam@1360: } cannam@1360: #endif cannam@1360: Chris@1359: AudioFileReaderFactory::Parameters params; Chris@1359: AudioFileReader *reader = Chris@1359: AudioFileReaderFactory::createReader Chris@1359: (encodingDir + "/" + audiofile, params); Chris@1359: QVERIFY(reader != nullptr); Chris@1359: Chris@1359: QString title = reader->getTitle(); Chris@1359: QVERIFY(title != QString()); Chris@1359: Chris@1359: for (int useTemporary = 0; useTemporary <= 1; ++useTemporary) { Chris@1359: Chris@1359: QString outfile = outDir + "/" + file + ".wav"; Chris@1359: WavFileWriter writer(outfile, Chris@1359: reader->getSampleRate(), Chris@1359: 1, Chris@1359: useTemporary ? Chris@1359: WavFileWriter::WriteToTemporary : Chris@1359: WavFileWriter::WriteToTarget); Chris@1359: Chris@1359: QVERIFY(writer.isOK()); Chris@1359: Chris@1359: floatvec_t data { 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0 }; Chris@1359: const float *samples = data.data(); Chris@1359: bool ok = writer.writeSamples(&samples, 8); Chris@1359: QVERIFY(ok); Chris@1359: Chris@1359: ok = writer.close(); Chris@1359: QVERIFY(ok); Chris@1359: Chris@1359: AudioFileReader *rereader = Chris@1359: AudioFileReaderFactory::createReader(outfile, params); Chris@1359: QVERIFY(rereader != nullptr); Chris@1359: Chris@1359: floatvec_t readFrames = rereader->getInterleavedFrames(0, 8); Chris@1359: QCOMPARE(readFrames, data); Chris@1359: Chris@1359: delete rereader; Chris@1359: } Chris@1359: Chris@1359: delete reader; Chris@1359: } Chris@1345: }; Chris@1345: Chris@1345: #endif