examples/SFExamples/oggvorbiscodec94/src/tremor/ivorbisfile_example.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
00004  *                                                                  *
00005  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00006  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00007  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00008  *                                                                  *
00009  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
00010  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
00011  *                                                                  *
00012  ********************************************************************
00013 
00014  function: simple example decoder using vorbisidec
00015 
00016  ********************************************************************/
00017 
00018 /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
00019    stdout using vorbisfile. Using vorbisfile is much simpler than
00020    dealing with libvorbis. */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <vorbis/ivorbiscodec.h>
00025 #include <vorbis/ivorbisfile.h>
00026 
00027 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
00028 #include <io.h>
00029 #include <fcntl.h>
00030 #endif
00031 
00032 char pcmout[4096]; /* take 4k out of the data segment, not the stack */
00033 
00034 int main(){
00035   OggVorbis_File vf;
00036   int eof=0;
00037   int current_section;
00038 
00039 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
00040   /* Beware the evil ifdef. We avoid these where we can, but this one we 
00041      cannot. Don't add any more, you'll probably go to hell if you do. */
00042   _setmode( _fileno( stdin ), _O_BINARY );
00043   _setmode( _fileno( stdout ), _O_BINARY );
00044 #endif
00045 
00046   if(ov_open(stdin, &vf, NULL, 0) < 0) {
00047       fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
00048       exit(1);
00049   }
00050 
00051   /* Throw the comments plus a few lines about the bitstream we're
00052      decoding */
00053   {
00054     char **ptr=ov_comment(&vf,-1)->user_comments;
00055     vorbis_info *vi=ov_info(&vf,-1);
00056     while(*ptr){
00057       fprintf(stderr,"%s\n",*ptr);
00058       ++ptr;
00059     }
00060     fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
00061     fprintf(stderr,"\nDecoded length: %ld samples\n",
00062             (long)ov_pcm_total(&vf,-1));
00063     fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
00064   }
00065   
00066   while(!eof){
00067     long ret=ov_read(&vf,pcmout,sizeof(pcmout),&current_section);
00068     if (ret == 0) {
00069       /* EOF */
00070       eof=1;
00071     } else if (ret < 0) {
00072       /* error in the stream.  Not a problem, just reporting it in
00073          case we (the app) cares.  In this case, we don't. */
00074     } else {
00075       /* we don't bother dealing with sample rate changes, etc, but
00076          you'll have to*/
00077       fwrite(pcmout,1,ret,stdout);
00078     }
00079   }
00080 
00081   /* cleanup */
00082   ov_clear(&vf);
00083     
00084   fprintf(stderr,"Done.\n");
00085   return(0);
00086 }

Generated by  doxygen 1.6.2