From 57c083e7ad3a5d74538b92ee66178bb3698e36c8 Mon Sep 17 00:00:00 2001 From: bicarus <202771338+bicarus-dev@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:41:41 -0800 Subject: [PATCH] audio: workaround audio buffer issue in wine (#937) ## Link to GitHub Issue or related Pull Request, if one exists Fixes #931 ## Description of change Implement workaround for audio bug in Wine which caused loud static with `-wasapishared` due to the way wine maps 24-bit PCM into 32-bit container when handing off to PulseAudio. ## Testing tbd --- .../audio/backends/wasapi/audio_client.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/spice2x/hooks/audio/backends/wasapi/audio_client.cpp b/src/spice2x/hooks/audio/backends/wasapi/audio_client.cpp index 734d4669..5589f493 100644 --- a/src/spice2x/hooks/audio/backends/wasapi/audio_client.cpp +++ b/src/spice2x/hooks/audio/backends/wasapi/audio_client.cpp @@ -183,6 +183,30 @@ HRESULT STDMETHODCALLTYPE WrappedIAudioClient::Initialize( WAVEFORMATEXTENSIBLE resample_storage = {}; const WAVEFORMATEX *device_format = pFormat; +#if SPICE_LINUX + WAVEFORMATEXTENSIBLE full_width_storage = {}; + if (this->shared.redirected_from_exclusive && + pFormat->wFormatTag == WAVE_FORMAT_EXTENSIBLE && + pFormat->cbSize == sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) && + pFormat->wBitsPerSample == 32) { + const auto *ext = reinterpret_cast(pFormat); + if (ext->SubFormat == GUID_KSDATAFORMAT_SUBTYPE_PCM && + ext->Samples.wValidBitsPerSample == 24) { + + // workaround for bug in wine + // + // Windows reads audio data in the **top** 24 bits, but Wine's PulseAudio mapping + // for this format reads the **bottom** 24 bits, causing static. + // report 32 valid bits so Wine selects s32le and reads the whole sample. + // the bottom 8 bits are zero padding, so the audio data and volume stay the same. + full_width_storage = *ext; + full_width_storage.Samples.wValidBitsPerSample = 32; + device_format = &full_width_storage.Format; + log_info("audio::wasapi", "using 32-bit PCM for Linux shared-mode 24-in-32 audio"); + } + } +#endif + if (!this->shared.redirected_from_exclusive) { // when downmixing, open the real device as stereo while the game keeps writing its native