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