Blame examples/vorbisfile_example.c

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