Blame doc/vorbisfile/example.html

Packit 06404a
<html>
Packit 06404a
Packit 06404a
<head>
Packit 06404a
<title>Vorbisfile - Example Code</title>
Packit 06404a
<link rel=stylesheet href="style.css" type="text/css">
Packit 06404a
</head>
Packit 06404a
Packit 06404a
<body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
Packit 06404a
Packit 06404a
Packit 06404a

Vorbisfile documentation

Packit 06404a

vorbisfile version 1.3.2 - 20101101

Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a

Decoding Example Code

Packit 06404a
Packit 06404a

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

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


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

Packit 06404a
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.
Packit 06404a


Packit 06404a
Packit 06404a
Packit 06404a
	
Packit 06404a
Packit 06404a
#ifdef _WIN32
Packit 06404a
#include <io.h>
Packit 06404a
#include <fcntl.h>
Packit 06404a
#endif
Packit 06404a
Packit 06404a
	
Packit 06404a
Packit 06404a
Packit 06404a

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


Packit 06404a
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
char pcmout[4096];
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a

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

Packit 06404a
Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.
Packit 06404a


Packit 06404a
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
int main(int argc, char **argv){
Packit 06404a
  OggVorbis_File vf;
Packit 06404a
  int eof=0;
Packit 06404a
  int current_section;
Packit 06404a
Packit 06404a
#ifdef _WIN32
Packit 06404a
  _setmode( _fileno( stdin ), _O_BINARY );
Packit 06404a
  _setmode( _fileno( stdout ), _O_BINARY );
Packit 06404a
#endif
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a

We call ov_open_callbacks() to

Packit 06404a
initialize the OggVorbis_File structure with default values.
Packit 06404a
ov_open_callbacks() also checks
Packit 06404a
to ensure that we're reading Vorbis format and not something else. The
Packit 06404a
OV_CALLBACKS_NOCLOSE callbacks instruct libvorbisfile not to close
Packit 06404a
stdin later during cleanup.
Packit 06404a
Packit 06404a


Packit 06404a
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
  if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
Packit 06404a
      fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
Packit 06404a
      exit(1);
Packit 06404a
  }
Packit 06404a
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a

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


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

Packit 06404a
Here's the read loop:
Packit 06404a
Packit 06404a


Packit 06404a
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
  while(!eof){
Packit 06404a
    long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
Packit 06404a
    if (ret == 0) {
Packit 06404a
      /* EOF */
Packit 06404a
      eof=1;
Packit 06404a
    } else if (ret < 0) {
Packit 06404a
      /* error in the stream.  Not a problem, just reporting it in
Packit 06404a
	 case we (the app) cares.  In this case, we don't. */
Packit 06404a
    } else {
Packit 06404a
      /* we don't bother dealing with sample rate changes, etc, but
Packit 06404a
	 you'll have to*/
Packit 06404a
      fwrite(pcmout,1,ret,stdout);
Packit 06404a
    }
Packit 06404a
  }
Packit 06404a
Packit 06404a
  
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a

Packit 06404a
The code is reading blocks of data using ov_read().
Packit 06404a
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.
Packit 06404a
Packit 06404a

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


Packit 06404a
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
  ov_clear(&vf);
Packit 06404a
    
Packit 06404a
  fprintf(stderr,"Done.\n");
Packit 06404a
  return(0);
Packit 06404a
}
Packit 06404a
Packit 06404a
        
Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a

Packit 06404a
Packit 06404a


Packit 06404a

Packit 06404a
Packit 06404a
Packit 06404a

copyright © 2000-2010 Xiph.Org

Packit 06404a

Ogg Vorbis

Packit 06404a
Packit 06404a

Vorbisfile documentation

Packit 06404a

vorbisfile version 1.3.2 - 20101101

Packit 06404a
Packit 06404a
Packit 06404a
Packit 06404a
</body>
Packit 06404a
Packit 06404a
</html>