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