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