cOggDecoder.cpp
00001
00002
00003
00004
00005 #include "../Headers/cOggDecoder.h"
00006
00007 #ifdef CAUDIO_COMPILE_WITH_OGG_DECODER
00008
00009 namespace cAudio
00010 {
00012 size_t VorbisRead(void *ptr, size_t byteSize,size_t sizeToRead, void *datasource)
00013 {
00014 IDataSource* Stream = (IDataSource*)datasource;
00015 return Stream->read(ptr,byteSize*sizeToRead);
00016 }
00017
00019 int VorbisSeek(void *datasource,ogg_int64_t offset,int whence)
00020 {
00021 IDataSource* Stream = (IDataSource*)datasource;
00022 switch (whence)
00023 {
00024 case SEEK_SET:
00025 Stream->seek(offset, false);
00026 break;
00027
00028 case SEEK_CUR:
00029 Stream->seek(offset, true);
00030 break;
00031
00032 case SEEK_END:
00033 Stream->seek(Stream->getSize()-offset, false);
00034 break;
00035 };
00036 return 0;
00037 }
00038
00040 long VorbisTell(void *datasource)
00041 {
00042 return ((IDataSource*)datasource)->getCurrentPos();
00043 }
00044
00045 cOggDecoder::cOggDecoder(IDataSource* stream) : IAudioDecoder(stream)
00046 {
00047 vorbisCallbacks.read_func = VorbisRead;
00048 vorbisCallbacks.close_func = NULL;
00049 vorbisCallbacks.seek_func = VorbisSeek;
00050 vorbisCallbacks.tell_func = VorbisTell;
00051 Valid = (ov_open_callbacks(Stream,&oggStream,NULL,0,vorbisCallbacks) == 0);
00052
00053 if(Valid)
00054 {
00055 vorbisInfo = ov_info(&oggStream, -1);
00056 vorbisComment = ov_comment(&oggStream,-1);
00057 }
00058 }
00059
00060 cOggDecoder::~cOggDecoder()
00061 {
00062 ov_clear(&oggStream);
00063 }
00064
00065 AudioFormats cOggDecoder::getFormat()
00066 {
00067 if(Valid)
00068 {
00069 if(vorbisInfo->channels == 1)
00070 {
00071 return EAF_16BIT_MONO;
00072 }
00073 else
00074 {
00075 return EAF_16BIT_STEREO;
00076 }
00077 }
00078 return EAF_8BIT_MONO;
00079 }
00080
00081 int cOggDecoder::getFrequency()
00082 {
00083 if(Valid)
00084 {
00085 return vorbisInfo->rate;
00086 }
00087 return 0;
00088 }
00089
00090 bool cOggDecoder::isSeekingSupported()
00091 {
00092 if(Valid)
00093 {
00094 return (ov_seekable(&oggStream)!=0);
00095 }
00096 return false;
00097 }
00098
00099 bool cOggDecoder::isValid()
00100 {
00101 return Valid;
00102 }
00103
00104 int cOggDecoder::readAudioData(void* output, int amount)
00105 {
00106 if(Valid)
00107 {
00108 int temp = 0;
00109 int result = ov_read(&oggStream,(char*)output,amount,0,2,1,&temp);
00110
00111 return result;
00112 }
00113 return 0;
00114 }
00115
00116 bool cOggDecoder::setPosition(int position, bool relative)
00117 {
00118 if(Valid)
00119 {
00120 if(ov_seekable(&oggStream))
00121 {
00122 return (ov_raw_seek(&oggStream,position)==0);
00123 }
00124 }
00125 return false;
00126 }
00127
00128 bool cOggDecoder::seek(float seconds, bool relative)
00129 {
00130 if(Valid)
00131 {
00132 if(ov_seekable(&oggStream))
00133 {
00134 if(relative)
00135 {
00136 float curtime = ov_time_tell(&oggStream);
00137 return (ov_time_seek(&oggStream,curtime+seconds)==0);
00138 }
00139 else
00140 return (ov_time_seek(&oggStream,seconds)==0);
00141 }
00142 }
00143 return false;
00144 }
00145
00146 float cOggDecoder::getTotalTime()
00147 {
00148 return ov_time_total(&oggStream, -1);
00149 }
00150
00151 int cOggDecoder::getTotalSize()
00152 {
00153 return ov_pcm_total(&oggStream, -1) * vorbisInfo->channels;
00154 }
00155
00156 int cOggDecoder::getCompressedSize()
00157 {
00158 return ov_raw_total(&oggStream, -1);
00159 }
00160
00161 float cOggDecoder::getCurrentTime()
00162 {
00163 return ov_time_tell(&oggStream);
00164 }
00165
00166 int cOggDecoder::getCurrentPosition()
00167 {
00168 return ov_pcm_tell(&oggStream) * vorbisInfo->channels;
00169 }
00170
00171 int cOggDecoder::getCurrentCompressedPosition()
00172 {
00173 return ov_raw_tell(&oggStream);
00174 }
00175 };
00176
00177 #endif