Hi There,
I'm really new to OpenAl and Ogg Vorbis. I tried to implement the tutorial about Ogg Vorbis Streaming in my game project. My problem was that if you wanted to playback a soundfile, the sound seemed to repeat always the first of two buffers. I tried to playback the sound in another project and there it worked. I think there is something wrong in the update function. Take a look at it:
Here a link to the Tutorial
void OggFile::Play()
{
if(IsPlaying())
return;
if(!Stream(mBuffers[0]))
return;
if(!Stream(mBuffers[1]))
return;
alSourceQueueBuffers(mSource, 2, mBuffers);
alSourcePlay(mSource);
}
void OggFile::Load(const char* pFileName)
{
int result;
if(!(mFile = fopen(pFileName, "rb")))
{
}
if((result = ov_open(mFile, &mStream, NULL, 0)) < 0)
{
fclose(mFile);
}
mVorbisInfo = ov_info(&mStream, -1);
mVorbisComment = ov_comment(&mStream, -1);
if(mVorbisInfo->channels == 1)
mFormat = AL_FORMAT_MONO16;
else
mFormat = AL_FORMAT_STEREO16;
alGenBuffers(2, mBuffers);
alGenSources(1, &mSource);
alSourcef(mSource, AL_PITCH, 1.0f);
alSourcef(mSource, AL_GAIN, 1.0f);
alSourcei(mSource, AL_BUFFER, mBuffers[0]);
alSourcei(mSource, AL_LOOPING, AL_TRUE);
alSource3f(mSource, AL_POSITION, 0, 0, 0);
alSource3f(mSource, AL_VELOCITY, 0,0,0);
}
bool OggFile::Stream(unsigned int pBuffer)
{
char data[BUFFER_SIZE];
int size = 0;
int section;
int result;
while(size < BUFFER_SIZE)
{
result = ov_read(&mStream, data + size, BUFFER_SIZE - size, 0, 2, 1, §ion);
if(result > 0)
size += result;
else
if(result < 0)
{
}
else
break;
}
if(size == 0)
return false;
alBufferData(pBuffer, mFormat, data, size, mVorbisInfo->rate);
return true;
}
void OggFile::Update()
{
int processed;
bool active = true;
alGetSourcei(mSource, AL_BUFFERS_PROCESSED, &processed);
while(processed--)
{
ALuint buffer;
alSourceUnqueueBuffers(mSource, 1, &buffer);
Stream(buffer);
alSourceQueueBuffers(mSource, 1, &buffer);
}
}
Thanks a lot for looking at it. Hope one of you can help me.
Regards Hannes