Blob Blame History Raw
<html>

<head>
<title>vorbisfile - Example Code</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>

<body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
<table border=0 width=100%>
<tr>
<td><p class=tiny>Vorbisfile documentation</p></td>
<td align=right><p class=tiny>vorbisfile version 1.3.2 - 20101101</p></td>
</tr>
</table>

<h1>Chaining Example Code</h1>

<p>
The following is a run-through of the chaining example program supplied
with vorbisfile - <a href="chaining_example_c.html">chaining_example.c</a>.  
This program demonstrates how to work with a chained bitstream.

<p>
First, relevant headers, including vorbis-specific "codec.h" and "vorbisfile.h" have to be included.

<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
	<td>
<pre><b>
#include "vorbis/codec.h"
#include "vorbis/vorbisfile.h"
#include "../lib/misc.h"
</b></pre>
	</td>
</tr>
</table>

<p>Inside main(), we declare our primary OggVorbis_File structure.  We also declare a other helpful variables to track our progress within the file.
<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
        <td>
<pre><b>
int main(){
  OggVorbis_File ov;
  int i;
</b></pre>
        </td>
</tr>
</table>

<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.
<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
        <td>
<pre><b>
#ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
  _setmode( _fileno( stdin ), _O_BINARY );
#endif
</b></pre>
        </td>
</tr>
</table>

<p>We call <a href="ov_open_callbacks.html">ov_open_callbacks()</a> to
initialize the <a href="OggVorbis_File.html">OggVorbis_File</a>
structure.  <a href="ov_open_callbacks.html">ov_open_callbacks()</a>
also checks to ensure that we're reading Vorbis format and not
something else. The OV_CALLBACKS_NOCLOSE callbacks instruct
libvorbisfile not to close stdin later during cleanup.<p>

<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
        <td>
<pre><b>
  if(ov_open_callbacks(stdin,&amp;ov,NULL,-1,OV_CALLBACKS_NOCLOSE)&lt;0){
    printf("Could not open input as an OggVorbis file.\n\n");
    exit(1);
  }

</b></pre>
        </td>
</tr>
</table>

<p>
First we check to make sure the stream is seekable using <a href="ov_seekable.html">ov_seekable</a>.

<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>.

<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>.

<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
        <td>
<pre><b>
  if(ov_seekable(&amp;ov)){
    printf("Input bitstream contained %ld logical bitstream section(s).\n",
	   ov_streams(&amp;ov));
    printf("Total bitstream playing time: %ld seconds\n\n",
	   (long)ov_time_total(&amp;ov,-1));

  }else{
    printf("Standard input was not seekable.\n"
	   "First logical bitstream information:\n\n");
  }
  
</b></pre>
        </td>
</tr>
</table>

<p>Now we're going to iterate through each logical bitstream and print information about that bitstream.

<p>We use <a href="ov_info.html">ov_info</a> to pull out the <a href="../libvorbis/vorbis_info.html">vorbis_info</a> struct for each logical bitstream.  This struct contains bitstream-specific info.

<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.

<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
        <td>
<pre><b>
  for(i=0;i&lt;ov_streams(&amp;ov);i++){
    vorbis_info *vi=ov_info(&amp;ov,i);
    printf("\tlogical bitstream section %d information:\n",i+1);
    printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
	   vi-&gt;rate,vi-&gt;channels,ov_bitrate(&amp;ov,i)/1000,
	   ov_serialnumber(&amp;ov,i));
    printf("\t\tcompressed length: %ld bytes ",(long)(ov_raw_total(&amp;ov,i)));
    printf(" play time: %lds\n",(long)ov_time_total(&amp;ov,i));
  } 
</b></pre>
        </td>
</tr>
</table>
<p>
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.

<br><br>
<table border=0 width=100% color=black cellspacing=0 cellpadding=7>
<tr bgcolor=#cccccc>
        <td>
<pre><b>
  ov_clear(&amp;ov);
  return 0;
}
</b></pre>
        </td>
</tr>
</table>

<p>
The full source for chaining_example.c can be found with the vorbis
distribution in <a href="chaining_example_c.html">chaining_example.c</a>.

<br><br>
<hr noshade>
<table border=0 width=100%>
<tr valign=top>
<td><p class=tiny>copyright &copy; 2000-2010 Xiph.Org</p></td>
<td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a></p></td>
</tr><tr>
<td><p class=tiny>Vorbisfile documentation</p></td>
<td align=right><p class=tiny>vorbisfile version 1.3.2 - 20101101</p></td>
</tr>
</table>

</body>

</html>