mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-22 22:00:39 -06:00
@ -11,8 +11,6 @@
|
||||
#include "AudioCommon/OpenSLESStream.h"
|
||||
#include "AudioCommon/PulseAudioStream.h"
|
||||
#include "AudioCommon/WASAPIStream.h"
|
||||
#include "AudioCommon/XAudio2Stream.h"
|
||||
#include "AudioCommon/XAudio2_7Stream.h"
|
||||
#include "Common/Common.h"
|
||||
#include "Common/FileUtil.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
@ -29,30 +27,37 @@ static bool s_sound_stream_running = false;
|
||||
constexpr int AUDIO_VOLUME_MIN = 0;
|
||||
constexpr int AUDIO_VOLUME_MAX = 100;
|
||||
|
||||
static std::unique_ptr<SoundStream> CreateSoundStreamForBackend(std::string_view backend)
|
||||
{
|
||||
if (backend == BACKEND_CUBEB)
|
||||
return std::make_unique<CubebStream>();
|
||||
else if (backend == BACKEND_OPENAL && OpenALStream::isValid())
|
||||
return std::make_unique<OpenALStream>();
|
||||
else if (backend == BACKEND_NULLSOUND)
|
||||
return std::make_unique<NullSound>();
|
||||
else if (backend == BACKEND_ALSA && AlsaSound::isValid())
|
||||
return std::make_unique<AlsaSound>();
|
||||
else if (backend == BACKEND_PULSEAUDIO && PulseAudio::isValid())
|
||||
return std::make_unique<PulseAudio>();
|
||||
else if (backend == BACKEND_OPENSLES && OpenSLESStream::isValid())
|
||||
return std::make_unique<OpenSLESStream>();
|
||||
else if (backend == BACKEND_WASAPI && WASAPIStream::isValid())
|
||||
return std::make_unique<WASAPIStream>();
|
||||
return {};
|
||||
}
|
||||
|
||||
void InitSoundStream()
|
||||
{
|
||||
std::string backend = SConfig::GetInstance().sBackend;
|
||||
if (backend == BACKEND_CUBEB)
|
||||
g_sound_stream = std::make_unique<CubebStream>();
|
||||
else if (backend == BACKEND_OPENAL && OpenALStream::isValid())
|
||||
g_sound_stream = std::make_unique<OpenALStream>();
|
||||
else if (backend == BACKEND_NULLSOUND)
|
||||
g_sound_stream = std::make_unique<NullSound>();
|
||||
else if (backend == BACKEND_XAUDIO2)
|
||||
g_sound_stream = CreateSoundStreamForBackend(backend);
|
||||
|
||||
if (!g_sound_stream)
|
||||
{
|
||||
if (XAudio2::isValid())
|
||||
g_sound_stream = std::make_unique<XAudio2>();
|
||||
else if (XAudio2_7::isValid())
|
||||
g_sound_stream = std::make_unique<XAudio2_7>();
|
||||
WARN_LOG(AUDIO, "Unknown backend %s, using %s instead.", backend.c_str(),
|
||||
GetDefaultSoundBackend().c_str());
|
||||
backend = GetDefaultSoundBackend();
|
||||
g_sound_stream = CreateSoundStreamForBackend(GetDefaultSoundBackend());
|
||||
}
|
||||
else if (backend == BACKEND_ALSA && AlsaSound::isValid())
|
||||
g_sound_stream = std::make_unique<AlsaSound>();
|
||||
else if (backend == BACKEND_PULSEAUDIO && PulseAudio::isValid())
|
||||
g_sound_stream = std::make_unique<PulseAudio>();
|
||||
else if (backend == BACKEND_OPENSLES && OpenSLESStream::isValid())
|
||||
g_sound_stream = std::make_unique<OpenSLESStream>();
|
||||
else if (backend == BACKEND_WASAPI && WASAPIStream::isValid())
|
||||
g_sound_stream = std::make_unique<WASAPIStream>();
|
||||
|
||||
if (!g_sound_stream || !g_sound_stream->Init())
|
||||
{
|
||||
@ -101,8 +106,6 @@ std::vector<std::string> GetSoundBackends()
|
||||
|
||||
backends.emplace_back(BACKEND_NULLSOUND);
|
||||
backends.emplace_back(BACKEND_CUBEB);
|
||||
if (XAudio2_7::isValid() || XAudio2::isValid())
|
||||
backends.emplace_back(BACKEND_XAUDIO2);
|
||||
if (AlsaSound::isValid())
|
||||
backends.emplace_back(BACKEND_ALSA);
|
||||
if (PulseAudio::isValid())
|
||||
@ -127,8 +130,6 @@ bool SupportsDPL2Decoder(std::string_view backend)
|
||||
return true;
|
||||
if (backend == BACKEND_PULSEAUDIO)
|
||||
return true;
|
||||
if (backend == BACKEND_XAUDIO2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -142,8 +143,7 @@ bool SupportsVolumeChanges(std::string_view backend)
|
||||
// FIXME: this one should ask the backend whether it supports it.
|
||||
// but getting the backend from string etc. is probably
|
||||
// too much just to enable/disable a stupid slider...
|
||||
return backend == BACKEND_CUBEB || backend == BACKEND_OPENAL || backend == BACKEND_XAUDIO2 ||
|
||||
backend == BACKEND_WASAPI;
|
||||
return backend == BACKEND_CUBEB || backend == BACKEND_OPENAL || backend == BACKEND_WASAPI;
|
||||
}
|
||||
|
||||
void UpdateSoundStream()
|
||||
|
@ -46,10 +46,6 @@
|
||||
<ClCompile Include="WASAPIStream.cpp" />
|
||||
<ClCompile Include="SurroundDecoder.cpp" />
|
||||
<ClCompile Include="WaveFile.cpp" />
|
||||
<ClCompile Include="XAudio2Stream.cpp" />
|
||||
<ClCompile Include="XAudio2_7Stream.cpp">
|
||||
<AdditionalIncludeDirectories>$(ExternalsDir)XAudio2_7;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="AlsaSoundStream.h" />
|
||||
@ -66,8 +62,6 @@
|
||||
<ClInclude Include="WASAPIStream.h" />
|
||||
<ClInclude Include="SurroundDecoder.h" />
|
||||
<ClInclude Include="WaveFile.h" />
|
||||
<ClInclude Include="XAudio2Stream.h" />
|
||||
<ClInclude Include="XAudio2_7Stream.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="CMakeLists.txt" />
|
||||
|
@ -17,12 +17,6 @@
|
||||
<ClCompile Include="OpenALStream.cpp">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="XAudio2Stream.cpp">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="XAudio2_7Stream.cpp">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CubebStream.cpp">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClCompile>
|
||||
@ -43,12 +37,6 @@
|
||||
<ClInclude Include="OpenALStream.h">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="XAudio2Stream.h">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="XAudio2_7Stream.h">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="PulseAudioStream.h">
|
||||
<Filter>SoundStreams</Filter>
|
||||
</ClInclude>
|
||||
|
@ -71,19 +71,7 @@ if(WIN32)
|
||||
|
||||
WASAPIStream.cpp
|
||||
WASAPIStream.h
|
||||
XAudio2Stream.cpp
|
||||
XAudio2Stream.h
|
||||
)
|
||||
|
||||
add_library(audiocommon_xaudio27
|
||||
XAudio2_7Stream.cpp
|
||||
XAudio2_7Stream.h
|
||||
)
|
||||
target_include_directories(audiocommon_xaudio27 PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/Externals
|
||||
${PROJECT_SOURCE_DIR}/Externals/XAudio2_7
|
||||
)
|
||||
target_link_libraries(audiocommon PRIVATE audiocommon_xaudio27)
|
||||
endif()
|
||||
|
||||
target_link_libraries(audiocommon PRIVATE cubeb SoundTouch FreeSurround)
|
||||
|
@ -1,309 +0,0 @@
|
||||
// Copyright 2010 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include <xaudio2.h>
|
||||
#include "AudioCommon/AudioCommon.h"
|
||||
#include "AudioCommon/XAudio2Stream.h"
|
||||
#include "Common/Event.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/MsgHandler.h"
|
||||
#include "Core/ConfigManager.h"
|
||||
|
||||
#ifndef XAUDIO2_DLL
|
||||
#error You are building this module against the wrong version of DirectX. You probably need to remove DXSDK_DIR from your include path.
|
||||
#endif
|
||||
|
||||
struct StreamingVoiceContext : public IXAudio2VoiceCallback
|
||||
{
|
||||
private:
|
||||
Mixer* const m_mixer;
|
||||
Common::Event& m_sound_sync_event;
|
||||
IXAudio2SourceVoice* m_source_voice;
|
||||
std::variant<std::unique_ptr<short[]>, std::unique_ptr<float[]>> xaudio_buffer;
|
||||
|
||||
void SubmitBuffer(PBYTE buf_data);
|
||||
|
||||
bool m_use_surround = SConfig::GetInstance().bDPL2Decoder;
|
||||
|
||||
public:
|
||||
StreamingVoiceContext(IXAudio2* pXAudio2, Mixer* pMixer, Common::Event& pSyncEvent);
|
||||
virtual ~StreamingVoiceContext();
|
||||
|
||||
void Stop();
|
||||
void Play();
|
||||
|
||||
STDMETHOD_(void, OnVoiceError)(THIS_ void* pBufferContext, HRESULT Error) {}
|
||||
STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) {}
|
||||
STDMETHOD_(void, OnVoiceProcessingPassEnd)() {}
|
||||
STDMETHOD_(void, OnBufferStart)(void*) {}
|
||||
STDMETHOD_(void, OnLoopEnd)(void*) {}
|
||||
STDMETHOD_(void, OnStreamEnd)() {}
|
||||
STDMETHOD_(void, OnBufferEnd)(void* context);
|
||||
};
|
||||
|
||||
const int NUM_BUFFERS = 3;
|
||||
const int SAMPLES_PER_BUFFER = 96;
|
||||
const int SAMPLES_PER_BUFFER_SURROUND = 256;
|
||||
|
||||
const int NUM_CHANNELS = 2;
|
||||
const int BUFFER_SIZE = SAMPLES_PER_BUFFER * NUM_CHANNELS;
|
||||
const int BUFFER_SIZE_BYTES = BUFFER_SIZE * sizeof(s16);
|
||||
|
||||
const int NUM_CHANNELS_SURROUND = 6;
|
||||
const int BUFFER_SIZE_SURROUND = SAMPLES_PER_BUFFER_SURROUND * NUM_CHANNELS_SURROUND;
|
||||
const int BUFFER_SIZE_BYTES_SURROUND = BUFFER_SIZE_SURROUND * sizeof(float);
|
||||
void StreamingVoiceContext::SubmitBuffer(PBYTE buf_data)
|
||||
{
|
||||
XAUDIO2_BUFFER buf = {};
|
||||
if (m_use_surround)
|
||||
{
|
||||
buf.AudioBytes = BUFFER_SIZE_BYTES_SURROUND;
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.AudioBytes = BUFFER_SIZE_BYTES;
|
||||
}
|
||||
buf.pContext = buf_data;
|
||||
buf.pAudioData = buf_data;
|
||||
|
||||
m_source_voice->SubmitSourceBuffer(&buf);
|
||||
}
|
||||
|
||||
StreamingVoiceContext::StreamingVoiceContext(IXAudio2* pXAudio2, Mixer* pMixer,
|
||||
Common::Event& pSyncEvent)
|
||||
: m_mixer(pMixer), m_sound_sync_event(pSyncEvent)
|
||||
{
|
||||
WAVEFORMATEXTENSIBLE wfx = {};
|
||||
wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
||||
wfx.Format.nSamplesPerSec = m_mixer->GetSampleRate();
|
||||
|
||||
// More information about these values:
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd390971(v=vs.85).aspx
|
||||
if (m_use_surround)
|
||||
{
|
||||
xaudio_buffer = std::make_unique<float[]>(NUM_BUFFERS * BUFFER_SIZE_SURROUND);
|
||||
|
||||
wfx.Format.nChannels = 6;
|
||||
wfx.Format.wBitsPerSample = 32;
|
||||
wfx.Samples.wValidBitsPerSample = 32;
|
||||
wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER |
|
||||
SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
|
||||
wfx.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
|
||||
wfx.Format.cbSize = sizeof(wfx.Samples) + sizeof(wfx.dwChannelMask) + sizeof(wfx.SubFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
xaudio_buffer = std::make_unique<short[]>(NUM_BUFFERS * BUFFER_SIZE);
|
||||
|
||||
wfx.Format.nChannels = 2;
|
||||
wfx.Format.wBitsPerSample = 16;
|
||||
wfx.Samples.wValidBitsPerSample = 16;
|
||||
wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
|
||||
wfx.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
|
||||
wfx.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
|
||||
}
|
||||
|
||||
wfx.Format.nBlockAlign = wfx.Format.nChannels * wfx.Format.wBitsPerSample / 8;
|
||||
wfx.Format.nAvgBytesPerSec = wfx.Format.nSamplesPerSec * wfx.Format.nBlockAlign;
|
||||
|
||||
// create source voice
|
||||
HRESULT hr;
|
||||
if (FAILED(hr = pXAudio2->CreateSourceVoice(&m_source_voice, &wfx.Format, XAUDIO2_VOICE_NOSRC,
|
||||
1.0f, this)))
|
||||
{
|
||||
PanicAlert("XAudio2 CreateSourceVoice failed: %#X", hr);
|
||||
return;
|
||||
}
|
||||
|
||||
m_source_voice->Start();
|
||||
|
||||
// start buffers with silence
|
||||
if (m_use_surround)
|
||||
{
|
||||
for (int i = 0; i != NUM_BUFFERS; ++i)
|
||||
{
|
||||
SubmitBuffer(
|
||||
reinterpret_cast<BYTE*>(std::get<std::unique_ptr<float[]>>(xaudio_buffer).get()) +
|
||||
(i * BUFFER_SIZE_BYTES_SURROUND));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i != NUM_BUFFERS; ++i)
|
||||
{
|
||||
SubmitBuffer(
|
||||
reinterpret_cast<BYTE*>(std::get<std::unique_ptr<short[]>>(xaudio_buffer).get()) +
|
||||
(i * BUFFER_SIZE_BYTES));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StreamingVoiceContext::~StreamingVoiceContext()
|
||||
{
|
||||
if (m_source_voice)
|
||||
{
|
||||
m_source_voice->Stop();
|
||||
m_source_voice->DestroyVoice();
|
||||
}
|
||||
}
|
||||
|
||||
void StreamingVoiceContext::Stop()
|
||||
{
|
||||
if (m_source_voice)
|
||||
m_source_voice->Stop();
|
||||
}
|
||||
|
||||
void StreamingVoiceContext::Play()
|
||||
{
|
||||
if (m_source_voice)
|
||||
m_source_voice->Start();
|
||||
}
|
||||
|
||||
void StreamingVoiceContext::OnBufferEnd(void* context)
|
||||
{
|
||||
// buffer end callback; gets SAMPLES_PER_BUFFER samples for a new buffer
|
||||
|
||||
if (!m_source_voice || !context)
|
||||
return;
|
||||
|
||||
// m_sound_sync_event->Wait(); // sync
|
||||
// m_sound_sync_event->Spin(); // or tight sync
|
||||
|
||||
if (m_use_surround)
|
||||
{
|
||||
m_mixer->MixSurround(static_cast<float*>(context), SAMPLES_PER_BUFFER_SURROUND);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_mixer->Mix(static_cast<short*>(context), SAMPLES_PER_BUFFER);
|
||||
}
|
||||
SubmitBuffer(reinterpret_cast<BYTE*>(context));
|
||||
}
|
||||
|
||||
HMODULE XAudio2::m_xaudio2_dll = nullptr;
|
||||
typedef decltype(&XAudio2Create) XAudio2Create_t;
|
||||
void* XAudio2::PXAudio2Create = nullptr;
|
||||
|
||||
bool XAudio2::InitLibrary()
|
||||
{
|
||||
if (m_xaudio2_dll)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
m_xaudio2_dll = ::LoadLibrary(XAUDIO2_DLL);
|
||||
if (!m_xaudio2_dll)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PXAudio2Create)
|
||||
{
|
||||
PXAudio2Create = (XAudio2Create_t)::GetProcAddress(m_xaudio2_dll, "XAudio2Create");
|
||||
if (!PXAudio2Create)
|
||||
{
|
||||
::FreeLibrary(m_xaudio2_dll);
|
||||
m_xaudio2_dll = nullptr;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
XAudio2::XAudio2()
|
||||
: m_mastering_voice(nullptr), m_volume(1.0f),
|
||||
m_cleanup_com(SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
|
||||
{
|
||||
}
|
||||
|
||||
XAudio2::~XAudio2()
|
||||
{
|
||||
Stop();
|
||||
if (m_cleanup_com)
|
||||
CoUninitialize();
|
||||
}
|
||||
|
||||
bool XAudio2::Init()
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
// callback doesn't seem to run on a specific CPU anyways
|
||||
IXAudio2* xaudptr;
|
||||
if (FAILED(hr = ((XAudio2Create_t)PXAudio2Create)(&xaudptr, 0, XAUDIO2_DEFAULT_PROCESSOR)))
|
||||
{
|
||||
PanicAlert("XAudio2 init failed: %#X", hr);
|
||||
Stop();
|
||||
return false;
|
||||
}
|
||||
m_xaudio2 = std::unique_ptr<IXAudio2, Releaser>(xaudptr);
|
||||
|
||||
// XAudio2 master voice
|
||||
// XAUDIO2_DEFAULT_CHANNELS instead of 2 for expansion?
|
||||
uint channels = NUM_CHANNELS;
|
||||
if (SConfig::GetInstance().bDPL2Decoder)
|
||||
channels = NUM_CHANNELS_SURROUND;
|
||||
|
||||
if (FAILED(hr = m_xaudio2->CreateMasteringVoice(&m_mastering_voice, channels,
|
||||
m_mixer->GetSampleRate())))
|
||||
{
|
||||
PanicAlert("XAudio2 master voice creation failed: %#X", hr);
|
||||
Stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Volume
|
||||
m_mastering_voice->SetVolume(m_volume);
|
||||
|
||||
m_voice_context = std::unique_ptr<StreamingVoiceContext>(
|
||||
new StreamingVoiceContext(m_xaudio2.get(), m_mixer.get(), m_sound_sync_event));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void XAudio2::SetVolume(int volume)
|
||||
{
|
||||
// linear 1- .01
|
||||
m_volume = (float)volume / 100.f;
|
||||
|
||||
if (m_mastering_voice)
|
||||
m_mastering_voice->SetVolume(m_volume);
|
||||
}
|
||||
|
||||
bool XAudio2::SetRunning(bool running)
|
||||
{
|
||||
if (!m_voice_context)
|
||||
return false;
|
||||
|
||||
if (running)
|
||||
m_voice_context->Play();
|
||||
else
|
||||
m_voice_context->Stop();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void XAudio2::Stop()
|
||||
{
|
||||
// m_sound_sync_event.Set();
|
||||
|
||||
m_voice_context.reset();
|
||||
|
||||
if (m_mastering_voice)
|
||||
{
|
||||
m_mastering_voice->DestroyVoice();
|
||||
m_mastering_voice = nullptr;
|
||||
}
|
||||
|
||||
m_xaudio2.reset(); // release interface
|
||||
|
||||
if (m_xaudio2_dll)
|
||||
{
|
||||
::FreeLibrary(m_xaudio2_dll);
|
||||
m_xaudio2_dll = nullptr;
|
||||
PXAudio2Create = nullptr;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
// Copyright 2008 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
// This audio backend uses XAudio2 via XAUDIO2_DLL
|
||||
// It works on Windows 8+, where it is included as an OS component.
|
||||
// This backend is always compiled, but only available if running on Win8+
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "AudioCommon/SoundStream.h"
|
||||
#include "Common/Event.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
struct StreamingVoiceContext;
|
||||
struct IXAudio2;
|
||||
struct IXAudio2MasteringVoice;
|
||||
|
||||
#endif
|
||||
|
||||
class XAudio2 final : public SoundStream
|
||||
{
|
||||
#ifdef _WIN32
|
||||
|
||||
private:
|
||||
class Releaser
|
||||
{
|
||||
public:
|
||||
template <typename R>
|
||||
void operator()(R* ptr)
|
||||
{
|
||||
ptr->Release();
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IXAudio2, Releaser> m_xaudio2;
|
||||
std::unique_ptr<StreamingVoiceContext> m_voice_context;
|
||||
IXAudio2MasteringVoice* m_mastering_voice;
|
||||
|
||||
Common::Event m_sound_sync_event;
|
||||
float m_volume;
|
||||
|
||||
const bool m_cleanup_com;
|
||||
|
||||
static HMODULE m_xaudio2_dll;
|
||||
static void* PXAudio2Create;
|
||||
|
||||
static bool InitLibrary();
|
||||
void Stop();
|
||||
|
||||
public:
|
||||
XAudio2();
|
||||
~XAudio2() override;
|
||||
|
||||
bool Init() override;
|
||||
|
||||
bool SetRunning(bool running) override;
|
||||
void SetVolume(int volume) override;
|
||||
|
||||
static bool isValid() { return InitLibrary(); }
|
||||
#endif
|
||||
};
|
@ -1,295 +0,0 @@
|
||||
// Copyright 2013 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
// Note that this file *and this file only* must include XAudio2.h from the old
|
||||
// DXSDK instead of other possible places.
|
||||
|
||||
#include <XAudio2_7/XAudio2.h>
|
||||
#include <variant>
|
||||
|
||||
#include "AudioCommon/AudioCommon.h"
|
||||
#include "AudioCommon/XAudio2_7Stream.h"
|
||||
#include "Common/Event.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/MsgHandler.h"
|
||||
#include "Core/ConfigManager.h"
|
||||
|
||||
struct StreamingVoiceContext2_7 : public IXAudio2VoiceCallback
|
||||
{
|
||||
private:
|
||||
Mixer* const m_mixer;
|
||||
Common::Event& m_sound_sync_event;
|
||||
IXAudio2SourceVoice* m_source_voice;
|
||||
std::variant<std::unique_ptr<short[]>, std::unique_ptr<float[]>> xaudio_buffer;
|
||||
|
||||
void SubmitBuffer(PBYTE buf_data);
|
||||
|
||||
bool m_use_surround = SConfig::GetInstance().bDPL2Decoder;
|
||||
|
||||
public:
|
||||
StreamingVoiceContext2_7(IXAudio2* pXAudio2, Mixer* pMixer, Common::Event& pSyncEvent);
|
||||
virtual ~StreamingVoiceContext2_7();
|
||||
|
||||
void Stop();
|
||||
void Play();
|
||||
|
||||
STDMETHOD_(void, OnVoiceError)(THIS_ void* pBufferContext, HRESULT Error) {}
|
||||
STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) {}
|
||||
STDMETHOD_(void, OnVoiceProcessingPassEnd)() {}
|
||||
STDMETHOD_(void, OnBufferStart)(void*) {}
|
||||
STDMETHOD_(void, OnLoopEnd)(void*) {}
|
||||
STDMETHOD_(void, OnStreamEnd)() {}
|
||||
STDMETHOD_(void, OnBufferEnd)(void* context);
|
||||
};
|
||||
|
||||
const int NUM_BUFFERS = 3;
|
||||
const int SAMPLES_PER_BUFFER = 96;
|
||||
const int SAMPLES_PER_BUFFER_SURROUND = 256;
|
||||
|
||||
const int NUM_CHANNELS = 2;
|
||||
const int BUFFER_SIZE = SAMPLES_PER_BUFFER * NUM_CHANNELS;
|
||||
const int BUFFER_SIZE_BYTES = BUFFER_SIZE * sizeof(s16);
|
||||
|
||||
const int NUM_CHANNELS_SURROUND = 6;
|
||||
const int BUFFER_SIZE_SURROUND = SAMPLES_PER_BUFFER_SURROUND * NUM_CHANNELS_SURROUND;
|
||||
const int BUFFER_SIZE_BYTES_SURROUND = BUFFER_SIZE_SURROUND * sizeof(float);
|
||||
void StreamingVoiceContext2_7::SubmitBuffer(PBYTE buf_data)
|
||||
{
|
||||
XAUDIO2_BUFFER buf = {};
|
||||
if (m_use_surround)
|
||||
{
|
||||
buf.AudioBytes = BUFFER_SIZE_BYTES_SURROUND;
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.AudioBytes = BUFFER_SIZE_BYTES;
|
||||
}
|
||||
buf.pContext = buf_data;
|
||||
buf.pAudioData = buf_data;
|
||||
|
||||
m_source_voice->SubmitSourceBuffer(&buf);
|
||||
}
|
||||
|
||||
StreamingVoiceContext2_7::StreamingVoiceContext2_7(IXAudio2* pXAudio2, Mixer* pMixer,
|
||||
Common::Event& pSyncEvent)
|
||||
: m_mixer(pMixer), m_sound_sync_event(pSyncEvent)
|
||||
{
|
||||
WAVEFORMATEXTENSIBLE wfx = {};
|
||||
wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
||||
wfx.Format.nSamplesPerSec = m_mixer->GetSampleRate();
|
||||
|
||||
// More information about these values:
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd390971(v=vs.85).aspx
|
||||
if (m_use_surround)
|
||||
{
|
||||
xaudio_buffer = std::make_unique<float[]>(NUM_BUFFERS * BUFFER_SIZE_SURROUND);
|
||||
|
||||
wfx.Format.nChannels = 6;
|
||||
wfx.Format.wBitsPerSample = 32;
|
||||
wfx.Samples.wValidBitsPerSample = 32;
|
||||
wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER |
|
||||
SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
|
||||
wfx.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
|
||||
wfx.Format.cbSize = sizeof(wfx.Samples) + sizeof(wfx.dwChannelMask) + sizeof(wfx.SubFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
xaudio_buffer = std::make_unique<short[]>(NUM_BUFFERS * BUFFER_SIZE);
|
||||
|
||||
wfx.Format.nChannels = 2;
|
||||
wfx.Format.wBitsPerSample = 16;
|
||||
wfx.Samples.wValidBitsPerSample = 16;
|
||||
wfx.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
|
||||
wfx.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
|
||||
wfx.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
|
||||
}
|
||||
|
||||
wfx.Format.nBlockAlign = wfx.Format.nChannels * wfx.Format.wBitsPerSample / 8;
|
||||
wfx.Format.nAvgBytesPerSec = wfx.Format.nSamplesPerSec * wfx.Format.nBlockAlign;
|
||||
|
||||
// create source voice
|
||||
HRESULT hr;
|
||||
if (FAILED(hr = pXAudio2->CreateSourceVoice(&m_source_voice, &wfx.Format, XAUDIO2_VOICE_NOSRC,
|
||||
1.0f, this)))
|
||||
{
|
||||
PanicAlert("XAudio2_7 CreateSourceVoice failed: %#X", hr);
|
||||
return;
|
||||
}
|
||||
|
||||
m_source_voice->Start();
|
||||
|
||||
// start buffers with silence
|
||||
if (m_use_surround)
|
||||
{
|
||||
for (int i = 0; i != NUM_BUFFERS; ++i)
|
||||
{
|
||||
SubmitBuffer(
|
||||
reinterpret_cast<BYTE*>(std::get<std::unique_ptr<float[]>>(xaudio_buffer).get()) +
|
||||
(i * BUFFER_SIZE_BYTES_SURROUND));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i != NUM_BUFFERS; ++i)
|
||||
{
|
||||
SubmitBuffer(
|
||||
reinterpret_cast<BYTE*>(std::get<std::unique_ptr<short[]>>(xaudio_buffer).get()) +
|
||||
(i * BUFFER_SIZE_BYTES));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StreamingVoiceContext2_7::~StreamingVoiceContext2_7()
|
||||
{
|
||||
if (m_source_voice)
|
||||
{
|
||||
m_source_voice->Stop();
|
||||
m_source_voice->DestroyVoice();
|
||||
}
|
||||
}
|
||||
|
||||
void StreamingVoiceContext2_7::Stop()
|
||||
{
|
||||
if (m_source_voice)
|
||||
m_source_voice->Stop();
|
||||
}
|
||||
|
||||
void StreamingVoiceContext2_7::Play()
|
||||
{
|
||||
if (m_source_voice)
|
||||
m_source_voice->Start();
|
||||
}
|
||||
|
||||
void StreamingVoiceContext2_7::OnBufferEnd(void* context)
|
||||
{
|
||||
// buffer end callback; gets SAMPLES_PER_BUFFER samples for a new buffer
|
||||
|
||||
if (!m_source_voice || !context)
|
||||
return;
|
||||
|
||||
// m_sound_sync_event->Wait(); // sync
|
||||
// m_sound_sync_event->Spin(); // or tight sync
|
||||
|
||||
if (m_use_surround)
|
||||
{
|
||||
m_mixer->MixSurround(static_cast<float*>(context), SAMPLES_PER_BUFFER_SURROUND);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_mixer->Mix(static_cast<short*>(context), SAMPLES_PER_BUFFER);
|
||||
}
|
||||
SubmitBuffer(reinterpret_cast<BYTE*>(context));
|
||||
}
|
||||
|
||||
HMODULE XAudio2_7::m_xaudio2_dll = nullptr;
|
||||
|
||||
void XAudio2_7::ReleaseIXAudio2(IXAudio2* ptr)
|
||||
{
|
||||
ptr->Release();
|
||||
}
|
||||
|
||||
bool XAudio2_7::InitLibrary()
|
||||
{
|
||||
if (m_xaudio2_dll)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
m_xaudio2_dll = ::LoadLibrary(TEXT("xaudio2_7.dll"));
|
||||
|
||||
return m_xaudio2_dll != nullptr;
|
||||
}
|
||||
|
||||
XAudio2_7::XAudio2_7()
|
||||
: m_mastering_voice(nullptr), m_volume(1.0f),
|
||||
m_cleanup_com(SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
|
||||
{
|
||||
}
|
||||
|
||||
XAudio2_7::~XAudio2_7()
|
||||
{
|
||||
Stop();
|
||||
if (m_cleanup_com)
|
||||
CoUninitialize();
|
||||
}
|
||||
|
||||
bool XAudio2_7::Init()
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
// callback doesn't seem to run on a specific CPU anyways
|
||||
IXAudio2* xaudptr;
|
||||
if (FAILED(hr = XAudio2Create(&xaudptr, 0, XAUDIO2_DEFAULT_PROCESSOR)))
|
||||
{
|
||||
PanicAlert("XAudio2_7 init failed: %#X", hr);
|
||||
Stop();
|
||||
return false;
|
||||
}
|
||||
m_xaudio2 = std::unique_ptr<IXAudio2, Releaser>(xaudptr);
|
||||
|
||||
// XAudio2 master voice
|
||||
// XAUDIO2_DEFAULT_CHANNELS instead of 2 for expansion?
|
||||
uint channels = NUM_CHANNELS;
|
||||
if (SConfig::GetInstance().bDPL2Decoder)
|
||||
channels = NUM_CHANNELS_SURROUND;
|
||||
|
||||
if (FAILED(hr = m_xaudio2->CreateMasteringVoice(&m_mastering_voice, channels,
|
||||
m_mixer->GetSampleRate())))
|
||||
{
|
||||
PanicAlert("XAudio2_7 master voice creation failed: %#X", hr);
|
||||
Stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Volume
|
||||
m_mastering_voice->SetVolume(m_volume);
|
||||
|
||||
m_voice_context = std::unique_ptr<StreamingVoiceContext2_7>(
|
||||
new StreamingVoiceContext2_7(m_xaudio2.get(), m_mixer.get(), m_sound_sync_event));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void XAudio2_7::SetVolume(int volume)
|
||||
{
|
||||
// linear 1- .01
|
||||
m_volume = (float)volume / 100.f;
|
||||
|
||||
if (m_mastering_voice)
|
||||
m_mastering_voice->SetVolume(m_volume);
|
||||
}
|
||||
|
||||
bool XAudio2_7::SetRunning(bool running)
|
||||
{
|
||||
if (!m_voice_context)
|
||||
return false;
|
||||
|
||||
if (running)
|
||||
m_voice_context->Play();
|
||||
else
|
||||
m_voice_context->Stop();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void XAudio2_7::Stop()
|
||||
{
|
||||
// m_sound_sync_event.Set();
|
||||
|
||||
m_voice_context.reset();
|
||||
|
||||
if (m_mastering_voice)
|
||||
{
|
||||
m_mastering_voice->DestroyVoice();
|
||||
m_mastering_voice = nullptr;
|
||||
}
|
||||
|
||||
m_xaudio2.reset(); // release interface
|
||||
|
||||
if (m_xaudio2_dll)
|
||||
{
|
||||
::FreeLibrary(m_xaudio2_dll);
|
||||
m_xaudio2_dll = nullptr;
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
// Copyright 2013 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
// This audio backend uses XAudio2 via XAudio2_7.dll
|
||||
// This version of the library is included in the June 2010 DirectX SDK and
|
||||
// works on all versions of Windows, however the SDK and/or redist must be
|
||||
// separately installed.
|
||||
// Therefore this backend is available iff:
|
||||
// * SDK is available at compile-time
|
||||
// * runtime dll is available at runtime
|
||||
// Dolphin ships the relevant SDK headers in Externals, so it's always available.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "AudioCommon/SoundStream.h"
|
||||
#include "Common/Event.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
struct StreamingVoiceContext2_7;
|
||||
struct IXAudio2;
|
||||
struct IXAudio2MasteringVoice;
|
||||
|
||||
#endif
|
||||
|
||||
class XAudio2_7 final : public SoundStream
|
||||
{
|
||||
#ifdef _WIN32
|
||||
|
||||
private:
|
||||
static void ReleaseIXAudio2(IXAudio2* ptr);
|
||||
|
||||
class Releaser
|
||||
{
|
||||
public:
|
||||
template <typename R>
|
||||
void operator()(R* ptr)
|
||||
{
|
||||
ReleaseIXAudio2(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IXAudio2, Releaser> m_xaudio2;
|
||||
std::unique_ptr<StreamingVoiceContext2_7> m_voice_context;
|
||||
IXAudio2MasteringVoice* m_mastering_voice;
|
||||
|
||||
Common::Event m_sound_sync_event;
|
||||
float m_volume;
|
||||
|
||||
const bool m_cleanup_com;
|
||||
|
||||
static HMODULE m_xaudio2_dll;
|
||||
|
||||
static bool InitLibrary();
|
||||
void Stop();
|
||||
|
||||
public:
|
||||
XAudio2_7();
|
||||
~XAudio2_7() override;
|
||||
|
||||
bool Init() override;
|
||||
|
||||
bool SetRunning(bool running) override;
|
||||
void SetVolume(int volume) override;
|
||||
|
||||
static bool isValid() { return InitLibrary(); }
|
||||
#endif
|
||||
};
|
@ -53,7 +53,6 @@ struct BootParameters;
|
||||
#define BACKEND_CUBEB "Cubeb"
|
||||
#define BACKEND_OPENAL "OpenAL"
|
||||
#define BACKEND_PULSEAUDIO "Pulse"
|
||||
#define BACKEND_XAUDIO2 "XAudio2"
|
||||
#define BACKEND_OPENSLES "OpenSLES"
|
||||
#define BACKEND_WASAPI _trans("WASAPI (Exclusive Mode)")
|
||||
|
||||
|
Reference in New Issue
Block a user