Exemple #1
0
int main()
{
	char buf[256];
	register int i;
	FILE *f;

	sampleFormat(PA_SAMPLE_S16BE,2); /* Valor por defecto, no hace falta llamar a esta función */

	if(openRecord("prueba")) puts("error");
	if(openPlay("prueba")) puts("error");

	f=fopen("pruebasonido","w+b");
	puts("Grabando");
	for(i=0; i < 10000; ++i)
	{
		recordSound(buf,160);
		fwrite(buf,1,160,f);
	}
	fclose(f);
	closeRecord();

	f=fopen("pruebasonido","rb");
	puts("Reproduciendo");
	for(i=0; i < 10000; ++i)
	{
		fread(buf,1,160,f);
		playSound(buf,160);
	}
	fclose(f);
	closePlay();
}
Exemple #2
0
void FFmpegDecoderAudio::adjustBufferEndTps(const size_t buffer_size)
{
    int sample_size = nbChannels() * frequency();

    switch (sampleFormat())
    {
    case osg::AudioStream::SAMPLE_FORMAT_U8:
        sample_size *= 1;
        break;

    case osg::AudioStream::SAMPLE_FORMAT_S16:
        sample_size *= 2;
        break;

    case osg::AudioStream::SAMPLE_FORMAT_S24:
        sample_size *= 3;
        break;

    case osg::AudioStream::SAMPLE_FORMAT_S32:
        sample_size *= 4;
        break;

    case osg::AudioStream::SAMPLE_FORMAT_F32:
        sample_size *= 4;
        break;

    default:
        throw std::runtime_error("unsupported audio sample format");
    }

    m_clocks.audioAdjustBufferEndPts(double(buffer_size) / double(sample_size));
}
Exemple #3
0
SampleFormat PcmDecoder::setHeader(msg::CodecHeader* chunk)
{
	if (chunk->payloadSize < 44)
		throw SnapException("PCM header too small");

    struct riff_wave_header riff_wave_header;
	struct chunk_header chunk_header;
	struct chunk_fmt chunk_fmt;
	chunk_fmt.sample_rate = SWAP_32(0);
	chunk_fmt.bits_per_sample = SWAP_16(0);
	chunk_fmt.num_channels = SWAP_16(0);

	size_t pos(0);
	memcpy(&riff_wave_header, chunk->payload + pos, sizeof(riff_wave_header));
	pos += sizeof(riff_wave_header);
	if ((SWAP_32(riff_wave_header.riff_id) != ID_RIFF) || (SWAP_32(riff_wave_header.wave_id) != ID_WAVE))
		throw SnapException("Not a riff/wave header");

	bool moreChunks(true);
	do
	{
		if (pos + sizeof(chunk_header) > chunk->payloadSize)
			throw SnapException("riff/wave header incomplete");
		memcpy(&chunk_header, chunk->payload + pos, sizeof(chunk_header));
		pos += sizeof(chunk_header);
		switch (SWAP_32(chunk_header.id))
		{
		case ID_FMT:
			if (pos + sizeof(chunk_fmt) > chunk->payloadSize)
				throw SnapException("riff/wave header incomplete");
			memcpy(&chunk_fmt, chunk->payload + pos, sizeof(chunk_fmt));
			pos += sizeof(chunk_fmt);
			/// If the format header is larger, skip the rest
			if (SWAP_32(chunk_header.sz) > sizeof(chunk_fmt))
				pos += (SWAP_32(chunk_header.sz) - sizeof(chunk_fmt));
			break;
		case ID_DATA:
			/// Stop looking for chunks
			moreChunks = false;
			break;
		default:
			/// Unknown chunk, skip bytes
			pos += SWAP_32(chunk_header.sz);
		}
	}
	while (moreChunks);


	if (SWAP_32(chunk_fmt.sample_rate) == 0)
		throw SnapException("Sample format not found");

	SampleFormat sampleFormat(
		SWAP_32(chunk_fmt.sample_rate),
		SWAP_16(chunk_fmt.bits_per_sample),
		SWAP_16(chunk_fmt.num_channels));

	return sampleFormat;
}