From efa069ef800c94bb7ac1d950e3af0d9d78459ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Tue, 18 Jun 2024 22:53:36 +0200 Subject: [PATCH] Ensure sample indices are initialized also with sound disabled The assertion of #8262 can be reproduced when sound is disabled or failed to be initialized, as the sample indices where not being initialized properly in these cases. It is still necessary to initialized them so sounds can be loaded in the editor also when sound is disabled. The potential thread-safety issues of the `CSound::AllocSample` function are not yet resolved so the issue remains open. --- src/engine/client/sound.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp index 3bfb8294e..9c9881b12 100644 --- a/src/engine/client/sound.cpp +++ b/src/engine/client/sound.cpp @@ -207,6 +207,18 @@ int CSound::Init() m_pGraphics = Kernel()->RequestInterface(); m_pStorage = Kernel()->RequestInterface(); + // Initialize sample indices. We always need them to load sounds in + // the editor even if sound is disabled or failed to be enabled. + m_FirstFreeSampleIndex = 0; + for(size_t i = 0; i < std::size(m_aSamples) - 1; ++i) + { + m_aSamples[i].m_Index = i; + m_aSamples[i].m_NextFreeSampleIndex = i + 1; + m_aSamples[i].m_pData = nullptr; + } + m_aSamples[std::size(m_aSamples) - 1].m_Index = std::size(m_aSamples) - 1; + m_aSamples[std::size(m_aSamples) - 1].m_NextFreeSampleIndex = SAMPLE_INDEX_FULL; + if(!g_Config.m_SndEnable) return 0; @@ -228,7 +240,6 @@ int CSound::Init() // Open the audio device and start playing sound! m_Device = SDL_OpenAudioDevice(nullptr, 0, &Format, &FormatOut, 0); - if(m_Device == 0) { dbg_msg("sound", "unable to open audio: %s", SDL_GetError()); @@ -243,15 +254,6 @@ int CSound::Init() #endif m_pMixBuffer = (int *)calloc(m_MaxFrames * 2, sizeof(int)); - m_FirstFreeSampleIndex = 0; - for(size_t i = 0; i < std::size(m_aSamples) - 1; ++i) - { - m_aSamples[i].m_Index = i; - m_aSamples[i].m_NextFreeSampleIndex = i + 1; - } - m_aSamples[std::size(m_aSamples) - 1].m_Index = std::size(m_aSamples) - 1; - m_aSamples[std::size(m_aSamples) - 1].m_NextFreeSampleIndex = SAMPLE_INDEX_FULL; - SDL_PauseAudioDevice(m_Device, 0); m_SoundEnabled = true;