4 <title>Tremor - Example Code
</title>
5 <link rel=stylesheet
href=
"style.css" type=
"text/css">
8 <body bgcolor=white text=black
link=
"#5555ff" alink=
"#5555ff" vlink=
"#5555ff">
9 <table border=
0 width=
100%
>
11 <td><p class=tiny
>Tremor documentation
</p></td>
12 <td align=right
><p class=tiny
>Tremor version
1.0 -
20020403</p></td>
19 The following is a run-through of the decoding example program supplied
20 with libvorbisidec, ivorbisfile_example.c.
21 This program takes a vorbis bitstream from stdin and writes raw pcm to stdout.
24 First, relevant headers, including vorbis-specific
"ivorbiscodec.h" and
"ivorbisfile.h" have to be included.
27 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
31 #include
<stdio.h
>
32 #include
<stdlib.h
>
33 #include
<math.h
>
34 #include
"ivorbiscodec.h"
35 #include
"ivorbisfile.h"
41 We also have to make a concession to Windows users here. If we are using windows for decoding, we must declare these libraries so that we can set stdin/stdout to binary.
43 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
49 #include
<fcntl.h
>
56 Next, a buffer for the pcm audio output is declared.
59 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
69 <p>Inside main(), we declare our primary OggVorbis_File structure. We also declare a few other helpful variables to track out progress within the file.
70 Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.
72 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
76 int main(int argc, char **argv){
82 _setmode( _fileno( stdin ), _O_BINARY );
83 _setmode( _fileno( stdout ), _O_BINARY );
90 <p><a href=
"ov_open.html">ov_open()
</a> must be
91 called to initialize the
<b>OggVorbis_File
</b> structure with default values.
92 <a href=
"ov_open.html">ov_open()
</a> also checks to ensure that we're reading Vorbis format and not something else.
95 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
99 if(ov_open(stdin, &vf, NULL,
0) <
0) {
100 fprintf(stderr,
"Input does not appear to be an Ogg bitstream.\n");
110 We're going to pull the channel and bitrate info from the file using
<a href=
"ov_info.html">ov_info()
</a> and show them to the user.
111 We also want to pull out and show the user a comment attached to the file using
<a href=
"ov_comment.html">ov_comment()
</a>.
114 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
119 char **ptr=ov_comment(&vf,-
1)-
>user_comments;
120 vorbis_info *vi=ov_info(&vf,-
1);
122 fprintf(stderr,
"%s\n",*ptr);
125 fprintf(stderr,
"\nBitstream is %d channel, %ldHz\n",vi-
>channels,vi-
>rate);
126 fprintf(stderr,
"\nDecoded length: %ld samples\n",
127 (long)ov_pcm_total(&vf,-
1));
128 fprintf(stderr,
"Encoded by: %s\n\n",ov_comment(&vf,-
1)-
>vendor);
137 Here's the read loop:
140 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
146 long ret=ov_read(&vf,pcmout,sizeof(pcmout),¤t_section);
150 } else if (ret <
0) {
151 /* error in the stream. Not a problem, just reporting it in
152 case we (the app) cares. In this case, we don't. */
154 /* we don't bother dealing with sample rate changes, etc, but
156 fwrite(pcmout,
1,ret,stdout);
167 The code is reading blocks of data using
<a href=
"ov_read.html">ov_read()
</a>.
168 Based on the value returned, we know if we're at the end of the file or have invalid data. If we have valid data, we write it to the pcm output.
171 Now that we've finished playing, we can pack up and go home. It's important to call
<a href=
"ov_clear.html">ov_clear()
</a> when we're finished.
174 <table border=
0 width=
100% color=black cellspacing=
0 cellpadding=
7>
181 fprintf(stderr,
"Done.\n");
193 <table border=
0 width=
100%
>
195 <td><p class=tiny
>copyright
© 2002 Xiph.org
</p></td>
196 <td align=right
><p class=tiny
><a href=
"http://www.xiph.org/ogg/vorbis/">Ogg Vorbis
</a></p></td>
198 <td><p class=tiny
>Tremor documentation
</p></td>
199 <td align=right
><p class=tiny
>Tremor version
1.0 -
20020403</p></td>