Skip to content

Commit f5a0222

Browse files
committed
aaudio: Try to select a more-useful microphone for recording.
Fixes #13402.
1 parent acb3b0b commit f5a0222

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/audio/aaudio/SDL_aaudio.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,16 @@ static bool AAUDIO_LoadFunctions(AAUDIO_Data *data)
6565
{
6666
#define SDL_PROC(ret, func, params) \
6767
do { \
68-
data->func = (ret (*) params)SDL_LoadFunction(data->handle, #func); \
69-
if (!data->func) { \
68+
data->func = (ret (*) params)SDL_LoadFunction(data->handle, #func); \
69+
if (!data->func) { \
7070
return SDL_SetError("Couldn't load AAUDIO function %s: %s", #func, SDL_GetError()); \
7171
} \
7272
} while (0);
73+
74+
#define SDL_PROC_OPTIONAL(ret, func, params) \
75+
do { \
76+
data->func = (ret (*) params)SDL_LoadFunction(data->handle, #func); /* if it fails, okay. */ \
77+
} while (0);
7378
#include "SDL_aaudiofuncs.h"
7479
return true;
7580
}
@@ -327,6 +332,12 @@ static bool BuildAAudioStream(SDL_AudioDevice *device)
327332
SDL_Log("Low latency audio disabled");
328333
}
329334

335+
if (recording && ctx.AAudioStreamBuilder_setInputPreset) { // optional API: requires Android 28
336+
// try to use a microphone that is for recording external audio. Otherwise Android might choose the mic used for talking
337+
// on the telephone when held to the user's ear, which is often not useful at any distance from the device.
338+
ctx.AAudioStreamBuilder_setInputPreset(builder, AAUDIO_INPUT_PRESET_CAMCORDER);
339+
}
340+
330341
LOGI("AAudio Try to open %u hz %s %u channels samples %u",
331342
device->spec.freq, SDL_GetAudioFormatName(device->spec.format),
332343
device->spec.channels, device->sample_frames);

src/audio/aaudio/SDL_aaudiofuncs.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
3. This notice may not be removed or altered from any source distribution.
2020
*/
2121

22+
#ifndef SDL_PROC_OPTIONAL(ret, func, params)
23+
#define SDL_PROC_OPTIONAL(ret, func, params) SDL_PROC(ret, func, params)
24+
#endif
25+
2226
#define SDL_PROC_UNUSED(ret, func, params)
2327

2428
SDL_PROC(const char *, AAudio_convertResultToText, (aaudio_result_t returnCode))
@@ -35,7 +39,7 @@ SDL_PROC(void, AAudioStreamBuilder_setBufferCapacityInFrames, (AAudioStreamBuild
3539
SDL_PROC(void, AAudioStreamBuilder_setPerformanceMode, (AAudioStreamBuilder * builder, aaudio_performance_mode_t mode))
3640
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setUsage, (AAudioStreamBuilder * builder, aaudio_usage_t usage)) // API 28
3741
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setContentType, (AAudioStreamBuilder * builder, aaudio_content_type_t contentType)) // API 28
38-
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setInputPreset, (AAudioStreamBuilder * builder, aaudio_input_preset_t inputPreset)) // API 28
42+
SDL_PROC_OPTIONAL(void, AAudioStreamBuilder_setInputPreset, (AAudioStreamBuilder * builder, aaudio_input_preset_t inputPreset)) // API 28
3943
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setAllowedCapturePolicy, (AAudioStreamBuilder * builder, aaudio_allowed_capture_policy_t capturePolicy)) // API 29
4044
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setSessionId, (AAudioStreamBuilder * builder, aaudio_session_id_t sessionId)) // API 28
4145
SDL_PROC_UNUSED(void, AAudioStreamBuilder_setPrivacySensitive, (AAudioStreamBuilder * builder, bool privacySensitive)) // API 30
@@ -80,3 +84,4 @@ SDL_PROC_UNUSED(bool, AAudioStream_isPrivacySensitive, (AAudioStream * stream))
8084

8185
#undef SDL_PROC
8286
#undef SDL_PROC_UNUSED
87+
#undef SDL_PROC_OPTIONAL

0 commit comments

Comments
 (0)