From ea057b1f129a411b756c11af0a4da43980ca39bd Mon Sep 17 00:00:00 2001 From: Tuomas Virtanen Date: Tue, 13 Feb 2024 23:49:08 +0200 Subject: [PATCH] Stop sound playback early if we did not acquire a channel This can happen if we have too many sounds playing at the same time. --- src/audio/audio.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/audio/audio.c b/src/audio/audio.c index 69948d330..5dfa86a6c 100644 --- a/src/audio/audio.c +++ b/src/audio/audio.c @@ -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) {