Skip to content

Commit

Permalink
Stop sound playback early if we did not acquire a channel
Browse files Browse the repository at this point in the history
This can happen if we have too many sounds playing at the same time.
  • Loading branch information
katajakasa committed Feb 13, 2024
1 parent ee9f4ac commit ea057b1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/audio/audio.c
Expand Up @@ -276,9 +276,14 @@ void audio_play_sound(int id, float volume, float panning, float pitch) {
pan_right = (panning < 0) ? 1.0f + panning : 1.0f;

Mix_Chunk *chunk;
if(!(chunk = audio_get_chunk(id, volume, pitch)))
if(!(chunk = audio_get_chunk(id, volume, pitch))) {
PERROR("Unable to play sound: Failed to load chunk");
return;
channel = Mix_GroupAvailable(-1);
}
if((channel = Mix_GroupAvailable(-1)) == -1) {
PERROR("Unable to play sound: No free channels");
return;
}
audio->channel_chunks[channel] = chunk;
Mix_SetPanning(channel, clamp(pan_left * 255, 0, 255), clamp(pan_right * 255, 0, 255));
if(Mix_PlayChannel(channel, chunk, 0) == -1) {
Expand Down

0 comments on commit ea057b1

Please sign in to comment.