LCOV - code coverage report
Current view: top level - source/soundmanager - SoundManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 442 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 52 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "SoundManager.h"
      21             : 
      22             : #include "items/CBufferItem.h"
      23             : #include "items/CSoundItem.h"
      24             : #include "items/CStreamItem.h"
      25             : #include "lib/code_generation.h"
      26             : #include "lib/debug.h"
      27             : #include "lib/path.h"
      28             : #include "lib/sysdep/os.h"
      29             : #include "lib/timer.h"
      30             : #include "ps/CLogger.h"
      31             : #include "ps/CStr.h"
      32             : #include "ps/ConfigDB.h"
      33             : #include "ps/Filesystem.h"
      34             : #include "ps/Profiler2.h"
      35             : #include "ps/Threading.h"
      36             : #include "ps/XMB/XMBStorage.h"
      37             : #include "ps/XML/Xeromyces.h"
      38             : #include "soundmanager/ISoundManager.h"
      39             : #include "soundmanager/data/SoundData.h"
      40             : #include "soundmanager/items/ISoundItem.h"
      41             : #include "soundmanager/scripting/SoundGroup.h"
      42             : 
      43             : #include <SDL_timer.h>
      44             : #include <algorithm>
      45             : #include <cstring>
      46             : #include <thread>
      47             : #include <utility>
      48             : 
      49             : ISoundManager* g_SoundManager = NULL;
      50             : 
      51             : #define SOURCE_NUM 64
      52             : 
      53             : #if CONFIG2_AUDIO
      54             : 
      55             : class CSoundManagerWorker
      56             : {
      57             :     NONCOPYABLE(CSoundManagerWorker);
      58             : 
      59             : public:
      60           0 :     CSoundManagerWorker()
      61           0 :     {
      62           0 :         m_Items = new ItemsList;
      63           0 :         m_DeadItems = new ItemsList;
      64           0 :         m_Shutdown = false;
      65             : 
      66           0 :         m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
      67           0 :     }
      68             : 
      69           0 :     ~CSoundManagerWorker()
      70             :     {
      71           0 :         delete m_Items;
      72           0 :         CleanupItems();
      73           0 :         delete m_DeadItems;
      74           0 :     }
      75             : 
      76           0 :     bool Shutdown()
      77             :     {
      78           0 :         {
      79           0 :             std::lock_guard<std::mutex> lock(m_WorkerMutex);
      80             : 
      81           0 :             m_Shutdown = true;
      82             : 
      83           0 :             ItemsList::iterator lstr = m_Items->begin();
      84           0 :             while (lstr != m_Items->end())
      85             :             {
      86           0 :                 delete *lstr;
      87           0 :                 ++lstr;
      88             :             }
      89             : 
      90           0 :         }
      91             : 
      92           0 :         m_WorkerThread.join();
      93             : 
      94           0 :         return true;
      95             :     }
      96             : 
      97           0 :     void addItem(ISoundItem* anItem)
      98             :     {
      99           0 :         std::lock_guard<std::mutex> lock(m_WorkerMutex);
     100           0 :         m_Items->push_back(anItem);
     101           0 :     }
     102             : 
     103           0 :     void CleanupItems()
     104             :     {
     105           0 :         std::lock_guard<std::mutex> lock(m_DeadItemsMutex);
     106           0 :         AL_CHECK;
     107           0 :         ItemsList::iterator deadItems = m_DeadItems->begin();
     108           0 :         while (deadItems != m_DeadItems->end())
     109             :         {
     110           0 :             delete *deadItems;
     111           0 :             ++deadItems;
     112             : 
     113           0 :             AL_CHECK;
     114             :         }
     115           0 :         m_DeadItems->clear();
     116           0 :     }
     117             : 
     118             : private:
     119           0 :     static void RunThread(CSoundManagerWorker* data)
     120             :     {
     121           0 :         debug_SetThreadName("CSoundManagerWorker");
     122           0 :         g_Profiler2.RegisterCurrentThread("soundmanager");
     123             : 
     124           0 :         data->Run();
     125           0 :     }
     126             : 
     127           0 :     void Run()
     128             :     {
     129           0 :         while (true)
     130             :         {
     131             :             // Handle shutdown requests as soon as possible
     132           0 :             if (GetShutdown())
     133           0 :                 return;
     134             : 
     135           0 :             int pauseTime = 500;
     136           0 :             if (g_SoundManager->InDistress())
     137           0 :                 pauseTime = 50;
     138             : 
     139           0 :             {
     140           0 :                 std::lock_guard<std::mutex> workerLock(m_WorkerMutex);
     141             : 
     142           0 :                 ItemsList::iterator lstr = m_Items->begin();
     143           0 :                 ItemsList* nextItemList = new ItemsList;
     144             : 
     145           0 :                 while (lstr != m_Items->end())
     146             :                 {
     147           0 :                     AL_CHECK;
     148           0 :                     if ((*lstr)->IdleTask())
     149             :                     {
     150           0 :                         if ((pauseTime == 500) && (*lstr)->IsFading())
     151             :                             pauseTime = 100;
     152             : 
     153           0 :                         nextItemList->push_back(*lstr);
     154             :                     }
     155             :                     else
     156             :                     {
     157           0 :                         std::lock_guard<std::mutex> deadItemsLock(m_DeadItemsMutex);
     158           0 :                         m_DeadItems->push_back(*lstr);
     159           0 :                     }
     160           0 :                     ++lstr;
     161             : 
     162           0 :                     AL_CHECK;
     163             :                 }
     164             : 
     165           0 :                 delete m_Items;
     166           0 :                 m_Items = nextItemList;
     167             : 
     168           0 :                 AL_CHECK;
     169           0 :             }
     170           0 :             SDL_Delay(pauseTime);
     171           0 :         }
     172             :     }
     173             : 
     174           0 :     bool GetShutdown()
     175             :     {
     176           0 :         std::lock_guard<std::mutex> lock(m_WorkerMutex);
     177           0 :         return m_Shutdown;
     178           0 :     }
     179             : 
     180             : private:
     181             :     // Thread-related members:
     182             :     std::thread m_WorkerThread;
     183             :     std::mutex m_WorkerMutex;
     184             :     std::mutex m_DeadItemsMutex;
     185             : 
     186             :     // Shared by main thread and worker thread:
     187             :     // These variables are all protected by a mutexes
     188             :     ItemsList* m_Items;
     189             :     ItemsList* m_DeadItems;
     190             : 
     191             :     bool m_Shutdown;
     192             : 
     193             :     CSoundManagerWorker(ISoundManager* /*other*/){};
     194             : };
     195             : 
     196           0 : void ISoundManager::CreateSoundManager()
     197             : {
     198           0 :     if (g_SoundManager)
     199             :         return;
     200             : 
     201           0 :     ALCdevice* device = alcOpenDevice(nullptr);
     202           0 :     if (!device)
     203             :     {
     204           0 :         LOGWARNING("No audio device was found.");
     205           0 :         return;
     206             :     }
     207             : 
     208           0 :     g_SoundManager = new CSoundManager(device);
     209           0 :     g_SoundManager->StartWorker();
     210             : }
     211             : 
     212           0 : void ISoundManager::SetEnabled(bool doEnable)
     213             : {
     214           0 :     if (g_SoundManager && !doEnable)
     215           0 :         SAFE_DELETE(g_SoundManager);
     216           0 :     else if (!g_SoundManager && doEnable)
     217           0 :         ISoundManager::CreateSoundManager();
     218           0 : }
     219           0 : void ISoundManager::CloseGame()
     220             : {
     221           0 :     if (CSoundManager* aSndMgr = (CSoundManager*)g_SoundManager)
     222           0 :         aSndMgr->SetAmbientItem(NULL);
     223           0 : }
     224             : 
     225           0 : void CSoundManager::al_ReportError(ALenum err, const char* caller, int line)
     226             : {
     227           0 :     LOGERROR("OpenAL error: {}; called from {} (line {})\n", alGetString(err), caller, line);
     228           0 : }
     229             : 
     230           0 : void CSoundManager::al_check(const char* caller, int line)
     231             : {
     232           0 :     ALenum err = alGetError();
     233           0 :     if (err != AL_NO_ERROR)
     234           0 :         al_ReportError(err, caller, line);
     235           0 : }
     236             : 
     237           0 : Status CSoundManager::ReloadChangedFiles(const VfsPath&)
     238             : {
     239             :     // TODO implement sound file hotloading
     240           0 :     return INFO::OK;
     241             : }
     242             : 
     243           0 : /*static*/ Status CSoundManager::ReloadChangedFileCB(void* param, const VfsPath& path)
     244             : {
     245           0 :     return static_cast<CSoundManager*>(param)->ReloadChangedFiles(path);
     246             : }
     247             : 
     248           0 : CSoundManager::CSoundManager(ALCdevice* device)
     249           0 :     : m_Context(nullptr), m_Device(device), m_ALSourceBuffer(nullptr),
     250           0 :     m_CurrentTune(nullptr), m_CurrentEnvirons(nullptr),
     251           0 :     m_Worker(nullptr), m_DistressMutex(), m_PlayListItems(nullptr), m_SoundGroups(),
     252           0 :     m_Gain{g_ConfigDB.Get("sound.mastergain", 0.5f)},
     253           0 :     m_MusicGain{g_ConfigDB.Get("sound.musicgain", 0.5f)},
     254           0 :     m_AmbientGain{g_ConfigDB.Get("sound.ambientgain", 0.5f)},
     255           0 :     m_ActionGain{g_ConfigDB.Get("sound.actiongain", 0.5f)},
     256           0 :     m_UIGain{g_ConfigDB.Get("sound.uigain", 0.5f)},
     257           0 :     m_Enabled(false), m_SoundEnabled(true), m_MusicEnabled(true),
     258           0 :     m_MusicPaused(false), m_AmbientPaused(false), m_ActionPaused(false),
     259           0 :     m_RunningPlaylist(false), m_PlayingPlaylist(false), m_LoopingPlaylist(false),
     260           0 :     m_PlaylistGap(0), m_DistressErrCount(0), m_DistressTime(0)
     261             : {
     262           0 :     AlcInit();
     263             : 
     264           0 :     if (m_Enabled)
     265             :     {
     266           0 :         SetMasterGain(m_Gain);
     267           0 :         InitListener();
     268             : 
     269           0 :         m_PlayListItems = new PlayList;
     270             :     }
     271             : 
     272           0 :     if (!g_Xeromyces.AddValidator(g_VFS, "sound_group", "audio/sound_group.rng"))
     273           0 :         LOGERROR("CSoundManager: failed to load grammar file 'audio/sound_group.rng'");
     274             : 
     275           0 :     RegisterFileReloadFunc(ReloadChangedFileCB, this);
     276             : 
     277           0 :     RunHardwareDetection();
     278           0 : }
     279             : 
     280           0 : CSoundManager::~CSoundManager()
     281             : {
     282           0 :     UnregisterFileReloadFunc(ReloadChangedFileCB, this);
     283             : 
     284           0 :     if (m_Worker)
     285             :     {
     286           0 :         AL_CHECK;
     287           0 :         m_Worker->Shutdown();
     288           0 :         AL_CHECK;
     289           0 :         m_Worker->CleanupItems();
     290           0 :         AL_CHECK;
     291             : 
     292           0 :         delete m_Worker;
     293             :     }
     294           0 :     AL_CHECK;
     295             : 
     296           0 :     for (const std::pair<const std::wstring, CSoundGroup*>& p : m_SoundGroups)
     297           0 :         delete p.second;
     298           0 :     m_SoundGroups.clear();
     299             : 
     300           0 :     if (m_PlayListItems)
     301           0 :         delete m_PlayListItems;
     302             : 
     303           0 :     if (m_ALSourceBuffer != NULL)
     304           0 :         delete[] m_ALSourceBuffer;
     305             : 
     306           0 :     if (m_Context)
     307           0 :         alcDestroyContext(m_Context);
     308             : 
     309           0 :     if (m_Device)
     310           0 :         alcCloseDevice(m_Device);
     311           0 : }
     312             : 
     313           0 : void CSoundManager::StartWorker()
     314             : {
     315           0 :     if (m_Enabled)
     316           0 :         m_Worker = new CSoundManagerWorker();
     317           0 : }
     318             : 
     319           0 : Status CSoundManager::AlcInit()
     320             : {
     321           0 :     Status ret = INFO::OK;
     322             : 
     323           0 :     if(!m_Device)
     324           0 :         m_Device = alcOpenDevice(nullptr);
     325             : 
     326           0 :     if (m_Device)
     327             :     {
     328           0 :         ALCint attribs[] = {ALC_STEREO_SOURCES, 16, 0};
     329           0 :         m_Context = alcCreateContext(m_Device, &attribs[0]);
     330             : 
     331           0 :         if (m_Context)
     332             :         {
     333           0 :             alcMakeContextCurrent(m_Context);
     334           0 :             m_ALSourceBuffer = new ALSourceHolder[SOURCE_NUM];
     335           0 :             ALuint* sourceList = new ALuint[SOURCE_NUM];
     336             : 
     337           0 :             alGenSources(SOURCE_NUM, sourceList);
     338           0 :             ALCenum err = alcGetError(m_Device);
     339             : 
     340           0 :             if (err == ALC_NO_ERROR)
     341             :             {
     342           0 :                 for (int x = 0; x < SOURCE_NUM; x++)
     343             :                 {
     344           0 :                     m_ALSourceBuffer[x].ALSource = sourceList[x];
     345           0 :                     m_ALSourceBuffer[x].SourceItem = NULL;
     346             :                 }
     347           0 :                 m_Enabled = true;
     348             :             }
     349             :             else
     350             :             {
     351           0 :                 LOGERROR("error in gensource = {}", err);
     352             :             }
     353           0 :             delete[] sourceList;
     354             :         }
     355             :     }
     356             : 
     357             :     // check if init succeeded.
     358             :     // some OpenAL implementations don't indicate failure here correctly;
     359             :     // we need to check if the device and context pointers are actually valid.
     360           0 :     ALCenum err = alcGetError(m_Device);
     361           0 :     const char* dev_name = (const char*)alcGetString(m_Device, ALC_DEVICE_SPECIFIER);
     362             : 
     363           0 :     if (err == ALC_NO_ERROR && m_Device && m_Context)
     364           0 :         debug_printf("Sound: AlcInit success, using %s\n", dev_name);
     365             :     else
     366             :     {
     367           0 :         LOGERROR("Sound: AlcInit failed, m_Device={} m_Context={} dev_name={} err={:x}\n",
     368             :             static_cast<void*>(m_Device), static_cast<void*>(m_Context), dev_name, err);
     369             : 
     370             : // FIXME Hack to get around exclusive access to the sound device
     371             : #if OS_UNIX
     372           0 :         ret = INFO::OK;
     373             : #else
     374             :         ret = ERR::FAIL;
     375             : #endif // !OS_UNIX
     376             :     }
     377             : 
     378           0 :     return ret;
     379             : }
     380             : 
     381           0 : bool CSoundManager::InDistress()
     382             : {
     383           0 :     std::lock_guard<std::mutex> lock(m_DistressMutex);
     384             : 
     385           0 :     if (m_DistressTime == 0)
     386             :         return false;
     387           0 :     else if ((timer_Time() - m_DistressTime) > 10)
     388             :     {
     389           0 :         m_DistressTime = 0;
     390             : // Coming out of distress mode
     391           0 :         m_DistressErrCount = 0;
     392           0 :         return false;
     393             :     }
     394             : 
     395             :     return true;
     396           0 : }
     397             : 
     398           0 : void CSoundManager::SetDistressThroughShortage()
     399             : {
     400           0 :     std::lock_guard<std::mutex> lock(m_DistressMutex);
     401             : 
     402             : // Going into distress for normal reasons
     403             : 
     404           0 :     m_DistressTime = timer_Time();
     405           0 : }
     406             : 
     407           0 : void CSoundManager::SetDistressThroughError()
     408             : {
     409           0 :     std::lock_guard<std::mutex> lock(m_DistressMutex);
     410             : 
     411             : // Going into distress due to unknown error
     412             : 
     413           0 :     m_DistressTime = timer_Time();
     414           0 :     m_DistressErrCount++;
     415           0 : }
     416             : 
     417             : 
     418             : 
     419           0 : ALuint CSoundManager::GetALSource(ISoundItem* anItem)
     420             : {
     421           0 :     for (int x = 0; x < SOURCE_NUM; x++)
     422             :     {
     423           0 :         if (!m_ALSourceBuffer[x].SourceItem)
     424             :         {
     425           0 :             m_ALSourceBuffer[x].SourceItem = anItem;
     426           0 :             return m_ALSourceBuffer[x].ALSource;
     427             :         }
     428             :     }
     429           0 :     SetDistressThroughShortage();
     430           0 :     return 0;
     431             : }
     432             : 
     433           0 : void CSoundManager::ReleaseALSource(ALuint theSource)
     434             : {
     435           0 :     for (int x = 0; x < SOURCE_NUM; x++)
     436             :     {
     437           0 :         if (m_ALSourceBuffer[x].ALSource == theSource)
     438             :         {
     439           0 :             m_ALSourceBuffer[x].SourceItem = NULL;
     440           0 :             return;
     441             :         }
     442             :     }
     443             : }
     444             : 
     445           0 : void CSoundManager::AddPlayListItem(const VfsPath& itemPath)
     446             : {
     447           0 :     if (m_Enabled)
     448           0 :         m_PlayListItems->push_back(itemPath);
     449           0 : }
     450             : 
     451           0 : void CSoundManager::ClearPlayListItems()
     452             : {
     453           0 :     if (m_Enabled)
     454             :     {
     455           0 :         if (m_PlayingPlaylist)
     456           0 :             SetMusicItem(NULL);
     457             : 
     458           0 :         m_PlayingPlaylist = false;
     459           0 :         m_LoopingPlaylist = false;
     460           0 :         m_RunningPlaylist = false;
     461             : 
     462           0 :         m_PlayListItems->clear();
     463             :     }
     464           0 : }
     465             : 
     466           0 : void CSoundManager::StartPlayList(bool doLoop)
     467             : {
     468           0 :     if (m_Enabled && m_MusicEnabled)
     469             :     {
     470           0 :         if (m_PlayListItems->size() > 0)
     471             :         {
     472           0 :             m_PlayingPlaylist = true;
     473           0 :             m_LoopingPlaylist = doLoop;
     474           0 :             m_RunningPlaylist = false;
     475             : 
     476           0 :             ISoundItem* aSnd = LoadItem((m_PlayListItems->at(0)));
     477           0 :             if (aSnd)
     478           0 :                 SetMusicItem(aSnd);
     479             :             else
     480           0 :                 SetMusicItem(NULL);
     481             :         }
     482             :     }
     483           0 : }
     484             : 
     485           0 : void CSoundManager::SetMasterGain(float gain)
     486             : {
     487           0 :     if (m_Enabled)
     488             :     {
     489           0 :         m_Gain = gain;
     490           0 :         alListenerf(AL_GAIN, m_Gain);
     491           0 :         AL_CHECK;
     492             :     }
     493           0 : }
     494             : 
     495           0 : void CSoundManager::SetMusicGain(float gain)
     496             : {
     497           0 :     m_MusicGain = gain;
     498             : 
     499           0 :     if (m_CurrentTune)
     500           0 :         m_CurrentTune->SetGain(m_MusicGain);
     501           0 : }
     502           0 : void CSoundManager::SetAmbientGain(float gain)
     503             : {
     504           0 :     m_AmbientGain = gain;
     505           0 : }
     506           0 : void CSoundManager::SetActionGain(float gain)
     507             : {
     508           0 :     m_ActionGain = gain;
     509           0 : }
     510           0 : void CSoundManager::SetUIGain(float gain)
     511             : {
     512           0 :     m_UIGain = gain;
     513           0 : }
     514             : 
     515             : 
     516           0 : ISoundItem* CSoundManager::LoadItem(const VfsPath& itemPath)
     517             : {
     518           0 :     AL_CHECK;
     519             : 
     520           0 :     if (m_Enabled)
     521             :     {
     522           0 :         CSoundData* itemData = CSoundData::SoundDataFromFile(itemPath);
     523             : 
     524           0 :         AL_CHECK;
     525           0 :         if (itemData)
     526           0 :             return CSoundManager::ItemForData(itemData);
     527             :     }
     528             : 
     529             :     return NULL;
     530             : }
     531             : 
     532           0 : ISoundItem* CSoundManager::ItemForData(CSoundData* itemData)
     533             : {
     534           0 :     AL_CHECK;
     535           0 :     ISoundItem* answer = NULL;
     536             : 
     537           0 :     AL_CHECK;
     538             : 
     539           0 :     if (m_Enabled && (itemData != NULL))
     540             :     {
     541           0 :         if (itemData->IsOneShot())
     542             :         {
     543           0 :             if (itemData->GetBufferCount() == 1)
     544           0 :                 answer = new CSoundItem(itemData);
     545             :             else
     546           0 :                 answer = new CBufferItem(itemData);
     547             :         }
     548             :         else
     549             :         {
     550           0 :             answer = new CStreamItem(itemData);
     551             :         }
     552             : 
     553           0 :         if (m_Worker)
     554           0 :             m_Worker->addItem(answer);
     555             :     }
     556             : 
     557           0 :     return answer;
     558             : }
     559             : 
     560           0 : void CSoundManager::IdleTask()
     561             : {
     562           0 :     if (m_Enabled)
     563             :     {
     564           0 :         if (m_CurrentTune)
     565             :         {
     566           0 :             m_CurrentTune->EnsurePlay();
     567           0 :             if (m_PlayingPlaylist && m_RunningPlaylist)
     568             :             {
     569           0 :                 if (m_CurrentTune->Finished())
     570             :                 {
     571           0 :                     if (m_PlaylistGap == 0)
     572             :                     {
     573           0 :                         m_PlaylistGap = timer_Time() + 15;
     574             :                     }
     575           0 :                     else if (m_PlaylistGap < timer_Time())
     576             :                     {
     577           0 :                         m_PlaylistGap = 0;
     578           0 :                         PlayList::iterator it = find(m_PlayListItems->begin(), m_PlayListItems->end(), m_CurrentTune->GetName());
     579           0 :                         if (it != m_PlayListItems->end())
     580             :                         {
     581           0 :                             ++it;
     582             : 
     583           0 :                             Path nextPath;
     584           0 :                             if (it == m_PlayListItems->end())
     585           0 :                                 nextPath = m_PlayListItems->at(0);
     586             :                             else
     587           0 :                                 nextPath = *it;
     588             : 
     589           0 :                             ISoundItem* aSnd = LoadItem(nextPath);
     590           0 :                             if (aSnd)
     591           0 :                                 SetMusicItem(aSnd);
     592           0 :                         }
     593             :                     }
     594             :                 }
     595             :             }
     596             :         }
     597             : 
     598           0 :         if (m_CurrentEnvirons)
     599           0 :             m_CurrentEnvirons->EnsurePlay();
     600             : 
     601           0 :         if (m_Worker)
     602           0 :             m_Worker->CleanupItems();
     603             :     }
     604           0 : }
     605             : 
     606           0 : ISoundItem* CSoundManager::ItemForEntity(entity_id_t /*source*/, CSoundData* sndData)
     607             : {
     608           0 :     ISoundItem* currentItem = NULL;
     609             : 
     610           0 :     if (m_Enabled)
     611           0 :         currentItem = ItemForData(sndData);
     612             : 
     613           0 :     return currentItem;
     614             : }
     615             : 
     616             : 
     617           0 : void CSoundManager::InitListener()
     618             : {
     619           0 :     ALfloat listenerPos[] = {0.0, 0.0, 0.0};
     620           0 :     ALfloat listenerVel[] = {0.0, 0.0, 0.0};
     621           0 :     ALfloat listenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0};
     622             : 
     623           0 :     alListenerfv(AL_POSITION, listenerPos);
     624           0 :     alListenerfv(AL_VELOCITY, listenerVel);
     625           0 :     alListenerfv(AL_ORIENTATION, listenerOri);
     626             : 
     627           0 :     alDistanceModel(AL_LINEAR_DISTANCE);
     628           0 : }
     629             : 
     630           0 : void CSoundManager::PlayGroupItem(ISoundItem* anItem, ALfloat groupGain)
     631             : {
     632           0 :     if (anItem)
     633             :     {
     634           0 :         if (m_Enabled && (m_ActionGain > 0))
     635             :         {
     636           0 :             anItem->SetGain(m_ActionGain * groupGain);
     637           0 :             anItem->PlayAndDelete();
     638           0 :             AL_CHECK;
     639             :         }
     640             :     }
     641           0 : }
     642             : 
     643           0 : void CSoundManager::SetMusicEnabled(bool isEnabled)
     644             : {
     645           0 :     if (m_CurrentTune && !isEnabled)
     646             :     {
     647           0 :         m_CurrentTune->FadeAndDelete(1.00);
     648           0 :         m_CurrentTune = NULL;
     649             :     }
     650           0 :     m_MusicEnabled = isEnabled;
     651           0 : }
     652             : 
     653           0 : void CSoundManager::PlayAsGroup(const VfsPath& groupPath, const CVector3D& sourcePos, entity_id_t source, bool ownedSound)
     654             : {
     655             :     // Make sure the sound group is loaded
     656           0 :     CSoundGroup* group;
     657           0 :     if (m_SoundGroups.find(groupPath.string()) == m_SoundGroups.end())
     658             :     {
     659           0 :         group = new CSoundGroup();
     660           0 :         if (!group->LoadSoundGroup(L"audio/" + groupPath.string()))
     661             :         {
     662           0 :             LOGERROR("Failed to load sound group '{}'", groupPath.string8());
     663           0 :             delete group;
     664           0 :             group = NULL;
     665             :         }
     666             :         // Cache the sound group (or the null, if it failed)
     667           0 :         m_SoundGroups[groupPath.string()] = group;
     668             :     }
     669             :     else
     670             :     {
     671           0 :         group = m_SoundGroups[groupPath.string()];
     672             :     }
     673             : 
     674             :     // Failed to load group -> do nothing
     675           0 :     if (group && (ownedSound || !group->TestFlag(eOwnerOnly)))
     676           0 :         group->PlayNext(sourcePos, source);
     677           0 : }
     678             : 
     679           0 : void CSoundManager::PlayAsMusic(const VfsPath& itemPath, bool /*looping*/)
     680             : {
     681           0 :     if (m_Enabled)
     682             :     {
     683           0 :         ISoundItem* aSnd = LoadItem(itemPath);
     684           0 :         if (aSnd != NULL)
     685           0 :             SetMusicItem(aSnd);
     686             :     }
     687           0 : }
     688             : 
     689           0 : void CSoundManager::PlayAsAmbient(const VfsPath& itemPath, bool /*looping*/)
     690             : {
     691           0 :     if (m_Enabled)
     692             :     {
     693           0 :         ISoundItem* aSnd = LoadItem(itemPath);
     694           0 :         if (aSnd != NULL)
     695           0 :             SetAmbientItem(aSnd);
     696             :     }
     697           0 : }
     698             : 
     699             : 
     700           0 : void CSoundManager::PlayAsUI(const VfsPath& itemPath, bool looping)
     701             : {
     702           0 :     if (m_Enabled)
     703             :     {
     704           0 :         IdleTask();
     705             : 
     706           0 :         if (ISoundItem* anItem = LoadItem(itemPath))
     707             :         {
     708           0 :             if (m_UIGain > 0)
     709             :             {
     710           0 :                 anItem->SetGain(m_UIGain);
     711           0 :                 anItem->SetLooping(looping);
     712           0 :                 anItem->PlayAndDelete();
     713             :             }
     714             :         }
     715           0 :         AL_CHECK;
     716             :     }
     717           0 : }
     718             : 
     719           0 : void CSoundManager::Pause(bool pauseIt)
     720             : {
     721           0 :     PauseMusic(pauseIt);
     722           0 :     PauseAmbient(pauseIt);
     723           0 :     PauseAction(pauseIt);
     724           0 : }
     725             : 
     726           0 : void CSoundManager::PauseMusic(bool pauseIt)
     727             : {
     728           0 :     if (m_CurrentTune && pauseIt && !m_MusicPaused)
     729             :     {
     730           0 :         m_CurrentTune->FadeAndPause(1.0);
     731             :     }
     732           0 :     else if (m_CurrentTune && m_MusicPaused && !pauseIt && m_MusicEnabled)
     733             :     {
     734           0 :         m_CurrentTune->SetGain(0);
     735           0 :         m_CurrentTune->Resume();
     736           0 :         m_CurrentTune->FadeToIn(m_MusicGain, 1.0);
     737             :     }
     738           0 :     m_MusicPaused = pauseIt;
     739           0 : }
     740             : 
     741           0 : void CSoundManager::PauseAmbient(bool pauseIt)
     742             : {
     743           0 :     if (m_CurrentEnvirons && pauseIt)
     744           0 :         m_CurrentEnvirons->Pause();
     745           0 :     else if (m_CurrentEnvirons)
     746           0 :         m_CurrentEnvirons->Resume();
     747             : 
     748           0 :     m_AmbientPaused = pauseIt;
     749           0 : }
     750             : 
     751           0 : void CSoundManager::PauseAction(bool pauseIt)
     752             : {
     753           0 :     m_ActionPaused = pauseIt;
     754           0 : }
     755             : 
     756           0 : void CSoundManager::SetMusicItem(ISoundItem* anItem)
     757             : {
     758           0 :     if (m_Enabled)
     759             :     {
     760           0 :         AL_CHECK;
     761           0 :         if (m_CurrentTune)
     762             :         {
     763           0 :             m_CurrentTune->FadeAndDelete(2.00);
     764           0 :             m_CurrentTune = NULL;
     765             :         }
     766             : 
     767           0 :         IdleTask();
     768             : 
     769           0 :         if (anItem)
     770             :         {
     771           0 :             if (m_MusicEnabled)
     772             :             {
     773           0 :                 m_CurrentTune = anItem;
     774           0 :                 m_CurrentTune->SetGain(0);
     775             : 
     776           0 :                 if (m_PlayingPlaylist)
     777             :                 {
     778           0 :                     m_RunningPlaylist = true;
     779           0 :                     m_CurrentTune->Play();
     780             :                 }
     781             :                 else
     782           0 :                     m_CurrentTune->PlayLoop();
     783             : 
     784           0 :                 m_MusicPaused = false;
     785           0 :                 m_CurrentTune->FadeToIn(m_MusicGain, 1.00);
     786             :             }
     787             :             else
     788             :             {
     789           0 :                 anItem->StopAndDelete();
     790             :             }
     791             :         }
     792           0 :         AL_CHECK;
     793             :     }
     794           0 : }
     795             : 
     796           0 : void CSoundManager::SetAmbientItem(ISoundItem* anItem)
     797             : {
     798           0 :     if (m_Enabled)
     799             :     {
     800           0 :         if (m_CurrentEnvirons)
     801             :         {
     802           0 :             m_CurrentEnvirons->FadeAndDelete(3.00);
     803           0 :             m_CurrentEnvirons = NULL;
     804             :         }
     805           0 :         IdleTask();
     806             : 
     807           0 :         if (anItem)
     808             :         {
     809           0 :             if (m_AmbientGain > 0)
     810             :             {
     811           0 :                 m_CurrentEnvirons = anItem;
     812           0 :                 m_CurrentEnvirons->SetGain(0);
     813           0 :                 m_CurrentEnvirons->PlayLoop();
     814           0 :                 m_CurrentEnvirons->FadeToIn(m_AmbientGain, 2.00);
     815             :             }
     816             :         }
     817           0 :         AL_CHECK;
     818             :     }
     819           0 : }
     820             : 
     821           0 : void CSoundManager::RunHardwareDetection()
     822             : {
     823             :     // OpenAL alGetString might not return anything interesting on certain platforms
     824             :     // (see https://stackoverflow.com/questions/28960638 for an example).
     825             :     // However our previous code supported only Windows, and alGetString does work on
     826             :     // Windows, so this is an improvement.
     827             : 
     828             :     // Sound cards
     829             : 
     830           0 :     const ALCchar* devices = nullptr;
     831           0 :     if (alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT") == AL_TRUE)
     832             :     {
     833           0 :         if (alcIsExtensionPresent(nullptr, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE)
     834           0 :             devices = alcGetString(nullptr, ALC_ALL_DEVICES_SPECIFIER);
     835             :         else
     836           0 :             devices = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
     837             :     }
     838             : 
     839           0 :     m_SoundCardNames.clear();
     840           0 :     if (devices)
     841             :     {
     842           0 :         do
     843             :         {
     844           0 :             m_SoundCardNames += devices;
     845           0 :             devices += strlen(devices) + 1;
     846           0 :             m_SoundCardNames += "; ";
     847           0 :         } while (*devices);
     848             :     }
     849             :     else
     850             :     {
     851           0 :         const char* failMsg = "Failed to query audio device names";
     852           0 :         LOGERROR("{}", failMsg);
     853           0 :         m_SoundCardNames = failMsg;
     854             :     }
     855             : 
     856             :     // Driver version
     857           0 :     const ALCchar* al_version = alGetString(AL_VERSION);
     858           0 :     if (al_version)
     859           0 :         m_OpenALVersion = al_version;
     860           0 : }
     861             : 
     862           0 : CStr8 CSoundManager::GetOpenALVersion() const
     863             : {
     864           0 :     return m_OpenALVersion;
     865             : }
     866             : 
     867           0 : CStr8 CSoundManager::GetSoundCardNames() const
     868             : {
     869           0 :     return m_SoundCardNames;
     870             : }
     871             : 
     872             : #else // CONFIG2_AUDIO
     873             : 
     874             : void ISoundManager::CreateSoundManager(){}
     875             : void ISoundManager::SetEnabled(bool /*doEnable*/){}
     876             : void ISoundManager::CloseGame(){}
     877             : void ISoundManager::RunHardwareDetection() {}
     878             : CStr8 ISoundManager::GetSoundCardNames() const { return CStr8(); };
     879             : CStr8 ISoundManager::GetOpenALVersion() const { return CStr8(); };
     880             : #endif // CONFIG2_AUDIO
     881             : 

Generated by: LCOV version 1.16