Skip to content

Commit

Permalink
wasapi: let Windows do the resampling for us if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Feb 22, 2018
1 parent 7e1fa0c commit c7e4366
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/audio/wasapi/SDL_wasapi.c
Expand Up @@ -527,6 +527,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
SDL_AudioFormat wasapi_format = 0;
SDL_bool valid_format = SDL_FALSE;
HRESULT ret = S_OK;
DWORD streamflags = 0;

SDL_assert(client != NULL);

Expand All @@ -548,9 +549,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
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)) {
Expand Down Expand Up @@ -588,7 +587,21 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
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);
}
Expand Down

0 comments on commit c7e4366

Please sign in to comment.