MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vorbisdecoder.cpp
1 // © Copyright 2010 - 2014 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 The Vorbis read callback.
60  size_t VorbisRead(void *ptr, size_t byteSize,size_t sizeToRead, void *datasource)
61  {
62  Mezzanine::Resource::DataStream* Stream = static_cast<Mezzanine::Resource::DataStream*>(datasource);
63  return Stream->Read(ptr,byteSize * sizeToRead);
64  }
65 
66  /// @internal
67  /// @brief The Vorbis seek callback (set position).
68  int VorbisSeek(void *datasource,ogg_int64_t offset,int whence)
69  {
70  Mezzanine::Resource::DataStream* Stream = static_cast<Mezzanine::Resource::DataStream*>(datasource);
71  switch(whence)
72  {
73  case SEEK_SET: Stream->SetStreamPosition(offset, Mezzanine::Resource::DataStream::SO_Beginning); break;
74  case SEEK_CUR: Stream->SetStreamPosition(offset, Mezzanine::Resource::DataStream::SO_Current); break;
75  case SEEK_END: Stream->SetStreamPosition(offset, Mezzanine::Resource::DataStream::SO_End); break;
76  };
77  return 0;
78  }
79 
80  /// @internal
81  /// @brief The Vorbis tell callback (retrieve position).
82  long VorbisTell(void *datasource)
83  {
84  return static_cast<Mezzanine::Resource::DataStream*>(datasource)->GetStreamPosition();
85  }
86 
87  /// @internal
88  /// @brief The stream close callback.
89  int VorbisClose(void *datasource)
90  {
91  return 0;
92  }
93 }
94 
95 namespace Mezzanine
96 {
97  namespace Audio
98  {
99  ///////////////////////////////////////////////////////////////////////////////
100  /// @internal
101  /// @brief Internal convenience class for the storage of Vorbis structs needed for Vorbis operations.
102  /// @details
103  ///////////////////////////////////////
104  class MEZZ_LIB VorbisDecoderInternalData
105  {
106  protected:
107  public:
108  ///////////////////////////////////////////////////////////////////////////////
109  // Publid Data Members
110 
111  /// @brief Format Information from the file.
112  vorbis_info* VorbisInfo;
113  /// @brief User comments embedded in the file.
114  vorbis_comment* VorbisComments;
115  /// @brief Mandatory callbacks for working with our streams.
116  ov_callbacks VorbisCallbacks;
117  /// @brief The main audio encoding.
118  OggVorbis_File VorbisFile;
119 
120  ///////////////////////////////////////////////////////////////////////////////
121  // Construction and destruction
122 
123  /// @brief Class constructor.
124  VorbisDecoderInternalData()
125  {
126  VorbisCallbacks.read_func = VorbisRead;
127  VorbisCallbacks.seek_func = VorbisSeek;
128  VorbisCallbacks.tell_func = VorbisTell;
129  VorbisCallbacks.close_func = VorbisClose;
130  }
131  /// @brief Class destructor.
132  ~VorbisDecoderInternalData() { }
133  };//VorbisDecoderInternalData
134 
135 
136  VorbisDecoder::VorbisDecoder(Resource::DataStreamPtr Stream)
137  : VorbisStream(Stream),
138  Valid(false)
139  {
140  this->VDID = new VorbisDecoderInternalData();
141  this->Valid = ( ov_open_callbacks(VorbisStream.get(),&(this->VDID->VorbisFile),NULL,0,this->VDID->VorbisCallbacks) == 0 );
142 
143  if( this->Valid )
144  {
145  this->VDID->VorbisInfo = ov_info( &(this->VDID->VorbisFile), -1 );
146  this->VDID->VorbisComments = ov_comment( &(this->VDID->VorbisFile), -1 );
147  }
148  }
149 
150  VorbisDecoder::~VorbisDecoder()
151  {
152  ov_clear( &(this->VDID->VorbisFile) );
153  }
154 
155  ///////////////////////////////////////////////////////////////////////////////
156  // Additional Vorbis Functionality
157 
158  String VorbisDecoder::GetUserComment(const UInt32 Index)
159  {
160  Char8* Comment = this->VDID->VorbisComments->user_comments[Index];
161  Integer CommentLength = this->VDID->VorbisComments->comment_lengths[Index];
162  return String(Comment,CommentLength);
163  }
164 
165  UInt32 VorbisDecoder::GetNumUserComments() const
166  {
167  return this->VDID->VorbisComments->comments;
168  }
169 
170  ///////////////////////////////////////////////////////////////////////////////
171  // Utility
172 
173  bool VorbisDecoder::IsValid()
174  {
175  return this->Valid;
176  }
177 
178  Audio::Encoding VorbisDecoder::GetEncoding() const
179  {
180  return Audio::Enc_VORBIS;
181  }
182 
183  bool VorbisDecoder::IsSeekingSupported()
184  {
185  if( this->Valid ) return (ov_seekable( &(this->VDID->VorbisFile) ) != 0);
186  else return false;
187  }
188 
189  Audio::BitConfig VorbisDecoder::GetBitConfiguration() const
190  {
191  if( this->Valid )
192  {
193  switch( this->VDID->VorbisInfo->channels )
194  {
195  case 1: return Audio::BC_16Bit_Mono; break;
196  case 2: return Audio::BC_16Bit_Stereo; break;
197  }
198  }
199 
200  return Audio::BC_8Bit_Mono;
201  }
202 
203  UInt32 VorbisDecoder::GetFrequency() const
204  {
205  if( this->Valid ) return this->VDID->VorbisInfo->rate;
206  else return 0;
207  }
208 
209  Resource::DataStreamPtr VorbisDecoder::GetStream() const
210  {
211  return this->VorbisStream;
212  }
213 
214  bool VorbisDecoder::SetPosition(Int32 Position, bool Relative)
215  {
216  if( this->IsSeekingSupported() )
217  {
218  if( Relative ) {
219  Real CurrPos = ov_raw_tell( &(this->VDID->VorbisFile) );
220  return ( ov_raw_seek( &(this->VDID->VorbisFile), CurrPos + Position ) == 0 );
221  }else{
222  return ( ov_raw_seek( &(this->VDID->VorbisFile), Position ) == 0 );
223  }
224  }
225  return false;
226  }
227 
228  bool VorbisDecoder::Seek(const Real Seconds, bool Relative)
229  {
230  if( this->IsSeekingSupported() )
231  {
232  if( Relative ) {
233  Real CurrTime = ov_time_tell( &(this->VDID->VorbisFile) );
234  return ( ov_time_seek( &(this->VDID->VorbisFile), CurrTime + Seconds ) == 0 );
235  }else{
236  return ( ov_time_seek( &(this->VDID->VorbisFile), Seconds ) == 0 );
237  }
238  }
239  return false;
240  }
241 
242  UInt32 VorbisDecoder::ReadAudioData(void* Output, UInt32 Amount)
243  {
244  if( this->Valid ) {
245  Integer Temp = 0;
246  Integer Ret = ov_read( &(this->VDID->VorbisFile), (Char8*)Output, Amount, 0, 2, 1, &Temp );
247  return Ret;
248  }else{
249  return 0;
250  }
251  }
252 
253  ///////////////////////////////////////////////////////////////////////////////
254  // Stream Stats
255 
256  Real VorbisDecoder::GetTotalTime() const
257  {
258  return ov_time_total( &(this->VDID->VorbisFile), -1 );
259  }
260 
261  Real VorbisDecoder::GetCurrentTime() const
262  {
263  return ov_time_tell( &(this->VDID->VorbisFile) );
264  }
265 
266  UInt32 VorbisDecoder::GetTotalSize() const
267  {
268  return ov_pcm_total( &(this->VDID->VorbisFile), -1 ) * this->VDID->VorbisInfo->channels;
269  }
270 
271  UInt32 VorbisDecoder::GetCompressedSize() const
272  {
273  return ov_raw_total( &(this->VDID->VorbisFile), -1 );
274  }
275 
276  UInt32 VorbisDecoder::GetCurrentPosition() const
277  {
278  return ov_pcm_tell( &(this->VDID->VorbisFile) ) * this->VDID->VorbisInfo->channels;
279  }
280 
281  UInt32 VorbisDecoder::GetCurrentCompressedPosition() const
282  {
283  return ov_raw_tell( &(this->VDID->VorbisFile) );
284  }
285  }//Audio
286 }//Mezzanine
287 
288 #endif //ENABLE_VORBIS_ENCODE
289 
290 #endif