| 1 |
/* |
| 2 |
Copyright (C) 2011-2013 Michael Pruett. All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with or without |
| 5 |
modification, are permitted provided that the following conditions |
| 6 |
are met: |
| 7 |
|
| 8 |
1. Redistributions of source code must retain the above copyright |
| 9 |
notice, this list of conditions and the following disclaimer. |
| 10 |
|
| 11 |
2. Redistributions in binary form must reproduce the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer in the |
| 13 |
documentation and/or other materials provided with the distribution. |
| 14 |
|
| 15 |
3. The name of the author may not be used to endorse or promote products |
| 16 |
derived from this software without specific prior written permission. |
| 17 |
|
| 18 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 |
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
/* |
| 31 |
For each supported file format, test reading and writing audio |
| 32 |
files with all applicable PCM audio data formats. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include <audiofile.h> |
| 36 |
#include <climits> |
| 37 |
#include <cmath> |
| 38 |
#include <gtest/gtest.h> |
| 39 |
#include <stdint.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string> |
| 42 |
#include <unistd.h> |
| 43 |
|
| 44 |
#include "TestUtilities.h" |
| 45 |
|
| 46 |
struct PCMMapping |
| 47 |
{ |
| 48 |
double slope, intercept, minClip, maxClip; |
| 49 |
}; |
| 50 |
|
| 51 |
void getDefaultPCMMapping(int sampleFormat, int bitsPerSample, PCMMapping &m) |
| 52 |
{ |
| 53 |
switch (sampleFormat) |
| 54 |
{ |
| 55 |
case AF_SAMPFMT_TWOSCOMP: |
| 56 |
{ |
| 57 |
double s = std::pow(2.0, bitsPerSample - 1); |
| 58 |
m.slope = s; |
| 59 |
m.intercept = 0; |
| 60 |
m.minClip = -s; |
| 61 |
m.maxClip = s - 1; |
| 62 |
break; |
| 63 |
} |
| 64 |
case AF_SAMPFMT_UNSIGNED: |
| 65 |
{ |
| 66 |
double s = std::pow(2.0, bitsPerSample - 1); |
| 67 |
m.slope = s; |
| 68 |
m.intercept = s; |
| 69 |
m.minClip = 0; |
| 70 |
m.maxClip = 2 * s - 1; |
| 71 |
break; |
| 72 |
} |
| 73 |
case AF_SAMPFMT_FLOAT: |
| 74 |
case AF_SAMPFMT_DOUBLE: |
| 75 |
m.slope = 1; |
| 76 |
m.intercept = 0; |
| 77 |
m.minClip = 0; |
| 78 |
m.maxClip = 0; |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
template <typename T, int kSampleFormat, int kBitsPerSample> |
| 84 |
void runTestWithChannels(int fileFormat, int channelCount) |
| 85 |
{ |
| 86 |
std::string testFileName; |
| 87 |
ASSERT_TRUE(createTemporaryFile("PCMData", &testFileName)); |
| 88 |
|
| 89 |
const int numFrames = 20; |
| 90 |
const int numSamples = numFrames * channelCount; |
| 91 |
T samples[numSamples]; |
| 92 |
for (int i=0; i<numFrames; i++) |
| 93 |
for (int c=0; c<channelCount; c++) |
| 94 |
samples[i*channelCount + c] = static_cast<T>(i*i + 3*c + 1); |
| 95 |
|
| 96 |
AFfilesetup setup = afNewFileSetup(); |
| 97 |
afInitFileFormat(setup, fileFormat); |
| 98 |
afInitChannels(setup, AF_DEFAULT_TRACK, channelCount); |
| 99 |
afInitSampleFormat(setup, AF_DEFAULT_TRACK, kSampleFormat, kBitsPerSample); |
| 100 |
|
| 101 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 102 |
ASSERT_TRUE(file) << "Could not open file for writing"; |
| 103 |
|
| 104 |
afFreeFileSetup(setup); |
| 105 |
|
| 106 |
PCMMapping mapping, defaultMapping; |
| 107 |
getDefaultPCMMapping(kSampleFormat, kBitsPerSample, defaultMapping); |
| 108 |
|
| 109 |
afGetPCMMapping(file, AF_DEFAULT_TRACK, |
| 110 |
&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip); |
| 111 |
ASSERT_EQ(mapping.slope, defaultMapping.slope); |
| 112 |
ASSERT_EQ(mapping.intercept, defaultMapping.intercept); |
| 113 |
ASSERT_EQ(mapping.minClip, defaultMapping.minClip); |
| 114 |
ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip); |
| 115 |
|
| 116 |
afGetVirtualPCMMapping(file, AF_DEFAULT_TRACK, |
| 117 |
&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip); |
| 118 |
ASSERT_EQ(mapping.slope, defaultMapping.slope); |
| 119 |
ASSERT_EQ(mapping.intercept, defaultMapping.intercept); |
| 120 |
ASSERT_EQ(mapping.minClip, defaultMapping.minClip); |
| 121 |
ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip); |
| 122 |
|
| 123 |
ASSERT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, samples, numFrames), |
| 124 |
numFrames) << |
| 125 |
"Number of frames written does not match number of frames requested"; |
| 126 |
|
| 127 |
ASSERT_EQ(afCloseFile(file), 0) << "Error closing file"; |
| 128 |
|
| 129 |
file = afOpenFile(testFileName.c_str(), "r", NULL); |
| 130 |
ASSERT_TRUE(file) << "Could not open file for reading"; |
| 131 |
|
| 132 |
ASSERT_EQ(afGetFileFormat(file, NULL), fileFormat) << |
| 133 |
"Incorrect file format"; |
| 134 |
|
| 135 |
int sampleFormat, sampleWidth; |
| 136 |
afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth); |
| 137 |
ASSERT_EQ(sampleFormat, kSampleFormat) << "Incorrect sample format"; |
| 138 |
ASSERT_EQ(sampleWidth, kBitsPerSample) << "Incorrect sample width"; |
| 139 |
|
| 140 |
ASSERT_EQ(afGetChannels(file, AF_DEFAULT_TRACK), channelCount) << |
| 141 |
"Incorrect number of channels"; |
| 142 |
|
| 143 |
afGetPCMMapping(file, AF_DEFAULT_TRACK, |
| 144 |
&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip); |
| 145 |
ASSERT_EQ(mapping.slope, defaultMapping.slope); |
| 146 |
ASSERT_EQ(mapping.intercept, defaultMapping.intercept); |
| 147 |
ASSERT_EQ(mapping.minClip, defaultMapping.minClip); |
| 148 |
ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip); |
| 149 |
|
| 150 |
afGetVirtualPCMMapping(file, AF_DEFAULT_TRACK, |
| 151 |
&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip); |
| 152 |
ASSERT_EQ(mapping.slope, defaultMapping.slope); |
| 153 |
ASSERT_EQ(mapping.intercept, defaultMapping.intercept); |
| 154 |
ASSERT_EQ(mapping.minClip, defaultMapping.minClip); |
| 155 |
ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip); |
| 156 |
|
| 157 |
T *samplesRead = new T[numSamples]; |
| 158 |
ASSERT_EQ(afReadFrames(file, AF_DEFAULT_TRACK, samplesRead, numFrames), |
| 159 |
numFrames) << |
| 160 |
"Number of frames read does not match number of frames requested"; |
| 161 |
|
| 162 |
for (int i=0; i<numSamples; i++) |
| 163 |
ASSERT_EQ(samplesRead[i], samples[i]) << |
| 164 |
"Data read from file does not match data written"; |
| 165 |
|
| 166 |
delete [] samplesRead; |
| 167 |
|
| 168 |
ASSERT_EQ(afCloseFile(file), 0) << "Error closing file"; |
| 169 |
|
| 170 |
ASSERT_EQ(::unlink(testFileName.c_str()), 0); |
| 171 |
} |
| 172 |
|
| 173 |
template <typename T, int kSampleFormat, int kBitsPerSample> |
| 174 |
void runTest(int fileFormat) |
| 175 |
{ |
| 176 |
runTestWithChannels<T, kSampleFormat, kBitsPerSample>(fileFormat, 1); |
| 177 |
runTestWithChannels<T, kSampleFormat, kBitsPerSample>(fileFormat, 2); |
| 178 |
runTestWithChannels<T, kSampleFormat, kBitsPerSample>(fileFormat, 4); |
| 179 |
} |
| 180 |
|
| 181 |
void testInt8(int fileFormat) |
| 182 |
{ |
| 183 |
runTest<int8_t, AF_SAMPFMT_TWOSCOMP, 8>(fileFormat); |
| 184 |
} |
| 185 |
|
| 186 |
void testInt8MonoOnly(int fileFormat) |
| 187 |
{ |
| 188 |
runTestWithChannels<int8_t, AF_SAMPFMT_TWOSCOMP, 8>(fileFormat, 1); |
| 189 |
} |
| 190 |
|
| 191 |
void testUInt8(int fileFormat) |
| 192 |
{ |
| 193 |
runTest<uint8_t, AF_SAMPFMT_UNSIGNED, 8>(fileFormat); |
| 194 |
} |
| 195 |
|
| 196 |
void testInt16(int fileFormat) |
| 197 |
{ |
| 198 |
runTest<int16_t, AF_SAMPFMT_TWOSCOMP, 16>(fileFormat); |
| 199 |
} |
| 200 |
|
| 201 |
void testInt16MonoOnly(int fileFormat) |
| 202 |
{ |
| 203 |
runTestWithChannels<int16_t, AF_SAMPFMT_TWOSCOMP, 16>(fileFormat, 1); |
| 204 |
} |
| 205 |
|
| 206 |
void testInt24(int fileFormat) |
| 207 |
{ |
| 208 |
runTest<int32_t, AF_SAMPFMT_TWOSCOMP, 24>(fileFormat); |
| 209 |
} |
| 210 |
|
| 211 |
void testInt32(int fileFormat) |
| 212 |
{ |
| 213 |
runTest<int32_t, AF_SAMPFMT_TWOSCOMP, 32>(fileFormat); |
| 214 |
} |
| 215 |
|
| 216 |
void testFloat32(int fileFormat) |
| 217 |
{ |
| 218 |
runTest<float, AF_SAMPFMT_FLOAT, 32>(fileFormat); |
| 219 |
} |
| 220 |
|
| 221 |
void testFloat64(int fileFormat) |
| 222 |
{ |
| 223 |
runTest<double, AF_SAMPFMT_DOUBLE, 64>(fileFormat); |
| 224 |
} |
| 225 |
|
| 226 |
TEST(AIFF, Int8) { testInt8(AF_FILE_AIFF); } |
| 227 |
TEST(AIFF, Int16) { testInt16(AF_FILE_AIFF); } |
| 228 |
TEST(AIFF, Int24) { testInt24(AF_FILE_AIFF); } |
| 229 |
TEST(AIFF, Int32) { testInt32(AF_FILE_AIFF); } |
| 230 |
|
| 231 |
TEST(AIFFC, Int8) { testInt8(AF_FILE_AIFFC); } |
| 232 |
TEST(AIFFC, Int16) { testInt16(AF_FILE_AIFFC); } |
| 233 |
TEST(AIFFC, Int24) { testInt24(AF_FILE_AIFFC); } |
| 234 |
TEST(AIFFC, Int32) { testInt32(AF_FILE_AIFFC); } |
| 235 |
TEST(AIFFC, Float) { testFloat32(AF_FILE_AIFFC); } |
| 236 |
TEST(AIFFC, Double) { testFloat64(AF_FILE_AIFFC); } |
| 237 |
|
| 238 |
TEST(WAVE, UInt8) { testUInt8(AF_FILE_WAVE); } |
| 239 |
TEST(WAVE, Int16) { testInt16(AF_FILE_WAVE); } |
| 240 |
TEST(WAVE, Int24) { testInt24(AF_FILE_WAVE); } |
| 241 |
TEST(WAVE, Int32) { testInt32(AF_FILE_WAVE); } |
| 242 |
TEST(WAVE, Float) { testFloat32(AF_FILE_WAVE); } |
| 243 |
TEST(WAVE, Double) { testFloat64(AF_FILE_WAVE); } |
| 244 |
|
| 245 |
TEST(NeXT, Int8) { testInt8(AF_FILE_NEXTSND); } |
| 246 |
TEST(NeXT, Int16) { testInt16(AF_FILE_NEXTSND); } |
| 247 |
TEST(NeXT, Int24) { testInt24(AF_FILE_NEXTSND); } |
| 248 |
TEST(NeXT, Int32) { testInt32(AF_FILE_NEXTSND); } |
| 249 |
TEST(NeXT, Float) { testFloat32(AF_FILE_NEXTSND); } |
| 250 |
TEST(NeXT, Double) { testFloat64(AF_FILE_NEXTSND); } |
| 251 |
|
| 252 |
TEST(IRCAM, Int8) { testInt8(AF_FILE_IRCAM); } |
| 253 |
TEST(IRCAM, Int16) { testInt16(AF_FILE_IRCAM); } |
| 254 |
TEST(IRCAM, Int24) { testInt24(AF_FILE_IRCAM); } |
| 255 |
TEST(IRCAM, Int32) { testInt32(AF_FILE_IRCAM); } |
| 256 |
TEST(IRCAM, Float) { testFloat32(AF_FILE_IRCAM); } |
| 257 |
TEST(IRCAM, Double) { testFloat64(AF_FILE_IRCAM); } |
| 258 |
|
| 259 |
TEST(IFF, Int8) { testInt8MonoOnly(AF_FILE_IFF_8SVX); } |
| 260 |
|
| 261 |
TEST(AVR, Int8) { testInt8MonoOnly(AF_FILE_AVR); } |
| 262 |
TEST(AVR, Int16) { testInt16MonoOnly(AF_FILE_AVR); } |
| 263 |
|
| 264 |
TEST(SampleVision, Int16) { testInt16MonoOnly(AF_FILE_SAMPLEVISION); } |
| 265 |
|
| 266 |
TEST(VOC, UInt8) { testUInt8(AF_FILE_VOC); } |
| 267 |
TEST(VOC, Int16) { testInt16(AF_FILE_VOC); } |
| 268 |
|
| 269 |
TEST(NIST, Int16) { testInt16(AF_FILE_NIST_SPHERE); } |
| 270 |
|
| 271 |
TEST(CAF, Int8) { testInt8(AF_FILE_CAF); } |
| 272 |
TEST(CAF, Int16) { testInt16(AF_FILE_CAF); } |
| 273 |
TEST(CAF, Int24) { testInt24(AF_FILE_CAF); } |
| 274 |
TEST(CAF, Int32) { testInt32(AF_FILE_CAF); } |
| 275 |
TEST(CAF, Float) { testFloat32(AF_FILE_CAF); } |
| 276 |
TEST(CAF, Double) { testFloat64(AF_FILE_CAF); } |
| 277 |
|
| 278 |
int main (int argc, char **argv) |
| 279 |
{ |
| 280 |
::testing::InitGoogleTest(&argc, argv); |
| 281 |
return RUN_ALL_TESTS(); |
| 282 |
} |