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 "CBufferItem.h" 21 : 22 : #if CONFIG2_AUDIO 23 : 24 : #include "soundmanager/ISoundManager.h" 25 : #include "soundmanager/SoundManager.h" 26 : #include "soundmanager/data/SoundData.h" 27 : 28 : #include <AL/al.h> 29 : #include <cstddef> 30 : #include <mutex> 31 : 32 0 : CBufferItem::CBufferItem(CSoundData* sndData) 33 : { 34 0 : ResetVars(); 35 0 : if (InitOpenAL()) 36 0 : Attach(sndData); 37 0 : } 38 : 39 : 40 0 : CBufferItem::~CBufferItem() 41 : { 42 0 : Stop(); 43 0 : ReleaseOpenALBuffer(); 44 0 : } 45 : 46 : 47 0 : void CBufferItem::ReleaseOpenALBuffer() 48 : { 49 0 : if ( m_ALSource == 0 ) 50 0 : return; 51 : 52 0 : int num_processed; 53 0 : AL_CHECK; 54 0 : alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed); 55 0 : AL_CHECK; 56 : 57 0 : if (num_processed > 0) 58 : { 59 0 : ALuint* al_buf = new ALuint[num_processed]; 60 0 : alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf); 61 : 62 0 : AL_CHECK; 63 0 : delete[] al_buf; 64 : } 65 0 : alSourcei(m_ALSource, AL_BUFFER, 0); 66 0 : ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource); 67 0 : AL_CHECK; 68 : 69 0 : m_ALSource = 0; 70 : } 71 : 72 0 : bool CBufferItem::IdleTask() 73 : { 74 0 : if ( m_ALSource == 0 ) 75 : return false; 76 : 77 0 : HandleFade(); 78 : 79 0 : if (m_LastPlay) 80 : { 81 0 : std::lock_guard<std::mutex> lock(m_ItemMutex); 82 0 : int proc_state; 83 0 : alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state); 84 0 : AL_CHECK; 85 0 : m_ShouldBePlaying = (proc_state != AL_STOPPED && proc_state != AL_INITIAL && proc_state != AL_PAUSED); 86 0 : return m_ShouldBePlaying; 87 0 : } 88 : 89 0 : if (GetLooping()) 90 : { 91 0 : int num_processed; 92 0 : AL_CHECK; 93 0 : alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed); 94 : 95 0 : AL_CHECK; 96 0 : for (int i = 0; i < num_processed; i++) 97 : { 98 0 : ALuint al_buf; 99 0 : alSourceUnqueueBuffers(m_ALSource, 1, &al_buf); 100 0 : AL_CHECK; 101 0 : alSourceQueueBuffers(m_ALSource, 1, &al_buf); 102 0 : AL_CHECK; 103 : } 104 : } 105 : 106 : return true; 107 : } 108 : 109 0 : void CBufferItem::Attach(CSoundData* itemData) 110 : { 111 0 : if ( m_ALSource == 0 ) 112 : return; 113 : 114 0 : AL_CHECK; 115 0 : if (m_SoundData != NULL) 116 : { 117 0 : CSoundData::ReleaseSoundData(m_SoundData); 118 0 : m_SoundData = 0; 119 : } 120 0 : AL_CHECK; 121 0 : if (itemData != NULL) 122 : { 123 0 : m_SoundData = itemData->IncrementCount(); 124 0 : alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(),(const ALuint *) m_SoundData->GetBufferPtr()); 125 0 : AL_CHECK; 126 : } 127 : } 128 : 129 0 : void CBufferItem::SetLooping(bool loops) 130 : { 131 0 : m_Looping = loops; 132 0 : } 133 : 134 : #endif // CONFIG2_AUDIO