123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <vorbis/codec.h>
- #ifdef _WIN32
- #include <io.h>
- #include <fcntl.h>
- #endif
- #if defined(__MACOS__) && defined(__MWERKS__)
- #include <console.h>
- #endif
- ogg_int16_t convbuffer[4096];
- int convsize=4096;
- extern void _VDBG_dump(void);
- int main(){
- ogg_sync_state oy;
- ogg_stream_state os;
- ogg_page og;
- ogg_packet op;
- vorbis_info vi;
- vorbis_comment vc;
- vorbis_dsp_state vd;
- vorbis_block vb;
- char *buffer;
- int bytes;
- #ifdef _WIN32
-
- _setmode( _fileno( stdin ), _O_BINARY );
- _setmode( _fileno( stdout ), _O_BINARY );
- #endif
- #if defined(macintosh) && defined(__MWERKS__)
- {
- int argc;
- char **argv;
- argc=ccommand(&argv);
-
- }
- #endif
-
- ogg_sync_init(&oy);
- while(1){
- int eos=0;
- int i;
-
-
- buffer=ogg_sync_buffer(&oy,4096);
- bytes=fread(buffer,1,4096,stdin);
- ogg_sync_wrote(&oy,bytes);
-
- if(ogg_sync_pageout(&oy,&og)!=1){
-
- if(bytes<4096)break;
-
- fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
- exit(1);
- }
-
-
- ogg_stream_init(&os,ogg_page_serialno(&og));
-
-
- vorbis_info_init(&vi);
- vorbis_comment_init(&vc);
- if(ogg_stream_pagein(&os,&og)<0){
-
- fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
- exit(1);
- }
- if(ogg_stream_packetout(&os,&op)!=1){
-
- fprintf(stderr,"Error reading initial header packet.\n");
- exit(1);
- }
- if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
-
- fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
- "audio data.\n");
- exit(1);
- }
-
-
- i=0;
- while(i<2){
- while(i<2){
- int result=ogg_sync_pageout(&oy,&og);
- if(result==0)break;
-
- if(result==1){
- ogg_stream_pagein(&os,&og);
- while(i<2){
- result=ogg_stream_packetout(&os,&op);
- if(result==0)break;
- if(result<0){
-
- fprintf(stderr,"Corrupt secondary header. Exiting.\n");
- exit(1);
- }
- result=vorbis_synthesis_headerin(&vi,&vc,&op);
- if(result<0){
- fprintf(stderr,"Corrupt secondary header. Exiting.\n");
- exit(1);
- }
- i++;
- }
- }
- }
-
- buffer=ogg_sync_buffer(&oy,4096);
- bytes=fread(buffer,1,4096,stdin);
- if(bytes==0 && i<2){
- fprintf(stderr,"End of file before finding all Vorbis headers!\n");
- exit(1);
- }
- ogg_sync_wrote(&oy,bytes);
- }
-
- {
- char **ptr=vc.user_comments;
- while(*ptr){
- fprintf(stderr,"%s\n",*ptr);
- ++ptr;
- }
- fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
- fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
- }
- convsize=4096/vi.channels;
-
- if(vorbis_synthesis_init(&vd,&vi)==0){
- vorbis_block_init(&vd,&vb);
-
- while(!eos){
- while(!eos){
- int result=ogg_sync_pageout(&oy,&og);
- if(result==0)break;
- if(result<0){
- fprintf(stderr,"Corrupt or missing data in bitstream; "
- "continuing...\n");
- }else{
- ogg_stream_pagein(&os,&og);
- while(1){
- result=ogg_stream_packetout(&os,&op);
- if(result==0)break;
- if(result<0){
-
- }else{
-
- float **pcm;
- int samples;
- if(vorbis_synthesis(&vb,&op)==0)
- vorbis_synthesis_blockin(&vd,&vb);
-
- while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
- int j;
- int clipflag=0;
- int bout=(samples<convsize?samples:convsize);
-
- for(i=0;i<vi.channels;i++){
- ogg_int16_t *ptr=convbuffer+i;
- float *mono=pcm[i];
- for(j=0;j<bout;j++){
- #if 1
- int val=floor(mono[j]*32767.f+.5f);
- #else
- int val=mono[j]*32767.f+drand48()-0.5f;
- #endif
-
- if(val>32767){
- val=32767;
- clipflag=1;
- }
- if(val<-32768){
- val=-32768;
- clipflag=1;
- }
- *ptr=val;
- ptr+=vi.channels;
- }
- }
- if(clipflag)
- fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
- fwrite(convbuffer,2*vi.channels,bout,stdout);
- vorbis_synthesis_read(&vd,bout);
- }
- }
- }
- if(ogg_page_eos(&og))eos=1;
- }
- }
- if(!eos){
- buffer=ogg_sync_buffer(&oy,4096);
- bytes=fread(buffer,1,4096,stdin);
- ogg_sync_wrote(&oy,bytes);
- if(bytes==0)eos=1;
- }
- }
-
- vorbis_block_clear(&vb);
- vorbis_dsp_clear(&vd);
- }else{
- fprintf(stderr,"Error: Corrupt header during playback initialization.\n");
- }
-
- ogg_stream_clear(&os);
- vorbis_comment_clear(&vc);
- vorbis_info_clear(&vi);
- }
-
- ogg_sync_clear(&oy);
- fprintf(stderr,"Done.\n");
- return(0);
- }
|