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.
This commit is contained in:
Ryan C. Gordon
2026-11-05 16:05:16 -05:00
parent 87f0e4a25f
commit 10d6488786
2 changed files with 18 additions and 23 deletions
+6 -3
View File
@@ -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.
*
+12 -20
View File
@@ -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;