Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CSoundBase.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 "CSoundBase.h"
21 
22 #if CONFIG2_AUDIO
23 
24 #include "lib/timer.h"
27 #include "ps/CLogger.h"
28 
29 #include <iostream>
30 
32 {
33  ResetVars();
34 }
35 
37 {
38  Stop();
39  ReleaseOpenAL();
40 }
41 
43 {
44  if (m_ALSource != 0)
45  {
46  AL_CHECK
47  alSourcei(m_ALSource, AL_BUFFER, 0L);
48  AL_CHECK
49  ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource);
50  AL_CHECK
51  m_ALSource = 0;
52  }
53  if (m_SoundData != 0)
54  {
55  AL_CHECK
57  AL_CHECK
58  m_SoundData = 0;
59  }
60 }
61 
63 {
64  UNUSED2(itemData);
65 }
66 
68 {
69  m_ALSource = 0;
70  m_SoundData = 0;
71  m_LastPlay = false;
72  m_Looping = false;
73  m_StartFadeTime = 0;
74  m_EndFadeTime = 0;
75  m_StartVolume = 0;
76  m_EndVolume = 0;
77  m_ShouldBePlaying = false;
78  m_IsPaused = false;
79  ResetFade();
80 }
81 
83 {
84  m_StartFadeTime = 0;
85  m_EndFadeTime = 0;
86  m_StartVolume = 0;
87  m_EndVolume = 0;
88  m_PauseAfterFade = false;
89 }
90 
92 {
93  return !m_ShouldBePlaying && !IsPlaying();
94 }
95 
97 {
98  alGetError(); /* clear error */
99  m_ALSource = ((CSoundManager*)g_SoundManager)->GetALSource( this );
100 
101  AL_CHECK
102 
103  if ( m_ALSource )
104  {
105  alSourcef(m_ALSource,AL_PITCH,1.0f);
106  AL_CHECK
107  alSourcef(m_ALSource,AL_GAIN,1.0f);
108  AL_CHECK
109  alSourcei(m_ALSource,AL_LOOPING,AL_FALSE);
110  AL_CHECK
111  return true;
112  }
113  else
114  {
115 // LOGERROR(L"Source not allocated by SoundManager\n", 0);
116  }
117  return false;
118 }
119 
120 void CSoundBase::SetGain(ALfloat gain)
121 {
122  AL_CHECK
123 
124  if ( m_ALSource )
125  {
126  CScopeLock lock(m_ItemMutex);
127  alSourcef(m_ALSource, AL_GAIN, gain);
128  AL_CHECK
129  }
130 }
131 
132 void CSoundBase::SetRollOff(ALfloat rolls)
133 {
134  if ( m_ALSource )
135  {
136  CScopeLock lock(m_ItemMutex);
137  alSourcef(m_ALSource, AL_REFERENCE_DISTANCE, 70.0f);
138  AL_CHECK
139  alSourcef(m_ALSource, AL_MAX_DISTANCE, 200.0);
140  AL_CHECK
141  alSourcef(m_ALSource, AL_ROLLOFF_FACTOR, rolls);
142  AL_CHECK
143  }
144 }
145 
147 {
148  if (m_ShouldBePlaying && !m_IsPaused && !IsPlaying())
149  Play();
150 }
151 
152 void CSoundBase::SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain)
153 {
154  if ( m_ALSource )
155  {
156  CScopeLock lock(m_ItemMutex);
157  AL_CHECK
158  alSourcef(m_ALSource, AL_CONE_INNER_ANGLE, innerCone);
159  AL_CHECK
160  alSourcef(m_ALSource, AL_CONE_OUTER_ANGLE, outerCone);
161  AL_CHECK
162  alSourcef(m_ALSource, AL_CONE_OUTER_GAIN, coneGain);
163  AL_CHECK
164  }
165 }
166 
167 void CSoundBase::SetPitch(ALfloat pitch)
168 {
169  if ( m_ALSource )
170  {
171  CScopeLock lock(m_ItemMutex);
172  alSourcef(m_ALSource, AL_PITCH, pitch);
173  AL_CHECK
174  }
175 }
176 
177 void CSoundBase::SetDirection(const CVector3D& direction)
178 {
179  if ( m_ALSource )
180  {
181  CScopeLock lock(m_ItemMutex);
182  alSourcefv(m_ALSource, AL_DIRECTION, direction.GetFloatArray());
183  AL_CHECK
184  }
185 }
186 
187 
189 {
190  if ( m_ALSource )
191  {
192  CScopeLock lock(m_ItemMutex);
193  int proc_state;
194  alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
195  AL_CHECK
196 
197  return (proc_state == AL_PLAYING);
198  }
199  return false;
200 }
201 
202 void CSoundBase::SetLastPlay(bool last)
203 {
204  m_LastPlay = last;
205 }
206 
208 {
209  return true;
210 }
211 
212 void CSoundBase::SetLocation (const CVector3D& position)
213 {
214  if ( m_ALSource != 0 )
215  {
216  CScopeLock lock(m_ItemMutex);
217  alSourcefv(m_ALSource,AL_POSITION, position.GetFloatArray());
218  AL_CHECK
219  }
220 }
221 
223 {
224  AL_CHECK
225  if (m_ALSource == 0)
226  return true;
227 
228  if (m_StartFadeTime != 0)
229  {
230  double currTime = timer_Time();
231  double pctDone = std::min(1.0, (currTime - m_StartFadeTime) / (m_EndFadeTime - m_StartFadeTime));
232  pctDone = std::max(0.0, pctDone);
233  ALfloat curGain = ((m_EndVolume - m_StartVolume) * pctDone) + m_StartVolume;
234 
235  if (curGain == 0)
236  {
237  if ( m_PauseAfterFade )
238  Pause();
239  else
240  Stop();
241  }
242  else if (curGain == m_EndVolume)
243  {
244  if (m_ALSource != 0)
245  alSourcef(m_ALSource, AL_GAIN, curGain);
246  ResetFade();
247  }
248  else if (m_ALSource != 0)
249  alSourcef(m_ALSource, AL_GAIN, curGain);
250 
251  AL_CHECK
252  }
253  return true;
254 }
255 
257 {
258  return ((m_ALSource != 0) && (m_StartFadeTime != 0));
259 }
260 
261 
263 {
264  return m_Looping;
265 }
266 
267 void CSoundBase::SetLooping(bool loops)
268 {
269  m_Looping = loops;
270  if (m_ALSource != 0)
271  {
272  alSourcei(m_ALSource, AL_LOOPING, loops ? AL_TRUE : AL_FALSE);
273  AL_CHECK
274  }
275 }
276 
278 {
279  CScopeLock lock(m_ItemMutex);
280 
281  m_ShouldBePlaying = true;
282  m_IsPaused = false;
283  AL_CHECK
284  if (m_ALSource != 0)
285  {
286  alSourcePlay(m_ALSource);
287  ALenum err = alGetError();
288  if (err != AL_NO_ERROR)
289  {
290  if (err == AL_INVALID)
291  ((CSoundManager*)g_SoundManager)->SetDistressThroughError();
292  else
293  ((CSoundManager*)g_SoundManager)->al_ReportError(err, __func__, __LINE__);
294  }
295  }
296 }
297 
299 {
300  SetLastPlay(true);
301  Play();
302 }
303 
304 void CSoundBase::FadeAndPause(double fadeTime)
305 {
306  m_PauseAfterFade = true;
307  FadeToIn(0, fadeTime);
308 }
309 
310 void CSoundBase::FadeAndDelete(double fadeTime)
311 {
312  SetLastPlay(true);
313  FadeToIn(0, fadeTime);
314 }
315 
317 {
318  SetLastPlay(true);
319  Stop();
320 }
321 
323 {
324  if (m_ALSource != 0)
325  {
326  SetLooping(true);
327  Play();
328  AL_CHECK
329  }
330 }
331 
332 void CSoundBase::FadeToIn(ALfloat newVolume, double fadeDuration)
333 {
334  if (m_ALSource != 0)
335  {
336  ALenum proc_state;
337  alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
338  if (proc_state == AL_PLAYING)
339  {
341  m_EndFadeTime = m_StartFadeTime + fadeDuration;
342  alGetSourcef(m_ALSource, AL_GAIN, &m_StartVolume);
343  m_EndVolume = newVolume;
344  }
345  AL_CHECK
346  }
347 }
348 
350 {
351  m_ShouldBePlaying = false;
352  if (m_ALSource != 0)
353  {
354  CScopeLock lock(m_ItemMutex);
355 
356  AL_CHECK
357  alSourcei(m_ALSource, AL_LOOPING, AL_FALSE);
358  AL_CHECK
359  alSourceStop(m_ALSource);
360  AL_CHECK
361  }
362 }
363 
365 {
366  if ( m_SoundData )
367  return m_SoundData->GetFileName();
368 
369  return NULL;
370 }
371 
373 {
374  if (m_ALSource != 0)
375  {
376  m_IsPaused = true;
377  alSourcePause(m_ALSource);
378  AL_CHECK
379  }
380 }
381 
383 {
384  if (m_ALSource != 0)
385  {
386  alSourcePlay(m_ALSource);
387  AL_CHECK
388  }
389 }
390 
391 #endif // CONFIG2_AUDIO
392 
#define AL_CHECK
Definition: SoundManager.h:40
void PlayLoop()
Definition: CSoundBase.cpp:322
void SetLocation(const CVector3D &position)
Definition: CSoundBase.cpp:212
void SetRollOff(ALfloat gain)
Definition: CSoundBase.cpp:132
ALuint m_ALSource
Definition: CSoundBase.h:34
void FadeAndDelete(double fadeTime)
Definition: CSoundBase.cpp:310
void StopAndDelete()
Definition: CSoundBase.cpp:316
ISoundManager * g_SoundManager
void Pause()
Definition: CSoundBase.cpp:372
Locks a CMutex over this object&#39;s lifetime.
Definition: ThreadUtil.h:73
bool m_ShouldBePlaying
Definition: CSoundBase.h:39
void Play()
Definition: CSoundBase.cpp:277
Path * GetName()
Definition: CSoundBase.cpp:364
void Resume()
Definition: CSoundBase.cpp:382
void ReleaseOpenAL()
Definition: CSoundBase.cpp:42
virtual ~CSoundBase()
Definition: CSoundBase.cpp:36
virtual Path * GetFileName()
Definition: SoundData.cpp:122
ALfloat m_StartVolume
Definition: CSoundBase.h:46
bool m_LastPlay
Definition: CSoundBase.h:37
virtual bool IdleTask()
Definition: CSoundBase.cpp:207
bool m_Looping
Definition: CSoundBase.h:38
void SetGain(ALfloat gain)
Definition: CSoundBase.cpp:120
void Stop()
Definition: CSoundBase.cpp:349
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
void SetPitch(ALfloat pitch)
Definition: CSoundBase.cpp:167
Definition: path.h:75
virtual void SetLooping(bool loops)
Definition: CSoundBase.cpp:267
bool m_PauseAfterFade
Definition: CSoundBase.h:40
bool GetLooping()
Definition: CSoundBase.cpp:262
bool HandleFade()
Definition: CSoundBase.cpp:222
void PlayAndDelete()
Definition: CSoundBase.cpp:298
void ResetFade()
Definition: CSoundBase.cpp:82
void ResetVars()
Definition: CSoundBase.cpp:67
double timer_Time()
Definition: timer.cpp:98
CMutex m_ItemMutex
Definition: CSoundBase.h:48
bool m_IsPaused
Definition: CSoundBase.h:41
void SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain)
Definition: CSoundBase.cpp:152
bool IsPlaying()
Definition: CSoundBase.cpp:188
double m_StartFadeTime
Definition: CSoundBase.h:43
const float * GetFloatArray() const
Definition: Vector3D.h:108
bool InitOpenAL()
Definition: CSoundBase.cpp:96
virtual void Attach(CSoundData *itemData)
Definition: CSoundBase.cpp:62
CSoundData * m_SoundData
Definition: CSoundBase.h:35
void EnsurePlay()
Definition: CSoundBase.cpp:146
bool Finished()
Definition: CSoundBase.cpp:91
ALfloat m_EndVolume
Definition: CSoundBase.h:47
double m_EndFadeTime
Definition: CSoundBase.h:44
void SetDirection(const CVector3D &direction)
Definition: CSoundBase.cpp:177
void FadeAndPause(double fadeTime)
Definition: CSoundBase.cpp:304
#define __func__
bool IsFading()
Definition: CSoundBase.cpp:256
static void ReleaseSoundData(CSoundData *theData)
Definition: SoundData.cpp:54
void FadeToIn(ALfloat newVolume, double fadeDuration)
Definition: CSoundBase.cpp:332
void SetLastPlay(bool last)
Definition: CSoundBase.cpp:202