diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c index 66b13ab31..ea80e53b9 100644 --- a/src/audio/wasapi/SDL_wasapi.c +++ b/src/audio/wasapi/SDL_wasapi.c @@ -507,7 +507,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream) const SDL_AudioSpec oldspec = this->spec; const AUDCLNT_SHAREMODE sharemode = AUDCLNT_SHAREMODE_SHARED; UINT32 bufsize = 0; /* this is in sample frames, not samples, not bytes. */ - REFERENCE_TIME duration = 0; + REFERENCE_TIME default_period = 0; IAudioClient *client = this->hidden->client; IAudioRenderClient *render = NULL; IAudioCaptureClient *capture = NULL; @@ -571,7 +571,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream) return SDL_SetError("WASAPI: Unsupported audio format"); } - ret = IAudioClient_GetDevicePeriod(client, NULL, &duration); + ret = IAudioClient_GetDevicePeriod(client, &default_period, NULL); if (FAILED(ret)) { return WIN_SetErrorFromHRESULT("WASAPI can't determine minimum device period", ret); } @@ -590,7 +590,7 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream) } streamflags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK; - ret = IAudioClient_Initialize(client, sharemode, streamflags, duration, sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : duration, waveformat, NULL); + ret = IAudioClient_Initialize(client, sharemode, streamflags, 0, 0, waveformat, NULL); if (FAILED(ret)) { return WIN_SetErrorFromHRESULT("WASAPI can't initialize audio client", ret); } @@ -605,10 +605,11 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream) return WIN_SetErrorFromHRESULT("WASAPI can't determine buffer size", ret); } - this->spec.samples = (Uint16) bufsize; - if (!this->iscapture) { - this->spec.samples /= 2; /* fill half of the DMA buffer on each run. */ - } + /* Match the callback size to the period size to cut down on the number of + interrupts waited for in each call to WaitDevice */ + float period_millis = default_period / 10000.0f; + float period_frames = period_millis * this->spec.freq / 1000.0f; + this->spec.samples = (Uint16)ceil(period_frames); /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(&this->spec);