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
void testfloat (int fileFormat)
{
	AFfilesetup	setup;
	AFfilehandle	file;
	int		framesWritten, framesRead;
	const int 	frameCount = SAMPLE_COUNT/2;
	float		readsamples[SAMPLE_COUNT];
	int		i;
	int		sampleFormat, sampleWidth;

	setup = afNewFileSetup();

	afInitFileFormat(setup, fileFormat);
	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_FLOAT, 32);
	afInitChannels(setup, AF_DEFAULT_TRACK, 2);

	ensure(createTemporaryFile("testfloat", &sTestFileName),
		"could not create temporary file");
	file = afOpenFile(sTestFileName, "w", setup);
	ensure(file != AF_NULL_FILEHANDLE, "could not open file for writing");

	afFreeFileSetup(setup);

	framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples,
		frameCount);
	ensure(framesWritten == frameCount, "number of frames written does not match number of frames requested");

	ensure(afCloseFile(file) == 0, "error closing file");

	file = afOpenFile(sTestFileName, "r", AF_NULL_FILESETUP);
	ensure(file != AF_NULL_FILEHANDLE, "could not open file for reading");

	ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 2,
		"file doesn't have exactly two channels");
	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
	ensure(sampleFormat == AF_SAMPFMT_FLOAT && sampleWidth == 32,
		"file doesn't contain 32-bit floating-point data");
	ensure(afGetFileFormat(file, NULL) == fileFormat,
		"file format doesn't match format requested");

	framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readsamples,
		frameCount);
	ensure(framesRead == frameCount, "number of frames read does not match number of frames requested");

	for (i=0; i<SAMPLE_COUNT; i++)
	{
		ensure(readsamples[i] == samples[i],
			"data written to file doesn't match data read");
	}

	ensure(afCloseFile(file) == 0, "error closing file");

	cleanup();
}
static enum sample_format
audiofile_setup_sample_format(AFfilehandle af_fp)
{
	int fs, bits;

	afGetSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);
	if (!audio_valid_sample_format(audiofile_bits_to_sample_format(bits))) {
		g_debug("input file has %d bit samples, converting to 16",
			bits);
		bits = 16;
	}

	afSetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK,
	                         AF_SAMPFMT_TWOSCOMP, bits);
	afGetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);

	return audiofile_bits_to_sample_format(bits);
}
Exemple #4
0
int
SoundSampleGetData(const char *file, SoundSampleData * ssd)
{
   AFfilehandle        in_file;
   int                 in_format, in_width, bytes_per_frame, frame_count;
   int                 frames_read;

   in_file = afOpenFile(file, "r", NULL);
   if (!in_file)
      return -1;

   frame_count = afGetFrameCount(in_file, AF_DEFAULT_TRACK);
   ssd->channels = afGetChannels(in_file, AF_DEFAULT_TRACK);
   ssd->rate = (unsigned int)(afGetRate(in_file, AF_DEFAULT_TRACK) + .5);
   afGetSampleFormat(in_file, AF_DEFAULT_TRACK, &in_format, &in_width);
   ssd->bit_per_sample = in_width;
#ifdef WORDS_BIGENDIAN
   afSetVirtualByteOrder(in_file, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN);
#else
   afSetVirtualByteOrder(in_file, AF_DEFAULT_TRACK, AF_BYTEORDER_LITTLEENDIAN);
#endif
   if (EDebug(EDBUG_TYPE_SOUND))
      Eprintf("SoundSampleGetData chan=%d width=%d rate=%d\n", ssd->channels,
	      ssd->bit_per_sample, ssd->rate);

   bytes_per_frame = (ssd->bit_per_sample * ssd->channels) / 8;
   ssd->size = frame_count * bytes_per_frame;
   ssd->data = EMALLOC(unsigned char, ssd->size);

   frames_read =
      afReadFrames(in_file, AF_DEFAULT_TRACK, ssd->data, frame_count);

   afCloseFile(in_file);

   if (frames_read <= 0)
     {
	ssd->size = 0;
	_EFREE(ssd->data);
	return -1;
     }

   return 0;
}
Exemple #5
0
/*
    0.73s 44100.00 aiff  1ch 16b -- flute.aif
*/
bool printshortinfo (const char *filename)
{
	AFfilehandle file = afOpenFile(filename, "r", NULL);;
	if (!file)
		return false;

	int fileFormat = afGetFileFormat(file, NULL);
	double sampleRate = afGetRate(file, AF_DEFAULT_TRACK);
	double duration = afGetFrameCount(file, AF_DEFAULT_TRACK) / sampleRate;
	const char *labelString =
		(const char *) afQueryPointer(AF_QUERYTYPE_FILEFMT, AF_QUERY_LABEL,
			fileFormat, 0, 0);
	int channels = afGetChannels(file, AF_DEFAULT_TRACK);
	int sampleFormat, sampleWidth;
	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);

	int compressionType = afGetCompression(file, AF_DEFAULT_TRACK);
	const char *compressionName = "--";
	if (compressionType != AF_COMPRESSION_NONE)
	{
		compressionName =
			(const char *) afQueryPointer(AF_QUERYTYPE_COMPRESSION,
				AF_QUERY_NAME, compressionType, 0, 0);
		if (!compressionName)
			compressionName = "unk";
	}

	printf("%8.2fs %8.2f %4s %2dch %2db %s %s\n",
		duration,
		sampleRate,
		labelString,
		channels,
		sampleWidth,
		compressionName,
		filename);

	afCloseFile(file);

	return true;
}
Exemple #6
0
int main (int argc, char **argv)
{
	AFfilehandle	file;
	AFfilesetup	setup;
	int8_t		samples[] = {11, 51, 101, -101, -54, 120, -15, 99};
	int		i;
	int		sampleFormat, sampleWidth;
	int		framesRead, framesWritten;

	setup = afNewFileSetup();
	afInitFileFormat(setup, AF_FILE_IFF_8SVX);
	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 8);

	file = afOpenFile(TEST_FILE, "w", setup);
	ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing");

	framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples, 7);
	ensure(framesWritten == 7,
		"number of frames written does not match number of frames requested");

	ensure(afCloseFile(file) == 0, "error closing file");
	afFreeFileSetup(setup);

	file = afOpenFile(TEST_FILE, "r", NULL);
	ensure(file != AF_NULL_FILEHANDLE, "unable to open file for reading");

	ensure(afGetFileFormat(file, NULL) == AF_FILE_IFF_8SVX,
		"test file not created as IFF/8SVX");

	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
	ensure(sampleFormat == AF_SAMPFMT_TWOSCOMP,
		"test file not two's complement");
	ensure(sampleWidth == 8,
		"test file sample format is not 8-bit");

	ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 1,
		"test file doesn't have exactly one channel");

	ensure(afGetByteOrder(file, AF_DEFAULT_TRACK) == AF_BYTEORDER_BIGENDIAN,
		"test file not big-endian");

	for (i=0; i<7; i++)
	{
		int8_t	temporary;

		framesRead = afReadFrames(file, AF_DEFAULT_TRACK, &temporary, 1);
		ensure(framesRead == 1,
			"number of frames read does not match number of frames requested");

		ensure(temporary == samples[i],
			"data written to file doesn't match data read from file");
	}

	ensure(afCloseFile(file) == 0, "error closing file");

	cleanup();

	printf("writeiff test passed.\n");

	exit(0);
}
Exemple #7
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 #8
0
static void
audiofile_stream_decode(struct decoder *decoder, struct input_stream *is)
{
	AFvirtualfile *vf;
	int fs, frame_count;
	AFfilehandle af_fp;
	int bits;
	struct audio_format audio_format;
	float total_time;
	uint16_t bit_rate;
	int ret, current = 0;
	char chunk[CHUNK_SIZE];
	enum decoder_command cmd;

	if (!is->seekable) {
		g_warning("not seekable");
		return;
	}

	vf = setup_virtual_fops(is);

	af_fp = afOpenVirtualFile(vf, "r", NULL);
	if (af_fp == AF_NULL_FILEHANDLE) {
		g_warning("failed to input stream\n");
		return;
	}

	afGetSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);
	if (!audio_valid_sample_format(bits)) {
		g_debug("input file has %d bit samples, converting to 16",
			bits);
		bits = 16;
	}

	afSetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK,
	                         AF_SAMPFMT_TWOSCOMP, bits);
	afGetVirtualSampleFormat(af_fp, AF_DEFAULT_TRACK, &fs, &bits);
	audio_format.bits = (uint8_t)bits;
	audio_format.sample_rate =
	                      (unsigned int)afGetRate(af_fp, AF_DEFAULT_TRACK);
	audio_format.channels =
	              (uint8_t)afGetVirtualChannels(af_fp, AF_DEFAULT_TRACK);

	if (!audio_format_valid(&audio_format)) {
		g_warning("Invalid audio format: %u:%u:%u\n",
			  audio_format.sample_rate, audio_format.bits,
			  audio_format.channels);
		afCloseFile(af_fp);
		return;
	}

	frame_count = afGetFrameCount(af_fp, AF_DEFAULT_TRACK);

	total_time = ((float)frame_count / (float)audio_format.sample_rate);

	bit_rate = (uint16_t)(is->size * 8.0 / total_time / 1000.0 + 0.5);

	fs = (int)afGetVirtualFrameSize(af_fp, AF_DEFAULT_TRACK, 1);

	decoder_initialized(decoder, &audio_format, true, total_time);

	do {
		ret = afReadFrames(af_fp, AF_DEFAULT_TRACK, chunk,
				   CHUNK_SIZE / fs);
		if (ret <= 0)
			break;

		current += ret;
		cmd = decoder_data(decoder, NULL,
				   chunk, ret * fs,
				   (float)current /
				   (float)audio_format.sample_rate,
				   bit_rate, NULL);

		if (cmd == DECODE_COMMAND_SEEK) {
			current = decoder_seek_where(decoder) *
				audio_format.sample_rate;
			afSeekFrame(af_fp, AF_DEFAULT_TRACK, current);

			decoder_command_finished(decoder);
			cmd = DECODE_COMMAND_NONE;
		}
	} while (cmd == DECODE_COMMAND_NONE);

	afCloseFile(af_fp);
}
	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;
	}
