00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <math.h>
00027 #include <vorbis/codec.h>
00028
00029 #if defined(_WIN32) && !defined(__SYMBIAN32__)
00030 #include <io.h>
00031 #include <fcntl.h>
00032 #endif
00033
00034 #if defined(__MACOS__) && defined(__MWERKS__)
00035 #include <console.h>
00036 #endif
00037
00038 ogg_int16_t convbuffer[4096];
00039 int convsize=4096;
00040
00041 extern void _VDBG_dump(void);
00042
00043 int main(){
00044 ogg_sync_state oy;
00045 ogg_stream_state os;
00046
00047 ogg_page og;
00048 ogg_packet op;
00049
00050 vorbis_info vi;
00051
00052 vorbis_comment vc;
00053 vorbis_dsp_state vd;
00054 vorbis_block vb;
00055
00056 char *buffer;
00057 int bytes;
00058
00059 #if defined(_WIN32) && !defined(__SYMBIAN32__)
00060
00061
00062 _setmode( _fileno( stdin ), _O_BINARY );
00063 _setmode( _fileno( stdout ), _O_BINARY );
00064 #endif
00065
00066 #if defined(macintosh) && defined(__MWERKS__)
00067 {
00068 int argc;
00069 char **argv;
00070 argc=ccommand(&argv);
00071
00072 }
00073 #endif
00074
00075
00076
00077 ogg_sync_init(&oy);
00078
00079 while(1){
00080 int eos=0;
00081 int i;
00082
00083
00084
00085
00086
00087
00088
00089 buffer=ogg_sync_buffer(&oy,4096);
00090 bytes=fread(buffer,1,4096,stdin);
00091 ogg_sync_wrote(&oy,bytes);
00092
00093
00094 if(ogg_sync_pageout(&oy,&og)!=1){
00095
00096 if(bytes<4096)break;
00097
00098
00099 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
00100 exit(1);
00101 }
00102
00103
00104
00105 ogg_stream_init(&os,ogg_page_serialno(&og));
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 vorbis_info_init(&vi);
00116 vorbis_comment_init(&vc);
00117 if(ogg_stream_pagein(&os,&og)<0){
00118
00119 fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
00120 exit(1);
00121 }
00122
00123 if(ogg_stream_packetout(&os,&op)!=1){
00124
00125 fprintf(stderr,"Error reading initial header packet.\n");
00126 exit(1);
00127 }
00128
00129 if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
00130
00131 fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
00132 "audio data.\n");
00133 exit(1);
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 i=0;
00147 while(i<2){
00148 while(i<2){
00149 int result=ogg_sync_pageout(&oy,&og);
00150 if(result==0)break;
00151
00152
00153 if(result==1){
00154 ogg_stream_pagein(&os,&og);
00155
00156
00157 while(i<2){
00158 result=ogg_stream_packetout(&os,&op);
00159 if(result==0)break;
00160 if(result<0){
00161
00162
00163 fprintf(stderr,"Corrupt secondary header. Exiting.\n");
00164 exit(1);
00165 }
00166 vorbis_synthesis_headerin(&vi,&vc,&op);
00167 i++;
00168 }
00169 }
00170 }
00171
00172 buffer=ogg_sync_buffer(&oy,4096);
00173 bytes=fread(buffer,1,4096,stdin);
00174 if(bytes==0 && i<2){
00175 fprintf(stderr,"End of file before finding all Vorbis headers!\n");
00176 exit(1);
00177 }
00178 ogg_sync_wrote(&oy,bytes);
00179 }
00180
00181
00182
00183 {
00184 char **ptr=vc.user_comments;
00185 while(*ptr){
00186 fprintf(stderr,"%s\n",*ptr);
00187 ++ptr;
00188 }
00189 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
00190 fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
00191 }
00192
00193 convsize=4096/vi.channels;
00194
00195
00196
00197 vorbis_synthesis_init(&vd,&vi);
00198 vorbis_block_init(&vd,&vb);
00199
00200
00201
00202
00203
00204
00205 while(!eos){
00206 while(!eos){
00207 int result=ogg_sync_pageout(&oy,&og);
00208 if(result==0)break;
00209 if(result<0){
00210 fprintf(stderr,"Corrupt or missing data in bitstream; "
00211 "continuing...\n");
00212 }else{
00213 ogg_stream_pagein(&os,&og);
00214
00215 while(1){
00216 result=ogg_stream_packetout(&os,&op);
00217
00218 if(result==0)break;
00219 if(result<0){
00220
00221 }else{
00222
00223 float **pcm;
00224 int samples;
00225
00226 if(vorbis_synthesis(&vb,&op)==0)
00227 vorbis_synthesis_blockin(&vd,&vb);
00228
00229
00230
00231
00232
00233
00234
00235 while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
00236 int j;
00237 int clipflag=0;
00238 int bout=(samples<convsize?samples:convsize);
00239
00240
00241
00242 for(i=0;i<vi.channels;i++){
00243 ogg_int16_t *ptr=convbuffer+i;
00244 float *mono=pcm[i];
00245 for(j=0;j<bout;j++){
00246 #if 1
00247 int val=mono[j]*32767.f;
00248 #else
00249 int val=mono[j]*32767.f+drand48()-0.5f;
00250 #endif
00251
00252 if(val>32767){
00253 val=32767;
00254 clipflag=1;
00255 }
00256 if(val<-32768){
00257 val=-32768;
00258 clipflag=1;
00259 }
00260 *ptr=val;
00261 ptr+=vi.channels;
00262 }
00263 }
00264
00265 if(clipflag)
00266 fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
00267
00268
00269 fwrite(convbuffer,2*vi.channels,bout,stdout);
00270
00271 vorbis_synthesis_read(&vd,bout);
00272
00273
00274 }
00275 }
00276 }
00277 if(ogg_page_eos(&og))eos=1;
00278 }
00279 }
00280 if(!eos){
00281 buffer=ogg_sync_buffer(&oy,4096);
00282 bytes=fread(buffer,1,4096,stdin);
00283 ogg_sync_wrote(&oy,bytes);
00284 if(bytes==0)eos=1;
00285 }
00286 }
00287
00288
00289
00290
00291 ogg_stream_clear(&os);
00292
00293
00294
00295
00296 vorbis_block_clear(&vb);
00297 vorbis_dsp_clear(&vd);
00298 vorbis_comment_clear(&vc);
00299 vorbis_info_clear(&vi);
00300 }
00301
00302
00303 ogg_sync_clear(&oy);
00304
00305 fprintf(stderr,"Done.\n");
00306 return(0);
00307 }