From 290977b6ddee64cc07d95f80bdaf45a3503baf85 Mon Sep 17 00:00:00 2001 From: bones_was_here Date: Thu, 5 Sep 2024 05:40:00 +1000 Subject: [PATCH] snd: make SDL buffer length configurable Makes it (approximately) independent of sample rate (snd_speed). Changes the default (at 48khz) from 2048 to 1024, still not as aggressive as some other engines. Signed-off-by: bones_was_here --- snd_main.c | 2 ++ snd_main.h | 1 + snd_sdl.c | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/snd_main.c b/snd_main.c index 4c946730..9ac20114 100644 --- a/snd_main.c +++ b/snd_main.c @@ -245,6 +245,7 @@ static cvar_t snd_show = {CF_CLIENT, "snd_show", "0", "shows some statistics abo static cvar_t snd_speed = {CF_CLIENT | CF_ARCHIVE, "snd_speed", "48000", "sound output frequency, in hertz"}; static cvar_t snd_width = {CF_CLIENT | CF_ARCHIVE, "snd_width", "4", "sound output precision, in bytes - 1 = 8bit, 2 = 16bit, 4 = 32bit float"}; static cvar_t snd_channels = {CF_CLIENT | CF_ARCHIVE, "snd_channels", "2", "number of channels for the sound output (2 for stereo; up to 8 supported for 3D sound)"}; +cvar_t snd_bufferlength = {CF_CLIENT | CF_ARCHIVE, "snd_bufferlength", "20", "Desired length of the SDL2 audio buffer in milliseconds, smaller values reduce latency but can lead to underflow if the system is heavily loaded. Affects only how many sample frames are requested (which will be a power of 2 between 512 and 8192 inclusive)"}; static cvar_t snd_startloopingsounds = {CF_CLIENT, "snd_startloopingsounds", "1", "whether to start sounds that would loop (you want this to be 1); existing sounds are not affected"}; static cvar_t snd_startnonloopingsounds = {CF_CLIENT, "snd_startnonloopingsounds", "1", "whether to start sounds that would not loop (you want this to be 1); existing sounds are not affected"}; @@ -765,6 +766,7 @@ void S_Init(void) Cvar_RegisterVariable(&snd_speed); Cvar_RegisterVariable(&snd_width); Cvar_RegisterVariable(&snd_channels); + Cvar_RegisterVariable(&snd_bufferlength); Cvar_RegisterVariable(&snd_mutewhenidle); Cvar_RegisterVariable(&snd_maxchannelvolume); Cvar_RegisterVariable(&snd_softclip); diff --git a/snd_main.h b/snd_main.h index 6eb7b8ea..681fc32f 100644 --- a/snd_main.h +++ b/snd_main.h @@ -146,6 +146,7 @@ extern struct mempool_s *snd_mempool; /// Used for isolating performance in the renderer. extern qbool simsound; +extern cvar_t snd_bufferlength; #define STREAM_BUFFERSIZE 16384 ///< in sampleframes diff --git a/snd_sdl.c b/snd_sdl.c index 3752dcde..e34a46e8 100644 --- a/snd_sdl.c +++ b/snd_sdl.c @@ -114,7 +114,8 @@ qbool SndSys_Init (snd_format_t* fmt) return false; } - buffersize = (unsigned int)ceil((double)fmt->speed / 25.0); // 2048 bytes on 24kHz to 48kHz + // SDL2 wiki recommends this range + buffersize = bound(512, ceil((double)fmt->speed * snd_bufferlength.value / 1000.0), 8192); // Init the SDL Audio subsystem memset(&wantspec, 0, sizeof(wantspec)); -- 2.39.2