Exemple #10
0
int main (int argc, char **argv)
{
	int	i = 1;
	char	*infilename, *outfilename;
	int	fileFormat, outFileFormat = AF_FILE_UNKNOWN;

	AFfilehandle	infile, outfile;
	AFfilesetup	outfilesetup;
	int		sampleFormat, sampleWidth, channelCount;
	double		sampleRate;
	int		outSampleFormat = -1, outSampleWidth = -1,
			outChannelCount = -1;
	double		outMaxAmp = 1.0;

	AFframecount	totalFrames;

	if (argc == 2)
	{
		if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v"))
		{
			printversion();
			exit(EXIT_SUCCESS);
		}

		if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
		{
			printusage();
			exit(EXIT_SUCCESS);
		}
	}

	if (argc < 3)
		usageerror();

	infilename = argv[1];
	outfilename = argv[2];

	i = 3;

	while (i < argc)
	{
		if (!strcmp(argv[i], "format"))
		{
			if (i + 1 >= argc)
				usageerror();
			if (!strcmp(argv[i+1], "aiff"))
				outFileFormat = AF_FILE_AIFF;
			else if (!strcmp(argv[i+1], "aifc"))
				outFileFormat = AF_FILE_AIFFC;
			else if (!strcmp(argv[i+1], "wave"))
				outFileFormat = AF_FILE_WAVE;
			else if (!strcmp(argv[i+1], "next"))
				outFileFormat = AF_FILE_NEXTSND;
			else if (!strcmp(argv[i+1], "bics"))
				outFileFormat = AF_FILE_BICSF;
			else if (!strcmp(argv[i+1], "voc"))
				outFileFormat = AF_FILE_VOC;
			else if (!strcmp(argv[i+1], "nist"))
				outFileFormat = AF_FILE_NIST_SPHERE;
			else if (!strcmp(argv[i+1], "caf"))
				outFileFormat = AF_FILE_CAF;
			else
			{
				fprintf(stderr, "sfconvert: Unknown format %s.\n", argv[i+1]);
				exit(EXIT_FAILURE);
			}

			/* Increment for argument. */
			i++;
		}
		else if (!strcmp(argv[i], "channels"))
		{
			if (i + 1 >= argc)
				usageerror();

			outChannelCount = atoi(argv[i+1]);
			if (outChannelCount < 1)
				usageerror();

			/* Increment for argument. */
			i++;
		}
		else if (!strcmp(argv[i], "float"))
		{
			if (i + 1 >= argc)
				usageerror();

			outSampleFormat = AF_SAMPFMT_FLOAT;
			outSampleWidth = 32;
			outMaxAmp = atof(argv[i+1]);

			/* Increment for argument. */
			i++;
		}
		else if (!strcmp(argv[i], "integer"))
		{
			if (i + 2 >= argc)
				usageerror();

			outSampleWidth = atoi(argv[i+1]);
			if (outSampleWidth < 1 || outSampleWidth > 32)
				usageerror();

			if (!strcmp(argv[i+2], "2scomp"))
				outSampleFormat = AF_SAMPFMT_TWOSCOMP;
			else if (!strcmp(argv[i+2], "unsigned"))
				outSampleFormat = AF_SAMPFMT_UNSIGNED;
			else
				usageerror();

			/* Increment for arguments. */
			i += 2;
		}
		else
		{
			printf("Unrecognized command %s\n", argv[i]);
		}

		i++;
	}

	infile = afOpenFile(infilename, "r", AF_NULL_FILESETUP);
	if (infile == AF_NULL_FILEHANDLE)
	{
		printf("Could not open file '%s' for reading.\n", infilename);
		return 1;
	}

	/* Get audio format parameters from input file. */
	fileFormat = afGetFileFormat(infile, NULL);
	totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
	channelCount = afGetChannels(infile, AF_DEFAULT_TRACK);
	sampleRate = afGetRate(infile, AF_DEFAULT_TRACK);
	afGetSampleFormat(infile, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);

	/* Initialize output audio format parameters. */
	outfilesetup = afNewFileSetup();

	if (outFileFormat == -1)
		outFileFormat = fileFormat;

	if (outSampleFormat == -1 || outSampleWidth == -1)
	{
		outSampleFormat = sampleFormat;
		outSampleWidth = sampleWidth;
	}

	if (outChannelCount == -1)
		outChannelCount = channelCount;

	afInitFileFormat(outfilesetup, outFileFormat);
	afInitSampleFormat(outfilesetup, AF_DEFAULT_TRACK, outSampleFormat,
		outSampleWidth);
	afInitChannels(outfilesetup, AF_DEFAULT_TRACK, outChannelCount);
	afInitRate(outfilesetup, AF_DEFAULT_TRACK, sampleRate);

	outfile = afOpenFile(outfilename, "w", outfilesetup);
	if (outfile == AF_NULL_FILEHANDLE)
	{
		printf("Could not open file '%s' for writing.\n", outfilename);
		return 1;
	}

	/*
		Set the output file's virtual audio format parameters
		to match the audio format parameters of the input file.
	*/
	afSetVirtualChannels(outfile, AF_DEFAULT_TRACK, channelCount);
	afSetVirtualSampleFormat(outfile, AF_DEFAULT_TRACK, sampleFormat,
		sampleWidth);

	afFreeFileSetup(outfilesetup);

	copyaudiodata(infile, outfile, AF_DEFAULT_TRACK, totalFrames);

	afCloseFile(infile);
	afCloseFile(outfile);

	printfileinfo(infilename);
	putchar('\n');
	printfileinfo(outfilename);

	return EXIT_SUCCESS;
}
Exemple #11
0
int main (int argc, char **argv)
{
	AFfilehandle	file;
	AFfilesetup	setup;
	uint16_t	samples[] = {11, 51, 101, 501, 1001, 5001, 10001, 50001};
	int		i;
	int		sampleFormat, sampleWidth;
	int		framesRead, framesWritten;
	int		nativeByteOrder;

#ifdef WORDS_BIGENDIAN
	nativeByteOrder = AF_BYTEORDER_BIGENDIAN;
#else
	nativeByteOrder = AF_BYTEORDER_LITTLEENDIAN;
#endif

	setup = afNewFileSetup();
	afInitFileFormat(setup, AF_FILE_RAWDATA);
	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);

	ensure(createTemporaryFile("writeraw", sTestFileName),
		"could not create temporary file");
	file = afOpenFile(sTestFileName, "w", setup);
	ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing");

	framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples, 8);
	ensure(framesWritten == 8,
		"number of frames written does not match number of frames requested");

	ensure(afCloseFile(file) == 0, "error closing file");

	file = afOpenFile(sTestFileName, "r", setup);
	ensure(file != AF_NULL_FILEHANDLE, "unable to open file for reading");
	afFreeFileSetup(setup);

	ensure(afGetFileFormat(file, NULL) == AF_FILE_RAWDATA,
		"test file not created as raw audio data file");

	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
	ensure(sampleFormat == AF_SAMPFMT_TWOSCOMP,
		"test file not two's complement");
	ensure(sampleWidth == 16,
		"test file sample format is not 16-bit");

	ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 1,
		"test file doesn't have exactly one channel");

	ensure(afGetByteOrder(file, AF_DEFAULT_TRACK) == nativeByteOrder,
		"test file not in native byte order");

	for (i=0; i<8; i++)
	{
		uint16_t	temporary;

		framesRead = afReadFrames(file, AF_DEFAULT_TRACK, &temporary, 1);
		ensure(framesRead == 1,
			"number of frames read does not match number of frames requested");

		ensure(temporary == samples[i],
			"data written to file doesn't match data read from file");
	}

	ensure(afCloseFile(file) == 0, "error closing file");

	cleanup();

	printf("writeraw test passed.\n");

	exit(EXIT_SUCCESS);
}
Exemple #12
0
void runTestWithChannels(int fileFormat, int channelCount)
{
	std::string testFileName;
	ASSERT_TRUE(createTemporaryFile("PCMData", &testFileName));

	const int numFrames = 20;
	const int numSamples = numFrames * channelCount;
	T samples[numSamples];
	for (int i=0; i<numFrames; i++)
		for (int c=0; c<channelCount; c++)
			samples[i*channelCount + c] = static_cast<T>(i*i + 3*c + 1);

	AFfilesetup setup = afNewFileSetup();
	afInitFileFormat(setup, fileFormat);
	afInitChannels(setup, AF_DEFAULT_TRACK, channelCount);
	afInitSampleFormat(setup, AF_DEFAULT_TRACK, kSampleFormat, kBitsPerSample);

	AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
	ASSERT_TRUE(file) << "Could not open file for writing";

	afFreeFileSetup(setup);

	PCMMapping mapping, defaultMapping;
	getDefaultPCMMapping(kSampleFormat, kBitsPerSample, defaultMapping);

	afGetPCMMapping(file, AF_DEFAULT_TRACK,
		&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip);
	ASSERT_EQ(mapping.slope, defaultMapping.slope);
	ASSERT_EQ(mapping.intercept, defaultMapping.intercept);
	ASSERT_EQ(mapping.minClip, defaultMapping.minClip);
	ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip);

	afGetVirtualPCMMapping(file, AF_DEFAULT_TRACK,
		&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip);
	ASSERT_EQ(mapping.slope, defaultMapping.slope);
	ASSERT_EQ(mapping.intercept, defaultMapping.intercept);
	ASSERT_EQ(mapping.minClip, defaultMapping.minClip);
	ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip);

	ASSERT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, samples, numFrames),
		numFrames) <<
		"Number of frames written does not match number of frames requested";

	ASSERT_EQ(afCloseFile(file), 0) << "Error closing file";

	file = afOpenFile(testFileName.c_str(), "r", NULL);
	ASSERT_TRUE(file) << "Could not open file for reading";

	ASSERT_EQ(afGetFileFormat(file, NULL), fileFormat) <<
		"Incorrect file format";

	int sampleFormat, sampleWidth;
	afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
	ASSERT_EQ(sampleFormat, kSampleFormat) << "Incorrect sample format";
	ASSERT_EQ(sampleWidth, kBitsPerSample) << "Incorrect sample width";

	ASSERT_EQ(afGetChannels(file, AF_DEFAULT_TRACK), channelCount) <<
		"Incorrect number of channels";

	afGetPCMMapping(file, AF_DEFAULT_TRACK,
		&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip);
	ASSERT_EQ(mapping.slope, defaultMapping.slope);
	ASSERT_EQ(mapping.intercept, defaultMapping.intercept);
	ASSERT_EQ(mapping.minClip, defaultMapping.minClip);
	ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip);

	afGetVirtualPCMMapping(file, AF_DEFAULT_TRACK,
		&mapping.slope, &mapping.intercept, &mapping.minClip, &mapping.maxClip);
	ASSERT_EQ(mapping.slope, defaultMapping.slope);
	ASSERT_EQ(mapping.intercept, defaultMapping.intercept);
	ASSERT_EQ(mapping.minClip, defaultMapping.minClip);
	ASSERT_EQ(mapping.maxClip, defaultMapping.maxClip);

	T *samplesRead = new T[numSamples];
	ASSERT_EQ(afReadFrames(file, AF_DEFAULT_TRACK, samplesRead, numFrames),
		numFrames) <<
		"Number of frames read does not match number of frames requested";

	for (int i=0; i<numSamples; i++)
		ASSERT_EQ(samplesRead[i], samples[i]) <<
			"Data read from file does not match data written";

	delete [] samplesRead;

	ASSERT_EQ(afCloseFile(file), 0) << "Error closing file";

	ASSERT_EQ(::unlink(testFileName.c_str()), 0);
}