From ccbd9297c8dfb29838d1d06377468f6759de962e Mon Sep 17 00:00:00 2001 From: Axel Nana Date: Tue, 29 Oct 2024 16:03:48 +0100 Subject: [PATCH] fix: Init Miniaudio context when opening the driver. --- .../Core/Common/Platforms/Windows/Config.h | 20 +++---- src/Core/Drivers/MiniAudio/Driver.cpp | 53 ++++++++++--------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/include/SparkyStudios/Audio/Amplitude/Core/Common/Platforms/Windows/Config.h b/include/SparkyStudios/Audio/Amplitude/Core/Common/Platforms/Windows/Config.h index 9649b81..7647a73 100644 --- a/include/SparkyStudios/Audio/Amplitude/Core/Common/Platforms/Windows/Config.h +++ b/include/SparkyStudios/Audio/Amplitude/Core/Common/Platforms/Windows/Config.h @@ -68,20 +68,20 @@ static AM_INLINE std::wstring am_string_widen(const std::string& str) { - std::wostringstream wstm; - const auto& ctfacet = std::use_facet>(wstm.getloc()); - for (char i : str) - wstm << ctfacet.widen(i); - return wstm.str(); + std::wostringstream s; + const auto& f = std::use_facet>(s.getloc()); + for (const char& i : str) + s << f.widen(i); + return s.str(); } static AM_INLINE std::string am_wstring_narrow(const std::wstring& str) { - std::ostringstream stm; - const auto& ctfacet = std::use_facet>(stm.getloc()); - for (wchar_t i : str) - stm << ctfacet.narrow(i, 0); - return stm.str(); + std::ostringstream s; + const auto& f = std::use_facet>(s.getloc()); + for (const wchar_t& i : str) + s << f.narrow(i, 0); + return s.str(); } // Conversion between OS strings and default strings diff --git a/src/Core/Drivers/MiniAudio/Driver.cpp b/src/Core/Drivers/MiniAudio/Driver.cpp index 2df87f1..04db69e 100644 --- a/src/Core/Drivers/MiniAudio/Driver.cpp +++ b/src/Core/Drivers/MiniAudio/Driver.cpp @@ -134,43 +134,38 @@ namespace SparkyStudios::Audio::Amplitude , _log() , _context() , _devices() - { - _logCallback = ma_log_callback_init(miniaudio_log, nullptr); - if (ma_log_init(nullptr, &_log) != MA_SUCCESS) - { - amLogCritical("Failed to initialize miniaudio log."); - return; - } - - ma_log_register_callback(&_log, _logCallback); - - ma_context_config config = ma_context_config_init(); - config.pLog = &_log; - config.pUserData = this; - config.threadPriority = ma_thread_priority_realtime; - - if (ma_context_init(nullptr, 0, &config, &_context) != MA_SUCCESS) - { - amLogCritical("Failed to initialize miniaudio context"); - return; - } - } + {} MiniAudioDriver::~MiniAudioDriver() { if (_initialized) Close(); - - ma_log_unregister_callback(&_log, _logCallback); - ma_log_uninit(&_log); - - ma_context_uninit(&_context); } bool MiniAudioDriver::Open(const DeviceDescription& device) { if (!_initialized) { + _logCallback = ma_log_callback_init(miniaudio_log, nullptr); + if (ma_log_init(nullptr, &_log) != MA_SUCCESS) + { + amLogCritical("Failed to initialize miniaudio log."); + return false; + } + + ma_log_register_callback(&_log, _logCallback); + + ma_context_config config = ma_context_config_init(); + config.pLog = &_log; + config.pUserData = this; + config.threadPriority = ma_thread_priority_realtime; + + if (ma_context_init(nullptr, 0, &config, &_context) != MA_SUCCESS) + { + amLogCritical("Failed to initialize miniaudio context"); + return false; + } + const auto channelsCount = static_cast(device.mRequestedOutputChannels); ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); @@ -244,6 +239,12 @@ namespace SparkyStudios::Audio::Amplitude m_deviceDescription.mDeviceState = DeviceState::Closed; ma_device_uninit(&_device); + + ma_log_unregister_callback(&_log, _logCallback); + ma_log_uninit(&_log); + + ma_context_uninit(&_context); + _initialized = false; return true;