cAudio  2.3.0
3d Audio Engine
cOggDecoder.cpp
1 // Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2 // This file is part of the "cAudio Engine"
3 // For conditions of distribution and use, see copyright notice in cAudio.h
4 
5 #include "cOggDecoder.h"
6 
7 #if CAUDIO_COMPILE_WITH_OGG_DECODER == 1
8 
9 namespace cAudio
10 {
12  size_t VorbisRead(void *ptr, size_t byteSize,size_t sizeToRead, void *datasource)
13  {
14  IDataSource* Stream = (IDataSource*)datasource;
15  return Stream->read(ptr,byteSize*sizeToRead);
16  }
17 
19  int VorbisSeek(void *datasource,ogg_int64_t offset,int whence)
20  {
21  IDataSource* Stream = (IDataSource*)datasource;
22  switch (whence)
23  {
24  case SEEK_SET:
25  Stream->seek(offset, false);
26  break;
27 
28  case SEEK_CUR:
29  Stream->seek(offset, true);
30  break;
31 
32  case SEEK_END:
33  Stream->seek(Stream->getSize()-offset, false);
34  break;
35  };
36  return 0;
37  }
38 
40  long VorbisTell(void *datasource)
41  {
42  return ((IDataSource*)datasource)->getCurrentPos();
43  }
44 
45  int VorbisCloseFunc(void *datasource)
46  {
47  return 0;
48  }
49 
50  cOggDecoder::cOggDecoder(IDataSource* stream) : IAudioDecoder(stream)
51  {
52  vorbisCallbacks.read_func = VorbisRead;
53  vorbisCallbacks.close_func = VorbisCloseFunc;
54  vorbisCallbacks.seek_func = VorbisSeek;
55  vorbisCallbacks.tell_func = VorbisTell;
56  Valid = (ov_open_callbacks(Stream,&oggStream,NULL,0,vorbisCallbacks) == 0);
57 
58  if(Valid)
59  {
60  vorbisInfo = ov_info(&oggStream, -1);
61  vorbisComment = ov_comment(&oggStream,-1);
62  }
63  }
64 
65  cOggDecoder::~cOggDecoder()
66  {
67  ov_clear(&oggStream);
68  }
69 
70  AudioFormats cOggDecoder::getFormat()
71  {
72  if(Valid)
73  {
74  if(vorbisInfo->channels == 1)
75  {
76  return EAF_16BIT_MONO;
77  }
78  else
79  {
80  return EAF_16BIT_STEREO;
81  }
82  }
83  return EAF_8BIT_MONO;
84  }
85 
86  int cOggDecoder::getFrequency()
87  {
88  if(Valid)
89  {
90  return vorbisInfo->rate;
91  }
92  return 0;
93  }
94 
95  bool cOggDecoder::isSeekingSupported()
96  {
97  if(Valid)
98  {
99  return (ov_seekable(&oggStream)!=0);
100  }
101  return false;
102  }
103 
104  bool cOggDecoder::isValid()
105  {
106  return Valid;
107  }
108 
109  int cOggDecoder::readAudioData(void* output, int amount)
110  {
111  if(Valid)
112  {
113  int temp = 0;
114  return ov_read(&oggStream,(char*)output,amount,0,2,1,&temp);
115  }
116  return 0;
117  }
118 
119  bool cOggDecoder::setPosition(int position, bool relative)
120  {
121  if(Valid)
122  {
123  if(ov_seekable(&oggStream))
124  {
125  return (ov_raw_seek(&oggStream,position)==0);
126  }
127  }
128  return false;
129  }
130 
131  bool cOggDecoder::seek(float seconds, bool relative)
132  {
133  if(Valid)
134  {
135  if(ov_seekable(&oggStream))
136  {
137  if(relative)
138  {
139  double curtime = ov_time_tell(&oggStream);
140  return (ov_time_seek(&oggStream,curtime+seconds)==0);
141  }
142  else
143  return (ov_time_seek(&oggStream,seconds)==0);
144  }
145  }
146  return false;
147  }
148 
149  float cOggDecoder::getTotalTime()
150  {
151  return ov_time_total(&oggStream, -1);
152  }
153 
154  int cOggDecoder::getTotalSize()
155  {
156  // ov_pcm_total is in samples
157  return ov_pcm_total(&oggStream, -1) * vorbisInfo->channels * 2;
158  }
159 
160  int cOggDecoder::getCompressedSize()
161  {
162  return ov_raw_total(&oggStream, -1);
163  }
164 
165  float cOggDecoder::getCurrentTime()
166  {
167  return ov_time_tell(&oggStream);
168  }
169 
170  int cOggDecoder::getCurrentPosition()
171  {
172  // ov_pcm_tell is in samples
173  return ov_pcm_tell(&oggStream) * vorbisInfo->channels * 2;
174  }
175 
176  int cOggDecoder::getCurrentCompressedPosition()
177  {
178  return ov_raw_tell(&oggStream);
179  }
180 
181  cAudioString cOggDecoder::getType() const
182  {
183  return cAudioString(_CTEXT("cOggDecoder"));
184  }
185 };
186 
187 #endif
cAudio::AudioFormats
AudioFormats
Enumeration of audio formats supported by the engine.
Definition: EAudioFormats.h:11
cAudio
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:16