aaudio: Make input preset into a hint, don't set it by default.

Fixes #16130.
Reference Issue #13402.
This commit is contained in:
Ryan C. Gordon
2026-09-01 14:03:05 -04:00
parent c5f07f0990
commit 22e42e6e1e
2 changed files with 34 additions and 3 deletions

View File

@@ -117,6 +117,33 @@ extern "C" {
*/
#define SDL_HINT_ANDROID_LOW_LATENCY_AUDIO "SDL_ANDROID_LOW_LATENCY_AUDIO"
/**
* A variable to control Android's AAudio input preset.
*
* This hint only applies to SDL's "aaudio" backend on Android 9+ devices.
*
* Some devices choose the wrong microphone by default (between the one meant
* to be spoken in when the phone is held to the user's ear for a phone call,
* or an external microphone that's meant to be used when recording video),
* or have DSP effects applied to the recorded audio, and changing the input
* preset can help control this.
*
* This can be any number that maps to an `AAUDIO_INPUT_PRESET_*` enum from
* the Android NDK headers. The most reasonable choices are 5 ("camcorder",
* for external microphones) and 7 ("voice communication", for speaking
* directly into the device like a mobile phone). 6 ("voice recognition")
* might also be a useful choice.
*
* If unset (the default), SDL will not specify an input preset at all, which
* lets the system choose. This is usually the correct thing to do unless
* your app is having problems.
*
* This hint should be set before a recording audio device is opened.
*
* \since This hint is available since SDL 3.4.16.
*/
#define SDL_HINT_ANDROID_AAUDIO_INPUT_PRESET "SDL_ANDROID_AAUDIO_INPUT_PRESET"
/**
* A variable to control whether we trap the Android back button to handle it
* manually.

View File

@@ -368,9 +368,13 @@ static bool BuildAAudioStream(SDL_AudioDevice *device)
SetOptionalStreamUsage(builder);
if (recording && ctx.AAudioStreamBuilder_setInputPreset) { // optional API: requires Android 28
// try to use a microphone that is for recording external audio. Otherwise Android might choose the mic used for talking
// on the telephone when held to the user's ear, which is often not useful at any distance from the device.
ctx.AAudioStreamBuilder_setInputPreset(builder, AAUDIO_INPUT_PRESET_CAMCORDER);
const char *hint = SDL_GetHint(SDL_HINT_ANDROID_AAUDIO_INPUT_PRESET);
if (hint) {
const aaudio_input_preset_t preset = (const aaudio_input_preset_t) SDL_atoi(hint);
if (preset) {
ctx.AAudioStreamBuilder_setInputPreset(builder, preset);
}
}
}
LOGI("AAudio Try to open %u hz %s %u channels samples %u",