OpenAL Sound Error C++

Started by
1 comment, last by inprazia 12 years ago
I've been working on a game in C++. I use OpenAL for my sound library. I've been having some issues with sound playback. For some reason, my sounds will either play normally, play halfway, or stop playing all together. I would like some help with this issue. So I'm posting my code below.


//Operations to Play Sources
ALuint buffers[20];
ALuint sources[20];
int SRC_VAL = 0;
float LISTNER_X = 0;
float LISTNER_Y = 0;

void InitSources(){
alGenSources(20,sources);
}
void PlaySource(int buf,bool ambient = true,float x = 0, float y = 0, float refdist = 1.0, float maxdist = 20){
float dist = sqrt((LISTNER_X-x)*(LISTNER_X-x) + (LISTNER_Y-y)*(LISTNER_Y-y));
if(dist <= maxdist || ambient){
alSourcei(sources[SRC_VAL], AL_SOURCE_RELATIVE, AL_FALSE);
alSourcei(sources[SRC_VAL], AL_BUFFER, buffers[buf]);
alSource3f(sources[SRC_VAL],AL_POSITION, x,y,0);
alSourcei(sources[SRC_VAL], AL_REFERENCE_DISTANCE, refdist);
alSourcei(sources[SRC_VAL], AL_MAX_DISTANCE, maxdist);
if(ambient){
alSourcei(sources[SRC_VAL], AL_SOURCE_RELATIVE, AL_TRUE);
alSource3f(sources[SRC_VAL], AL_POSITION, 0.0f, 0.0f, 0.0f);
}
alSourcePlay(sources[SRC_VAL]);
SRC_VAL++;
if(SRC_VAL >= 20){
SRC_VAL = 0;
}
}
}

//this is inside main for initialization and buffer loading
int main(){
//code
alutInit(0, NULL);
alGetError();
alGenBuffers(20,buffers);
InitSources();
// load sounds
buffers[0] = alutCreateBufferFromFile("SFX/walk.wav");
buffers[1] = alutCreateBufferFromFile("SFX/jump.wav");
buffers[2] = alutCreateBufferFromFile("SFX/pain.wav");
buffers[3] = alutCreateBufferFromFile("SFX/break.wav");
buffers[4] = alutCreateBufferFromFile("SFX/pain.wav");
buffers[5] = alutCreateBufferFromFile("SFX/collect.wav");
buffers[6] = alutCreateBufferFromFile("SFX/zombie.wav");
buffers[7] = alutCreateBufferFromFile("SFX/splash.wav");
buffers[8] = alutCreateBufferFromFile("SFX/fall.wav");
// code
}


To play a sound I just call the function PlaySource
Advertisement
So many people have read this topic, yet there's still no answer to my problem :/ I really would appreciate any help.
I'm not using the ALUT lib, just basic OpenAL. Didn't have any problems. Just follow the samples included in the OpenAL SDK.

This topic is closed to new replies.

Advertisement