Jump to content
  • Advertisement
Sign in to follow this  

OpenAL Sound Error C++

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

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

void InitSources(){
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_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);
alSource3f(sources[SRC_VAL], AL_POSITION, 0.0f, 0.0f, 0.0f);
if(SRC_VAL >= 20){
SRC_VAL = 0;

//this is inside main for initialization and buffer loading
int main(){
alutInit(0, NULL);
// 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

Share this post

Link to post
Share on other sites
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

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!