From 043d1ac111ffb3ec62e3bcd263f1294726a7389e Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 16 Jan 2025 17:45:44 +0100 Subject: [PATCH] Codechange: use std::vector/std::unique_ptr over new/malloc --- src/sound/xaudio2_s.cpp | 62 +++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/src/sound/xaudio2_s.cpp b/src/sound/xaudio2_s.cpp index 4b8d8236a6..7c5d34a357 100644 --- a/src/sound/xaudio2_s.cpp +++ b/src/sound/xaudio2_s.cpp @@ -47,38 +47,30 @@ static FSoundDriver_XAudio2 iFSoundDriver_XAudio2; class StreamingVoiceContext : public IXAudio2VoiceCallback { private: - int bufferLength; - char *buffer; + std::vector buffer; public: - IXAudio2SourceVoice *SourceVoice; + IXAudio2SourceVoice *source_voice = nullptr; - StreamingVoiceContext(int bufferLength) + StreamingVoiceContext(int buffer_length) { - this->bufferLength = bufferLength; - this->buffer = MallocT(bufferLength); - } - - virtual ~StreamingVoiceContext() - { - free(this->buffer); + this->buffer.resize(buffer_length); } HRESULT SubmitBuffer() { // Ensure we do have a valid voice - if (this->SourceVoice == nullptr) - { + if (this->source_voice == nullptr) { return E_FAIL; } - MxMixSamples(this->buffer, this->bufferLength / 4); + MxMixSamples(this->buffer.data(), static_cast(this->buffer.size() / 4)); XAUDIO2_BUFFER buf = { 0 }; - buf.AudioBytes = this->bufferLength; - buf.pAudioData = (const BYTE *) this->buffer; + buf.AudioBytes = static_cast(this->buffer.size()); + buf.pAudioData = this->buffer.data(); - return SourceVoice->SubmitSourceBuffer(&buf); + return source_voice->SubmitSourceBuffer(&buf); } STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) override @@ -115,7 +107,7 @@ static HMODULE _xaudio_dll_handle; static IXAudio2SourceVoice *_source_voice = nullptr; static IXAudio2MasteringVoice *_mastering_voice = nullptr; static ComPtr _xaudio2; -static StreamingVoiceContext *_voice_context = nullptr; +static std::unique_ptr _voice_context; /** Create XAudio2 context with SEH exception checking. */ static HRESULT CreateXAudio(API_XAudio2Create xAudio2Create) @@ -142,16 +134,14 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par { HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); - if (FAILED(hr)) - { + if (FAILED(hr)) { Debug(driver, 0, "xaudio2_s: CoInitializeEx failed ({:08x})", (uint)hr); return "Failed to initialise COM"; } _xaudio_dll_handle = LoadLibraryA(XAUDIO2_DLL_A); - if (_xaudio_dll_handle == nullptr) - { + if (_xaudio_dll_handle == nullptr) { CoUninitialize(); Debug(driver, 0, "xaudio2_s: Unable to load " XAUDIO2_DLL_A); @@ -160,8 +150,7 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par API_XAudio2Create xAudio2Create = (API_XAudio2Create) GetProcAddress(_xaudio_dll_handle, "XAudio2Create"); - if (xAudio2Create == nullptr) - { + if (xAudio2Create == nullptr) { FreeLibrary(_xaudio_dll_handle); CoUninitialize(); @@ -172,8 +161,7 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par // Create the XAudio engine hr = CreateXAudio(xAudio2Create); - if (FAILED(hr)) - { + if (FAILED(hr)) { FreeLibrary(_xaudio_dll_handle); CoUninitialize(); @@ -184,8 +172,7 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par // Create a mastering voice hr = _xaudio2->CreateMasteringVoice(&_mastering_voice); - if (FAILED(hr)) - { + if (FAILED(hr)) { _xaudio2.Reset(); FreeLibrary(_xaudio_dll_handle); CoUninitialize(); @@ -208,10 +195,9 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par int bufsize = GetDriverParamInt(parm, "samples", 1024); bufsize = std::min(bufsize, UINT16_MAX); - _voice_context = new StreamingVoiceContext(bufsize * 4); + _voice_context = std::make_unique(bufsize * 4); - if (_voice_context == nullptr) - { + if (_voice_context == nullptr) { _mastering_voice->DestroyVoice(); _xaudio2.Reset(); FreeLibrary(_xaudio_dll_handle); @@ -220,10 +206,9 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par 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(); _xaudio2.Reset(); FreeLibrary(_xaudio_dll_handle); @@ -233,11 +218,10 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par return "Failed to create a source voice"; } - _voice_context->SourceVoice = _source_voice; + _voice_context->source_voice = _source_voice; hr = _source_voice->Start(0, 0); - if (FAILED(hr)) - { + if (FAILED(hr)) { Debug(driver, 0, "xaudio2_s: _source_voice->Start failed ({:08x})", (uint)hr); Stop(); @@ -249,8 +233,7 @@ std::optional SoundDriver_XAudio2::Start(const StringList &par // Submit the first buffer hr = _voice_context->SubmitBuffer(); - if (FAILED(hr)) - { + if (FAILED(hr)) { Debug(driver, 0, "xaudio2_s: _voice_context->SubmitBuffer failed ({:08x})", (uint)hr); Stop(); @@ -268,7 +251,6 @@ void SoundDriver_XAudio2::Stop() // Clean up XAudio2 _source_voice->DestroyVoice(); - delete _voice_context; _voice_context = nullptr; _mastering_voice->DestroyVoice();