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>Chaining Example Code</h1>
     17 
     18 <p>
     19 The following is a run-through of the chaining example program supplied
     20 with vorbisfile - <a href="chaining_example_c.html">chaining_example.c</a>.  
     21 This program demonstrates how to work with a chained bitstream.
     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 "vorbis/codec.h"
     32 #include "vorbis/vorbisfile.h"
     33 #include "../lib/misc.h"
     34 </b></pre>
     35 	</td>
     36 </tr>
     37 </table>
     38 
     39 <p>Inside main(), we declare our primary OggVorbis_File structure.  We also declare a other helpful variables to track our progress within the file.
     40 <br><br>
     41 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     42 <tr bgcolor=#cccccc>
     43         <td>
     44 <pre><b>
     45 int main(){
     46   OggVorbis_File ov;
     47   int i;
     48 </b></pre>
     49         </td>
     50 </tr>
     51 </table>
     52 
     53 <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.
     54 <br><br>
     55 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     56 <tr bgcolor=#cccccc>
     57         <td>
     58 <pre><b>
     59 #ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
     60   _setmode( _fileno( stdin ), _O_BINARY );
     61 #endif
     62 </b></pre>
     63         </td>
     64 </tr>
     65 </table>
     66 
     67 <p>We call <a href="ov_open_callbacks.html">ov_open_callbacks()</a> to
     68 initialize the <a href="OggVorbis_File.html">OggVorbis_File</a>
     69 structure.  <a href="ov_open_callbacks.html">ov_open_callbacks()</a>
     70 also checks to ensure that we're reading Vorbis format and not
     71 something else. The OV_CALLBACKS_NOCLOSE callbacks instruct
     72 libvorbisfile not to close stdin later during cleanup.<p>
     73 
     74 <br><br>
     75 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     76 <tr bgcolor=#cccccc>
     77         <td>
     78 <pre><b>
     79   if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){
     80     printf("Could not open input as an OggVorbis file.\n\n");
     81     exit(1);
     82   }
     83 
     84 </b></pre>
     85         </td>
     86 </tr>
     87 </table>
     88 
     89 <p>
     90 First we check to make sure the stream is seekable using <a href="ov_seekable.html">ov_seekable</a>.
     91 
     92 <p>Then we're going to find the number of logical bitstreams in the physical bitstream using <a href="ov_streams.html">ov_streams</a>.
     93 
     94 <p>We use <a href="ov_time_total.html">ov_time_total</a> to determine the total length of the physical bitstream.  We specify that we want the entire bitstream by using the argument <tt>-1</tt>.
     95 
     96 <br><br>
     97 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
     98 <tr bgcolor=#cccccc>
     99         <td>
    100 <pre><b>
    101   if(ov_seekable(&amp;ov)){
    102     printf("Input bitstream contained %ld logical bitstream section(s).\n",
    103 	   ov_streams(&amp;ov));
    104     printf("Total bitstream playing time: %ld seconds\n\n",
    105 	   (long)ov_time_total(&amp;ov,-1));
    106 
    107   }else{
    108     printf("Standard input was not seekable.\n"
    109 	   "First logical bitstream information:\n\n");
    110   }
    111   
    112 </b></pre>
    113         </td>
    114 </tr>
    115 </table>
    116 
    117 <p>Now we're going to iterate through each logical bitstream and print information about that bitstream.
    118 
    119 <p>We use <a href="ov_info.html">ov_info</a> to pull out the <a href="vorbis_info.html">vorbis_info</a> struct for each logical bitstream.  This struct contains bitstream-specific info.
    120 
    121 <p><a href="ov_serialnumber.html">ov_serialnumber</a> retrieves the unique serial number for the logical bistream.  <a href="ov_raw_total.html">ov_raw_total</a> gives the total compressed bytes for the logical bitstream, and <a href="ov_time_total.html">ov_time_total</a> gives the total time in the logical bitstream.
    122 
    123 <br><br>
    124 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
    125 <tr bgcolor=#cccccc>
    126         <td>
    127 <pre><b>
    128   for(i=0;i&lt;ov_streams(&amp;ov);i++){
    129     vorbis_info *vi=ov_info(&amp;ov,i);
    130     printf("\tlogical bitstream section %d information:\n",i+1);
    131     printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
    132 	   vi-&gt;rate,vi-&gt;channels,ov_bitrate(&amp;ov,i)/1000,
    133 	   ov_serialnumber(&amp;ov,i));
    134     printf("\t\tcompressed length: %ld bytes ",(long)(ov_raw_total(&amp;ov,i)));
    135     printf(" play time: %lds\n",(long)ov_time_total(&amp;ov,i));
    136   } 
    137 </b></pre>
    138         </td>
    139 </tr>
    140 </table>
    141 <p>
    142 When we're done with the entire physical bitstream, we need to call <a href="ov_clear.html">ov_clear()</a> to release the bitstream.
    143 
    144 <br><br>
    145 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
    146 <tr bgcolor=#cccccc>
    147         <td>
    148 <pre><b>
    149   ov_clear(&amp;ov);
    150   return 0;
    151 }
    152 </b></pre>
    153         </td>
    154 </tr>
    155 </table>
    156 
    157 <p>
    158 The full source for chaining_example.c can be found with the vorbis
    159 distribution in <a href="chaining_example_c.html">chaining_example.c</a>.
    160 
    161 <br><br>
    162 <hr noshade>
    163 <table border=0 width=100%>
    164 <tr valign=top>
    165 <td><p class=tiny>copyright &copy; 2007 Xiph.org</p></td>
    166 <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>
    167 </tr><tr>
    168 <td><p class=tiny>Vorbisfile documentation</p></td>
    169 <td align=right><p class=tiny>vorbisfile version 1.2.0 - 20070723</p></td>
    170 </tr>
    171 </table>
    172 
    173 </body>
    174 
    175 </html>
    176