examples/sfexamples/oggvorbiscodec/src/libvorbis/examples/vorbisfile_example.c

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

Generated by  doxygen 1.6.2