| 1 |
/* |
| 2 |
Copyright (C) 2011, 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 |
#include <audiofile.h> |
| 31 |
#include <gtest/gtest.h> |
| 32 |
#include <string> |
| 33 |
|
| 34 |
#include "TestUtilities.h" |
| 35 |
|
| 36 |
template <typename T> |
| 37 |
void testChannelMatrixReading(int sampleFormat, int sampleWidth) |
| 38 |
{ |
| 39 |
// Create test file. |
| 40 |
std::string testFileName; |
| 41 |
ASSERT_TRUE(createTemporaryFile("ChannelMatrix", &testFileName)); |
| 42 |
|
| 43 |
const int channelCount = 2; |
| 44 |
const int frameCount = 10; |
| 45 |
const T samples[channelCount * frameCount] = |
| 46 |
{ |
| 47 |
2, 3, 5, 7, 11, |
| 48 |
13, 17, 19, 23, 29, |
| 49 |
31, 37, 41, 43, 47, |
| 50 |
53, 59, 61, 67, 71 |
| 51 |
}; |
| 52 |
AFfilesetup setup = afNewFileSetup(); |
| 53 |
afInitFileFormat(setup, AF_FILE_AIFFC); |
| 54 |
afInitChannels(setup, AF_DEFAULT_TRACK, 2); |
| 55 |
afInitSampleFormat(setup, AF_DEFAULT_TRACK, sampleFormat, sampleWidth); |
| 56 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 57 |
afFreeFileSetup(setup); |
| 58 |
EXPECT_TRUE(file); |
| 59 |
|
| 60 |
AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, |
| 61 |
samples, frameCount); |
| 62 |
EXPECT_EQ(framesWritten, frameCount); |
| 63 |
|
| 64 |
EXPECT_EQ(afCloseFile(file), 0); |
| 65 |
|
| 66 |
// Open file for reading and read data using different channel matrices. |
| 67 |
file = afOpenFile(testFileName.c_str(), "r", NULL); |
| 68 |
EXPECT_TRUE(file); |
| 69 |
|
| 70 |
EXPECT_EQ(afGetChannels(file, AF_DEFAULT_TRACK), 2); |
| 71 |
EXPECT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), frameCount); |
| 72 |
|
| 73 |
afSetVirtualChannels(file, AF_DEFAULT_TRACK, 1); |
| 74 |
|
| 75 |
for (int c=0; c<2; c++) |
| 76 |
{ |
| 77 |
double channelMatrix[2] = { 0, 0 }; |
| 78 |
channelMatrix[c] = 1; |
| 79 |
afSetChannelMatrix(file, AF_DEFAULT_TRACK, channelMatrix); |
| 80 |
|
| 81 |
EXPECT_EQ(afSeekFrame(file, AF_DEFAULT_TRACK, 0), 0); |
| 82 |
|
| 83 |
T *readSamples = new T[frameCount]; |
| 84 |
AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, |
| 85 |
readSamples, frameCount); |
| 86 |
EXPECT_EQ(framesRead, frameCount); |
| 87 |
|
| 88 |
for (int i=0; i<frameCount; i++) |
| 89 |
EXPECT_EQ(readSamples[i], samples[2*i + c]); |
| 90 |
|
| 91 |
delete [] readSamples; |
| 92 |
} |
| 93 |
|
| 94 |
EXPECT_EQ(afCloseFile(file), 0); |
| 95 |
|
| 96 |
ASSERT_EQ(::unlink(testFileName.c_str()), 0); |
| 97 |
} |
| 98 |
|
| 99 |
TEST(ChannelMatrix, ReadInt8) |
| 100 |
{ |
| 101 |
testChannelMatrixReading<int8_t>(AF_SAMPFMT_TWOSCOMP, 8); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(ChannelMatrix, ReadInt16) |
| 105 |
{ |
| 106 |
testChannelMatrixReading<int16_t>(AF_SAMPFMT_TWOSCOMP, 16); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(ChannelMatrix, ReadInt24) |
| 110 |
{ |
| 111 |
testChannelMatrixReading<int32_t>(AF_SAMPFMT_TWOSCOMP, 24); |
| 112 |
} |
| 113 |
|
| 114 |
TEST(ChannelMatrix, ReadInt32) |
| 115 |
{ |
| 116 |
testChannelMatrixReading<int32_t>(AF_SAMPFMT_TWOSCOMP, 32); |
| 117 |
} |
| 118 |
|
| 119 |
TEST(ChannelMatrix, ReadFloat) |
| 120 |
{ |
| 121 |
testChannelMatrixReading<float>(AF_SAMPFMT_FLOAT, 32); |
| 122 |
} |
| 123 |
|
| 124 |
TEST(ChannelMatrix, ReadDouble) |
| 125 |
{ |
| 126 |
testChannelMatrixReading<double>(AF_SAMPFMT_DOUBLE, 64); |
| 127 |
} |
| 128 |
|
| 129 |
template <typename T> |
| 130 |
void testChannelMatrixWriting(int sampleFormat, int sampleWidth) |
| 131 |
{ |
| 132 |
const int channelCount = 2; |
| 133 |
const int frameCount = 10; |
| 134 |
const T samples[channelCount * frameCount] = |
| 135 |
{ |
| 136 |
2, 3, 5, 7, 11, |
| 137 |
13, 17, 19, 23, 29, |
| 138 |
31, 37, 41, 43, 47, |
| 139 |
53, 59, 61, 67, 71 |
| 140 |
}; |
| 141 |
|
| 142 |
for (int c=0; c<2; c++) |
| 143 |
{ |
| 144 |
// Create test file. |
| 145 |
std::string testFileName; |
| 146 |
ASSERT_TRUE(createTemporaryFile("ChannelMatrix", &testFileName)); |
| 147 |
|
| 148 |
AFfilesetup setup = afNewFileSetup(); |
| 149 |
afInitFileFormat(setup, AF_FILE_AIFFC); |
| 150 |
afInitChannels(setup, AF_DEFAULT_TRACK, 1); |
| 151 |
afInitSampleFormat(setup, AF_DEFAULT_TRACK, sampleFormat, sampleWidth); |
| 152 |
AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); |
| 153 |
afFreeFileSetup(setup); |
| 154 |
EXPECT_TRUE(file); |
| 155 |
|
| 156 |
afSetVirtualChannels(file, AF_DEFAULT_TRACK, 2); |
| 157 |
|
| 158 |
double channelMatrix[2] = { 0, 0 }; |
| 159 |
channelMatrix[c] = 1; |
| 160 |
afSetChannelMatrix(file, AF_DEFAULT_TRACK, channelMatrix); |
| 161 |
|
| 162 |
AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, |
| 163 |
samples, frameCount); |
| 164 |
EXPECT_EQ(framesWritten, frameCount); |
| 165 |
|
| 166 |
EXPECT_EQ(afCloseFile(file), 0); |
| 167 |
|
| 168 |
// Open file for reading. |
| 169 |
file = afOpenFile(testFileName.c_str(), "r", NULL); |
| 170 |
EXPECT_TRUE(file); |
| 171 |
|
| 172 |
EXPECT_EQ(afGetChannels(file, AF_DEFAULT_TRACK), 1); |
| 173 |
EXPECT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), frameCount); |
| 174 |
|
| 175 |
T *readSamples = new T[frameCount]; |
| 176 |
AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, |
| 177 |
readSamples, frameCount); |
| 178 |
EXPECT_EQ(framesRead, frameCount); |
| 179 |
|
| 180 |
for (int i=0; i<frameCount; i++) |
| 181 |
EXPECT_EQ(readSamples[i], samples[2*i + c]); |
| 182 |
|
| 183 |
delete [] readSamples; |
| 184 |
|
| 185 |
EXPECT_EQ(afCloseFile(file), 0); |
| 186 |
|
| 187 |
ASSERT_EQ(::unlink(testFileName.c_str()), 0); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
TEST(ChannelMatrix, WriteInt8) |
| 192 |
{ |
| 193 |
testChannelMatrixWriting<int8_t>(AF_SAMPFMT_TWOSCOMP, 8); |
| 194 |
} |
| 195 |
|
| 196 |
TEST(ChannelMatrix, WriteInt16) |
| 197 |
{ |
| 198 |
testChannelMatrixWriting<int16_t>(AF_SAMPFMT_TWOSCOMP, 16); |
| 199 |
} |
| 200 |
|
| 201 |
TEST(ChannelMatrix, WriteInt24) |
| 202 |
{ |
| 203 |
testChannelMatrixWriting<int32_t>(AF_SAMPFMT_TWOSCOMP, 24); |
| 204 |
} |
| 205 |
|
| 206 |
TEST(ChannelMatrix, WriteInt32) |
| 207 |
{ |
| 208 |
testChannelMatrixWriting<int32_t>(AF_SAMPFMT_TWOSCOMP, 32); |
| 209 |
} |
| 210 |
|
| 211 |
TEST(ChannelMatrix, WriteFloat) |
| 212 |
{ |
| 213 |
testChannelMatrixWriting<float>(AF_SAMPFMT_FLOAT, 32); |
| 214 |
} |
| 215 |
|
| 216 |
TEST(ChannelMatrix, WriteDouble) |
| 217 |
{ |
| 218 |
testChannelMatrixWriting<double>(AF_SAMPFMT_DOUBLE, 64); |
| 219 |
} |
| 220 |
|
| 221 |
int main(int argc, char **argv) |
| 222 |
{ |
| 223 |
::testing::InitGoogleTest(&argc, argv); |
| 224 |
return RUN_ALL_TESTS(); |
| 225 |
} |