| 1 |
/* |
| 2 |
Audio File Library |
| 3 |
Copyright (C) 2013 Michael Pruett <[email protected]> |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along |
| 16 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef Lossless_h |
| 21 |
#define Lossless_h |
| 22 |
|
| 23 |
#include <audiofile.h> |
| 24 |
#include <gtest/gtest.h> |
| 25 |
|
| 26 |
#include <stdint.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include "TestUtilities.h" |
| 30 |
|
| 31 |
struct NaturalGenerator |
| 32 |
{ |
| 33 |
NaturalGenerator() : m_n(0) |
| 34 |
{ |
| 35 |
} |
| 36 |
int32_t operator() () { return ++m_n; } |
| 37 |
|
| 38 |
private: |
| 39 |
int32_t m_n; |
| 40 |
}; |
| 41 |
|
| 42 |
struct LinearCongruentialGenerator |
| 43 |
{ |
| 44 |
LinearCongruentialGenerator() : m_n(0) |
| 45 |
{ |
| 46 |
} |
| 47 |
int32_t operator() () |
| 48 |
{ |
| 49 |
m_n = 1664525 * m_n + 1013904223; |
| 50 |
return static_cast<int32_t>(m_n); |
| 51 |
} |
| 52 |
|
| 53 |
private: |
| 54 |
uint32_t m_n; |
| 55 |
}; |
| 56 |
|
| 57 |
static inline int32_t trim(int32_t n, int sampleWidth) |
| 58 |
{ |
| 59 |
unsigned shift = (((sampleWidth + 7) >> 3) << 3) - sampleWidth; |
| 60 |
unsigned maskShift = 32 - sampleWidth; |
| 61 |
n <<= shift; |
| 62 |
return (n << maskShift) >> maskShift; |
| 63 |
} |
| 64 |
|
| 65 |
template <typename SampleType, typename Generator> |
| 66 |
static void testLossless(const char *prefix, int fileFormat, |
| 67 |
int compressionFormat, int channelCount, int sampleWidth, int frameCount) |
| 68 |
{ |
| 69 |
SCOPED_TRACE(channelCount); |
| 70 |
std::string testFileName; |
| 71 |
ASSERT_TRUE(createTemporaryFile(prefix, &testFileName)); |
| 72 |
|
| 73 |
AFfilesetup setup = afNewFileSetup(); |
| 74 |
afInitFileFormat(setup, fileFormat); |
| 75 |
afInitChannels(setup, AF_DEFAULT_TRACK, channelCount); |
| 76 |
afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, sampleWidth); |
| 77 |
afInitCompression(setup, AF_DEFAULT_TRACK, compressionFormat); |
| 78 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 79 |
ASSERT_TRUE(file); |
| 80 |
afFreeFileSetup(setup); |
| 81 |
|
| 82 |
Generator g; |
| 83 |
SampleType *data = new SampleType[frameCount * channelCount]; |
| 84 |
for (int i=0; i<frameCount; i++) |
| 85 |
for (int c=0; c<channelCount; c++) |
| 86 |
data[i*channelCount + c] = trim(g(), sampleWidth) * ((c&1) ? -1 : 1); |
| 87 |
|
| 88 |
AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, data, frameCount); |
| 89 |
ASSERT_EQ(frameCount, framesWritten); |
| 90 |
|
| 91 |
ASSERT_EQ(afCloseFile(file), 0); |
| 92 |
|
| 93 |
file = afOpenFile(testFileName.c_str(), "r", AF_NULL_FILESETUP); |
| 94 |
ASSERT_TRUE(file); |
| 95 |
ASSERT_EQ(compressionFormat, afGetCompression(file, AF_DEFAULT_TRACK)); |
| 96 |
int readSampleFormat, readSampleWidth; |
| 97 |
afGetSampleFormat(file, AF_DEFAULT_TRACK, &readSampleFormat, &readSampleWidth); |
| 98 |
ASSERT_EQ(AF_SAMPFMT_TWOSCOMP, readSampleFormat); |
| 99 |
ASSERT_EQ(sampleWidth, readSampleWidth); |
| 100 |
ASSERT_EQ(frameCount, afGetFrameCount(file, AF_DEFAULT_TRACK)); |
| 101 |
|
| 102 |
SampleType *readData = new SampleType[frameCount * channelCount]; |
| 103 |
AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, frameCount); |
| 104 |
ASSERT_EQ(frameCount, framesRead); |
| 105 |
|
| 106 |
for (int i=0; i<frameCount; i++) |
| 107 |
for (int c=0; c<channelCount; c++) |
| 108 |
EXPECT_EQ(data[i*channelCount + c], readData[i*channelCount + c]) << |
| 109 |
"failed at " << i; |
| 110 |
|
| 111 |
// Read entire file with a seek before each read operation. |
| 112 |
for (AFframecount offset = 0; offset < frameCount; offset += 373) |
| 113 |
{ |
| 114 |
ASSERT_EQ(offset, afSeekFrame(file, AF_DEFAULT_TRACK, offset)); |
| 115 |
|
| 116 |
AFframecount framesToRead = 1091; |
| 117 |
framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, framesToRead); |
| 118 |
ASSERT_EQ(std::min(framesToRead, frameCount - offset), framesRead); |
| 119 |
|
| 120 |
for (int i=0; i<framesRead; i++) |
| 121 |
for (int c=0; c<channelCount; c++) |
| 122 |
EXPECT_EQ(data[(i+offset)*channelCount + c], |
| 123 |
readData[i*channelCount + c]) << "failed at " << i; |
| 124 |
} |
| 125 |
|
| 126 |
// Read entire file sequentially in multiple read operations. |
| 127 |
ASSERT_EQ(afSeekFrame(file, AF_DEFAULT_TRACK, 0), 0); |
| 128 |
|
| 129 |
AFframecount framesToRead = 1087; |
| 130 |
for (AFframecount offset = 0; offset < frameCount; offset += framesToRead) |
| 131 |
{ |
| 132 |
framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, framesToRead); |
| 133 |
ASSERT_EQ(std::min(framesToRead, frameCount - offset), framesRead); |
| 134 |
|
| 135 |
for (int i=0; i<framesRead; i++) |
| 136 |
for (int c=0; c<channelCount; c++) |
| 137 |
EXPECT_EQ(data[(i+offset)*channelCount + c], |
| 138 |
readData[i*channelCount + c]) << "failed at " << i; |
| 139 |
} |
| 140 |
|
| 141 |
ASSERT_EQ(0, afCloseFile(file)); |
| 142 |
|
| 143 |
delete [] data; |
| 144 |
delete [] readData; |
| 145 |
|
| 146 |
ASSERT_EQ(0, ::unlink(testFileName.c_str())); |
| 147 |
} |
| 148 |
|
| 149 |
template <typename SampleType> |
| 150 |
static void testLossless(const char *prefix, int fileFormat, |
| 151 |
int compressionFormat, int channelCount, int sampleWidth, int frameCount) |
| 152 |
{ |
| 153 |
{ |
| 154 |
SCOPED_TRACE("Sequential"); |
| 155 |
testLossless<SampleType, NaturalGenerator>(prefix, fileFormat, compressionFormat, channelCount, sampleWidth, frameCount); |
| 156 |
} |
| 157 |
{ |
| 158 |
SCOPED_TRACE("Pseudo-random"); |
| 159 |
testLossless<SampleType, LinearCongruentialGenerator>(prefix, fileFormat, compressionFormat, channelCount, sampleWidth, frameCount); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
#endif |