comparison data/fileio/test/EncodingTest.h @ 1365:3382d914e110

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