Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CBufferItem.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 
26 
27 #include <iostream>
28 
30 {
31  ResetVars();
32  if (InitOpenAL())
33  Attach(sndData);
34 }
35 
36 
38 {
39  Stop();
41 }
42 
43 
45 {
46  if ( m_ALSource == 0 )
47  return;
48 
49  int num_processed;
50  AL_CHECK
51  alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
52  AL_CHECK
53 
54  if (num_processed > 0)
55  {
56  ALuint* al_buf = new ALuint[num_processed];
57  alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf);
58 
59  AL_CHECK
60  delete[] al_buf;
61  }
62  alSourcei(m_ALSource, AL_BUFFER, 0);
63  ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource);
64  AL_CHECK
65 
66  m_ALSource = 0;
67 }
68 
70 {
71  if ( m_ALSource == 0 )
72  return false;
73 
74  HandleFade();
75 
76  if (m_LastPlay)
77  {
78  CScopeLock lock(m_ItemMutex);
79  int proc_state;
80  alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
81  AL_CHECK
82  m_ShouldBePlaying = (proc_state != AL_STOPPED);
83  return (proc_state != AL_STOPPED);
84  }
85 
86  if (GetLooping())
87  {
88  int num_processed;
89  AL_CHECK
90  alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
91 
92  AL_CHECK
93  for (int i = 0; i < num_processed; i++)
94  {
95  ALuint al_buf;
96  alSourceUnqueueBuffers(m_ALSource, 1, &al_buf);
97  AL_CHECK
98  alSourceQueueBuffers(m_ALSource, 1, &al_buf);
99  AL_CHECK
100  }
101  }
102 
103  return true;
104 }
105 
107 {
108  if ( m_ALSource == 0 )
109  return;
110 
111  AL_CHECK
112  if (m_SoundData != NULL)
113  {
115  m_SoundData = 0;
116  }
117  AL_CHECK
118  if (itemData != NULL)
119  {
120  m_SoundData = itemData->IncrementCount();
121  alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(),(const ALuint *) m_SoundData->GetBufferPtr());
122  AL_CHECK
123  }
124 }
125 
126 void CBufferItem::SetLooping(bool loops)
127 {
128  m_Looping = loops;
129 }
130 
131 #endif // CONFIG2_AUDIO
132 
#define AL_CHECK
Definition: SoundManager.h:40
ALuint m_ALSource
Definition: CSoundBase.h:34
ISoundManager * g_SoundManager
virtual void SetLooping(bool loops)
Locks a CMutex over this object&#39;s lifetime.
Definition: ThreadUtil.h:73
bool m_ShouldBePlaying
Definition: CSoundBase.h:39
bool m_LastPlay
Definition: CSoundBase.h:37
bool m_Looping
Definition: CSoundBase.h:38
void Stop()
Definition: CSoundBase.cpp:349
virtual bool IdleTask()
Definition: CBufferItem.cpp:69
CSoundData * IncrementCount()
Definition: SoundData.cpp:134
virtual ~CBufferItem()
Definition: CBufferItem.cpp:37
virtual int GetBufferCount()
Definition: SoundData.cpp:117
bool GetLooping()
Definition: CSoundBase.cpp:262
bool HandleFade()
Definition: CSoundBase.cpp:222
void ResetVars()
Definition: CSoundBase.cpp:67
CMutex m_ItemMutex
Definition: CSoundBase.h:48
virtual unsigned int * GetBufferPtr()
Definition: SoundData.cpp:152
virtual void Attach(CSoundData *itemData)
CBufferItem(CSoundData *sndData)
Definition: CBufferItem.cpp:29
bool InitOpenAL()
Definition: CSoundBase.cpp:96
void ReleaseOpenALBuffer()
Definition: CBufferItem.cpp:44
CSoundData * m_SoundData
Definition: CSoundBase.h:35
static void ReleaseSoundData(CSoundData *theData)
Definition: SoundData.cpp:54