3

im trying to play a looping sound in openGL/SDL with openAL but when im calling it its just looping infinitely and its stuck at the start ive got no idea how to fix this so i came here hoping some1 knows heres my code of how im calling openAL

SDL_Thread * AudioBG;

    bool Init(void)
    {
     m_Music->LoadFile("Sonido/BGSP.wav",true);
    }

    int MusicThread(void *arg)//Creating the Thread
    {
       arg+=0;
        for(;;)
        {
            m_Music->PlaySound();
        }
        return 0;
    }

void Draw()//this is my cycle of the game
{
    gui->Draw(x,y,z);

    AudioBG = SDL_CreateThread(Music,NULL);

}

this is my class Sound.cpp

Sound::Sound()
{
    Device = alcOpenDevice((ALCchar*)"DirectSound3D");
    Context = alcCreateContext(Device,NULL);
    alcMakeContextCurrent(Context);
    alGetError();
}

Sound::~Sound()
{
    Context = alcGetCurrentContext();
    Device = alcGetContextsDevice(Context);
    alcMakeContextCurrent(NULL);
    alcDestroyContext(Context);
    alcCloseDevice(Device);
}

void Sound::LoadFile(char archivo[40],bool looping)
{
    ALboolean loop;
    loop = looping;
    alutLoadWAVFile(archivo,&alFormatBuffer,
                    (void **)&alBuffer,
                    (ALsizei *)&alBufferLen,
                    &alFreqBuffer,&loop);

    alGenSources(1,&this->alSource);
    alGenBuffers(1,&alSampleSet);
    alBufferData(alSampleSet,alFormatBuffer,alBuffer,alBufferLen,alFreqBuffer);
    alSourcei(this->alSource,AL_BUFFER,alSampleSet);

    alutUnloadWAV(alFormatBuffer,alBuffer,alBufferLen,alFreqBuffer);

    alSourcef(this->alSource,AL_PITCH,1.0f);

    alSourcef(this->alSource,AL_GAIN,1.0f);

    if(looping)
    alSourcei(this->alSource,AL_LOOPING,AL_TRUE);
    else
    alSourcei(this->alSource,AL_LOOPING,AL_FALSE);
}

void Sound::Direction(float x,float y,float z,float vx,float vy,float vz)
{
    alSource3f(this->alSource,AL_POSITION,x,y,z);
    alSource3f(this->alSource,AL_VELOCITY,vx,vy,vz);
}

void Sound::RelativeSound()
{
    alSourcei(this->alSource,AL_SOURCE_RELATIVE,AL_TRUE);
}

void Sound::PlaySound()
{
    alSourcePlay(this->alSource);
}

im really lost... if i call the function LoadFile and initialize the song out of the game loop and without a thread it plays well but inside the loop and with or without the thread im getting the error i tried to describe :S

genpfault
  • 51,148
  • 11
  • 85
  • 139
Makenshi
  • 993
  • 3
  • 15
  • 28
  • There is stuff missing in your code snippets. What is `m_Music` (I assume it is a `Sound`)? Do you call `Init()` before creating the thread? Define what you mean with "cycle of the game", is `Draw()` called each frame or only once (Generally, I would say you only want to create this audio thread once, and not in a function called Draw). Finally, check the documentation for `alSourcePlay()`. I believe that OpenAL keeps its own threads (in other words, play is not a blocking function). So if you call play from an infinite loop, you are probably just resetting the sound. Try playing it only once. – Orka Jul 02 '11 at 18:35
  • The thing is if i call it once it wont work the way i want since i want to do multiple threads instead of just 1 :S – Makenshi Jul 03 '11 at 21:06

0 Answers0