SDL Sound stops playing

Hi there

Working on a simple space shooter and currently implementing sound using the SDL. Got it working but encountered the following problem:

I have two sound files, one for the laser cannon and one for explosions. When I start the game both play fine and and at the right time. But after about 15 or so shots and explosions the sounds stop being played (the game essentially becomes silent). Everything else (movement, firing, asteroid-spawning) keeps working, but there is no more sound. Any idea?

See relevant code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);


void Play_Sound(std::string Filename)
{
	SDL_AudioSpec wavSpec;
	Uint32 wavLength;
	Uint8 *wavBuffer;

	SDL_LoadWAV(Filename.c_str(), &wavSpec, &wavBuffer, &wavLength);

	SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);

	SDL_QueueAudio(deviceId, wavBuffer, wavLength);

	SDL_PauseAudioDevice(deviceId, 0);

	SDL_FreeWAV(wavBuffer);
}


void C_Player::Process_Shooting()
{
	Play_Sound("Data/Sounds/Laser.wav");

}


void C_Game::Check_Collisions()
{
	Play_Sound("Data/Sounds/AsteroidExplosion.wav");
}
Last edited on
https://wiki.libsdl.org/SDL_LoadWAV
https://wiki.libsdl.org/SDL_OpenAudioDevice
https://wiki.libsdl.org/SDL_QueueAudio
Since pretty much everything returns a success/fail in on form or another, start applying LOTS of error checks to the code.

It might give you a clue when things are going pear shaped, rather than you just staring at the nothingness and wondering what to do next.
Yeah, I hoped it might be something obvious as I'm still struggling with error-reporting aside from outputting it through the console at runtime. And unfortunately I can't output it through the console for this game. So I would have to build a complete error reporting to file system just to catch this problem, which is far out of scope of this little test.

Having tried to investigate the problem further, I can now at least say with certainty that the sound stops working after exactly 15 sounds have been played (it doesn't matter which sounds).

So it seems that something is running full or getting clogged. But after looking at several examples online as well as the SDL wiki, I feel that my code is complete. Still, obviously it's missing something.

I also tried playing the sound directly from within the Process_Shooting() and Check_Collisions() functions without calling the Play_Sound() function. Same result. 15 sounds are played, then silence.
Well that seems like some kind of resource leak, where you're not cleaning up properly after playing a sound.

If you can't output to a console, you can certainly output to a file.
So I cobbled together a simple output to file error detection and narrowed the problem down:

The code fails at line 13 "SDL_OpenAudioDevice". The error message reads: "Invalid audio device ID".

Unfortunately, googling this problem does not return useful results. So, any idea as to why the hell the program fails to return the correct audio device id after exactly 15 successful tries?

A few additional notes:

The program cannot wait for a sound to be played completely, and depending on how fast the player pushes buttons, sound can (and does) overlap. So any solution that calls for a wait until current sound is played completely is not going to work. I have already tried that and all it does is pausing the program until the sound is played.

However, how fast or slow the 15 sounds are played does not matter. The audio stops working after 15 played sounds no matter how much time passes in between.
Last edited on
If lazer is already playing, then you have two choices
- let the current one finish, and don't play the new one.
- stop the old one, and start afresh.

Firing 20 times in quick succession, then wait for all the sound to finish while you go and have a cup of tea is not realistic.

You want the sound to stop momentarily after the last shot.
Well, actually I need the third option:
- Let the current sound finish, but start the new one while the current is still playing

Also, explosions might occur while the player is shooting or two explosions might happen at the same time, so these too can (and must be able to) overlap (with each other and laser) as well.

From my testing, it seems like it is already working that way as I did not hear any sound cut-offs. And rapidly hitting the fire button seems to overlap multiple laser-sounds as well.
Last edited on
I switched to using SDL2_mixer and got it to work.

Still would have preferred to get it working just using regular SDL...
Topic archived. No new replies allowed.