2013-10-19 09:27:57 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-10-19 09:27:57 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-08-23 01:18:37 +00:00
|
|
|
// 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>
|
2017-08-09 19:58:30 +00:00
|
|
|
#include <variant>
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "AudioCommon/AudioCommon.h"
|
|
|
|
#include "AudioCommon/XAudio2_7Stream.h"
|
2014-04-13 23:15:23 +00:00
|
|
|
#include "Common/Event.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2017-08-09 19:58:30 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2013-10-19 09:27:57 +00:00
|
|
|
|
|
|
|
struct StreamingVoiceContext2_7 : public IXAudio2VoiceCallback
|
|
|
|
{
|
|
|
|
private:
|
2017-06-26 21:41:12 +00:00
|
|
|
Mixer* const m_mixer;
|
2016-06-24 08:43:46 +00:00
|
|
|
Common::Event& m_sound_sync_event;
|
|
|
|
IXAudio2SourceVoice* m_source_voice;
|
2017-08-09 19:58:30 +00:00
|
|
|
std::variant<std::unique_ptr<short[]>, std::unique_ptr<float[]>> xaudio_buffer;
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void SubmitBuffer(PBYTE buf_data);
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2017-08-09 19:58:30 +00:00
|
|
|
bool m_use_surround = SConfig::GetInstance().bDPL2Decoder;
|
|
|
|
|
2013-10-19 09:27:57 +00:00
|
|
|
public:
|
2017-06-26 21:41:12 +00:00
|
|
|
StreamingVoiceContext2_7(IXAudio2* pXAudio2, Mixer* pMixer, Common::Event& pSyncEvent);
|
2017-06-07 22:25:13 +00:00
|
|
|
virtual ~StreamingVoiceContext2_7();
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2017-02-16 04:37:04 +00:00
|
|
|
void Stop();
|
|
|
|
void Play();
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
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);
|
2013-10-19 09:27:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int NUM_BUFFERS = 3;
|
|
|
|
const int SAMPLES_PER_BUFFER = 96;
|
2017-08-09 19:58:30 +00:00
|
|
|
const int SAMPLES_PER_BUFFER_SURROUND = 256;
|
2013-10-19 09:27:57 +00:00
|
|
|
|
|
|
|
const int NUM_CHANNELS = 2;
|
|
|
|
const int BUFFER_SIZE = SAMPLES_PER_BUFFER * NUM_CHANNELS;
|
|
|
|
const int BUFFER_SIZE_BYTES = BUFFER_SIZE * sizeof(s16);
|
|
|
|
|
2017-08-09 19:58:30 +00:00
|
|
|
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);
|
2013-10-19 09:27:57 +00:00
|
|
|
void StreamingVoiceContext2_7::SubmitBuffer(PBYTE buf_data)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
XAUDIO2_BUFFER buf = {};
|
2017-08-09 19:58:30 +00:00
|
|
|
if (m_use_surround)
|
|
|
|
{
|
|
|
|
buf.AudioBytes = BUFFER_SIZE_BYTES_SURROUND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.AudioBytes = BUFFER_SIZE_BYTES;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
buf.pContext = buf_data;
|
|
|
|
buf.pAudioData = buf_data;
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_source_voice->SubmitSourceBuffer(&buf);
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 21:41:12 +00:00
|
|
|
StreamingVoiceContext2_7::StreamingVoiceContext2_7(IXAudio2* pXAudio2, Mixer* pMixer,
|
2016-06-24 08:43:46 +00:00
|
|
|
Common::Event& pSyncEvent)
|
2017-08-09 19:58:30 +00:00
|
|
|
: m_mixer(pMixer), m_sound_sync_event(pSyncEvent)
|
2013-10-19 09:27:57 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
WAVEFORMATEXTENSIBLE wfx = {};
|
|
|
|
wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
|
|
|
wfx.Format.nSamplesPerSec = m_mixer->GetSampleRate();
|
2017-08-09 19:58:30 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
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
|
2017-08-09 19:58:30 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamingVoiceContext2_7::~StreamingVoiceContext2_7()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_source_voice)
|
|
|
|
{
|
|
|
|
m_source_voice->Stop();
|
|
|
|
m_source_voice->DestroyVoice();
|
|
|
|
}
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingVoiceContext2_7::Stop()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_source_voice)
|
|
|
|
m_source_voice->Stop();
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingVoiceContext2_7::Play()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_source_voice)
|
|
|
|
m_source_voice->Start();
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingVoiceContext2_7::OnBufferEnd(void* context)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// buffer end callback; gets SAMPLES_PER_BUFFER samples for a new buffer
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_source_voice || !context)
|
|
|
|
return;
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// m_sound_sync_event->Wait(); // sync
|
|
|
|
// m_sound_sync_event->Spin(); // or tight sync
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2017-08-09 19:58:30 +00:00
|
|
|
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));
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HMODULE XAudio2_7::m_xaudio2_dll = nullptr;
|
|
|
|
|
|
|
|
void XAudio2_7::ReleaseIXAudio2(IXAudio2* ptr)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
ptr->Release();
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool XAudio2_7::InitLibrary()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_xaudio2_dll)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_xaudio2_dll = ::LoadLibrary(TEXT("xaudio2_7.dll"));
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return m_xaudio2_dll != nullptr;
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 08:13:02 +00:00
|
|
|
XAudio2_7::XAudio2_7()
|
2016-06-24 08:43:46 +00:00
|
|
|
: m_mastering_voice(nullptr), m_volume(1.0f),
|
|
|
|
m_cleanup_com(SUCCEEDED(CoInitializeEx(nullptr, COINIT_MULTITHREADED)))
|
2013-10-19 09:27:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XAudio2_7::~XAudio2_7()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Stop();
|
|
|
|
if (m_cleanup_com)
|
|
|
|
CoUninitialize();
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
bool XAudio2_7::Init()
|
2013-10-19 09:27:57 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
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?
|
2017-08-09 19:58:30 +00:00
|
|
|
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())))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
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;
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XAudio2_7::SetVolume(int volume)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// linear 1- .01
|
|
|
|
m_volume = (float)volume / 100.f;
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_mastering_voice)
|
|
|
|
m_mastering_voice->SetVolume(m_volume);
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
bool XAudio2_7::SetRunning(bool running)
|
2013-10-19 09:27:57 +00:00
|
|
|
{
|
2017-10-21 23:23:40 +00:00
|
|
|
if (!m_voice_context)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (running)
|
|
|
|
m_voice_context->Play();
|
|
|
|
else
|
|
|
|
m_voice_context->Stop();
|
|
|
|
|
|
|
|
return true;
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XAudio2_7::Stop()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// m_sound_sync_event.Set();
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_voice_context.reset();
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_mastering_voice)
|
|
|
|
{
|
|
|
|
m_mastering_voice->DestroyVoice();
|
|
|
|
m_mastering_voice = nullptr;
|
|
|
|
}
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_xaudio2.reset(); // release interface
|
2013-10-19 09:27:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_xaudio2_dll)
|
|
|
|
{
|
|
|
|
::FreeLibrary(m_xaudio2_dll);
|
|
|
|
m_xaudio2_dll = nullptr;
|
|
|
|
}
|
2013-10-19 09:27:57 +00:00
|
|
|
}
|