Exemple #1
0
TEST(NeXT, UnspecifiedLength)
{
	std::string testFileName;
	ASSERT_TRUE(createTemporaryFile("NeXT", &testFileName));

	int fd = ::open(testFileName.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
	ASSERT_GT(fd, -1);
	ASSERT_EQ(::write(fd, kDataUnspecifiedLength, sizeof (kDataUnspecifiedLength)), sizeof (kDataUnspecifiedLength));
	::close(fd);

	AFfilehandle file = afOpenFile(testFileName.c_str(), "r", NULL);
	EXPECT_TRUE(file);

	int sampleFormat, sampleWidth;
	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
	EXPECT_TRUE(sampleFormat == AF_SAMPFMT_TWOSCOMP);
	EXPECT_EQ(sampleWidth, 16);
	EXPECT_EQ(afGetChannels(file, AF_DEFAULT_TRACK), 1);
	EXPECT_EQ(afGetTrackBytes(file, AF_DEFAULT_TRACK),
		kFrameCount * sizeof (int16_t));
	EXPECT_EQ(afGetFrameCount(file, AF_DEFAULT_TRACK), kFrameCount);

	int16_t *data = new int16_t[kFrameCount];
	AFframecount framesRead = afReadFrames(file, AF_DEFAULT_TRACK, data,
		kFrameCount);
	EXPECT_EQ(framesRead, kFrameCount);
	for (int i=0; i<kFrameCount; i++)
		EXPECT_EQ(data[i], kFrames[i]);
	delete [] data;

	afCloseFile(file);

	ASSERT_EQ(::unlink(testFileName.c_str()), 0);
}
Exemple #2
0
bool printfileinfo (const char *filename)
{
	AFfilehandle file = afOpenFile(filename, "r", NULL);
	if (!file)
		return false;

	int fileFormat = afGetFileFormat(file, NULL);
	const char *formatstring =
		(const char *) afQueryPointer(AF_QUERYTYPE_FILEFMT, AF_QUERY_DESC,
			fileFormat, 0, 0);
	const char *labelstring =
		(const char *) afQueryPointer(AF_QUERYTYPE_FILEFMT, AF_QUERY_LABEL,
			fileFormat, 0, 0);

	if (!formatstring || !labelstring)
		return false;

	printf("File Name      %s\n", filename);
	printf("File Format    %s (%s)\n", formatstring, labelstring);

	int sampleFormat, sampleWidth;
	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);

	int byteOrder = afGetByteOrder(file, AF_DEFAULT_TRACK);

	printf("Data Format    ");

	int compressionType = afGetCompression(file, AF_DEFAULT_TRACK);
	if (compressionType == AF_COMPRESSION_NONE)
	{
		switch (sampleFormat)
		{
			case AF_SAMPFMT_TWOSCOMP:
				printf("%d-bit integer (2's complement, %s)",
					sampleWidth,
					byteOrder == AF_BYTEORDER_BIGENDIAN ?
						"big endian" : "little endian");
				break;
			case AF_SAMPFMT_UNSIGNED:
				printf("%d-bit integer (unsigned, %s)",
					sampleWidth,
					byteOrder == AF_BYTEORDER_BIGENDIAN ?
						"big endian" : "little endian");
				break;
			case AF_SAMPFMT_FLOAT:
				printf("single-precision (32-bit) floating point, %s",
					byteOrder == AF_BYTEORDER_BIGENDIAN ?
						"big endian" : "little endian");
				break;
			case AF_SAMPFMT_DOUBLE:
				printf("double-precision (64-bit) floating point, %s",
					byteOrder == AF_BYTEORDER_BIGENDIAN ?
						"big endian" : "little endian");
				break;
			default:
				printf("unknown");
				break;
		}
	}
	else
	{
		const char *compressionName =
			(const char *) afQueryPointer(AF_QUERYTYPE_COMPRESSION,
				AF_QUERY_NAME, compressionType, 0, 0);

		if (!compressionName)
			printf("unknown compression");
		else
			printf("%s compression", compressionName);
	}
	printf("\n");

	printf("Audio Data     %jd bytes begins at offset %jd (%jx hex)\n",
		(intmax_t) afGetTrackBytes(file, AF_DEFAULT_TRACK),
		(intmax_t) afGetDataOffset(file, AF_DEFAULT_TRACK),
		(uintmax_t) afGetDataOffset(file, AF_DEFAULT_TRACK));

	printf("               %d channel%s, %jd frames\n",
		afGetChannels(file, AF_DEFAULT_TRACK),
		afGetChannels(file, AF_DEFAULT_TRACK) > 1 ? "s" : "",
		(intmax_t) afGetFrameCount(file, AF_DEFAULT_TRACK));

	printf("Sampling Rate  %.2f Hz\n", afGetRate(file, AF_DEFAULT_TRACK));

	printf("Duration       %.3f seconds\n",
		afGetFrameCount(file, AF_DEFAULT_TRACK) /
		afGetRate(file, AF_DEFAULT_TRACK));

	char *copyright = copyrightstring(file);
	if (copyright)
	{
		printf("Copyright      %s\n", copyright);
		free(copyright);
	}

	afCloseFile(file);

	return true;
}
Exemple #3
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();
}
Exemple #4
0
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);
}
	Result process()
	{
		if (const YAML::Node *n = m_entry.FindValue(kSkip))
			return kSkipped;

		if (const YAML::Node *n = m_entry.FindValue(kPath))
		{
			n->GetScalar(m_path);
		}
		else
		{
			logerr("no path specified, line %d", n->GetMark().line);
			return kManifestError;
		}

		if (const YAML::Node *n = m_entry.FindValue(kMD5Sum))
		{
			std::string md5 = md5sum(m_path);
			std::string expectedMD5;
			n->GetScalar(expectedMD5);
			if (md5 != expectedMD5)
			{
				logerr("md5 checksum differs from expected value");
				return kFailure;
			}
		}

		AFfilehandle file = afOpenFile(m_path.c_str(), "r", NULL);

		if (const YAML::Node *n = m_entry.FindValue(kInvalid))
		{
			if (!file)
				return kSuccess;
			logerr("opening invalid file did not fail as expected");
			return kFailure;
		}

		if (!file)
		{
			logerr("could not open file");
			return kFailure;
		}

		for (YAML::Iterator i = m_entry.begin(); i != m_entry.end(); ++i)
		{
			std::string key = i.first().to<std::string>();
			std::string value = i.second().to<std::string>();

			if (key == kFileFormat)
			{
				const char *fileFormat =
					(const char *) afQueryPointer(AF_QUERYTYPE_FILEFMT,
						AF_QUERY_LABEL, afGetFileFormat(file, NULL), 0, 0);
				assert(fileFormat);
				expect(key, std::string(fileFormat), value);
			}
			else if (key == kChannels)
			{
				int expectedChannels = atoi(value.c_str());
				expect(key, expectedChannels,
					afGetChannels(file, AF_DEFAULT_TRACK));
			}
			else if (key == kByteOrder)
			{
				int expectedByteOrder;
				if (value == kByteOrder_Big)
					expectedByteOrder = AF_BYTEORDER_BIGENDIAN;
				else if (value == kByteOrder_Little)
					expectedByteOrder = AF_BYTEORDER_LITTLEENDIAN;
				else
				{
					logerr("bad value for byte order: %s, line %d",
						value.c_str(),
						i.second().GetMark().line);
					return kManifestError;
				}

				expect(key, expectedByteOrder,
					afGetByteOrder(file, AF_DEFAULT_TRACK));
			}
			else if (key == kSampleRate)
			{
				double expectedSampleRate = atof(value.c_str());

				expect(key, expectedSampleRate,
					afGetRate(file, AF_DEFAULT_TRACK));
			}
			else if (key == kSampleFormat)
			{
				std::string width = value.substr(1, value.length() - 1);
				char format = value[0];

				int expectedSampleWidth = atoi(width.c_str());
				bool isValidSampleWidth =
					(expectedSampleWidth >= 1 && expectedSampleWidth <= 32) ||
					expectedSampleWidth == 64;
				if (!isValidSampleWidth)
				{
					logerr("bad value for sample format: %s, line %d",
						value.c_str(), i.second().GetMark().line);
					return kManifestError;
				}

				int expectedSampleFormat = -1;
				switch (format)
				{
					case 's':
						expectedSampleFormat = AF_SAMPFMT_TWOSCOMP; break;
					case 'u':
						expectedSampleFormat = AF_SAMPFMT_UNSIGNED; break;
					case 'f':
						if (expectedSampleWidth == 32)
							expectedSampleFormat = AF_SAMPFMT_FLOAT;
						else if (expectedSampleWidth == 64)
							expectedSampleFormat = AF_SAMPFMT_DOUBLE;
						break;
					default:
						logerr("bad value for sample format: %s, line %d",
							value.c_str(), i.second().GetMark().line);
						return kManifestError;
				}

				int sampleFormat, sampleWidth;
				afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
				expect(key, expectedSampleFormat, sampleFormat);
				expect(key, expectedSampleWidth, sampleWidth);
			}
			else if (key == kCompression)
			{
				int expectedCompression;
				if (value == kCompression_None)
					expectedCompression = AF_COMPRESSION_NONE;
				else if (value == kCompression_IMA_ADPCM)
					expectedCompression = AF_COMPRESSION_IMA;
				else if (value == kCompression_MS_ADPCM)
					expectedCompression = AF_COMPRESSION_MS_ADPCM;
				else if (value == kCompression_ulaw)
					expectedCompression = AF_COMPRESSION_G711_ULAW;
				else if (value == kCompression_alaw)
					expectedCompression = AF_COMPRESSION_G711_ALAW;
				else if (value == kCompression_FLAC)
					expectedCompression = AF_COMPRESSION_FLAC;
				else if (value == kCompression_ALAC)
					expectedCompression = AF_COMPRESSION_ALAC;
				else
				{
					logerr("bad value for compression: %s, line %d",
						value.c_str(), i.second().GetMark().line);
					return kManifestError;
				}

				expect(key, expectedCompression,
					afGetCompression(file, AF_DEFAULT_TRACK));
			}
			else if (key == kFrames)
			{
				AFframecount expectedFrameCount = atoll(value.c_str());
				expect(key, expectedFrameCount,
					afGetFrameCount(file, AF_DEFAULT_TRACK));

				int bufferFrameCount = 1024;
				int channels = afGetChannels(file, AF_DEFAULT_TRACK);
				int maxBytesPerFrame = 8;
				char *buffer = new char[channels * bufferFrameCount * maxBytesPerFrame];
				AFframecount framesRead = 0;
				while (framesRead < expectedFrameCount)
				{
					AFframecount framesToRead = std::min<AFframecount>(bufferFrameCount,
						expectedFrameCount - framesRead);
					AFframecount result = afReadFrames(file, AF_DEFAULT_TRACK,
						buffer, framesToRead);
					if (result != framesToRead)
					{
						m_failures++;
						break;
					}
					framesRead += result;
				}
				delete [] buffer;
			}
			else if (key == kBytes)
			{
				AFfileoffset expectedTrackBytes = atoll(value.c_str());
				expect(key, expectedTrackBytes,
					afGetTrackBytes(file, AF_DEFAULT_TRACK));
			}
		}

		afCloseFile(file);

		return m_failures == 0 ? kSuccess : kFailure;
	}