| 1 |
/* |
| 2 |
Audio File Library |
| 3 |
|
| 4 |
Copyright (C) 2012, Michael Pruett <[email protected]> |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <audiofile.h> |
| 22 |
#include <gtest/gtest.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <cstdlib> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include "TestUtilities.h" |
| 29 |
|
| 30 |
static const int kIMABytesPerPacketQT = 34; |
| 31 |
static const int kIMABytesPerPacketWAVE = 256; |
| 32 |
|
| 33 |
static const int kIMAFramesPerPacketQT = 64; |
| 34 |
static const int kIMAFramesPerPacketWAVE = 505; |
| 35 |
|
| 36 |
static const int kIMAThresholdQT = 128; |
| 37 |
static const int kIMAThresholdWAVE = 16; |
| 38 |
|
| 39 |
static const int kMSADPCMBytesPerPacket = 256; |
| 40 |
static const int kMSADPCMFramesPerPacket = 500; |
| 41 |
static const int kMSADPCMThreshold = 16; |
| 42 |
|
| 43 |
static void testADPCM(int fileFormat, int compressionFormat, int channelCount, |
| 44 |
int bytesPerPacket, int framesPerPacket, int frameCount, int threshold) |
| 45 |
{ |
| 46 |
std::string testFileName; |
| 47 |
ASSERT_TRUE(createTemporaryFile("ADPCM", &testFileName)); |
| 48 |
|
| 49 |
AFfilesetup setup = afNewFileSetup(); |
| 50 |
afInitFileFormat(setup, fileFormat); |
| 51 |
afInitChannels(setup, AF_DEFAULT_TRACK, channelCount); |
| 52 |
afInitCompression(setup, AF_DEFAULT_TRACK, compressionFormat); |
| 53 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 54 |
ASSERT_TRUE(file); |
| 55 |
afFreeFileSetup(setup); |
| 56 |
|
| 57 |
int16_t *data = new int16_t[frameCount * channelCount]; |
| 58 |
for (int i=0; i<frameCount; i++) |
| 59 |
for (int c=0; c<channelCount; c++) |
| 60 |
data[i*channelCount + c] = i * ((c&1) ? -1 : 1); |
| 61 |
|
| 62 |
AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, data, frameCount); |
| 63 |
ASSERT_EQ(framesWritten, frameCount); |
| 64 |
|
| 65 |
ASSERT_EQ(afCloseFile(file), 0); |
| 66 |
|
| 67 |
file = afOpenFile(testFileName.c_str(), "r", AF_NULL_FILESETUP); |
| 68 |
ASSERT_TRUE(file); |
| 69 |
ASSERT_EQ(afGetCompression(file, AF_DEFAULT_TRACK), compressionFormat); |
| 70 |
ASSERT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), frameCount); |
| 71 |
ASSERT_EQ(afGetTrackBytes(file, AF_DEFAULT_TRACK), |
| 72 |
(bytesPerPacket * frameCount) / framesPerPacket); |
| 73 |
|
| 74 |
int16_t *readData = new int16_t[frameCount * channelCount]; |
| 75 |
AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, frameCount); |
| 76 |
ASSERT_EQ(framesRead, frameCount); |
| 77 |
|
| 78 |
for (int i=0; i<frameCount; i++) |
| 79 |
for (int c=0; c<channelCount; c++) |
| 80 |
EXPECT_LE(std::abs(data[i*channelCount + c] - readData[i*channelCount + c]), threshold); |
| 81 |
|
| 82 |
int16_t *offsetReadData = new int16_t[frameCount * channelCount]; |
| 83 |
|
| 84 |
// Read entire file with a seek before each read operation. |
| 85 |
for (AFframecount offset = 0; offset < frameCount; offset += framesPerPacket + 3) |
| 86 |
{ |
| 87 |
ASSERT_EQ(afSeekFrame(file, AF_DEFAULT_TRACK, offset), offset); |
| 88 |
|
| 89 |
AFframecount framesToRead = 1091; |
| 90 |
framesRead = afReadFrames(file, AF_DEFAULT_TRACK, offsetReadData, framesToRead); |
| 91 |
ASSERT_EQ(framesRead, std::min(framesToRead, frameCount - offset)); |
| 92 |
|
| 93 |
for (int i=0; i<framesRead; i++) |
| 94 |
for (int c=0; c<channelCount; c++) |
| 95 |
EXPECT_EQ(readData[(i+offset)*channelCount + c], |
| 96 |
offsetReadData[i*channelCount + c]); |
| 97 |
} |
| 98 |
|
| 99 |
// Read entire file sequentially in multiple read operations. |
| 100 |
ASSERT_EQ(afSeekFrame(file, AF_DEFAULT_TRACK, 0), 0); |
| 101 |
|
| 102 |
AFframecount framesToRead = 1087; |
| 103 |
for (AFframecount offset = 0; offset < frameCount; offset += framesToRead) |
| 104 |
{ |
| 105 |
framesRead = afReadFrames(file, AF_DEFAULT_TRACK, offsetReadData, framesToRead); |
| 106 |
ASSERT_EQ(framesRead, std::min(framesToRead, frameCount - offset)); |
| 107 |
|
| 108 |
for (int i=0; i<framesRead; i++) |
| 109 |
for (int c=0; c<channelCount; c++) |
| 110 |
EXPECT_EQ(readData[(i+offset)*channelCount + c], |
| 111 |
offsetReadData[i*channelCount + c]); |
| 112 |
} |
| 113 |
|
| 114 |
ASSERT_EQ(afCloseFile(file), 0); |
| 115 |
|
| 116 |
delete [] data; |
| 117 |
delete [] readData; |
| 118 |
delete [] offsetReadData; |
| 119 |
|
| 120 |
ASSERT_EQ(::unlink(testFileName.c_str()), 0); |
| 121 |
} |
| 122 |
|
| 123 |
TEST(IMA, CAF) |
| 124 |
{ |
| 125 |
for (int channelCount=1; channelCount<=2; channelCount++) |
| 126 |
testADPCM(AF_FILE_CAF, AF_COMPRESSION_IMA, channelCount, |
| 127 |
channelCount * kIMABytesPerPacketQT, kIMAFramesPerPacketQT, |
| 128 |
500 * kIMAFramesPerPacketQT, kIMAThresholdQT); |
| 129 |
} |
| 130 |
|
| 131 |
TEST(IMA, AIFFC) |
| 132 |
{ |
| 133 |
for (int channelCount=1; channelCount<=2; channelCount++) |
| 134 |
testADPCM(AF_FILE_AIFFC, AF_COMPRESSION_IMA, channelCount, |
| 135 |
channelCount * kIMABytesPerPacketQT, kIMAFramesPerPacketQT, |
| 136 |
500 * kIMAFramesPerPacketQT, kIMAThresholdQT); |
| 137 |
} |
| 138 |
|
| 139 |
TEST(IMA, WAVE) |
| 140 |
{ |
| 141 |
for (int channelCount=1; channelCount<=2; channelCount++) |
| 142 |
testADPCM(AF_FILE_WAVE, AF_COMPRESSION_IMA, channelCount, |
| 143 |
channelCount * kIMABytesPerPacketWAVE, kIMAFramesPerPacketWAVE, |
| 144 |
50 * kIMAFramesPerPacketWAVE, kIMAThresholdWAVE); |
| 145 |
} |
| 146 |
|
| 147 |
TEST(MSADPCM, WAVE) |
| 148 |
{ |
| 149 |
for (int channelCount=1; channelCount<=2; channelCount++) |
| 150 |
testADPCM(AF_FILE_WAVE, AF_COMPRESSION_MS_ADPCM, channelCount, |
| 151 |
channelCount * kMSADPCMBytesPerPacket, kMSADPCMFramesPerPacket, |
| 152 |
50 * kMSADPCMFramesPerPacket, kMSADPCMThreshold); |
| 153 |
} |
| 154 |
|
| 155 |
int main(int argc, char **argv) |
| 156 |
{ |
| 157 |
::testing::InitGoogleTest(&argc, argv); |
| 158 |
return RUN_ALL_TESTS(); |
| 159 |
} |