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 "CSoundItem.h" 21 : 22 : #if CONFIG2_AUDIO 23 : 24 : #include "soundmanager/SoundManager.h" 25 : #include "soundmanager/data/SoundData.h" 26 : 27 : #include <AL/al.h> 28 : #include <cstddef> 29 : #include <mutex> 30 : 31 0 : CSoundItem::CSoundItem() 32 : { 33 0 : ResetVars(); 34 0 : } 35 : 36 0 : CSoundItem::CSoundItem(CSoundData* sndData) 37 : { 38 0 : ResetVars(); 39 0 : if (InitOpenAL()) 40 0 : Attach(sndData); 41 0 : } 42 : 43 0 : CSoundItem::~CSoundItem() 44 : { 45 0 : Stop(); 46 0 : ReleaseOpenAL(); 47 0 : } 48 : 49 0 : bool CSoundItem::IdleTask() 50 : { 51 0 : if ( m_ALSource == 0 ) 52 : return false; 53 : 54 0 : HandleFade(); 55 : 56 0 : if (m_LastPlay && m_ALSource) 57 : { 58 0 : std::lock_guard<std::mutex> lock(m_ItemMutex); 59 0 : int proc_state; 60 0 : alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state); 61 0 : AL_CHECK; 62 0 : m_ShouldBePlaying = (proc_state != AL_STOPPED && proc_state != AL_INITIAL && proc_state != AL_PAUSED); 63 0 : return m_ShouldBePlaying; 64 0 : } 65 : return true; 66 : } 67 : 68 0 : void CSoundItem::Attach(CSoundData* itemData) 69 : { 70 0 : if (m_SoundData != NULL) 71 : { 72 0 : CSoundData::ReleaseSoundData(m_SoundData); 73 0 : m_SoundData = 0; 74 : } 75 : 76 0 : if (itemData != NULL) 77 : { 78 0 : AL_CHECK; 79 0 : alSourcei(m_ALSource, AL_BUFFER, 0); 80 0 : AL_CHECK; 81 0 : m_SoundData = itemData->IncrementCount(); 82 0 : alSourcei(m_ALSource, AL_BUFFER, m_SoundData->GetBuffer()); 83 : 84 0 : AL_CHECK; 85 : } 86 0 : } 87 : 88 : #endif // CONFIG2_AUDIO