XAudio2: Try orignal sound format first, and fallback to 16bit mono on failure

This commit is contained in:
wangqr 2019-11-01 21:58:11 -04:00
parent 2ca9cf0de0
commit 2929b9db37

View file

@ -297,32 +297,34 @@ void XAudio2Thread::Run() {
REPORT_ERROR("Failed initializing XAudio2 MasteringVoice") REPORT_ERROR("Failed initializing XAudio2 MasteringVoice")
} }
#ifdef XAUDIO2_SAFE_FORMATS_ONLY
const bool original = provider->GetChannels() <= 2 && provider->AreSamplesFloat() ? provider->GetBytesPerSample() == 4 : provider->GetBytesPerSample() <= 2;
#else
const bool original = true;
#endif
// Describe the wave format // Describe the wave format
WAVEFORMATEX wfx; WAVEFORMATEX wfx;
wfx.nSamplesPerSec = provider->GetSampleRate(); wfx.nSamplesPerSec = provider->GetSampleRate();
wfx.cbSize = 0; wfx.cbSize = 0;
if (original) { bool original = true;
wfx.wFormatTag = provider->AreSamplesFloat() ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM; wfx.wFormatTag = provider->AreSamplesFloat() ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
wfx.nChannels = provider->GetChannels(); wfx.nChannels = provider->GetChannels();
wfx.wBitsPerSample = provider->GetBytesPerSample() * 8; wfx.wBitsPerSample = provider->GetBytesPerSample() * 8;
}
else {
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 1;
wfx.wBitsPerSample = sizeof(int16_t) * 8;
}
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, &wfx, 0, 2, this))) {
if (hr == XAUDIO2_E_INVALID_CALL) {
// Retry with 16bit mono
original = false;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 1;
wfx.wBitsPerSample = sizeof(int16_t) * 8;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, &wfx, 0, 2, this))) { if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, &wfx, 0, 2, this))) {
REPORT_ERROR("Failed initializing XAudio2 SourceVoice") REPORT_ERROR("Failed initializing XAudio2 SourceVoice")
} }
}
else {
REPORT_ERROR("Failed initializing XAudio2 SourceVoice")
}
}
// Now we're ready to roll! // Now we're ready to roll!
SetEvent(thread_running); SetEvent(thread_running);