static void testInvalidSampleFormat(int sampleFormat, int sampleWidth) { std::string testFileName; ASSERT_TRUE(createTemporaryFile("ALAC", &testFileName)); AFfilesetup setup = afNewFileSetup(); afInitFileFormat(setup, AF_FILE_CAF); afInitChannels(setup, AF_DEFAULT_TRACK, 1); afInitSampleFormat(setup, AF_DEFAULT_TRACK, sampleFormat, sampleWidth); afInitCompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_ALAC); AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); ASSERT_FALSE(file); afFreeFileSetup(setup); ASSERT_EQ(::unlink(testFileName.c_str()), 0); }
TEST(ALAC, InvalidChannels) { IgnoreErrors ignoreErrors; std::string testFileName; ASSERT_TRUE(createTemporaryFile("ALAC", &testFileName)); AFfilesetup setup = afNewFileSetup(); afInitFileFormat(setup, AF_FILE_CAF); afInitChannels(setup, AF_DEFAULT_TRACK, 9); afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); afInitCompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_ALAC); AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); ASSERT_FALSE(file); afFreeFileSetup(setup); ASSERT_EQ(::unlink(testFileName.c_str()), 0); }
void testalaw (int fileFormat) { AFfilehandle file; AFfilesetup setup; uint16_t samples[] = {8, 24, 88, 120, 184, 784, 912, 976, 1120, 1440, 1888, 8960, 9984, 16128, 19968, 32256}; uint16_t readsamples[SAMPLE_COUNT]; AFframecount framesWritten, framesRead; int i; setup = afNewFileSetup(); afInitCompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_G711_ALAW); afInitFileFormat(setup, fileFormat); afInitChannels(setup, AF_DEFAULT_TRACK, 1); file = afOpenFile(TEST_FILE, "w", setup); afFreeFileSetup(setup); ensure(afGetCompression(file, AF_DEFAULT_TRACK) == AF_COMPRESSION_G711_ALAW, "test file not created with G.711 A-law compression"); ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing"); framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples, FRAME_COUNT); ensure(framesWritten == FRAME_COUNT, "number of frames requested does not match number of frames written"); afCloseFile(file); /* Open the file for reading and verify the data. */ file = afOpenFile(TEST_FILE, "r", NULL); ensure(file != AF_NULL_FILEHANDLE, "unable to open file for reading"); ensure(afGetFileFormat(file, NULL) == fileFormat, "test file format incorrect"); ensure(afGetCompression(file, AF_DEFAULT_TRACK) == AF_COMPRESSION_G711_ALAW, "test file not opened with G.711 A-law compression"); framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readsamples, FRAME_COUNT); ensure(framesRead == FRAME_COUNT, "number of frames read does not match number of frames requested"); #ifdef DEBUG for (i=0; i<SAMPLE_COUNT; i++) printf("readsamples[%d]: %d\n", i, readsamples[i]); for (i=0; i<SAMPLE_COUNT; i++) printf("samples[%d]: %d\n", i, samples[i]); #endif for (i=0; i<SAMPLE_COUNT; i++) { ensure(samples[i] == readsamples[i], "data written does not match data read"); } /* G.711 compression uses one byte per sample. */ ensure(afGetTrackBytes(file, AF_DEFAULT_TRACK) == SAMPLE_COUNT, "track byte count is incorrect"); ensure(afGetFrameCount(file, AF_DEFAULT_TRACK) == FRAME_COUNT, "frame count is incorrect"); ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 1, "channel count is incorrect"); ensure(afCloseFile(file) == 0, "error closing file"); cleanup(); }
static void testADPCM(int fileFormat, int compressionFormat, int channelCount, int bytesPerPacket, int framesPerPacket, int frameCount, int threshold) { std::string testFileName; ASSERT_TRUE(createTemporaryFile("ADPCM", &testFileName)); AFfilesetup setup = afNewFileSetup(); afInitFileFormat(setup, fileFormat); afInitChannels(setup, AF_DEFAULT_TRACK, channelCount); afInitCompression(setup, AF_DEFAULT_TRACK, compressionFormat); AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup); ASSERT_TRUE(file); afFreeFileSetup(setup); int16_t *data = new int16_t[frameCount * channelCount]; for (int i=0; i<frameCount; i++) for (int c=0; c<channelCount; c++) data[i*channelCount + c] = i * ((c&1) ? -1 : 1); AFframecount framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, data, frameCount); ASSERT_EQ(framesWritten, frameCount); ASSERT_EQ(afCloseFile(file), 0); file = afOpenFile(testFileName.c_str(), "r", AF_NULL_FILESETUP); ASSERT_TRUE(file); ASSERT_EQ(afGetCompression(file, AF_DEFAULT_TRACK), compressionFormat); ASSERT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), frameCount); ASSERT_EQ(afGetTrackBytes(file, AF_DEFAULT_TRACK), (bytesPerPacket * frameCount) / framesPerPacket); int16_t *readData = new int16_t[frameCount * channelCount]; AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readData, frameCount); ASSERT_EQ(framesRead, frameCount); for (int i=0; i<frameCount; i++) for (int c=0; c<channelCount; c++) EXPECT_LE(std::abs(data[i*channelCount + c] - readData[i*channelCount + c]), threshold); int16_t *offsetReadData = new int16_t[frameCount * channelCount]; // Read entire file with a seek before each read operation. for (AFframecount offset = 0; offset < frameCount; offset += framesPerPacket + 3) { ASSERT_EQ(afSeekFrame(file, AF_DEFAULT_TRACK, offset), offset); AFframecount framesToRead = 1091; framesRead = afReadFrames(file, AF_DEFAULT_TRACK, offsetReadData, framesToRead); ASSERT_EQ(framesRead, std::min(framesToRead, frameCount - offset)); for (int i=0; i<framesRead; i++) for (int c=0; c<channelCount; c++) EXPECT_EQ(readData[(i+offset)*channelCount + c], offsetReadData[i*channelCount + c]); } // Read entire file sequentially in multiple read operations. ASSERT_EQ(afSeekFrame(file, AF_DEFAULT_TRACK, 0), 0); AFframecount framesToRead = 1087; for (AFframecount offset = 0; offset < frameCount; offset += framesToRead) { framesRead = afReadFrames(file, AF_DEFAULT_TRACK, offsetReadData, framesToRead); ASSERT_EQ(framesRead, std::min(framesToRead, frameCount - offset)); for (int i=0; i<framesRead; i++) for (int c=0; c<channelCount; c++) EXPECT_EQ(readData[(i+offset)*channelCount + c], offsetReadData[i*channelCount + c]); } ASSERT_EQ(afCloseFile(file), 0); delete [] data; delete [] readData; delete [] offsetReadData; ASSERT_EQ(::unlink(testFileName.c_str()), 0); }