Add snd_mixingbuffersize cvar

Allows the user to configure the mixing buffer size to reduce
latency at the cost of higher CPU usage, or vice versa.

This also raises the default buffer size from 1024 to 2048, to
address some underrun problems people have with the current buffer
size.
This commit is contained in:
Eidolon 2024-04-30 23:31:18 -05:00
parent bf3e99d8a9
commit f9d70530ed
5 changed files with 24 additions and 9 deletions

View file

@ -409,6 +409,11 @@ consvar_t cv_netticbuffer = Player("netticbuffer", "1").min_max(0, 3);
void SetChannelsNum(void);
consvar_t cv_numChannels = Player("snd_channels", "64").values(CV_Unsigned).onchange(SetChannelsNum);
extern CV_PossibleValue_t soundmixingbuffersize_cons_t[];
consvar_t cv_soundmixingbuffersize = Player("snd_mixingbuffersize", "2048")
.values(soundmixingbuffersize_cons_t)
.onchange_noinit([]() { COM_ImmedExecute("restartaudio"); });
extern CV_PossibleValue_t perfstats_cons_t[];
consvar_t cv_perfstats = Player("perfstats", "Off").dont_save().values(perfstats_cons_t);

View file

@ -304,6 +304,9 @@ menuitem_t OPTIONS_Sound[] =
{IT_STRING | IT_CVAR, "Hear Tabbed-out", "Keep playing game audio when the window is out of focus (FOCUS LOST).",
NULL, {.cvar = &cv_bgaudio}, 0, 0},
{IT_STRING | IT_CVAR, "Mixing Buffer Size", "Audio buffer size. Higher is faster but more delay.",
NULL, {.cvar = &cv_soundmixingbuffersize}, 0, 0},
{IT_SPACE | IT_NOTHING, NULL, NULL,
NULL, {NULL}, 0, 0},

View file

@ -43,6 +43,15 @@
extern consvar_t cv_mastervolume;
CV_PossibleValue_t soundmixingbuffersize_cons_t[] = {
{256, "256"},
{512, "512"},
{1024, "1024"},
{2048, "2048"},
{4096, "4096"},
{0, NULL}
};
static boolean S_AdjustSoundParams(const mobj_t *listener, const mobj_t *source, INT32 *vol, INT32 *sep, INT32 *pitch, sfxinfo_t *sfxinfo);
static void Command_Tunes_f(void);
@ -2424,8 +2433,6 @@ static void Command_RestartAudio_f(void)
S_SetMusicVolume();
S_SetMasterVolume();
S_StartSound(NULL, sfx_strpst);
S_AttemptToRestoreMusic();
}

View file

@ -38,6 +38,8 @@ extern consvar_t cv_soundvolume, cv_closedcaptioning, cv_digmusicvolume;
extern consvar_t surround;
extern consvar_t cv_numChannels;
extern CV_PossibleValue_t soundmixingbuffersize_cons_t[];
extern consvar_t cv_soundmixingbuffersize;
extern consvar_t cv_gamedigimusic;

View file

@ -183,7 +183,7 @@ void initialize_sound()
SDL_AudioSpec desired;
desired.format = AUDIO_F32SYS;
desired.channels = 2;
desired.samples = 1024;
desired.samples = cv_soundmixingbuffersize.value;
desired.freq = 44100;
desired.callback = audio_callback;
@ -215,6 +215,7 @@ void initialize_sound()
master->add_source(gain_sound_effects);
master->add_source(gain_music_channel);
mixer_music->add_source(gain_music_player);
sound_effect_channels.clear();
for (size_t i = 0; i < static_cast<size_t>(cv_numChannels.value); i++)
{
shared_ptr<SoundEffectPlayer> player = make_shared<SoundEffectPlayer>();
@ -236,13 +237,10 @@ void I_StartupSound(void)
void I_ShutdownSound(void)
{
SdlAudioLockHandle _;
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
for (auto& channel : sound_effect_channels)
{
if (channel)
*channel = audio::SoundEffectPlayer();
}
sound_started = false;
}
void I_UpdateSound(void)