Home | History | Annotate | Download | only in examples
      1 /********************************************************************
      2  *                                                                  *
      3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
      4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
      5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
      6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
      7  *                                                                  *
      8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
      9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
     10  *                                                                  *
     11  ********************************************************************
     12 
     13  function: simple example decoder using vorbisfile
     14  last mod: $Id: vorbisfile_example.c 16328 2009-07-24 01:51:10Z xiphmont $
     15 
     16  ********************************************************************/
     17 
     18 /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
     19    stdout using vorbisfile. Using vorbisfile is much simpler than
     20    dealing with libvorbis. */
     21 
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <math.h>
     25 #include <vorbis/codec.h>
     26 #include <vorbis/vorbisfile.h>
     27 
     28 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
     29 #include <io.h>
     30 #include <fcntl.h>
     31 #endif
     32 
     33 char pcmout[4096]; /* take 4k out of the data segment, not the stack */
     34 
     35 int main(){
     36   OggVorbis_File vf;
     37   int eof=0;
     38   int current_section;
     39 
     40 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
     41   /* Beware the evil ifdef. We avoid these where we can, but this one we
     42      cannot. Don't add any more, you'll probably go to hell if you do. */
     43   _setmode( _fileno( stdin ), _O_BINARY );
     44   _setmode( _fileno( stdout ), _O_BINARY );
     45 #endif
     46 
     47   if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
     48       fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
     49       exit(1);
     50   }
     51 
     52   /* Throw the comments plus a few lines about the bitstream we're
     53      decoding */
     54   {
     55     char **ptr=ov_comment(&vf,-1)->user_comments;
     56     vorbis_info *vi=ov_info(&vf,-1);
     57     while(*ptr){
     58       fprintf(stderr,"%s\n",*ptr);
     59       ++ptr;
     60     }
     61     fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
     62     fprintf(stderr,"\nDecoded length: %ld samples\n",
     63             (long)ov_pcm_total(&vf,-1));
     64     fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
     65   }
     66 
     67   while(!eof){
     68     long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
     69     if (ret == 0) {
     70       /* EOF */
     71       eof=1;
     72     } else if (ret < 0) {
     73       if(ret==OV_EBADLINK){
     74         fprintf(stderr,"Corrupt bitstream section! Exiting.\n");
     75         exit(1);
     76       }
     77 
     78       /* some other error in the stream.  Not a problem, just reporting it in
     79          case we (the app) cares.  In this case, we don't. */
     80     } else {
     81       /* we don't bother dealing with sample rate changes, etc, but
     82          you'll have to*/
     83       fwrite(pcmout,1,ret,stdout);
     84     }
     85   }
     86 
     87   /* cleanup */
     88   ov_clear(&vf);
     89 
     90   fprintf(stderr,"Done.\n");
     91   return(0);
     92 }
     93