mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::vector/std::unique_ptr over new/malloc
parent
8016cfc0ef
commit
043d1ac111
|
@ -47,38 +47,30 @@ static FSoundDriver_XAudio2 iFSoundDriver_XAudio2;
|
||||||
class StreamingVoiceContext : public IXAudio2VoiceCallback
|
class StreamingVoiceContext : public IXAudio2VoiceCallback
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int bufferLength;
|
std::vector<BYTE> buffer;
|
||||||
char *buffer;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IXAudio2SourceVoice *SourceVoice;
|
IXAudio2SourceVoice *source_voice = nullptr;
|
||||||
|
|
||||||
StreamingVoiceContext(int bufferLength)
|
StreamingVoiceContext(int buffer_length)
|
||||||
{
|
{
|
||||||
this->bufferLength = bufferLength;
|
this->buffer.resize(buffer_length);
|
||||||
this->buffer = MallocT<char>(bufferLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~StreamingVoiceContext()
|
|
||||||
{
|
|
||||||
free(this->buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HRESULT SubmitBuffer()
|
HRESULT SubmitBuffer()
|
||||||
{
|
{
|
||||||
// Ensure we do have a valid voice
|
// Ensure we do have a valid voice
|
||||||
if (this->SourceVoice == nullptr)
|
if (this->source_voice == nullptr) {
|
||||||
{
|
|
||||||
return E_FAIL;
|
return E_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
MxMixSamples(this->buffer, this->bufferLength / 4);
|
MxMixSamples(this->buffer.data(), static_cast<uint>(this->buffer.size() / 4));
|
||||||
|
|
||||||
XAUDIO2_BUFFER buf = { 0 };
|
XAUDIO2_BUFFER buf = { 0 };
|
||||||
buf.AudioBytes = this->bufferLength;
|
buf.AudioBytes = static_cast<UINT32>(this->buffer.size());
|
||||||
buf.pAudioData = (const BYTE *) this->buffer;
|
buf.pAudioData = this->buffer.data();
|
||||||
|
|
||||||
return SourceVoice->SubmitSourceBuffer(&buf);
|
return source_voice->SubmitSourceBuffer(&buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) override
|
STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) override
|
||||||
|
@ -115,7 +107,7 @@ static HMODULE _xaudio_dll_handle;
|
||||||
static IXAudio2SourceVoice *_source_voice = nullptr;
|
static IXAudio2SourceVoice *_source_voice = nullptr;
|
||||||
static IXAudio2MasteringVoice *_mastering_voice = nullptr;
|
static IXAudio2MasteringVoice *_mastering_voice = nullptr;
|
||||||
static ComPtr<IXAudio2> _xaudio2;
|
static ComPtr<IXAudio2> _xaudio2;
|
||||||
static StreamingVoiceContext *_voice_context = nullptr;
|
static std::unique_ptr<StreamingVoiceContext> _voice_context;
|
||||||
|
|
||||||
/** Create XAudio2 context with SEH exception checking. */
|
/** Create XAudio2 context with SEH exception checking. */
|
||||||
static HRESULT CreateXAudio(API_XAudio2Create xAudio2Create)
|
static HRESULT CreateXAudio(API_XAudio2Create xAudio2Create)
|
||||||
|
@ -142,16 +134,14 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
{
|
{
|
||||||
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr)) {
|
||||||
{
|
|
||||||
Debug(driver, 0, "xaudio2_s: CoInitializeEx failed ({:08x})", (uint)hr);
|
Debug(driver, 0, "xaudio2_s: CoInitializeEx failed ({:08x})", (uint)hr);
|
||||||
return "Failed to initialise COM";
|
return "Failed to initialise COM";
|
||||||
}
|
}
|
||||||
|
|
||||||
_xaudio_dll_handle = LoadLibraryA(XAUDIO2_DLL_A);
|
_xaudio_dll_handle = LoadLibraryA(XAUDIO2_DLL_A);
|
||||||
|
|
||||||
if (_xaudio_dll_handle == nullptr)
|
if (_xaudio_dll_handle == nullptr) {
|
||||||
{
|
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
|
||||||
Debug(driver, 0, "xaudio2_s: Unable to load " XAUDIO2_DLL_A);
|
Debug(driver, 0, "xaudio2_s: Unable to load " XAUDIO2_DLL_A);
|
||||||
|
@ -160,8 +150,7 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
|
|
||||||
API_XAudio2Create xAudio2Create = (API_XAudio2Create) GetProcAddress(_xaudio_dll_handle, "XAudio2Create");
|
API_XAudio2Create xAudio2Create = (API_XAudio2Create) GetProcAddress(_xaudio_dll_handle, "XAudio2Create");
|
||||||
|
|
||||||
if (xAudio2Create == nullptr)
|
if (xAudio2Create == nullptr) {
|
||||||
{
|
|
||||||
FreeLibrary(_xaudio_dll_handle);
|
FreeLibrary(_xaudio_dll_handle);
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
|
||||||
|
@ -172,8 +161,7 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
// Create the XAudio engine
|
// Create the XAudio engine
|
||||||
hr = CreateXAudio(xAudio2Create);
|
hr = CreateXAudio(xAudio2Create);
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr)) {
|
||||||
{
|
|
||||||
FreeLibrary(_xaudio_dll_handle);
|
FreeLibrary(_xaudio_dll_handle);
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
|
||||||
|
@ -184,8 +172,7 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
// Create a mastering voice
|
// Create a mastering voice
|
||||||
hr = _xaudio2->CreateMasteringVoice(&_mastering_voice);
|
hr = _xaudio2->CreateMasteringVoice(&_mastering_voice);
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr)) {
|
||||||
{
|
|
||||||
_xaudio2.Reset();
|
_xaudio2.Reset();
|
||||||
FreeLibrary(_xaudio_dll_handle);
|
FreeLibrary(_xaudio_dll_handle);
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
@ -208,10 +195,9 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
int bufsize = GetDriverParamInt(parm, "samples", 1024);
|
int bufsize = GetDriverParamInt(parm, "samples", 1024);
|
||||||
bufsize = std::min<int>(bufsize, UINT16_MAX);
|
bufsize = std::min<int>(bufsize, UINT16_MAX);
|
||||||
|
|
||||||
_voice_context = new StreamingVoiceContext(bufsize * 4);
|
_voice_context = std::make_unique<StreamingVoiceContext>(bufsize * 4);
|
||||||
|
|
||||||
if (_voice_context == nullptr)
|
if (_voice_context == nullptr) {
|
||||||
{
|
|
||||||
_mastering_voice->DestroyVoice();
|
_mastering_voice->DestroyVoice();
|
||||||
_xaudio2.Reset();
|
_xaudio2.Reset();
|
||||||
FreeLibrary(_xaudio_dll_handle);
|
FreeLibrary(_xaudio_dll_handle);
|
||||||
|
@ -220,10 +206,9 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
return "Failed to create streaming voice context";
|
return "Failed to create streaming voice context";
|
||||||
}
|
}
|
||||||
|
|
||||||
hr = _xaudio2->CreateSourceVoice(&_source_voice, &wfex, 0, 1.0f, _voice_context);
|
hr = _xaudio2->CreateSourceVoice(&_source_voice, &wfex, 0, 1.0f, _voice_context.get());
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr)) {
|
||||||
{
|
|
||||||
_mastering_voice->DestroyVoice();
|
_mastering_voice->DestroyVoice();
|
||||||
_xaudio2.Reset();
|
_xaudio2.Reset();
|
||||||
FreeLibrary(_xaudio_dll_handle);
|
FreeLibrary(_xaudio_dll_handle);
|
||||||
|
@ -233,11 +218,10 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
return "Failed to create a source voice";
|
return "Failed to create a source voice";
|
||||||
}
|
}
|
||||||
|
|
||||||
_voice_context->SourceVoice = _source_voice;
|
_voice_context->source_voice = _source_voice;
|
||||||
hr = _source_voice->Start(0, 0);
|
hr = _source_voice->Start(0, 0);
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr)) {
|
||||||
{
|
|
||||||
Debug(driver, 0, "xaudio2_s: _source_voice->Start failed ({:08x})", (uint)hr);
|
Debug(driver, 0, "xaudio2_s: _source_voice->Start failed ({:08x})", (uint)hr);
|
||||||
|
|
||||||
Stop();
|
Stop();
|
||||||
|
@ -249,8 +233,7 @@ std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &par
|
||||||
// Submit the first buffer
|
// Submit the first buffer
|
||||||
hr = _voice_context->SubmitBuffer();
|
hr = _voice_context->SubmitBuffer();
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr)) {
|
||||||
{
|
|
||||||
Debug(driver, 0, "xaudio2_s: _voice_context->SubmitBuffer failed ({:08x})", (uint)hr);
|
Debug(driver, 0, "xaudio2_s: _voice_context->SubmitBuffer failed ({:08x})", (uint)hr);
|
||||||
|
|
||||||
Stop();
|
Stop();
|
||||||
|
@ -268,7 +251,6 @@ void SoundDriver_XAudio2::Stop()
|
||||||
// Clean up XAudio2
|
// Clean up XAudio2
|
||||||
_source_voice->DestroyVoice();
|
_source_voice->DestroyVoice();
|
||||||
|
|
||||||
delete _voice_context;
|
|
||||||
_voice_context = nullptr;
|
_voice_context = nullptr;
|
||||||
|
|
||||||
_mastering_voice->DestroyVoice();
|
_mastering_voice->DestroyVoice();
|
||||||
|
|
Loading…
Reference in New Issue