Skip to content

Commit

Permalink
fix: Init Miniaudio context when opening the driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
na2axl committed Oct 29, 2024
1 parent 3e22812 commit ccbd929
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::ctype<wchar_t>>(wstm.getloc());
for (char i : str)
wstm << ctfacet.widen(i);
return wstm.str();
std::wostringstream s;
const auto& f = std::use_facet<std::ctype<wchar_t>>(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<std::ctype<wchar_t>>(stm.getloc());
for (wchar_t i : str)
stm << ctfacet.narrow(i, 0);
return stm.str();
std::ostringstream s;
const auto& f = std::use_facet<std::ctype<wchar_t>>(s.getloc());
for (const wchar_t& i : str)
s << f.narrow(i, 0);
return s.str();
}

// Conversion between OS strings and default strings
Expand Down
53 changes: 27 additions & 26 deletions src/Core/Drivers/MiniAudio/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AmInt16>(device.mRequestedOutputChannels);

ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ccbd929

Please sign in to comment.