]> Repos - portaudio/commitdiff
wasapi: Fixed underruns in the Exclusive mode on UWP platform (or when timeBeginPerio...
authordmitrykos <dmitrykos@neutroncode.com>
Fri, 11 Dec 2020 13:30:08 +0000 (15:30 +0200)
committerdmitrykos <dmitrykos@neutroncode.com>
Fri, 11 Dec 2020 13:30:08 +0000 (15:30 +0200)
src/hostapi/wasapi/pa_win_wasapi.c

index 7e321f1509a36439245813bc328544581905a47f..216796ee5eb8507d6a61e7cb17c829d0ad1cbaad 100644 (file)
@@ -825,7 +825,7 @@ static inline UINT32 ThreadIdleScheduler_NextSleep(ThreadIdleScheduler *sched)
 // ------------------------------------------------------------------------------------------
 typedef struct _SystemTimer
 {
-    UINT32 granularity;
+    INT32 granularity;
 
 } SystemTimer;
 static LARGE_INTEGER g_SystemTimerFrequency;
@@ -849,12 +849,15 @@ static BOOL SystemTimer_SetGranularity(SystemTimer *timer, UINT32 granularity)
     {
         PRINT(("SetSystemTimer: timeBeginPeriod(1) failed!\n"));
 
-        timer->granularity = 0;
+        timer->granularity = 10;
         return FALSE;
     }
 #else
-    (void)timer;
     (void)granularity;
+
+    // UWP does not support increase of the timer precision change and thus calling WaitForSingleObject with anything 
+    // below 10 milliseconds will cause underruns for input and output stream.
+    timer->granularity = 10;
 #endif
 
     return TRUE;
@@ -6113,8 +6116,11 @@ static inline INT32 GetNextSleepTime(SystemTimer *timer, ThreadIdleScheduler *sc
     //
     INT32 procTime = (INT32)(SystemTimer_GetTime(timer) - startTime);
     nextSleepTime -= procTime;
-    if (nextSleepTime < 0)
+    if (nextSleepTime < timer->granularity)
         nextSleepTime = 0;
+    else
+    if (timer->granularity > 1)
+        nextSleepTime = ALIGN_BWD(nextSleepTime, timer->granularity);
 
 #ifdef PA_WASAPI_LOG_TIME_SLOTS
     printf("{%d},", procTime);