Advertisement

openAL not looping source

Started by October 13, 2011 10:31 PM
2 comments, last by swilkewitz 13 years, 1 month ago
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);
}
}
}
}
bump
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.
Hey, that worked! Thank you!

This topic is closed to new replies.

Advertisement