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