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