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-2007 *
|
---|
9 | * by the Xiph.Org Foundation https://xiph.org/ *
|
---|
10 | * *
|
---|
11 | ********************************************************************
|
---|
12 |
|
---|
13 | function: simple example decoder using vorbisfile
|
---|
14 |
|
---|
15 | ********************************************************************/
|
---|
16 |
|
---|
17 | /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
|
---|
18 | stdout using vorbisfile. Using vorbisfile is much simpler than
|
---|
19 | dealing with libvorbis. */
|
---|
20 |
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <math.h>
|
---|
24 | #include <vorbis/codec.h>
|
---|
25 | #include <vorbis/vorbisfile.h>
|
---|
26 |
|
---|
27 | #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
---|
28 | #include <io.h>
|
---|
29 | #include <fcntl.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | char pcmout[4096]; /* take 4k out of the data segment, not the stack */
|
---|
33 |
|
---|
34 | int main(){
|
---|
35 | OggVorbis_File vf;
|
---|
36 | int eof=0;
|
---|
37 | int current_section;
|
---|
38 |
|
---|
39 | #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
---|
40 | /* Beware the evil ifdef. We avoid these where we can, but this one we
|
---|
41 | cannot. Don't add any more, you'll probably go to hell if you do. */
|
---|
42 | _setmode( _fileno( stdin ), _O_BINARY );
|
---|
43 | _setmode( _fileno( stdout ), _O_BINARY );
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
|
---|
47 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
|
---|
48 | exit(1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /* Throw the comments plus a few lines about the bitstream we're
|
---|
52 | decoding */
|
---|
53 | {
|
---|
54 | char **ptr=ov_comment(&vf,-1)->user_comments;
|
---|
55 | vorbis_info *vi=ov_info(&vf,-1);
|
---|
56 | while(*ptr){
|
---|
57 | fprintf(stderr,"%s\n",*ptr);
|
---|
58 | ++ptr;
|
---|
59 | }
|
---|
60 | fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
|
---|
61 | fprintf(stderr,"\nDecoded length: %ld samples\n",
|
---|
62 | (long)ov_pcm_total(&vf,-1));
|
---|
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,¤t_section);
|
---|
68 | if (ret == 0) {
|
---|
69 | /* EOF */
|
---|
70 | eof=1;
|
---|
71 | } else if (ret < 0) {
|
---|
72 | if(ret==OV_EBADLINK){
|
---|
73 | fprintf(stderr,"Corrupt bitstream section! Exiting.\n");
|
---|
74 | exit(1);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* 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. */
|
---|
79 | } else {
|
---|
80 | /* we don't bother dealing with sample rate changes, etc, but
|
---|
81 | you'll have to*/
|
---|
82 | fwrite(pcmout,1,ret,stdout);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* cleanup */
|
---|
87 | ov_clear(&vf);
|
---|
88 |
|
---|
89 | fprintf(stderr,"Done.\n");
|
---|
90 | return(0);
|
---|
91 | }
|
---|