00001 <html>
00002
00003 <head>
00004 <title>Vorbisfile - Example Code</title>
00005 <link rel=stylesheet href="style.css" type="text/css">
00006 </head>
00007
00008 <body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
00009 <table border=0 width=100%>
00010 <tr>
00011 <td><p class=tiny>Vorbisfile documentation</p></td>
00012 <td align=right><p class=tiny>vorbisfile version 1.68 - 20030307</p></td>
00013 </tr>
00014 </table>
00015
00016 <h1>Decoding Example Code</h1>
00017
00018 <p>
00019 The following is a run-through of the decoding example program supplied
00020 with libvorbisfile, vorbisfile_example.c.
00021 This program takes a vorbis bitstream from stdin and writes raw pcm to stdout.
00022
00023 <p>
00024 First, relevant headers, including vorbis-specific "vorbis/codec.h" and "vorbisfile.h" have to be included.
00025
00026 <br><br>
00027 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00028 <tr bgcolor=#cccccc>
00029 <td>
00030 <pre><b>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <math.h>
00034 #include "vorbis/codec.h"
00035 #include "vorbisfile.h"
00036 </b></pre>
00037 </td>
00038 </tr>
00039 </table>
00040 <p>
00041 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.
00042 <br><br>
00043 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00044 <tr bgcolor=#cccccc>
00045 <td>
00046 <pre><b>
00047 #ifdef _WIN32
00048 #include <io.h>
00049 #include <fcntl.h>
00050 #endif
00051 </b></pre>
00052 </td>
00053 </tr>
00054 </table>
00055 <p>
00056 Next, a buffer for the pcm audio output is declared.
00057
00058 <br><br>
00059 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00060 <tr bgcolor=#cccccc>
00061 <td>
00062 <pre><b>
00063 char pcmout[4096];
00064 </b></pre>
00065 </td>
00066 </tr>
00067 </table>
00068
00069 <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.
00070 Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.
00071 <br><br>
00072 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00073 <tr bgcolor=#cccccc>
00074 <td>
00075 <pre><b>
00076 int main(int argc, char **argv){
00077 OggVorbis_File vf;
00078 int eof=0;
00079 int current_section;
00080
00081 #ifdef _WIN32
00082 _setmode( _fileno( stdin ), _O_BINARY );
00083 _setmode( _fileno( stdout ), _O_BINARY );
00084 #endif
00085 </b></pre>
00086 </td>
00087 </tr>
00088 </table>
00089
00090 <p><a href="ov_open.html">ov_open()</a> must be
00091 called to initialize the <b>OggVorbis_File</b> structure with default values.
00092 <a href="ov_open.html">ov_open()</a> also checks to ensure that we're reading Vorbis format and not something else.
00093
00094 <br><br>
00095 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00096 <tr bgcolor=#cccccc>
00097 <td>
00098 <pre><b>
00099 if(ov_open(stdin, &vf, NULL, 0) < 0) {
00100 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
00101 exit(1);
00102 }
00103
00104 </b></pre>
00105 </td>
00106 </tr>
00107 </table>
00108
00109 <p>
00110 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.
00111 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>.
00112
00113 <br><br>
00114 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00115 <tr bgcolor=#cccccc>
00116 <td>
00117 <pre><b>
00118 {
00119 char **ptr=ov_comment(&vf,-1)->user_comments;
00120 vorbis_info *vi=ov_info(&vf,-1);
00121 while(*ptr){
00122 fprintf(stderr,"%s\n",*ptr);
00123 ++ptr;
00124 }
00125 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
00126 fprintf(stderr,"\nDecoded length: %ld samples\n",
00127 (long)ov_pcm_total(&vf,-1));
00128 fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
00129 }
00130
00131 </b></pre>
00132 </td>
00133 </tr>
00134 </table>
00135
00136 <p>
00137 Here's the read loop:
00138
00139 <br><br>
00140 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00141 <tr bgcolor=#cccccc>
00142 <td>
00143 <pre><b>
00144
00145 while(!eof){
00146 long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
00147 if (ret == 0) {
00148 /* EOF */
00149 eof=1;
00150 } else if (ret < 0) {
00151 /* error in the stream. Not a problem, just reporting it in
00152 case we (the app) cares. In this case, we don't. */
00153 } else {
00154
00155
00156 fwrite(pcmout,1,ret,stdout);
00157 }
00158 }
00159
00160
00161 </b></pre>
00162 </td>
00163 </tr>
00164 </table>
00165
00166 <p>
00167 The code is reading blocks of data using <a href="ov_read.html">ov_read()</a>.
00168 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.
00169
00170 <p>
00171 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.
00172
00173 <br><br>
00174 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00175 <tr bgcolor=#cccccc>
00176 <td>
00177 <pre><b>
00178
00179 ov_clear(&vf);
00180
00181 fprintf(stderr,"Done.\n");
00182 return(0);
00183 }
00184 </b></pre>
00185 </td>
00186 </tr>
00187 </table>
00188
00189 <p>
00190
00191 <br><br>
00192 <hr noshade>
00193 <table border=0 width=100%>
00194 <tr valign=top>
00195 <td><p class=tiny>copyright © 2003 Xiph.org</p></td>
00196 <td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a></p></td>
00197 </tr><tr>
00198 <td><p class=tiny>Vorbisfile documentation</p></td>
00199 <td align=right><p class=tiny>vorbisfile version 1.68 - 20030307</p></td>
00200 </tr>
00201 </table>
00202
00203 </body>
00204
00205 </html>