cannam@86: cannam@86: cannam@86: cannam@86: vorbisfile - Example Code cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:

Vorbisfile documentation

vorbisfile version 1.3.2 - 20101101

cannam@86: cannam@86:

Chaining Example Code

cannam@86: cannam@86:

cannam@86: The following is a run-through of the chaining example program supplied cannam@86: with vorbisfile - chaining_example.c. cannam@86: This program demonstrates how to work with a chained bitstream. cannam@86: cannam@86:

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

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86: #include "vorbis/codec.h"
cannam@86: #include "vorbis/vorbisfile.h"
cannam@86: #include "../lib/misc.h"
cannam@86: 
cannam@86:
cannam@86: cannam@86:

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

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86: int main(){
cannam@86:   OggVorbis_File ov;
cannam@86:   int i;
cannam@86: 
cannam@86:
cannam@86: cannam@86:

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. cannam@86:

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86: #ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
cannam@86:   _setmode( _fileno( stdin ), _O_BINARY );
cannam@86: #endif
cannam@86: 
cannam@86:
cannam@86: cannam@86:

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

cannam@86: cannam@86:

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86:   if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){
cannam@86:     printf("Could not open input as an OggVorbis file.\n\n");
cannam@86:     exit(1);
cannam@86:   }
cannam@86: 
cannam@86: 
cannam@86:
cannam@86: cannam@86:

cannam@86: First we check to make sure the stream is seekable using ov_seekable. cannam@86: cannam@86:

Then we're going to find the number of logical bitstreams in the physical bitstream using ov_streams. cannam@86: cannam@86:

We use ov_time_total to determine the total length of the physical bitstream. We specify that we want the entire bitstream by using the argument -1. cannam@86: cannam@86:

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86:   if(ov_seekable(&ov)){
cannam@86:     printf("Input bitstream contained %ld logical bitstream section(s).\n",
cannam@86: 	   ov_streams(&ov));
cannam@86:     printf("Total bitstream playing time: %ld seconds\n\n",
cannam@86: 	   (long)ov_time_total(&ov,-1));
cannam@86: 
cannam@86:   }else{
cannam@86:     printf("Standard input was not seekable.\n"
cannam@86: 	   "First logical bitstream information:\n\n");
cannam@86:   }
cannam@86:   
cannam@86: 
cannam@86:
cannam@86: cannam@86:

Now we're going to iterate through each logical bitstream and print information about that bitstream. cannam@86: cannam@86:

We use ov_info to pull out the vorbis_info struct for each logical bitstream. This struct contains bitstream-specific info. cannam@86: cannam@86:

ov_serialnumber retrieves the unique serial number for the logical bistream. ov_raw_total gives the total compressed bytes for the logical bitstream, and ov_time_total gives the total time in the logical bitstream. cannam@86: cannam@86:

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86:   for(i=0;i<ov_streams(&ov);i++){
cannam@86:     vorbis_info *vi=ov_info(&ov,i);
cannam@86:     printf("\tlogical bitstream section %d information:\n",i+1);
cannam@86:     printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
cannam@86: 	   vi->rate,vi->channels,ov_bitrate(&ov,i)/1000,
cannam@86: 	   ov_serialnumber(&ov,i));
cannam@86:     printf("\t\tcompressed length: %ld bytes ",(long)(ov_raw_total(&ov,i)));
cannam@86:     printf(" play time: %lds\n",(long)ov_time_total(&ov,i));
cannam@86:   } 
cannam@86: 
cannam@86:
cannam@86:

cannam@86: When we're done with the entire physical bitstream, we need to call ov_clear() to release the bitstream. cannam@86: cannam@86:

cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:
cannam@86:

cannam@86:   ov_clear(&ov);
cannam@86:   return 0;
cannam@86: }
cannam@86: 
cannam@86:
cannam@86: cannam@86:

cannam@86: The full source for chaining_example.c can be found with the vorbis cannam@86: distribution in chaining_example.c. cannam@86: cannam@86:

cannam@86:


cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86: cannam@86:

copyright © 2000-2010 Xiph.Org

Ogg Vorbis

Vorbisfile documentation

vorbisfile version 1.3.2 - 20101101

cannam@86: cannam@86: cannam@86: cannam@86: