Vorbisfile documentation | vorbisfile version 1.3.2 - 20101101

# Chaining Example Code

The following is a run-through of the chaining example program supplied with vorbisfile - chaining_example.c. This program demonstrates how to work with a chained bitstream.

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

    #include "vorbis/codec.h"
    #include "vorbis/vorbisfile.h"
    #include "../lib/misc.h"

Inside main(), we declare our primary OggVorbis_File structure. We also declare a other helpful variables to track our progress within the file.

    int main(){
      OggVorbis_File ov;
      int i;

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.

    #ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
      _setmode( _fileno( stdin ), _O_BINARY );
    #endif

We call ov_open_callbacks() to initialize the OggVorbis_File structure. ov_open_callbacks() 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.

      if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){
        printf("Could not open input as an OggVorbis file.\n\n");
        exit(1);
      }

First we check to make sure the stream is seekable using ov_seekable.

Then we're going to find the number of logical bitstreams in the physical bitstream using ov_streams.

We use ov_time_total to determine the total length of the physical bitstream. We specify that we want the entire bitstream by using the argument `-1`.

      if(ov_seekable(&ov)){
        printf("Input bitstream contained %ld logical bitstream section(s).\n",
    	   ov_streams(&ov));
        printf("Total bitstream playing time: %ld seconds\n\n",
    	   (long)ov_time_total(&ov,-1));

      }else{
        printf("Standard input was not seekable.\n"
    	   "First logical bitstream information:\n\n");
      }

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

We use ov_info to pull out the vorbis_info struct for each logical bitstream. This struct contains bitstream-specific info.

ov_serialnumber retrieves the unique serial number for the logical bistream. ov_raw_total gives the total compressed bytes for the logical bitstream, and ov_time_total gives the total time in the logical bitstream.

      for(i=0;i<ov_streams(&ov);i++){
        vorbis_info *vi=ov_info(&ov,i);
        printf("\tlogical bitstream section %d information:\n",i+1);
        printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
    	   vi->rate,vi->channels,ov_bitrate(&ov,i)/1000,
    	   ov_serialnumber(&ov,i));
        printf("\t\tcompressed length: %ld bytes ",(long)(ov_raw_total(&ov,i)));
        printf(" play time: %lds\n",(long)ov_time_total(&ov,i));
      }

When we're done with the entire physical bitstream, we need to call ov_clear() to release the bitstream.

      ov_clear(&ov);
      return 0;
    }

The full source for chaining_example.c can be found with the vorbis distribution in chaining_example.c.

copyright © 2000-2010 Xiph.Org | Ogg Vorbis
Vorbisfile documentation | vorbisfile version 1.3.2 - 20101101