diff data/fileio/test/EncodingTest.h @ 1359:1c9bbbb6116a 3.0-integration

Use W64 instead of WAV for decoded files; use Ogg reader in preference to WAV one for Ogg files (WAV reader works, via libsndfile, but doesn't load metadata); fix Ogg reader to use QFile open instead of non-Win32-compatible API; add more encoder tests, audio writer test, midi reader test
author Chris Cannam
date Tue, 10 Jan 2017 10:58:25 +0000
parents 281a8c9d4886
children 8eddb528ef0c
line wrap: on
line diff
--- a/data/fileio/test/EncodingTest.h	Mon Jan 09 18:51:42 2017 +0000
+++ b/data/fileio/test/EncodingTest.h	Tue Jan 10 10:58:25 2017 +0000
@@ -20,6 +20,7 @@
 
 #include "../AudioFileReaderFactory.h"
 #include "../AudioFileReader.h"
+#include "../WavFileWriter.h"
 
 #include <cmath>
 
@@ -36,6 +37,7 @@
 const char utf8_name_tsprk[] = "T\303\253mple of Sp\303\266rks";
 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";
 
+// Mapping between filename and expected title metadata field
 static const char *mapping[][2] = {
     { "id3v2-iso-8859-1", utf8_name_cdp_1 },
     { "id3v2-ucs-2", utf8_name_cdp_2 },
@@ -51,6 +53,7 @@
 private:
     QString testDirBase;
     QString encodingDir;
+    QString outDir;
 
 public:
     EncodingTest(QString base) {
@@ -59,6 +62,7 @@
         }
         testDirBase = base;
         encodingDir = base + "/encodings";
+        outDir = base + "/outfiles";
     }
 
 private:
@@ -66,6 +70,14 @@
         return strdup(s.toLocal8Bit().data());
     }
 
+    void addAudioFiles() {
+        QTest::addColumn<QString>("audiofile");
+        QStringList files = QDir(encodingDir).entryList(QDir::Files);
+        foreach (QString filename, files) {
+            QTest::newRow(strOf(filename)) << filename;
+        }
+    }
+
 private slots:
     void init()
     {
@@ -73,19 +85,40 @@
             cerr << "ERROR: Audio encoding file directory \"" << encodingDir << "\" does not exist" << endl;
             QVERIFY2(QDir(encodingDir).exists(), "Audio encoding file directory not found");
         }
-    }
-
-    void read_data()
-    {
-        QTest::addColumn<QString>("audiofile");
-        QStringList files = QDir(encodingDir).entryList(QDir::Files);
-        foreach (QString filename, files) {
-            QTest::newRow(strOf(filename)) << filename;
+        if (!QDir(outDir).exists() && !QDir().mkpath(outDir)) {
+            cerr << "ERROR: Audio out directory \"" << outDir << "\" does not exist and could not be created" << endl;
+            QVERIFY2(QDir(outDir).exists(), "Audio out directory not found and could not be created");
         }
     }
 
-    void read()
-    {
+    void readAudio_data() {
+        addAudioFiles();
+    }
+
+    void readAudio() {
+
+        // Ensure that we can open all the files
+        
+        QFETCH(QString, audiofile);
+
+        AudioFileReaderFactory::Parameters params;
+        AudioFileReader *reader =
+            AudioFileReaderFactory::createReader
+            (encodingDir + "/" + audiofile, params);
+
+        QVERIFY(reader != nullptr);
+    }
+
+    void readMetadata_data() {
+        addAudioFiles();
+    }
+    
+    void readMetadata() {
+        
+        // All files other than WAVs should have title metadata; check
+        // that the title matches whatever is in our mapping structure
+        // defined at the top
+        
         QFETCH(QString, audiofile);
 
         AudioFileReaderFactory::Parameters params;
@@ -99,8 +132,10 @@
         QString file = fileAndExt[0];
         QString extension = fileAndExt[1];
 
-        if (extension == "mp3") {
+        if (extension != "wav") {
 
+            auto blah = reader->getInterleavedFrames(0, 10);
+            
             QString title = reader->getTitle();
             QVERIFY(title != QString());
 
@@ -128,12 +163,83 @@
             }
 
             if (!found) {
+                // Note that this can happen legitimately on Windows,
+                // where (for annoying VCS-related reasons) the test
+                // files may have a different filename encoding from
+                // the expected UTF-16. We check this properly in
+                // readWriteAudio below, by saving out the file to a
+                // name matching the metadata
                 cerr << "Couldn't find filename \""
                      << file << "\" in title mapping array" << endl;
                 QSKIP("Couldn't find filename in title mapping array");
             }
         }
     }
+
+    void readWriteAudio_data() {
+        addAudioFiles();
+    }
+
+    void readWriteAudio()
+    {
+        // For those files that have title metadata (i.e. all of them
+        // except the WAVs), read the title metadata and write a wav
+        // file (of arbitrary content) whose name matches that.  Then
+        // check that we can re-read it. This is intended to exercise
+        // systems on which the original test filename is miscoded (as
+        // can happen on Windows).
+        
+        QFETCH(QString, audiofile);
+
+        QStringList fileAndExt = audiofile.split(".");
+        QString file = fileAndExt[0];
+        QString extension = fileAndExt[1];
+
+        if (extension == "wav") {
+            return;
+        }
+
+        AudioFileReaderFactory::Parameters params;
+        AudioFileReader *reader =
+            AudioFileReaderFactory::createReader
+            (encodingDir + "/" + audiofile, params);
+        QVERIFY(reader != nullptr);
+
+        QString title = reader->getTitle();
+        QVERIFY(title != QString());
+
+        for (int useTemporary = 0; useTemporary <= 1; ++useTemporary) {
+        
+            QString outfile = outDir + "/" + file + ".wav";
+            WavFileWriter writer(outfile,
+                                 reader->getSampleRate(),
+                                 1,
+                                 useTemporary ?
+                                 WavFileWriter::WriteToTemporary :
+                                 WavFileWriter::WriteToTarget);
+
+            QVERIFY(writer.isOK());
+
+            floatvec_t data { 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0 };
+            const float *samples = data.data();
+            bool ok = writer.writeSamples(&samples, 8);
+            QVERIFY(ok);
+
+            ok = writer.close();
+            QVERIFY(ok);
+
+            AudioFileReader *rereader =
+                AudioFileReaderFactory::createReader(outfile, params);
+            QVERIFY(rereader != nullptr);
+
+            floatvec_t readFrames = rereader->getInterleavedFrames(0, 8);
+            QCOMPARE(readFrames, data);
+
+            delete rereader;
+        }
+
+        delete reader;
+    }
 };
 
 #endif