From e6922b360cbafdb8e09bc81341d9d7ac6f495d7c Mon Sep 17 00:00:00 2001 From: David Reid Date: Mon, 22 Dec 2025 13:41:37 +1000 Subject: [PATCH] Minor refactor the AAudio backend. --- miniaudio.h | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index d607c6a6..d59b6504 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -41075,18 +41075,39 @@ static ma_result ma_job_process__device__aaudio_reroute(ma_job* pJob) return result; } -static ma_result ma_device_wait__aaudio(ma_device* pDevice) + +static ma_result ma_device_step__aaudio(ma_device* pDevice, ma_blocking_mode blockingMode) { ma_device_state_aaudio* pDeviceStateAAudio = ma_device_get_backend_state__aaudio(pDevice); - ma_device_state_async_wait(&pDeviceStateAAudio->async); - return MA_SUCCESS; -} + for (;;) { + ma_result result; -static ma_result ma_device_step__aaudio(ma_device* pDevice) -{ - ma_device_state_aaudio* pDeviceStateAAudio = ma_device_get_backend_state__aaudio(pDevice); - ma_device_state_async_step(&pDeviceStateAAudio->async, pDevice); + if (blockingMode == MA_BLOCKING_MODE_BLOCKING) { + ma_device_state_async_wait(&pDeviceStateAAudio->async); + } + + if (!ma_device_is_started(pDevice)) { + return MA_DEVICE_NOT_STARTED; + } + + result = ma_device_state_async_step(&pDeviceStateAAudio->async, pDevice); + if (result == MA_SUCCESS) { + break; + } + + if (result != MA_NO_DATA_AVAILABLE) { + return result; + } + + /* Getting here means no data was processed. In non-blocking mode we don't care, just get out of the loop. */ + if (blockingMode == MA_BLOCKING_MODE_NON_BLOCKING) { + break; + } + + /* Getting here means we're in blocking mode and no data was processed. In this case we'd rather keep waiting for data to be available. */ + continue; + } return MA_SUCCESS; } @@ -41096,17 +41117,7 @@ static void ma_device_loop__aaudio(ma_device* pDevice) MA_ASSERT(pDevice != NULL); for (;;) { - ma_result result = ma_device_wait__aaudio(pDevice); - if (result != MA_SUCCESS) { - break; - } - - /* If the wait terminated due to the device being stopped, abort now. */ - if (!ma_device_is_started(pDevice)) { - break; - } - - result = ma_device_step__aaudio(pDevice); + ma_result result = ma_device_step__aaudio(pDevice, MA_BLOCKING_MODE_BLOCKING); if (result != MA_SUCCESS) { break; }