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