Skip to content

Commit

Permalink
wasapi: Don't use the system's resampler.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 15, 2021
1 parent 8ba735c commit b98b5ad
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/audio/wasapi/SDL_wasapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,18 +551,21 @@ WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
return WIN_SetErrorFromHRESULT("WASAPI can't determine minimum device period", ret);
}

#if 1 /* we're getting reports that WASAPI's resampler introduces distortions, so it's disabled for now. --ryan. */
this->spec.freq = waveformat->nSamplesPerSec; /* force sampling rate so our resampler kicks in, if necessary. */
#else
/* 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 {
} else {
this->spec.freq = waveformat->nSamplesPerSec; /* force sampling rate so our resampler kicks in. */
}
}
#endif

streamflags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
ret = IAudioClient_Initialize(client, sharemode, streamflags, 0, 0, waveformat, NULL);
Expand Down

4 comments on commit b98b5ad

@Wohlstand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What bad with it? Does this fix the glitchy playback on Wine? (right now when I run the Windows-built SDL2 application on Wine, the sound gets squashed and played like fast-forwarding when WASAPI is using but works fine when DSound or WinMM is used)

@icculus
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't speak for Wine (and Wine probably wouldn't be using the same internal resampler that Windows does), but we were finding instances where using AUDCLNT_STREAMFLAGS_RATEADJUST produces distortion in the final resampled output that SDL's resampler does not produce. The only reason we were using this flag is because we assumed Windows would produce better-quality output, and more efficiently, than SDL does, but this does not appear to be the case in reality. At least for now. It seems likely that Windows developers complained that they were required to provide a resampler of their own for any reasonable use of WASAPI, so Microsoft added a basic resampler into the API for these cases but probably aimed for speed over quality. But I don't really know. WASAPI originally provided no resampling at all, though.

For Wine specifically: it's possible that Wine doesn't support this flag, and just ignores it...and the absence of resampling at all would very likely produce the result you describe. In that case: this will instead use SDL's resampler and probably fix this problem. Please report back whether it does!

@vitor-k
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up, I've bisected this commit as the cause of audio problems on resampled audio when I was testing building SDL from source for Citra.
The audio gets distorted, with much higher pitch, and sometimes clipping. The sample rate of the original 3ds audio is 32728 Hz.

I'm not sure what's the policy for bugs only present on the dev build, if they're allowed I may soon put the necessary information together into an issue.

@icculus
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32728Hz is a weird sample rate. :/

Please open a bug report, even on dev builds; it’s how we know to look at problems!

Please sign in to comment.