Re: Changing buffer content
Chris Robinson <[email protected]>
| Newsgroups | gmane.comp.lib.openal |
|---|---|
| Message-ID | <[email protected]> |
On Sunday 17 January 2010 8:38:16 am Peter Soxberger wrote:
> Hi!
>
> I'm creating a voice chat and am using OpenAL for the output. The Problem
> that I currently have is, that if I change the content of a buffer of a
> source and play it, I can't hear anything.
>
> Code:
> //--------------------------------------------
> alSourceStop(soundsOut_current->source);
> alBufferData(soundsOut_current->buffer, AL_FORMAT_MONO16, decoded_buffer,
> voice_Bufsize, voice_SamplesPerSecond);
> alSourcei(soundsOut_current->source, AL_BUFFER, soundsOut_current->buffer);
> alSourcePlay(soundsOut_current->source);
> //--------------------------------------------
>
> Everytime I receive a packet with new data, I want to play it with the code
> above. Because I don't want to initialize a source and buffer for every
> received packed I want to change the content of the buffer only. But I
> simply hear nohting...
>
> What am I doing wrong?
You have to detach the buffer form the source before you can fill it with new
data.
alSourceStop(soundsOut_current->source);
alSourcei(soundsOut_current->source, AL_BUFFER, 0);
alBufferData(soundsOut_current->buffer, AL_FORMAT_MONO16, decoded_buffer,
voice_Bufsize, voice_SamplesPerSecond);
alSourcei(soundsOut_current->source, AL_BUFFER, soundsOut_current->buffer);
alSourcePlay(soundsOut_current->source);
alBufferData will error if it's still attached to a source.
However, if you want to play a continuous audio stream, you would probably be
better off using buffer queues. You create one source and an array of buffers,
then use alSourceQueueBuffers to put them on the source. Then when you get a
packet with new data, unqueue a processed buffer, fill it with new data, then
queue it back on the playing source:
ALint processed, state, queued;
alGetSourcei(soundsOut_current->source, AL_SOURCE_STATE, &state);
alGetSourcei(soundsOut_current->source, AL_BUFFERS_PROCESSED, &processed);
if(processed > 0)
{
ALuint buffer;
alSourceUnqueueBuffers(soundsOut_current->source, 1, &buffer);
alBufferData(buffer, AL_FORMAT_MONO16, decoded_buffer, voice_Bufsize,
voice_SamplesPerSecond);
alSourceQueueBuffers(soundsOut_current->source, 1, &buffer);
}
// Restart in case we didn't get more data in time
if(state != AL_PLAYING)
alSourcePlay(soundsOut_current->source);
The amount of buffers to initially queue on the source depends on how much
latency is acceptable. A larger queue will create more of a delay between the
person talking and you hearing, while a smaller queue will increase the risk
of drop-outs.
_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal