• Advertisement
Sign in to follow this  

OpenAL Sound Error C++

This topic is 2107 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'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.

[CODE]
//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
}
[/CODE]

To play a sound I just call the function PlaySource

Share this post


Link to post
Share on other sites
Advertisement
I'm not using the ALUT lib, just basic OpenAL. Didn't have any problems. Just follow the samples included in the OpenAL SDK.

Share this post


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

  • Advertisement