Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OggData.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 "OggData.h"
21 
22 
23 #if CONFIG2_AUDIO
24 
25 #include "ps/CLogger.h"
26 #include "ps/Filesystem.h"
28 #include "ps/CLogger.h"
29 
31 {
32  m_OneShot = false;
33  m_Format = 0;
34  m_Frequency = 0;
35  m_BuffersUsed = 0;
36 }
37 
39 {
40  AL_CHECK
41  ogg->Close();
42 
43  AL_CHECK
44  if ( m_BuffersUsed > 0 )
45  alDeleteBuffers(m_BuffersUsed, &m_Buffer[0] );
46 
47  AL_CHECK
48  m_BuffersUsed = 0;
49 }
50 
51 void COggData::SetFormatAndFreq(int form, ALsizei freq)
52 {
53  m_Format = form;
54  m_Frequency = freq;
55 }
56 
58 {
59  return ( m_Format == AL_FORMAT_STEREO16 );
60 }
61 
62 bool COggData::InitOggFile(const VfsPath& itemPath)
63 {
64  if ( CSoundManager* sndManager = (CSoundManager*)g_SoundManager )
65  {
66  int buffersToStart = sndManager->GetBufferCount();
67  if ( OpenOggNonstream( g_VFS, itemPath, ogg) == INFO::OK )
68  {
69  m_FileFinished = false;
70 
71  SetFormatAndFreq(ogg->Format(), ogg->SamplingRate() );
72  SetFileName( itemPath );
73 
74  AL_CHECK
75 
76  alGenBuffers(buffersToStart, m_Buffer);
77 
78  if(alGetError() != AL_NO_ERROR)
79  {
80  LOGERROR( L"- Error creating initial buffer !!\n");
81  return false;
82  }
83  else
84  {
85  m_BuffersUsed = FetchDataIntoBuffer(buffersToStart, m_Buffer);
86  if (m_FileFinished)
87  {
88  m_OneShot = true;
89  if (m_BuffersUsed < buffersToStart)
90  {
91  alDeleteBuffers(buffersToStart - m_BuffersUsed, &m_Buffer[m_BuffersUsed]);
92  }
93  }
94  AL_CHECK
95  }
96  return true;
97  }
98  return false;
99  }
100  return false;
101 }
102 
104 {
105  return m_BuffersUsed;
106 }
107 
109 {
110  return m_FileFinished;
111 }
112 
114 {
115  ogg->ResetFile();
116  m_FileFinished = false;
117 }
118 
120 {
121  return m_OneShot;
122 }
123 
124 int COggData::FetchDataIntoBuffer(int count, ALuint* buffers)
125 {
126  if ( CSoundManager* sndManager = (CSoundManager*)g_SoundManager )
127  {
128  long bufferSize = sndManager->GetBufferSize();
129 
130  u8* pcmout = new u8[bufferSize + 5000];
131  int buffersWritten = 0;
132 
133  for (int i = 0; (i < count) && !m_FileFinished; i++)
134  {
135  memset( pcmout, 0, bufferSize + 5000 );
136  Status totalRet = ogg->GetNextChunk( pcmout, bufferSize);
137  m_FileFinished = ogg->atFileEOF();
138  if (totalRet > 0)
139  {
140  buffersWritten++;
141  alBufferData(buffers[i], m_Format, pcmout, (ALsizei)totalRet, (int)m_Frequency);
142  }
143  }
144  delete[] pcmout;
145  return buffersWritten;
146  }
147  return 0;
148 }
149 
150 
152 {
153  return m_Buffer[0];
154 }
155 
157 {
158  return m_Buffer;
159 }
160 
161 #endif // CONFIG2_AUDIO
162 
#define AL_CHECK
Definition: SoundManager.h:40
#define u8
Definition: types.h:39
virtual bool IsFileFinished()
Definition: OggData.cpp:108
virtual void ResetFile()
Definition: OggData.cpp:113
const Status OK
Definition: status.h:386
#define LOGERROR
Definition: CLogger.h:35
ALuint m_Buffer[100]
Definition: OggData.h:53
ISoundManager * g_SoundManager
unsigned int GetBuffer()
Definition: OggData.cpp:151
virtual bool IsStereo()
Definition: OggData.cpp:57
OggStreamPtr ogg
Definition: OggData.h:49
unsigned int * GetBufferPtr()
Definition: OggData.cpp:156
bool m_FileFinished
Definition: OggData.h:51
long m_Frequency
Definition: OggData.h:34
Definition: path.h:75
ALuint m_Format
Definition: OggData.h:33
virtual bool IsOneShot()
Definition: OggData.cpp:119
int GetBufferCount()
Definition: OggData.cpp:103
i64 Status
Error handling system.
Definition: status.h:171
virtual void SetFileName(const Path &aName)
Definition: SoundData.cpp:127
COggData()
Definition: OggData.cpp:30
virtual bool InitOggFile(const VfsPath &itemPath)
Definition: OggData.cpp:62
virtual ~COggData()
Definition: OggData.cpp:38
Status OpenOggNonstream(const PIVFS &vfs, const VfsPath &pathname, OggStreamPtr &stream)
A non-streaming OggStream (reading the whole file in advance) that can cope with archived/compressed ...
Definition: ogg.cpp:320
int m_BuffersUsed
Definition: OggData.h:54
virtual int FetchDataIntoBuffer(int count, ALuint *buffers)
Definition: OggData.cpp:124
void SetFormatAndFreq(int form, ALsizei freq)
Definition: OggData.cpp:51
PIVFS g_VFS
Definition: Filesystem.cpp:30
bool m_OneShot
Definition: OggData.h:52