Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Ogg Vorbis Streaming in Games (Read 2542 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Ogg Vorbis Streaming in Games

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


Code: [Select]
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, &section);

 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