Add support for named files on the command line, while maintaining
[platform/upstream/libvorbis.git] / examples / vorbisfile_example.c
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-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10
11  ********************************************************************
12
13  function: simple example decoder using vorbisfile
14  last mod: $Id: vorbisfile_example.c,v 1.6 2001/09/15 04:47:48 cwolf Exp $
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(int argc, char **argv){
36   OggVorbis_File vf;
37   int eof=0;
38   int current_section;
39   FILE *fpin=NULL;
40   FILE *fpout=NULL;
41   char msg[512];
42
43 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
44   /* Beware the evil ifdef. We avoid these where we can, but this one we 
45      cannot. Don't add any more, you'll probably go to hell if you do. */
46   _setmode( _fileno( stdin ), _O_BINARY );
47   _setmode( _fileno( stdout ), _O_BINARY );
48 #endif
49
50   if (argc == 3)
51   {
52     if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
53     {
54       (void)sprintf(msg, "Can't open %s for reading.", argv[1]);
55       perror(msg);
56       return 1;
57     }
58
59     if ((fpout = fopen(argv[2], "wb")) == (FILE*)NULL)
60     {
61       (void)sprintf(msg, "Can't open %s for writing.", argv[2]);
62       perror(msg);
63       return 1;
64     }
65   }
66   else
67   {
68     fpin = stdin;
69     fpout = stdout;
70   }
71
72   if(ov_open(fpin, &vf, NULL, 0) < 0) {
73       fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
74       exit(1);
75   }
76
77   /* Throw the comments plus a few lines about the bitstream we're
78      decoding */
79   {
80     char **ptr=ov_comment(&vf,-1)->user_comments;
81     vorbis_info *vi=ov_info(&vf,-1);
82     while(*ptr){
83       fprintf(stderr,"%s\n",*ptr);
84       ++ptr;
85     }
86     fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
87     fprintf(stderr,"\nDecoded length: %ld samples\n",
88             (long)ov_pcm_total(&vf,-1));
89     fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
90   }
91   
92   while(!eof){
93     long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
94     if (ret == 0) {
95       /* EOF */
96       eof=1;
97     } else if (ret < 0) {
98       /* error in the stream.  Not a problem, just reporting it in
99          case we (the app) cares.  In this case, we don't. */
100     } else {
101       /* we don't bother dealing with sample rate changes, etc, but
102          you'll have to*/
103       fwrite(pcmout,1,ret,fpout);
104     }
105   }
106
107   /* cleanup */
108   ov_clear(&vf);
109     
110   fprintf(stderr,"Done.\n");
111   return(0);
112 }
113