Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: simple example decoder using vorbisfile Chris@1: last mod: $Id: vorbisfile_example.c 16328 2009-07-24 01:51:10Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to Chris@1: stdout using vorbisfile. Using vorbisfile is much simpler than Chris@1: dealing with libvorbis. */ Chris@1: Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: Chris@1: #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */ Chris@1: #include Chris@1: #include Chris@1: #endif Chris@1: Chris@1: char pcmout[4096]; /* take 4k out of the data segment, not the stack */ Chris@1: Chris@1: int main(){ Chris@1: OggVorbis_File vf; Chris@1: int eof=0; Chris@1: int current_section; Chris@1: Chris@1: #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ Chris@1: /* Beware the evil ifdef. We avoid these where we can, but this one we Chris@1: cannot. Don't add any more, you'll probably go to hell if you do. */ Chris@1: _setmode( _fileno( stdin ), _O_BINARY ); Chris@1: _setmode( _fileno( stdout ), _O_BINARY ); Chris@1: #endif Chris@1: Chris@1: if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) { Chris@1: fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: /* Throw the comments plus a few lines about the bitstream we're Chris@1: decoding */ Chris@1: { Chris@1: char **ptr=ov_comment(&vf,-1)->user_comments; Chris@1: vorbis_info *vi=ov_info(&vf,-1); Chris@1: while(*ptr){ Chris@1: fprintf(stderr,"%s\n",*ptr); Chris@1: ++ptr; Chris@1: } Chris@1: fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate); Chris@1: fprintf(stderr,"\nDecoded length: %ld samples\n", Chris@1: (long)ov_pcm_total(&vf,-1)); Chris@1: fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor); Chris@1: } Chris@1: Chris@1: while(!eof){ Chris@1: long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section); Chris@1: if (ret == 0) { Chris@1: /* EOF */ Chris@1: eof=1; Chris@1: } else if (ret < 0) { Chris@1: if(ret==OV_EBADLINK){ Chris@1: fprintf(stderr,"Corrupt bitstream section! Exiting.\n"); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: /* some other error in the stream. Not a problem, just reporting it in Chris@1: case we (the app) cares. In this case, we don't. */ Chris@1: } else { Chris@1: /* we don't bother dealing with sample rate changes, etc, but Chris@1: you'll have to*/ Chris@1: fwrite(pcmout,1,ret,stdout); Chris@1: } Chris@1: } Chris@1: Chris@1: /* cleanup */ Chris@1: ov_clear(&vf); Chris@1: Chris@1: fprintf(stderr,"Done.\n"); Chris@1: return(0); Chris@1: }