mirror of
https://github.com/spice2x/spice2x.github.io.git
synced 2026-08-02 14:50:41 -07:00
audio: WASAPI Force Shared Mode option (#745)
## Link to GitHub Issue or related Pull Request, if one exists n/a ## Description of change This new option, `-wasapishared`, detects when the game opens WASAPI Exclusive stream and forcibly opens a shared mode stream instead on the real device. #### Benefits of this: 1. No need for `force wasapi shared` patches 2. No need to change sample rate of audio devices before starting up games 3. Adds ability to boot in shared mode even for games that do not have shared wasapi changes (old GITADORA, popn HC) As a result, this option is strictly better than `shared wasapi` patches available for many games. #### Downsides: 1. OS resampling will add a small latency, but not big enough to be noticeable. 2. Using shared mode instead of exclusive mode adds latency, of course. (If you cared about latency, you would use the low latency option, or use exclusive or asio) Basically it's the "make things work" button for audio that should work for almost all games that we support. The option has no effect if the game opens in shared mode. ## Testing SDVX7 - ok GITADORA GW - ok popn HC - ok IIDX - ok
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#include "shared.h"
|
||||
|
||||
#include <audioclient.h>
|
||||
|
||||
#include "hooks/audio/audio.h"
|
||||
#include "util/logging.h"
|
||||
|
||||
#include "defs.h"
|
||||
|
||||
namespace hooks::audio {
|
||||
|
||||
// whether the engine's PCM converter can handle this format. PCM / float only; non-PCM
|
||||
// bitstream (AC-3 / DTS passthrough) must be left alone.
|
||||
static bool is_pcm_or_float(const WAVEFORMATEX *format) {
|
||||
if (format == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (format->wFormatTag) {
|
||||
case WAVE_FORMAT_PCM:
|
||||
case WAVE_FORMAT_IEEE_FLOAT:
|
||||
return true;
|
||||
case WAVE_FORMAT_EXTENSIBLE: {
|
||||
|
||||
// SubFormat is only valid when the extra-bytes block is large enough
|
||||
if (format->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX)) {
|
||||
return false;
|
||||
}
|
||||
const auto *ext = reinterpret_cast<const WAVEFORMATEXTENSIBLE *>(format);
|
||||
return ext->SubFormat == GUID_KSDATAFORMAT_SUBTYPE_PCM
|
||||
|| ext->SubFormat == GUID_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool SharedRedirect::wants(AUDCLNT_SHAREMODE share_mode, const WAVEFORMATEX *format) {
|
||||
|
||||
// only redirect PCM / float exclusive streams: the engine converter (AUTOCONVERTPCM) can
|
||||
// handle those, but non-PCM bitstream (AC-3 / DTS passthrough) would fail in shared mode,
|
||||
// so leave it in exclusive untouched.
|
||||
return hooks::audio::WASAPI_COMPATIBILITY_MODE
|
||||
&& share_mode == AUDCLNT_SHAREMODE_EXCLUSIVE
|
||||
&& is_pcm_or_float(format);
|
||||
}
|
||||
|
||||
void SharedRedirect::apply(AUDCLNT_SHAREMODE *share_mode, DWORD *stream_flags,
|
||||
REFERENCE_TIME *periodicity) {
|
||||
|
||||
// shared mode requires periodicity == 0; AUTOCONVERTPCM lets the engine accept the game's
|
||||
// native format (else shared Initialize returns AUDCLNT_E_UNSUPPORTED_FORMAT).
|
||||
log_info("audio::wasapi", "redirecting exclusive WASAPI to shared mode");
|
||||
*share_mode = AUDCLNT_SHAREMODE_SHARED;
|
||||
*periodicity = 0;
|
||||
*stream_flags |= AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY;
|
||||
this->redirected_from_exclusive = true;
|
||||
}
|
||||
|
||||
UINT32 SharedRedirect::clamp_buffer_size(IAudioClient *real, uint32_t sample_rate,
|
||||
UINT32 device_frames) const {
|
||||
if (!this->redirected_from_exclusive || real == nullptr || sample_rate == 0 || device_frames == 0) {
|
||||
return device_frames;
|
||||
}
|
||||
|
||||
// GetDevicePeriod returns REFERENCE_TIME units (100 ns), 10^7 per second, so
|
||||
// period_frames = period * sample_rate / 10^7.
|
||||
REFERENCE_TIME period = 0;
|
||||
if (SUCCEEDED(real->GetDevicePeriod(&period, nullptr)) && period > 0) {
|
||||
const UINT32 period_frames = (UINT32) ((period * sample_rate) / 10000000);
|
||||
if (period_frames > 0 && period_frames < device_frames) {
|
||||
return period_frames;
|
||||
}
|
||||
}
|
||||
|
||||
return device_frames;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user