[Commits] SDL: wasapi: let Windows do the resampling for us if possible.

libsdl.org revision control commits-owner at libsdl.org
Wed Feb 21 19:05:01 PST 2018


details:   https://hg.libsdl.org/SDL/rev/2d6c0c719ced
changeset: 11890:2d6c0c719ced
user:      Ryan C. Gordon <icculus at icculus.org>
date:      Wed Feb 21 21:34:06 2018 -0500
description:
wasapi: let Windows do the resampling for us if possible.

diffstat:

 src/audio/wasapi/SDL_wasapi.c |  19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)

diffs (44 lines):

diff -r 150d030bfd48 -r 2d6c0c719ced src/audio/wasapi/SDL_wasapi.c
--- a/src/audio/wasapi/SDL_wasapi.c	Wed Feb 21 21:34:35 2018 -0500
+++ b/src/audio/wasapi/SDL_wasapi.c	Wed Feb 21 21:34:06 2018 -0500
@@ -527,6 +527,7 @@
     SDL_AudioFormat wasapi_format = 0;
     SDL_bool valid_format = SDL_FALSE;
     HRESULT ret = S_OK;
+    DWORD streamflags = 0;
 
     SDL_assert(client != NULL);
 
@@ -548,9 +549,7 @@
     SDL_assert(waveformat != NULL);
     this->hidden->waveformat = waveformat;
 
-    /* WASAPI will not do any conversion on our behalf. Force channels and sample rate. */
     this->spec.channels = (Uint8) waveformat->nChannels;
-    this->spec.freq = waveformat->nSamplesPerSec;
 
     /* Make sure we have a valid format that we can convert to whatever WASAPI wants. */
     if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
@@ -588,7 +587,21 @@
         return WIN_SetErrorFromHRESULT("WASAPI can't determine minimum device period", ret);
     }
 
-    ret = IAudioClient_Initialize(client, sharemode, AUDCLNT_STREAMFLAGS_EVENTCALLBACK, duration, sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : duration, waveformat, NULL);
+    /* favor WASAPI's resampler over our own, in Win7+. */
+    if (this->spec.freq != waveformat->nSamplesPerSec) {
+        /* RATEADJUST only works with output devices in share mode, and is available in Win7 and later.*/
+        if (WIN_IsWindows7OrGreater() && !this->iscapture && (sharemode == AUDCLNT_SHAREMODE_SHARED)) {
+            streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
+            waveformat->nSamplesPerSec = this->spec.freq;
+            waveformat->nAvgBytesPerSec = waveformat->nSamplesPerSec * waveformat->nChannels * (waveformat->wBitsPerSample / 8);
+        }
+        else {
+            this->spec.freq = waveformat->nSamplesPerSec;  /* force sampling rate so our resampler kicks in. */
+        }
+    }
+
+    streamflags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
+    ret = IAudioClient_Initialize(client, sharemode, streamflags, duration, sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : duration, waveformat, NULL);
     if (FAILED(ret)) {
         return WIN_SetErrorFromHRESULT("WASAPI can't initialize audio client", ret);
     }


More information about the commits mailing list