examples/SFExamples/oggvorbiscodec/src/libvorbis/doc/vorbisfile/seekingexample.html

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.25 - 20000615</p></td>
00013 </tr>
00014 </table>
00015 
00016 <h1>Example Code</h1>
00017 
00018 <p>
00019 The following is a run-through of the decoding example program supplied
00020 with vorbisfile - <a href="vorbisfile_example_c.html">vorbisfile_example.c</a>.  
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 "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 &lt;stdio.h&gt;
00032 #include &lt;stdlib.h&gt;
00033 #include &lt;math.h&gt;
00034 #include "vorbis/codec.h"
00035 #include "vorbis/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 &lt;io.h&gt;
00049 #include &lt;fcntl.h&gt;
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,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
00127   }
00128   
00129 </b></pre>
00130         </td>
00131 </tr>
00132 </table>
00133 
00134 <p>
00135 Here's the read loop:
00136 
00137 <br><br>
00138 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00139 <tr bgcolor=#cccccc>
00140         <td>
00141 <pre><b>
00142 
00143   while(!eof){
00144     long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
00145     switch(ret){
00146     case 0:
00147       /* EOF */
00148       eof=1;
00149       break;
00150     case -1:
00151       break;
00152     default:
00153       fwrite(pcmout,1,ret,stdout);
00154       break;
00155     }
00156   }
00157   
00158 </b></pre>
00159         </td>
00160 </tr>
00161 </table>
00162 
00163 <p>
00164 The code is reading blocks of data using <a href="ov_read.html">ov_read()</a>.
00165 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.
00166 
00167 <p>
00168 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.
00169 
00170 <br><br>
00171 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
00172 <tr bgcolor=#cccccc>
00173         <td>
00174 <pre><b>
00175 
00176   ov_clear(&vf);
00177     
00178   fprintf(stderr,"Done.\n");
00179   return(0);
00180 }
00181 </b></pre>
00182         </td>
00183 </tr>
00184 </table>
00185 
00186 <p>
00187 The full source for vorbisfile_example.c can be found with the vorbis
00188 distribution in <a href="vorbisfile_example_c.html">vorbisfile_example.c</a>.
00189 
00190 <br><br>
00191 <hr noshade>
00192 <table border=0 width=100%>
00193 <tr valign=top>
00194 <td><p class=tiny>copyright &copy; 2000 vorbis team</p></td>
00195 <td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a><br><a href="mailto:team@vorbis.org">team@vorbis.org</a></p></td>
00196 </tr><tr>
00197 <td><p class=tiny>vorbisfile documentation</p></td>
00198 <td align=right><p class=tiny>vorbisfile version 1.25 - 20000615</p></td>
00199 </tr>
00200 </table>
00201 
00202 </body>
00203 
00204 </html>

Generated by  doxygen 1.6.2