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