From 7572245aec09a7c3fc4cbd1c8e83b8c4667d1162 Mon Sep 17 00:00:00 2001 From: dmitrykos Date: Wed, 12 Dec 2018 23:29:44 +0200 Subject: [PATCH] wasapi: relax check for a latency range when deciding to switch from Event to Poll mode to have 1-21 ms range fine tuneable, add check for a min period when correcting period for UAC1 devices to avoid failure to initialize audio client due to invalid period error --- src/hostapi/wasapi/pa_win_wasapi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hostapi/wasapi/pa_win_wasapi.c b/src/hostapi/wasapi/pa_win_wasapi.c index b95c777..9809cda 100644 --- a/src/hostapi/wasapi/pa_win_wasapi.c +++ b/src/hostapi/wasapi/pa_win_wasapi.c @@ -2937,10 +2937,10 @@ static HRESULT CreateAudioClient(PaWasapiStream *pStream, PaWasapiSubStream *pSu // Work 1:1 with user buffer (only polling allows to use >1) framesPerLatency += MakeFramesFromHns(SecondsTonano100(params->suggestedLatency), pSub->wavex.Format.nSamplesPerSec); - // Use Polling if overall latency is > 5ms as it allows to use 100% CPU in a callback, + // Use Polling if overall latency is >= 21.33ms as it allows to use 100% CPU in a callback, // or user specified latency parameter overall = MakeHnsPeriod(framesPerLatency, pSub->wavex.Format.nSamplesPerSec); - if ((overall >= (106667*2)/*21.33ms*/) || ((INT32)(params->suggestedLatency*100000.0) != 0/*0.01 msec granularity*/)) + if (overall >= (106667 * 2)/*21.33ms*/) { framesPerLatency = _GetFramesPerHostBuffer(userFramesPerBuffer, params->suggestedLatency, pSub->wavex.Format.nSamplesPerSec, 0/*, @@ -2967,9 +2967,7 @@ static HRESULT CreateAudioClient(PaWasapiStream *pStream, PaWasapiSubStream *pSu { // Do it only for Polling mode if ((pSub->streamFlags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) == 0) - { framesPerLatency /= WASAPI_PACKETS_PER_INPUT_BUFFER; - } } // Calculate aligned period @@ -3106,6 +3104,10 @@ static HRESULT CreateAudioClient(PaWasapiStream *pStream, PaWasapiSubStream *pSu // Get new aligned frames lowered by calculated ratio framesPerLatency = MakeFramesFromHns(pSub->period / ratio, pSub->wavex.Format.nSamplesPerSec); _CalculateAlignedPeriod(pSub, &framesPerLatency, ALIGN_BWD); + + // Make sure we are not below the minimum period + if (pSub->period < pInfo->MinimumDevicePeriod) + pSub->period = pInfo->MinimumDevicePeriod; // Release previous client SAFE_RELEASE(audioClient); -- 2.43.0