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

          Line data    Source code
       1             : /* Copyright (C) 2025 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 "CSoundBase.h"
      21             : 
      22             : #if CONFIG2_AUDIO
      23             : 
      24             : #include "lib/path.h"
      25             : #include "lib/timer.h"
      26             : #include "maths/Vector3D.h"
      27             : #include "soundmanager/ISoundManager.h"
      28             : #include "soundmanager/SoundManager.h"
      29             : #include "soundmanager/data/SoundData.h"
      30             : 
      31             : #include <algorithm>
      32             : 
      33           0 : CSoundBase::CSoundBase()
      34             : {
      35           0 :     ResetVars();
      36           0 : }
      37             : 
      38           0 : CSoundBase::~CSoundBase()
      39             : {
      40           0 :     Stop();
      41           0 :     ReleaseOpenAL();
      42           0 : }
      43             : 
      44           0 : void CSoundBase::ReleaseOpenAL()
      45             : {
      46           0 :     if (m_ALSource != 0)
      47             :     {
      48           0 :         AL_CHECK;
      49           0 :         alSourcei(m_ALSource, AL_BUFFER, 0L);
      50           0 :         AL_CHECK;
      51           0 :         ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource);
      52           0 :         AL_CHECK;
      53           0 :         m_ALSource = 0;
      54             :     }
      55           0 :     if (m_SoundData != 0)
      56             :     {
      57           0 :         AL_CHECK;
      58           0 :         CSoundData::ReleaseSoundData(m_SoundData);
      59           0 :         AL_CHECK;
      60           0 :         m_SoundData = 0;
      61             :     }
      62           0 : }
      63             : 
      64           0 : void CSoundBase::Attach(CSoundData* /*itemData*/)
      65             : {
      66           0 : }
      67             : 
      68           0 : void CSoundBase::ResetVars()
      69             : {
      70           0 :     m_ALSource = 0;
      71           0 :     m_SoundData = 0;
      72           0 :     m_LastPlay = false;
      73           0 :     m_Looping = false;
      74           0 :     m_StartFadeTime = 0;
      75           0 :     m_EndFadeTime = 0;
      76           0 :     m_StartVolume = 0;
      77           0 :     m_EndVolume = 0;
      78           0 :     m_ShouldBePlaying = false;
      79           0 :     m_IsPaused = false;
      80           0 :     ResetFade();
      81           0 : }
      82             : 
      83           0 : void CSoundBase::ResetFade()
      84             : {
      85           0 :     m_StartFadeTime = 0;
      86           0 :     m_EndFadeTime = 0;
      87           0 :     m_StartVolume = 0;
      88           0 :     m_EndVolume = 0;
      89           0 :     m_PauseAfterFade = false;
      90           0 : }
      91             : 
      92           0 : bool CSoundBase::Finished()
      93             : {
      94           0 :   return !m_ShouldBePlaying && !IsPlaying();
      95             : }
      96             : 
      97           0 : bool CSoundBase::InitOpenAL()
      98             : {
      99           0 :     alGetError(); /* clear error */
     100           0 :     m_ALSource = ((CSoundManager*)g_SoundManager)->GetALSource( this );
     101             : 
     102           0 :     AL_CHECK;
     103             : 
     104           0 :     if ( m_ALSource )
     105             :     {
     106           0 :         alSourcef(m_ALSource,AL_PITCH,1.0f);
     107           0 :         AL_CHECK;
     108           0 :         alSourcef(m_ALSource,AL_GAIN,1.0f);
     109           0 :         AL_CHECK;
     110           0 :         alSourcei(m_ALSource,AL_LOOPING,AL_FALSE);
     111           0 :         AL_CHECK;
     112           0 :         return true;
     113             :     }
     114             :     else
     115             :     {
     116             : //      LOGERROR("Source not allocated by SoundManager\n", 0);
     117             :     }
     118             :     return false;
     119             : }
     120             : 
     121           0 : void CSoundBase::SetGain(ALfloat gain)
     122             : {
     123           0 :     AL_CHECK;
     124             : 
     125           0 :     if ( m_ALSource )
     126             :     {
     127           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     128           0 :         alSourcef(m_ALSource, AL_GAIN, gain);
     129           0 :         AL_CHECK;
     130           0 :     }
     131           0 : }
     132             : 
     133           0 : void CSoundBase::SetRollOff(float rolls, float minDist, float maxDist)
     134             : {
     135           0 :     if ( m_ALSource )
     136             :     {
     137           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     138           0 :         alSourcef(m_ALSource, AL_REFERENCE_DISTANCE, minDist);
     139           0 :         AL_CHECK;
     140           0 :         alSourcef(m_ALSource, AL_MAX_DISTANCE, maxDist);
     141           0 :         AL_CHECK;
     142           0 :         alSourcef(m_ALSource, AL_ROLLOFF_FACTOR, rolls);
     143           0 :         AL_CHECK;
     144           0 :     }
     145           0 : }
     146             : 
     147           0 : void CSoundBase::EnsurePlay()
     148             : {
     149           0 :     if (m_ShouldBePlaying && !m_IsPaused && !IsPlaying())
     150           0 :         Play();
     151           0 : }
     152             : 
     153           0 : void CSoundBase::SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain)
     154             : {
     155           0 :     if ( m_ALSource )
     156             :     {
     157           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     158           0 :         AL_CHECK;
     159           0 :         alSourcef(m_ALSource, AL_CONE_INNER_ANGLE, innerCone);
     160           0 :         AL_CHECK;
     161           0 :         alSourcef(m_ALSource, AL_CONE_OUTER_ANGLE, outerCone);
     162           0 :         AL_CHECK;
     163           0 :         alSourcef(m_ALSource, AL_CONE_OUTER_GAIN, coneGain);
     164           0 :         AL_CHECK;
     165           0 :     }
     166           0 : }
     167             : 
     168           0 : void CSoundBase::SetPitch(ALfloat pitch)
     169             : {
     170           0 :     if ( m_ALSource )
     171             :     {
     172           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     173           0 :         alSourcef(m_ALSource, AL_PITCH, pitch);
     174           0 :         AL_CHECK;
     175           0 :     }
     176           0 : }
     177             : 
     178           0 : void CSoundBase::SetDirection(const CVector3D& direction)
     179             : {
     180           0 :     if ( m_ALSource )
     181             :     {
     182           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     183           0 :         alSourcefv(m_ALSource, AL_DIRECTION, direction.AsFloatArray().data());
     184           0 :         AL_CHECK;
     185           0 :     }
     186           0 : }
     187             : 
     188             : 
     189           0 : bool CSoundBase::IsPlaying()
     190             : {
     191           0 :     if ( m_ALSource )
     192             :     {
     193           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     194           0 :         int proc_state;
     195           0 :         alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
     196           0 :         AL_CHECK;
     197             : 
     198           0 :         return (proc_state == AL_PLAYING);
     199           0 :     }
     200             :     return false;
     201             : }
     202             : 
     203           0 : void CSoundBase::SetLastPlay(bool last)
     204             : {
     205           0 :     m_LastPlay = last;
     206           0 : }
     207             : 
     208           0 : bool CSoundBase::IdleTask()
     209             : {
     210           0 :     return true;
     211             : }
     212             : 
     213           0 : void CSoundBase::SetLocation (const CVector3D& position)
     214             : {
     215           0 :     if ( m_ALSource != 0 )
     216             :     {
     217           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     218           0 :         alSourcefv(m_ALSource,AL_POSITION, position.AsFloatArray().data());
     219           0 :         AL_CHECK;
     220           0 :     }
     221           0 : }
     222             : 
     223           0 : bool CSoundBase::HandleFade()
     224             : {
     225           0 :     AL_CHECK;
     226           0 :     if (m_ALSource == 0)
     227             :         return true;
     228             : 
     229           0 :     if (m_StartFadeTime != 0)
     230             :     {
     231           0 :         double currTime = timer_Time();
     232           0 :         double pctDone = std::min(1.0, (currTime - m_StartFadeTime) / (m_EndFadeTime - m_StartFadeTime));
     233           0 :         pctDone = std::max(0.0, pctDone);
     234           0 :         ALfloat curGain = ((m_EndVolume - m_StartVolume) * pctDone) + m_StartVolume;
     235             : 
     236           0 :         if  (curGain == 0)
     237             :         {
     238           0 :             if ( m_PauseAfterFade )
     239           0 :                 Pause();
     240             :             else
     241           0 :                 Stop();
     242             :         }
     243           0 :         else if (curGain == m_EndVolume)
     244             :         {
     245           0 :             if (m_ALSource != 0)
     246           0 :                 alSourcef(m_ALSource, AL_GAIN, curGain);
     247           0 :             ResetFade();
     248             :         }
     249           0 :         else if (m_ALSource != 0)
     250           0 :             alSourcef(m_ALSource, AL_GAIN, curGain);
     251             : 
     252           0 :         AL_CHECK;
     253             :     }
     254             :     return true;
     255             : }
     256             : 
     257           0 : bool CSoundBase::IsFading()
     258             : {
     259           0 :     return ((m_ALSource != 0) && (m_StartFadeTime != 0));
     260             : }
     261             : 
     262             : 
     263           0 : bool CSoundBase::GetLooping()
     264             : {
     265           0 :     return m_Looping;
     266             : }
     267             : 
     268           0 : void CSoundBase::SetLooping(bool loops)
     269             : {
     270           0 :     m_Looping = loops;
     271           0 :     if (m_ALSource != 0)
     272             :     {
     273           0 :         alSourcei(m_ALSource, AL_LOOPING, loops ? AL_TRUE : AL_FALSE);
     274           0 :         AL_CHECK;
     275             :     }
     276           0 : }
     277             : 
     278           0 : void CSoundBase::Play()
     279             : {
     280           0 :     std::lock_guard<std::mutex> lock(m_ItemMutex);
     281             : 
     282           0 :     m_ShouldBePlaying = true;
     283           0 :     m_IsPaused = false;
     284           0 :     AL_CHECK;
     285           0 :     if (m_ALSource != 0)
     286             :     {
     287           0 :         alSourcePlay(m_ALSource);
     288           0 :         ALenum err = alGetError();
     289           0 :         if (err != AL_NO_ERROR)
     290             :         {
     291           0 :             if (err == AL_INVALID)
     292           0 :                 ((CSoundManager*)g_SoundManager)->SetDistressThroughError();
     293             :             else
     294           0 :                 ((CSoundManager*)g_SoundManager)->al_ReportError(err, __func__, __LINE__);
     295             :         }
     296             :     }
     297           0 : }
     298             : 
     299           0 : void CSoundBase::PlayAndDelete()
     300             : {
     301           0 :     SetLastPlay(true);
     302           0 :     Play();
     303           0 : }
     304             : 
     305           0 : void CSoundBase::FadeAndPause(double fadeTime)
     306             : {
     307           0 :     m_PauseAfterFade = true;
     308           0 :     FadeToIn(0, fadeTime);
     309           0 : }
     310             : 
     311           0 : void CSoundBase::FadeAndDelete(double fadeTime)
     312             : {
     313           0 :     SetLastPlay(true);
     314           0 :     FadeToIn(0, fadeTime);
     315           0 : }
     316             : 
     317           0 : void CSoundBase::StopAndDelete()
     318             : {
     319           0 :     SetLastPlay(true);
     320           0 :     Stop();
     321           0 : }
     322             : 
     323           0 : void CSoundBase::PlayLoop()
     324             : {
     325           0 :     if (m_ALSource != 0)
     326             :     {
     327           0 :         SetLooping(true);
     328           0 :         Play();
     329           0 :         AL_CHECK;
     330             :     }
     331           0 : }
     332             : 
     333           0 : void CSoundBase::FadeToIn(ALfloat newVolume, double fadeDuration)
     334             : {
     335           0 :     if (m_ALSource != 0)
     336             :     {
     337           0 :         ALenum proc_state;
     338           0 :         alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
     339           0 :         if (proc_state == AL_PLAYING)
     340             :         {
     341           0 :             m_StartFadeTime = timer_Time();
     342           0 :             m_EndFadeTime = m_StartFadeTime + fadeDuration;
     343           0 :             alGetSourcef(m_ALSource, AL_GAIN, &m_StartVolume);
     344           0 :             m_EndVolume = newVolume;
     345             :         }
     346           0 :         AL_CHECK;
     347             :     }
     348           0 : }
     349             : 
     350           0 : void CSoundBase::Stop()
     351             : {
     352           0 :     m_ShouldBePlaying = false;
     353           0 :     if (m_ALSource != 0)
     354             :     {
     355           0 :         std::lock_guard<std::mutex> lock(m_ItemMutex);
     356             : 
     357           0 :         AL_CHECK;
     358           0 :         alSourcei(m_ALSource, AL_LOOPING, AL_FALSE);
     359           0 :         AL_CHECK;
     360           0 :         alSourceStop(m_ALSource);
     361           0 :         AL_CHECK;
     362           0 :     }
     363           0 : }
     364             : 
     365           0 : const Path CSoundBase::GetName()
     366             : {
     367           0 :     if (m_SoundData)
     368           0 :         return m_SoundData->GetFileName();
     369             : 
     370           0 :     return Path();
     371             : }
     372             : 
     373           0 : void CSoundBase::Pause()
     374             : {
     375           0 :     if (m_ALSource != 0)
     376             :     {
     377           0 :         m_IsPaused = true;
     378           0 :         alSourcePause(m_ALSource);
     379           0 :         AL_CHECK;
     380             :     }
     381           0 : }
     382             : 
     383           0 : void CSoundBase::Resume()
     384             : {
     385           0 :     if (m_ALSource != 0)
     386             :     {
     387           0 :         alSourcePlay(m_ALSource);
     388           0 :         AL_CHECK;
     389             :     }
     390           0 : }
     391             : 
     392             : #endif // CONFIG2_AUDIO
     393             : 

Generated by: LCOV version 1.16