Vorbisfile documentation | vorbisfile version 1.68 - 20030307

# 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"
    #include "../lib/misc.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;

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

      if(ov_open(stdin,&ov,NULL,-1)<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 © 2003 Xiph.org | Ogg Vorbis team@vorbis.org
Vorbisfile documentation | vorbisfile version 1.68 - 20030307