• Advertisement
Sign in to follow this  

openAL not looping source

This topic is 2374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently set up openAL to add sound to my game and everything is going fairly well except for the fact that I can't loop my .wav file.

I am aware that I don't need to use a loop to get multiple sources, and that I should probably do some error checking in my sound loading method, I will fix that soon. However, do I need to explicitly create a device and context or not? When I do create the device and context, the sound plays very slow and choppy.

Anyway, does my code look right or do you guys see a misconception somewhere?

Thanks!
Scott

ALboolean InitAL()
{

//device = alcOpenDevice(NULL);
//context = alcCreateContext(device, NULL);
//alcMakeContextCurrent(context);

alutInit(NULL, 0);

if(alGetError() != AL_NO_ERROR)
return false;
return true;
}


vector<ALuint> FileManager::loadSound(int num, bool l, string id)
{

//Get Full Path
std::stringstream ss;
ss.str("");
ss << soundPath << id;

vector<ALuint> sources;

ALuint buffer(alutCreateBufferFromFile(ss.str().c_str()));

for(int i = 0; i < num; i++){

ALuint tempSource;
alGenSources(1, &tempSource);

alSourcei(tempSource, AL_BUFFER, buffer);
alSourcef(tempSource, AL_PITCH, 1.0);
alSourcef(tempSource, AL_GAIN, 1.0);
if(l) alSourcef(tempSource, AL_LOOPING, AL_TRUE);
sources.push_back(tempSource);
}

return sources;
}


bool Game::start()
{

userShip = shared_ptr<Actor>(new Ship());
shared_ptr<class Ship> tempShip(dynamic_pointer_cast<class Ship>(userShip));

tempShip->addThrusterSound(
fileManager.loadSound(tempShip->getNumThrusters(), true, "Thruster.wav"),
fileManager.loadSound(tempShip->getNumThrusters(), false, "ThrusterEnd.wav"));

return true;
}


void Thruster::control(Controller& input, GLfloat dt)
{

for each(int i in key){
if(input.wasPressed(i)){

isActive = true;
alSourcePlay(thrusterLoop);
alSourceStop(thrusterEnd);
break;

}else if(input.wasReleased(i)){

bool stillActive = false;
for each(int x in key){
if(input.isDown(i)){
stillActive = true;
break;
}
}

if(!stillActive){
isActive = false;
alSourcePlay(thrusterEnd);
alSourceStop(thrusterLoop);
}
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Everything looks fine to me. It's been like a year since I used OpenAL tho.

I don't use alut but it looks like it's already creating a context by itself. As for the looping issue, the only difference I see is that I use

alSourcei(source, AL_LOOPING, loop);
alSourcei instead of alSourcef, but I'm not sure if it matters.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement