diff --git a/tools/chatgui/src/sound_manager.cpp b/tools/chatgui/src/sound_manager.cpp index 12d743d6..7b8813d3 100644 --- a/tools/chatgui/src/sound_manager.cpp +++ b/tools/chatgui/src/sound_manager.cpp @@ -35,6 +35,10 @@ void SoundManager::shutdown() { ma_sound_uninit(it->sound); delete it->sound; } + if (it->decoder) { + ma_decoder_uninit(it->decoder); + delete it->decoder; + } } m_sounds.clear(); @@ -81,33 +85,36 @@ bool SoundManager::loadSound(const QString& name, const QString& qrcPath) { qWarning("SoundManager: cannot open %s", qPrintable(qrcPath)); return false; } - QByteArray mp3Data = f.readAll(); + + LoadedSound ls; + ls.mp3Data = f.readAll(); f.close(); + ls.decoder = new ma_decoder; ma_decoder_config decConfig = ma_decoder_config_init_default(); - ma_decoder decoder; - ma_result result = ma_decoder_init_memory(mp3Data.constData(), mp3Data.size(), &decConfig, &decoder); + ma_result result = ma_decoder_init_memory(ls.mp3Data.constData(), ls.mp3Data.size(), + &decConfig, ls.decoder); if (result != MA_SUCCESS) { qWarning("SoundManager: ma_decoder_init_memory failed for %s (%d)", qPrintable(name), result); + delete ls.decoder; return false; } - LoadedSound ls; ls.sound = new ma_sound; - result = ma_sound_init_from_data_source(m_engine, (ma_data_source*)&decoder.ds, - MA_SOUND_FLAG_DECODE, - NULL, ls.sound); - ma_decoder_uninit(&decoder); - + result = ma_sound_init_from_data_source(m_engine, (ma_data_source*)&ls.decoder->ds, + 0, NULL, ls.sound); if (result != MA_SUCCESS) { qWarning("SoundManager: sound init failed for %s (%d)", qPrintable(name), result); + ma_decoder_uninit(ls.decoder); + delete ls.decoder; delete ls.sound; return false; } + ma_sound_set_volume(ls.sound, m_volume); m_sounds.insert(name, ls); qDebug("SoundManager: loaded '%s' from %s (%lld bytes)", - qPrintable(name), qPrintable(qrcPath), (long long)mp3Data.size()); + qPrintable(name), qPrintable(qrcPath), (long long)ls.mp3Data.size()); return true; } diff --git a/tools/chatgui/src/sound_manager.h b/tools/chatgui/src/sound_manager.h index fa0255d5..48e78bd2 100644 --- a/tools/chatgui/src/sound_manager.h +++ b/tools/chatgui/src/sound_manager.h @@ -3,8 +3,10 @@ #include #include #include +#include struct ma_engine; +struct ma_decoder; struct ma_sound; class SoundManager : public QObject { @@ -28,6 +30,8 @@ private: ~SoundManager(); struct LoadedSound { + QByteArray mp3Data; + ma_decoder* decoder = nullptr; ma_sound* sound = nullptr; };