vorbisfile_example.c 3 KB
Newer Older
Monty's avatar
 
Monty committed
1 2
/********************************************************************
 *                                                                  *
Monty's avatar
 
Monty committed
3
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
 
Monty committed
4 5 6
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
Monty's avatar
 
Monty committed
7
 *                                                                  *
8 9
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
10
 *                                                                  *
Monty's avatar
 
Monty committed
11 12 13 14 15 16 17 18 19 20 21 22 23
 ********************************************************************

 function: simple example decoder using vorbisfile

 ********************************************************************/

/* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
   stdout using vorbisfile. Using vorbisfile is much simpler than
   dealing with libvorbis. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Monty's avatar
 
Monty committed
24 25
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
Monty's avatar
 
Monty committed
26 27 28 29 30 31 32 33

#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
#include <io.h>
#include <fcntl.h>
#endif

char pcmout[4096]; /* take 4k out of the data segment, not the stack */

Segher Boessenkool's avatar
Segher Boessenkool committed
34
int main(){
Monty's avatar
 
Monty committed
35 36 37 38 39 40 41 42 43 44 45
  OggVorbis_File vf;
  int eof=0;
  int current_section;

#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  /* Beware the evil ifdef. We avoid these where we can, but this one we 
     cannot. Don't add any more, you'll probably go to hell if you do. */
  _setmode( _fileno( stdin ), _O_BINARY );
  _setmode( _fileno( stdout ), _O_BINARY );
#endif

46
  if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
Monty's avatar
 
Monty committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60
      fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
      exit(1);
  }

  /* Throw the comments plus a few lines about the bitstream we're
     decoding */
  {
    char **ptr=ov_comment(&vf,-1)->user_comments;
    vorbis_info *vi=ov_info(&vf,-1);
    while(*ptr){
      fprintf(stderr,"%s\n",*ptr);
      ++ptr;
    }
    fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
Monty's avatar
 
Monty committed
61
    fprintf(stderr,"\nDecoded length: %ld samples\n",
62
            (long)ov_pcm_total(&vf,-1));
Monty's avatar
 
Monty committed
63 64 65 66 67
    fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
  }
  
  while(!eof){
    long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
Monty's avatar
 
Monty committed
68
    if (ret == 0) {
Monty's avatar
 
Monty committed
69 70
      /* EOF */
      eof=1;
Monty's avatar
 
Monty committed
71
    } else if (ret < 0) {
Monty's avatar
Monty committed
72 73 74 75 76 77
      if(ret==OV_EBADLINK){
        fprintf(stderr,"Corrupt bitstream section! Exiting.\n");
        exit(1);
      }

      /* some other error in the stream.  Not a problem, just reporting it in
78
         case we (the app) cares.  In this case, we don't. */
Monty's avatar
 
Monty committed
79
    } else {
Monty's avatar
 
Monty committed
80
      /* we don't bother dealing with sample rate changes, etc, but
81
         you'll have to*/
82
      fwrite(pcmout,1,ret,stdout);
Monty's avatar
 
Monty committed
83 84 85 86 87 88 89 90 91
    }
  }

  /* cleanup */
  ov_clear(&vf);
    
  fprintf(stderr,"Done.\n");
  return(0);
}