diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h index ca29e6eecb..37acb45020 100644 --- a/include/SDL3/SDL_hints.h +++ b/include/SDL3/SDL_hints.h @@ -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. diff --git a/src/audio/aaudio/SDL_aaudio.c b/src/audio/aaudio/SDL_aaudio.c index ea5e1c09a5..3d5283c2e6 100644 --- a/src/audio/aaudio/SDL_aaudio.c +++ b/src/audio/aaudio/SDL_aaudio.c @@ -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",