From 0129fa3b2a5ee9b0b7f15db9b8a3e9288b5106f4 Mon Sep 17 00:00:00 2001 From: David Reid Date: Thu, 8 Jan 2026 10:52:37 +1000 Subject: [PATCH] ALSA: Fix a bug where a default device is not detected properly. --- miniaudio.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index 852eb94b..47cdb754 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -28937,6 +28937,8 @@ static ma_result ma_context_enumerate_devices__alsa(ma_context* pContext, ma_enu ma_device_id* pUniqueIDs = NULL; ma_uint32 uniqueIDCount = 0; char** ppNextDeviceHint; + ma_bool32 hasEnumeratedDefaultPlaybackDevice = MA_FALSE; + ma_bool32 hasEnumeratedDefaultCaptureDevice = MA_FALSE; MA_ASSERT(pContextStateALSA != NULL); MA_ASSERT(callback != NULL); @@ -29008,18 +29010,31 @@ static ma_result ma_context_enumerate_devices__alsa(ma_context* pContext, ma_enu MA_ZERO_OBJECT(&deviceInfo); + /* ID. */ + ma_strncpy_s(deviceInfo.id.alsa, sizeof(deviceInfo.id.alsa), hwid, (size_t)-1); + /* Defualt. There's no good way to determine whether or not a device is the default with ALSA. We're just going to do something simple and just use the name of "default" as the indicator. */ - if (ma_strcmp(deviceInfo.id.alsa, "default") == 0) { - deviceInfo.isDefault = MA_TRUE; + if ((deviceType == ma_device_type_playback && !hasEnumeratedDefaultPlaybackDevice) || (deviceType == ma_device_type_capture && !hasEnumeratedDefaultCaptureDevice)) { + /* */ if (ma_strcmp(deviceInfo.id.alsa, "default") == 0) { + deviceInfo.isDefault = MA_TRUE; + } else if (ma_strcmp(deviceInfo.id.alsa, "sysdefault") == 0) { + deviceInfo.isDefault = MA_TRUE; + } } - /* ID. */ - ma_strncpy_s(deviceInfo.id.alsa, sizeof(deviceInfo.id.alsa), hwid, (size_t)-1); + if (deviceInfo.isDefault) { + if (deviceType == ma_device_type_playback) { + hasEnumeratedDefaultPlaybackDevice = MA_TRUE; + } + if (deviceType == ma_device_type_capture) { + hasEnumeratedDefaultCaptureDevice = MA_TRUE; + } + } /* Name.