Chris@1: Chris@1: Chris@1: Chris@1: Vorbisfile - Example Code Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:

Vorbisfile documentation

vorbisfile version 1.3.2 - 20101101

Chris@1: Chris@1:

Decoding Example Code

Chris@1: Chris@1:

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

Chris@1: First, relevant headers, including vorbis-specific "vorbis/codec.h" and "vorbisfile.h" have to be included. Chris@1: Chris@1:

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

Chris@1: #include <stdio.h>
Chris@1: #include <stdlib.h>
Chris@1: #include <math.h>
Chris@1: #include "vorbis/codec.h"
Chris@1: #include "vorbisfile.h"
Chris@1: 
Chris@1:
Chris@1:

Chris@1: 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. Chris@1:

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

Chris@1: #ifdef _WIN32
Chris@1: #include <io.h>
Chris@1: #include <fcntl.h>
Chris@1: #endif
Chris@1: 
Chris@1:
Chris@1:

Chris@1: Next, a buffer for the pcm audio output is declared. Chris@1: Chris@1:

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

Chris@1: char pcmout[4096];
Chris@1: 
Chris@1:
Chris@1: Chris@1:

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

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

Chris@1: int main(int argc, char **argv){
Chris@1:   OggVorbis_File vf;
Chris@1:   int eof=0;
Chris@1:   int current_section;
Chris@1: 
Chris@1: #ifdef _WIN32
Chris@1:   _setmode( _fileno( stdin ), _O_BINARY );
Chris@1:   _setmode( _fileno( stdout ), _O_BINARY );
Chris@1: #endif
Chris@1: 
Chris@1:
Chris@1: Chris@1:

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

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

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

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

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

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

Chris@1: Here's the read loop: Chris@1: Chris@1:

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

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

Chris@1: The code is reading blocks of data using ov_read(). Chris@1: 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. Chris@1: Chris@1:

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

Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:
Chris@1:

Chris@1: 
Chris@1:   ov_clear(&vf);
Chris@1:     
Chris@1:   fprintf(stderr,"Done.\n");
Chris@1:   return(0);
Chris@1: }
Chris@1: 
Chris@1:
Chris@1: Chris@1:

Chris@1: Chris@1:

Chris@1:


Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1:

copyright © 2000-2010 Xiph.Org

Ogg Vorbis

Vorbisfile documentation

vorbisfile version 1.3.2 - 20101101

Chris@1: Chris@1: Chris@1: Chris@1: