Commit 27315f2f authored by nanahira's avatar nanahira

fix sound conflict

parent d405b636
...@@ -36,7 +36,6 @@ bool SoundManager::Init() { ...@@ -36,7 +36,6 @@ bool SoundManager::Init() {
} }
engineConfig.pResourceManager = &resourceManager; engineConfig.pResourceManager = &resourceManager;
#endif #endif
playingSoundEffect = FALSE;
if(ma_engine_init(&engineConfig, &engineSound) != MA_SUCCESS || ma_engine_init(&engineConfig, &engineMusic) != MA_SUCCESS) { if(ma_engine_init(&engineConfig, &engineSound) != MA_SUCCESS || ma_engine_init(&engineConfig, &engineMusic) != MA_SUCCESS) {
return false; return false;
} else { } else {
...@@ -90,21 +89,41 @@ void SoundManager::PlaySound(char* sound) { ...@@ -90,21 +89,41 @@ void SoundManager::PlaySound(char* sound) {
#ifdef YGOPRO_USE_AUDIO #ifdef YGOPRO_USE_AUDIO
if(!mainGame->chkEnableSound->isChecked()) if(!mainGame->chkEnableSound->isChecked())
return; return;
#ifdef YGOPRO_USE_MINIAUDIO
StopSound();
SetSoundVolume(mainGame->gameConf.sound_volume); SetSoundVolume(mainGame->gameConf.sound_volume);
playingSoundEffect = TRUE; #ifdef YGOPRO_USE_MINIAUDIO
ma_sound *usingSoundEffectPointer = nullptr;
for(int i = 0; i < 10; i++) {
if(playingSoundEffect[i] && !ma_sound_is_playing(playingSoundEffect[i])) {
ma_sound_uninit(playingSoundEffect[i]);
if(usingSoundEffectPointer) {
free(playingSoundEffect[i]);
playingSoundEffect[i] = nullptr;
printf("free sound %d\n", i);
} else {
usingSoundEffectPointer = playingSoundEffect[i];
printf("reusing sound %d\n", i);
}
}
if(!playingSoundEffect[i] && !usingSoundEffectPointer) {
usingSoundEffectPointer = playingSoundEffect[i] = (ma_sound*)malloc(sizeof(ma_sound));
printf("alloc sound %d\n", i);
}
}
if (!usingSoundEffectPointer) {
// force to stop the first sound
usingSoundEffectPointer = playingSoundEffect[0];
ma_sound_uninit(usingSoundEffectPointer);
}
#ifdef _WIN32 #ifdef _WIN32
wchar_t sound_w[1024]; wchar_t sound_w[1024];
BufferIO::DecodeUTF8(sound, sound_w); BufferIO::DecodeUTF8(sound, sound_w);
ma_sound_init_from_file_w(&engineSound, sound_w, MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, nullptr, nullptr, &soundEffect); ma_sound_init_from_file_w(&engineSound, sound_w, MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, nullptr, nullptr, usingSoundEffectPointer);
#else #else
ma_sound_init_from_file(&engineSound, sound, MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, nullptr, nullptr, &soundEffect); ma_sound_init_from_file(&engineSound, sound, MA_SOUND_FLAG_ASYNC | MA_SOUND_FLAG_STREAM, nullptr, nullptr, usingSoundEffectPointer);
#endif #endif
ma_sound_start(&soundEffect); ma_sound_start(usingSoundEffectPointer);
#endif #endif
#ifdef YGOPRO_USE_IRRKLANG #ifdef YGOPRO_USE_IRRKLANG
SetSoundVolume(mainGame->gameConf.sound_volume);
engineSound->play2D(soundPath); engineSound->play2D(soundPath);
#endif #endif
#endif #endif
...@@ -358,10 +377,13 @@ void SoundManager::StopBGM() { ...@@ -358,10 +377,13 @@ void SoundManager::StopBGM() {
} }
void SoundManager::StopSound() { void SoundManager::StopSound() {
#ifdef YGOPRO_USE_MINIAUDIO #ifdef YGOPRO_USE_MINIAUDIO
if(!playingSoundEffect) for(int i = 0; i < 10; i++) {
return; if(playingSoundEffect[i]) {
playingSoundEffect = FALSE; ma_sound_uninit(playingSoundEffect[i]);
ma_sound_uninit(&soundEffect); free(playingSoundEffect[i]);
playingSoundEffect[i] = nullptr;
}
}
#endif #endif
#ifdef YGOPRO_USE_IRRKLANG #ifdef YGOPRO_USE_IRRKLANG
engineSound->stopAllSounds(); engineSound->stopAllSounds();
......
...@@ -29,8 +29,7 @@ private: ...@@ -29,8 +29,7 @@ private:
ma_engine engineMusic; ma_engine engineMusic;
ma_sound soundBGM; ma_sound soundBGM;
char currentPlayingMusic[1024]{}; char currentPlayingMusic[1024]{};
ma_sound soundEffect; ma_sound* playingSoundEffect[10]{};
char playingSoundEffect;
#endif #endif
#ifdef YGOPRO_USE_IRRKLANG #ifdef YGOPRO_USE_IRRKLANG
irrklang::ISoundEngine* engineSound; irrklang::ISoundEngine* engineSound;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment