Home | History | Annotate | Download | only in vorbisfile
      1 <html>
      2 
      3 <head>
      4 <title>vorbisfile - Example Code</title>
      5 <link rel=stylesheet href="style.css" type="text/css">
      6 </head>
      7 
      8 <body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
      9 <table border=0 width=100%>
     10 <tr>
     11 <td><p class=tiny>Vorbisfile documentation</p></td>
     12 <td align=right><p class=tiny>vorbisfile version 1.2.0 - 20070723</p></td>
     13 </tr>
     14 </table>
     15 
     16 <h1>Example Code: seeking</h1>
     17 
     18 <p>
     19 The following is a run-through of the seeking example program supplied
     20 with vorbisfile - <a href="seeking_test_c.html">seeking_test.c</a>.  
     21 This program tests the vorbisfile <a href="ov_time_seek.html">ov_time_seek</a> function by seeking to random points within the file.
     22 
     23 <p>
     24 First, relevant headers, including vorbis-specific "codec.h" and "vorbisfile.h" have to be included.
     25 
     26 <br><br>
     27 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     28 <tr bgcolor=#cccccc>
     29 	<td>
     30 <pre><b>
     31 #include &lt;stdlib.h>
     32 #include &lt;stdio.h>
     33 #include "vorbis/codec.h"
     34 #include "vorbis/vorbisfile.h"
     35 </b></pre>
     36 	</td>
     37 </tr>
     38 </table>
     39 
     40 <p>Inside main(), we declare our primary OggVorbis_File structure.  We also declare other helpful variables to track our progress within the file.
     41 <br><br>
     42 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     43 <tr bgcolor=#cccccc>
     44         <td>
     45 <pre><b>
     46 int main(){
     47   OggVorbis_File ov;
     48   int i;
     49 </b></pre>
     50         </td>
     51 </tr>
     52 </table>
     53 
     54 <p>This example takes its input on stdin which is in 'text' mode by default under Windows; this will corrupt the input data unless set to binary mode.  This applies only to Windows.
     55 <br><br>
     56 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     57 <tr bgcolor=#cccccc>
     58         <td>
     59 <pre><b>
     60 #ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
     61   _setmode( _fileno( stdin ), _O_BINARY );
     62 #endif
     63 </b></pre>
     64         </td>
     65 </tr>
     66 </table>
     67 
     68 <p><a href="ov_open_callbacks.html">ov_open()</a> must be
     69 called to initialize the <a href="OggVorbis_File.html">OggVorbis_File</a> structure with default values.  
     70 <a href="ov_open_callbacks.html">ov_open_callbacks()</a> also checks to ensure that we're reading Vorbis format and not something else.
     71 
     72 <br><br>
     73 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     74 <tr bgcolor=#cccccc>
     75         <td>
     76 <pre><b>
     77   if(ov_open_callbacks(stdin,&ov,NULL,-1, OV_CALLBACKS_NOCLOSE)<0){
     78     printf("Could not open input as an OggVorbis file.\n\n");
     79     exit(1);
     80   }
     81 
     82 </b></pre>
     83         </td>
     84 </tr>
     85 </table>
     86 
     87 <p>
     88 First we check to make sure the stream is seekable using <a href="ov_seekable.html">ov_seekable</a>.
     89 
     90 <p>Then we seek to 100 random spots in the bitstream using <a href="ov_time_seek.html">ov_time_seek</a> with randomly generated values.
     91 
     92 <br><br>
     93 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     94 <tr bgcolor=#cccccc>
     95         <td>
     96 <pre><b>
     97   
     98   /* print details about each logical bitstream in the input */
     99   if(ov_seekable(&ov)){
    100     double length=ov_time_total(&ov,-1);
    101     printf("testing seeking to random places in %g seconds....\n",length);
    102     for(i=0;i<100;i++){
    103       double val=(double)rand()/RAND_MAX*length;
    104       ov_time_seek(&ov,val);
    105       printf("\r\t%d [%gs]...     ",i,val);
    106       fflush(stdout);
    107     }
    108     
    109     printf("\r                                   \nOK.\n\n");
    110   }else{
    111     printf("Standard input was not seekable.\n");
    112   }
    113   
    114 </b></pre>
    115         </td>
    116 </tr>
    117 </table>
    118 <p>
    119 When we're done seeking, we need to call <a href="ov_clear.html">ov_clear()</a> to release the bitstream.
    120 
    121 <br><br>
    122 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
    123 <tr bgcolor=#cccccc>
    124         <td>
    125 <pre><b>
    126   ov_clear(&ov);
    127   return 0;
    128 }
    129 </b></pre>
    130         </td>
    131 </tr>
    132 </table>
    133 
    134 <p>
    135 The full source for seeking_test.c can be found with the vorbis
    136 distribution in <a href="seeking_test_c.html">seeking_test.c</a>.
    137 
    138 <br><br>
    139 <hr noshade>
    140 <table border=0 width=100%>
    141 <tr valign=top>
    142 <td><p class=tiny>copyright &copy; 2007 Xiph.org</p></td>
    143 <td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a><br><a href="mailto:team (a] vorbis.org">team (a] vorbis.org</a></p></td>
    144 </tr><tr>
    145 <td><p class=tiny>Vorbisfile documentation</p></td>
    146 <td align=right><p class=tiny>vorbisfile version 1.2.0 - 20070723</p></td>
    147 </tr>
    148 </table>
    149 
    150 </body>
    151 
    152 </html>
    153