Spinning Topp Logo BlackTopp Studios
inc
vorbisdecoder.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine 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 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine 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 The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 // Copyright (c) 2008-2010 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones
41 // This file is part of the "cAudio Engine"
42 // For conditions of distribution and use, see copyright notice in cAudio-ZLIBLicense.txt
43 #ifndef _audiovorbisdecoder_cpp
44 #define _audiovorbisdecoder_cpp
45 
46 #ifdef ENABLE_VORBIS_ENCODE
47 
48 #include "Audio/vorbisdecoder.h"
49 
50 #include "Resource/datastream.h"
51 
52 #include <ogg/ogg.h>
53 #include <vorbis/codec.h>
54 #include <vorbis/vorbisfile.h>
55 
56 namespace
57 {
58  /// @internal
59  /// @brief Convenience method to check for and clear EoF flags that may be encountered during decoding.
60  void ClearEoF(Mezzanine::Resource::DataStream* Stream)
61  {
62  if( Stream->eof() ) {
63  Stream->clear( Stream->rdstate() ^ ( std::ios::eofbit | std::ios::failbit ) );
64  }
65  }
66 
67  /// @internal
68  /// @brief The Vorbis read callback.
69  size_t VorbisRead(void *ptr, size_t byteSize,size_t sizeToRead, void *datasource)
70  {
71  Mezzanine::Resource::DataStream* Stream = static_cast<Mezzanine::Resource::DataStream*>(datasource);
72  Stream->read( static_cast<char*>(ptr), byteSize * sizeToRead );
73  return Stream->gcount();
74  }
75 
76  /// @internal
77  /// @brief The Vorbis seek callback (set position).
78  int VorbisSeek(void *datasource,ogg_int64_t offset,int whence)
79  {
80  Mezzanine::Resource::DataStream* Stream = static_cast<Mezzanine::Resource::DataStream*>(datasource);
81  ClearEoF(Stream);
82  switch(whence)
83  {
84  case SEEK_SET: Stream->seekg(offset,std::ios_base::beg); break;
85  case SEEK_CUR: Stream->seekg(offset,std::ios_base::cur); break;
86  case SEEK_END: Stream->seekg(offset,std::ios_base::end); break;
87  };
88  return ( Stream->good() ? 0 : -1 );
89  }
90 
91  /// @internal
92  /// @brief The Vorbis tell callback (retrieve position).
93  long VorbisTell(void *datasource)
94  {
95  Mezzanine::Resource::DataStream* Stream = static_cast<Mezzanine::Resource::DataStream*>(datasource);
96  ClearEoF(Stream);
97  return Stream->tellg();
98  }
99 
100  /// @internal
101  /// @brief The stream close callback.
102  int VorbisClose(void *datasource)
103  {
104  return 0;
105  }
106 }
107 
108 namespace Mezzanine
109 {
110  namespace Audio
111  {
112  ///////////////////////////////////////////////////////////////////////////////
113  /// @internal
114  /// @brief Internal convenience class for the storage of Vorbis structs needed for Vorbis operations.
115  /// @details
116  ///////////////////////////////////////
117  class MEZZ_LIB VorbisDecoderInternalData
118  {
119  protected:
120  public:
121  ///////////////////////////////////////////////////////////////////////////////
122  // Publid Data Members
123 
124  /// @brief Format Information from the file.
125  vorbis_info* VorbisInfo;
126  /// @brief User comments embedded in the file.
127  vorbis_comment* VorbisComments;
128  /// @brief Mandatory callbacks for working with our streams.
129  ov_callbacks VorbisCallbacks;
130  /// @brief The main audio encoding.
131  OggVorbis_File VorbisFile;
132 
133  ///////////////////////////////////////////////////////////////////////////////
134  // Construction and destruction
135 
136  /// @brief Class constructor.
137  VorbisDecoderInternalData()
138  {
139  this->VorbisCallbacks.read_func = VorbisRead;
140  this->VorbisCallbacks.seek_func = VorbisSeek;
141  this->VorbisCallbacks.tell_func = VorbisTell;
142  this->VorbisCallbacks.close_func = VorbisClose;
143  }
144  /// @brief Class destructor.
145  ~VorbisDecoderInternalData() { }
146  };//VorbisDecoderInternalData
147 
148  ///////////////////////////////////////////////////////////////////////////////
149  // VorbisDecoder Methods
150 
151  VorbisDecoder::VorbisDecoder(Resource::DataStreamPtr Stream) :
152  VorbisStream(Stream),
153  VorbisStreamSize(0),
154  VorbisStreamPos(0),
155  Valid(false)
156  {
157  this->VorbisStream->seekg(0,std::ios_base::end);
158  this->VorbisStreamSize = this->VorbisStream->tellg();
159  this->VorbisStream->seekg(0);
160  this->VDID = new VorbisDecoderInternalData();
161  this->Valid = ( ov_open_callbacks(VorbisStream.get(),&(this->VDID->VorbisFile),NULL,0,this->VDID->VorbisCallbacks) == 0 );
162 
163  if( this->Valid ) {
164  this->VDID->VorbisInfo = ov_info( &(this->VDID->VorbisFile), -1 );
165  this->VDID->VorbisComments = ov_comment( &(this->VDID->VorbisFile), -1 );
166  this->VorbisStreamPos = this->VorbisStream->tellg();
167  }
168  }
169 
170  VorbisDecoder::~VorbisDecoder()
171  {
172  ov_clear( &(this->VDID->VorbisFile) );
173  }
174 
175  void VorbisDecoder::ClearStreamErrors()
176  {
177  if( this->VorbisStream->eof() ) {
178  this->VorbisStream->clear( this->VorbisStream->rdstate() ^ ( std::ios::eofbit | std::ios::failbit ) );
179  }
180  }
181 
182  ///////////////////////////////////////////////////////////////////////////////
183  // Additional Vorbis Functionality
184 
185  String VorbisDecoder::GetUserComment(const UInt32 Index)
186  {
187  Char8* Comment = this->VDID->VorbisComments->user_comments[Index];
188  Integer CommentLength = this->VDID->VorbisComments->comment_lengths[Index];
189  return String(Comment,CommentLength);
190  }
191 
192  UInt32 VorbisDecoder::GetNumUserComments() const
193  {
194  return this->VDID->VorbisComments->comments;
195  }
196 
197  ///////////////////////////////////////////////////////////////////////////////
198  // Utility
199 
200  Boole VorbisDecoder::IsValid()
201  {
202  return this->Valid;
203  }
204 
205  Audio::Encoding VorbisDecoder::GetEncoding() const
206  {
207  return Audio::Enc_VORBIS;
208  }
209 
210  Boole VorbisDecoder::IsSeekingSupported()
211  {
212  if( this->Valid ) return (ov_seekable( &(this->VDID->VorbisFile) ) != 0);
213  else return false;
214  }
215 
216  Audio::BitConfig VorbisDecoder::GetBitConfiguration() const
217  {
218  if( this->Valid ) {
219  switch( this->VDID->VorbisInfo->channels )
220  {
221  case 1: return Audio::BC_16Bit_Mono; break;
222  case 2: return Audio::BC_16Bit_Stereo; break;
223  }
224  }
225 
226  return Audio::BC_8Bit_Mono;
227  }
228 
229  UInt32 VorbisDecoder::GetFrequency() const
230  {
231  if( this->Valid ) return this->VDID->VorbisInfo->rate;
232  else return 0;
233  }
234 
235  Resource::DataStreamPtr VorbisDecoder::GetStream() const
236  {
237  return this->VorbisStream;
238  }
239 
240  Boole VorbisDecoder::IsEndOfStream() const
241  {
242  return ( this->VorbisStream->eof() || this->VorbisStream->tellg() >= this->VorbisStreamSize );
243  }
244 
245  Boole VorbisDecoder::SetPosition(Int32 Position, const Boole Relative)
246  {
247  if( this->IsSeekingSupported() ) {
248  if( Relative ) {
249  Real CurrPos = ov_raw_tell( &(this->VDID->VorbisFile) );
250  if( ov_raw_seek( &(this->VDID->VorbisFile), CurrPos + Position ) == 0 ) {
251  this->VorbisStreamPos = this->VorbisStream->tellg();
252  return true;
253  }
254  }else{
255  if( ov_raw_seek( &(this->VDID->VorbisFile), Position ) == 0 ) {
256  this->VorbisStreamPos = this->VorbisStream->tellg();
257  return true;
258  }
259  }
260  }
261  return false;
262  }
263 
264  Boole VorbisDecoder::Seek(const Real Seconds, const Boole Relative)
265  {
266  if( this->IsSeekingSupported() ) {
267  if( Relative ) {
268  Real CurrTime = ov_time_tell( &(this->VDID->VorbisFile) );
269  if( ov_time_seek( &(this->VDID->VorbisFile), CurrTime + Seconds ) == 0 ) {
270  this->VorbisStreamPos = this->VorbisStream->tellg();
271  return true;
272  }
273  }else{
274  if( ov_time_seek( &(this->VDID->VorbisFile), Seconds ) == 0 ) {
275  this->VorbisStreamPos = this->VorbisStream->tellg();
276  return true;
277  }
278  }
279  }
280  return false;
281  }
282 
283  UInt32 VorbisDecoder::ReadAudioData(void* Output, UInt32 Amount)
284  {
285  if( this->Valid ) {
286  if( this->VorbisStreamPos != this->VorbisStream->tellg() ) {
287  this->ClearStreamErrors();
288  this->VorbisStream->seekg( this->VorbisStreamPos );
289  }
290  Integer Temp = 0;
291  Integer Ret = ov_read( &(this->VDID->VorbisFile), (Char8*)Output, Amount, 0, 2, 1, &Temp );
292  this->VorbisStreamPos = this->VorbisStream->tellg();
293  return ( Ret > 0 ? Ret : 0 );
294  }else{
295  return 0;
296  }
297  }
298 
299  ///////////////////////////////////////////////////////////////////////////////
300  // Stream Stats
301 
302  Real VorbisDecoder::GetTotalTime() const
303  { return ov_time_total( &(this->VDID->VorbisFile), -1 ); }
304 
305  Real VorbisDecoder::GetCurrentTime() const
306  { return ov_time_tell( &(this->VDID->VorbisFile) ); }
307 
308  UInt32 VorbisDecoder::GetTotalSize() const
309  { return ov_pcm_total( &(this->VDID->VorbisFile), -1 ) * this->VDID->VorbisInfo->channels; }
310 
311  UInt32 VorbisDecoder::GetCompressedSize() const
312  { return ov_raw_total( &(this->VDID->VorbisFile), -1 ); }
313 
314  UInt32 VorbisDecoder::GetCurrentPosition() const
315  { return ov_pcm_tell( &(this->VDID->VorbisFile) ) * this->VDID->VorbisInfo->channels; }
316 
317  UInt32 VorbisDecoder::GetCurrentCompressedPosition() const
318  { return ov_raw_tell( &(this->VDID->VorbisFile) ); }
319  }//Audio
320 }//Mezzanine
321 
322 #endif //ENABLE_VORBIS_ENCODE
323 
324 #endif
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Base class for streams that support both read and write operations.
Definition: datastream.h:263
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
Encoding
The encoding to use when reading or writing an audio buffer.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
CountedPtr< DataStream > DataStreamPtr
This is a convenience type for a data stream in a counted pointer.
Definition: datastream.h:383
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Declaration of DataStream.
BitConfig
Used to describe the different bit configurations supported by this audio system. ...