Vorbisfile documentation

vorbisfile version 1.2.0 - 20070723

Example Code: seeking

The following is a run-through of the seeking example program supplied with vorbisfile - seeking_test.c. This program tests the vorbisfile ov_time_seek function by seeking to random points within the file.

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


#include <stdlib.h>
#include <stdio.h>
#include "vorbis/codec.h"
#include "vorbis/vorbisfile.h"

Inside main(), we declare our primary OggVorbis_File structure. We also declare 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

ov_open() must be called to initialize the OggVorbis_File structure with default values. ov_open_callbacks() also checks to ensure that we're reading Vorbis format and not something else.


  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 seek to 100 random spots in the bitstream using ov_time_seek with randomly generated values.


  
  /* print details about each logical bitstream in the input */
  if(ov_seekable(&ov)){
    double length=ov_time_total(&ov,-1);
    printf("testing seeking to random places in %g seconds....\n",length);
    for(i=0;i<100;i++){
      double val=(double)rand()/RAND_MAX*length;
      ov_time_seek(&ov,val);
      printf("\r\t%d [%gs]...     ",i,val);
      fflush(stdout);
    }
    
    printf("\r                                   \nOK.\n\n");
  }else{
    printf("Standard input was not seekable.\n");
  }
  

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


  ov_clear(&ov);
  return 0;
}

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


copyright © 2007 Xiph.org

Ogg Vorbis
team@vorbis.org

Vorbisfile documentation

vorbisfile version 1.2.0 - 20070723