99SoundManager::SoundManager (QObject *parent, Preferences *preferences)
1010 : QObject(parent), preferences_(preferences), audioEngineInitialized_(false ), soundInitialized_(false ), shouldLoop_(false ), connectedEventQueued_(false )
1111{
12- initialize (); // NOLINT
1312}
1413
1514SoundManager::~SoundManager ()
1615{
17- cleanup ();
18- }
19-
20- void SoundManager::initialize ()
21- {
22- ma_result result = ma_engine_init (NULL , &audioEngine_);
23- if (result != MA_SUCCESS) {
24- qCDebug (LOG_BASIC) << " Failed to initialize miniaudio engine: " << result;
25- return ;
26- }
27- audioEngineInitialized_ = true ;
28- }
29-
30- void SoundManager::cleanup ()
31- {
32- if (audioEngineInitialized_) {
33- cleanupCurrentSoundData ();
34- ma_engine_uninit (&audioEngine_);
35- audioEngineInitialized_ = false ;
36- }
16+ cleanupCurrentSoundData ();
3717}
3818
3919void SoundManager::handleQueuedConnectedEvent ()
@@ -84,12 +64,16 @@ void SoundManager::stop()
8464void SoundManager::playSound (const QString &path, bool loop)
8565{
8666 qCDebug (LOG_BASIC) << " Playing sound" << path;
87- if (!audioEngineInitialized_) {
88- qCDebug (LOG_BASIC) << " Audio engine not initialized" ;
67+
68+ cleanupCurrentSoundData ();
69+
70+ ma_result result = ma_engine_init (NULL , &audioEngine_);
71+ if (result != MA_SUCCESS) {
72+ qCDebug (LOG_BASIC) << " Failed to initialize miniaudio engine: " << result;
8973 return ;
9074 }
91- cleanupCurrentSoundData () ;
92- ma_result result;
75+ audioEngineInitialized_ = true ;
76+
9377 if (path.startsWith (" :/sounds" )) { // load from resources
9478 QFile file (path);
9579 if (!file.open (QIODevice::ReadOnly)) {
@@ -140,6 +124,7 @@ void SoundManager::playSound(const QString &path, bool loop)
140124
141125void SoundManager::cleanupCurrentSoundData ()
142126{
127+ qCDebug (LOG_BASIC) << " Cleaning up current sound data" ;
143128 if (soundInitialized_) {
144129 ma_sound_stop (&sound_);
145130 ma_sound_uninit (&sound_);
@@ -150,6 +135,12 @@ void SoundManager::cleanupCurrentSoundData()
150135 ma_decoder_uninit (&decoder_);
151136 decoderInitialized_ = false ;
152137 }
138+
139+ if (audioEngineInitialized_) {
140+ ma_engine_uninit (&audioEngine_);
141+ audioEngineInitialized_ = false ;
142+ }
143+
153144 audioBuffer_.clear ();
154145}
155146
0 commit comments