From 10d6488786cc1603dece1fb5a29651eb3e4e52c2 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 5 Nov 2026 15:25:29 -0500 Subject: [PATCH] audio: Adjust some device-opening hints. - They now work when the app specifies a NULL spec (verses a spec with zero'd fields). - They now favor the hint, falling back to the device's default settings (before they would fall back to `DEFAULT_AUDIO_*` defines, but it's better to default to what the hardware natively offers, and if the device doesn't specify a default, they'll be set to those defines earlier anyhow). This uses `device->default_spec` instead of `device->spec` so if something opens the device at a low-quality, it doesn't force all future default opens to conform to it. - Hint docs now explain this. Fixes #16652. --- include/SDL3/SDL_hints.h | 9 ++++++--- src/audio/SDL_audio.c | 32 ++++++++++++-------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h index 8c5a97447f..93b5459208 100644 --- a/include/SDL3/SDL_hints.h +++ b/include/SDL3/SDL_hints.h @@ -339,7 +339,8 @@ extern "C" { * * If the application doesn't specify the audio channel count when opening the * device, this hint can be used to specify a default channel count that will - * be used. This defaults to "1" for recording and "2" for playback devices. + * be used. If the application didn't specify and this hint isn't set, SDL will + * choose a reasonable value. * * This hint should be set before an audio device is opened. * @@ -568,7 +569,8 @@ extern "C" { * * If the application doesn't specify the audio format when opening the * device, this hint can be used to specify a default format that will be - * used. + * used. If the application didn't specify and this hint isn't set, SDL will + * choose a reasonable value. * * The variable can be set to the following values: * @@ -595,7 +597,8 @@ extern "C" { * * If the application doesn't specify the audio frequency when opening the * device, this hint can be used to specify a default frequency that will be - * used. This defaults to "44100". + * used. If the application didn't specify and this hint isn't set, SDL will + * choose a reasonable value. * * This hint should be set before an audio device is opened. * diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index 1c4d5671d1..f678df9fc1 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -1831,35 +1831,23 @@ static SDL_AudioFormat ParseAudioFormatString(const char *string) return SDL_AUDIO_UNKNOWN; } -static void PrepareAudioFormat(bool recording, SDL_AudioSpec *spec) +static void PrepareAudioFormat(SDL_AudioSpec *spec, const SDL_AudioSpec *devspec) { if (spec->freq == 0) { - spec->freq = recording ? DEFAULT_AUDIO_RECORDING_FREQUENCY : DEFAULT_AUDIO_PLAYBACK_FREQUENCY; - const char *hint = SDL_GetHint(SDL_HINT_AUDIO_FREQUENCY); - if (hint) { - const int val = SDL_atoi(hint); - if (val > 0) { - spec->freq = val; - } - } + const int val = hint ? SDL_atoi(hint) : 0; + spec->freq = (val > 0) ? val : devspec->freq; } if (spec->channels == 0) { - spec->channels = recording ? DEFAULT_AUDIO_RECORDING_CHANNELS : DEFAULT_AUDIO_PLAYBACK_CHANNELS; - const char *hint = SDL_GetHint(SDL_HINT_AUDIO_CHANNELS); - if (hint) { - const int val = SDL_atoi(hint); - if (val > 0) { - spec->channels = val; - } - } + const int val = hint ? SDL_atoi(hint) : 0; + spec->channels = (val > 0) ? val : devspec->channels; } if (spec->format == 0) { const SDL_AudioFormat val = ParseAudioFormatString(SDL_GetHint(SDL_HINT_AUDIO_FORMAT)); - spec->format = (val != SDL_AUDIO_UNKNOWN) ? val : (recording ? DEFAULT_AUDIO_RECORDING_FORMAT : DEFAULT_AUDIO_PLAYBACK_FORMAT); + spec->format = (val != SDL_AUDIO_UNKNOWN) ? val : devspec->format; } } @@ -1897,8 +1885,12 @@ static bool OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec const char *devtypestr = device->recording ? "recording" : "playback"; SDL_AudioSpec spec; - SDL_copyp(&spec, inspec ? inspec : &device->default_spec); - PrepareAudioFormat(device->recording, &spec); + if (inspec) { + SDL_copyp(&spec, inspec); + } else { + SDL_zero(spec); + } + PrepareAudioFormat(&spec, &device->default_spec); if (device->currently_opened) { SDL_AudioSpec current;