comparison data/fileio/test/AudioFileWriterTest.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
children 87ae75da6527
comparison
equal deleted inserted replaced
1358:b7be05d57f0a 1359:1c9bbbb6116a
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_FILE_WRITER_H
16 #define TEST_AUDIO_FILE_WRITER_H
17
18 #include "../AudioFileReaderFactory.h"
19 #include "../AudioFileReader.h"
20 #include "../WavFileWriter.h"
21
22 #include "AudioTestData.h"
23
24 #include "bqvec/VectorOps.h"
25 #include "bqvec/Allocators.h"
26
27 #include <cmath>
28
29 #include <QObject>
30 #include <QtTest>
31 #include <QDir>
32
33 #include <iostream>
34
35 using namespace std;
36 using namespace breakfastquay;
37
38 class AudioFileWriterTest : public QObject
39 {
40 Q_OBJECT
41
42 private:
43 QString testDirBase;
44 QString outDir;
45
46 static const int rate = 44100;
47
48 public:
49 AudioFileWriterTest(QString base) {
50 if (base == "") {
51 base = "svcore/data/fileio/test";
52 }
53 testDirBase = base;
54 outDir = base + "/outfiles";
55 }
56
57 const char *strOf(QString s) {
58 return strdup(s.toLocal8Bit().data());
59 }
60
61 QString testName(bool direct, int channels) {
62 return QString("%1 %2 %3")
63 .arg(channels)
64 .arg(channels > 1 ? "channels" : "channel")
65 .arg(direct ? "direct" : "via temporary");
66 }
67
68 private slots:
69 void init()
70 {
71 if (!QDir(outDir).exists() && !QDir().mkpath(outDir)) {
72 cerr << "ERROR: Audio out directory \"" << outDir << "\" does not exist and could not be created" << endl;
73 QVERIFY2(QDir(outDir).exists(), "Audio out directory not found and could not be created");
74 }
75 }
76
77 void write_data()
78 {
79 QTest::addColumn<bool>("direct");
80 QTest::addColumn<int>("channels");
81 for (int direct = 0; direct <= 1; ++direct) {
82 for (int channels = 1; channels < 8; ++channels) {
83 if (channels == 1 || channels == 2 ||
84 channels == 5 || channels == 8) {
85 QString desc = testName(direct, channels);
86 QTest::newRow(strOf(desc)) << (bool)direct << channels;
87 }
88 }
89 }
90 }
91
92 void write()
93 {
94 QFETCH(bool, direct);
95 QFETCH(int, channels);
96
97 QString outfile = QString("%1/out-%2ch-%3.wav")
98 .arg(outDir).arg(channels).arg(direct ? "direct" : "via-temporary");
99
100 WavFileWriter writer(outfile,
101 rate,
102 channels,
103 direct ?
104 WavFileWriter::WriteToTarget :
105 WavFileWriter::WriteToTemporary);
106 QVERIFY(writer.isOK());
107
108 AudioTestData data(rate, channels);
109 data.generate();
110
111 sv_frame_t frameCount = data.getFrameCount();
112 float *interleaved = data.getInterleavedData();
113 float **nonInterleaved = allocate_channels<float>(channels, frameCount);
114 v_deinterleave(nonInterleaved, interleaved, channels, int(frameCount));
115 bool ok = writer.writeSamples(nonInterleaved, frameCount);
116 deallocate_channels(nonInterleaved, channels);
117 QVERIFY(ok);
118
119 ok = writer.close();
120 QVERIFY(ok);
121
122 AudioFileReaderFactory::Parameters params;
123 AudioFileReader *rereader =
124 AudioFileReaderFactory::createReader(outfile, params);
125 QVERIFY(rereader != nullptr);
126
127 floatvec_t readFrames = rereader->getInterleavedFrames(0, frameCount);
128 floatvec_t expected(interleaved, interleaved + frameCount * channels);
129 QCOMPARE(readFrames, expected);
130
131 delete rereader;
132 }
133 };
134
135 #endif