vorbisfile documentation | vorbisfile version 1.25 - 20000615

# Example Code

The following is a run-through of the decoding example program supplied with vorbisfile - vorbisfile_example.c. This program takes a vorbis bitstream from stdin and writes raw pcm to stdout.

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

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

We also have to make a concession to Windows users here. If we are using windows for decoding, we must declare these libraries so that we can set stdin/stdout to binary.

    #ifdef _WIN32
    #include <io.h>
    #include <fcntl.h>
    #endif

Next, a buffer for the pcm audio output is declared.

    char pcmout[4096];

Inside main(), we declare our primary OggVorbis_File structure. We also declare a few other helpful variables to track out progress within the file. Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.

    int main(int argc, char **argv){
      OggVorbis_File vf;
      int eof=0;
      int current_section;

    #ifdef _WIN32
      _setmode( _fileno( stdin ), _O_BINARY );
    #endif

ov_open_callbacks() 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, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
          fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
          exit(1);
      }

We're going to pull the channel and bitrate info from the file using ov_info() and show them to the user. We also want to pull out and show the user a comment attached to the file using ov_comment().

      {
        char **ptr=ov_comment(&vf,-1)->user_comments;
        vorbis_info *vi=ov_info(&vf,-1);
        while(*ptr){
          fprintf(stderr,"%s\n",*ptr);
          ++ptr;
        }
        fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
        fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
      }

Here's the read loop:

      while(!eof){
        long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
        switch(ret){
        case 0:
          /* EOF */
          eof=1;
          break;
        case -1:
          break;
        default:
          fwrite(pcmout,1,ret,stdout);
          break;
        }
      }

The code is reading blocks of data using ov_read(). Based on the value returned, we know if we're at the end of the file or have invalid data. If we have valid data, we write it to the pcm output.

Now that we've finished playing, we can pack up and go home. It's important to call ov_clear() when we're finished.

      ov_clear(&vf);

      fprintf(stderr,"Done.\n");
      return(0);
    }

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

copyright © 2000 vorbis team | Ogg Vorbis
vorbisfile documentation | vorbisfile version 1.25 - 20000615