home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Ogg Vorbis / vorbis_sdk / examples / vorbis / seeking_example.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-10  |  5.8 KB  |  218 lines

  1. /********************************************************************
  2.  *                                                                  *
  3.  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
  4.  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
  5.  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6.  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  7.  *                                                                  *
  8.  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
  9.  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
  10.  *                                                                  *
  11.  ********************************************************************
  12.  
  13.  function: illustrate seeking, and test it too
  14.  last mod: $Id: seeking_example.c,v 1.15 2002/07/11 06:40:47 xiphmont Exp $
  15.  
  16.  ********************************************************************/
  17.  
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include "vorbis/codec.h"
  21. #include "vorbis/vorbisfile.h"
  22.  
  23. #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
  24. # include <io.h>
  25. # include <fcntl.h>
  26. #endif
  27.  
  28. void _verify(OggVorbis_File *ov,ogg_int64_t pos,
  29.          ogg_int64_t val,ogg_int64_t pcmval,
  30.          ogg_int64_t pcmlength,
  31.          char *bigassbuffer){
  32.   int j;
  33.   long bread;
  34.   char buffer[4096];
  35.   int dummy;
  36.  
  37.   /* verify the raw position, the pcm position and position decode */
  38.   if(val!=-1 && ov_raw_tell(ov)<val){
  39.     printf("raw position out of tolerance: requested %ld, got %ld\n",
  40.        (long)val,(long)ov_raw_tell(ov));
  41.     exit(1);
  42.   }
  43.   if(pcmval!=-1 && ov_pcm_tell(ov)>pcmval){
  44.     printf("pcm position out of tolerance: requested %ld, got %ld\n",
  45.        (long)pcmval,(long)ov_pcm_tell(ov));
  46.     exit(1);
  47.   }
  48.   pos=ov_pcm_tell(ov);
  49.   if(pos<0 || pos>pcmlength){
  50.     printf("pcm position out of bounds: got %ld\n",(long)pos);
  51.     exit(1);
  52.   }
  53.   bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
  54.   for(j=0;j<bread;j++){
  55.     if(buffer[j]!=bigassbuffer[j+pos*2]){
  56.       printf("data position after seek doesn't match pcm position\n");
  57.  
  58.       {
  59.     FILE *f=fopen("a.m","w");
  60.     for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)buffer[j]);
  61.     fclose(f);
  62.     f=fopen("b.m","w");
  63.     for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)bigassbuffer[j+pos*2]);
  64.     fclose(f);
  65.       }
  66.  
  67.       exit(1);
  68.     }
  69.   }
  70. }
  71.  
  72. int main(){
  73.   OggVorbis_File ov;
  74.   int i,ret;
  75.   ogg_int64_t pcmlength;
  76.   char *bigassbuffer;
  77.   int dummy;
  78.  
  79. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  80.   _setmode( _fileno( stdin ), _O_BINARY );
  81.   _setmode( _fileno( stdout ), _O_BINARY );
  82. #endif
  83.  
  84.  
  85.   /* open the file/pipe on stdin */
  86.   if(ov_open(stdin,&ov,NULL,-1)<0){
  87.     printf("Could not open input as an OggVorbis file.\n\n");
  88.     exit(1);
  89.   }
  90.  
  91.   if(ov_seekable(&ov)){
  92.  
  93.     /* to simplify our own lives, we want to assume the whole file is
  94.        stereo.  Verify this to avoid potentially mystifying users
  95.        (pissing them off is OK, just don't confuse them) */
  96.     for(i=0;i<ov.links;i++){
  97.       vorbis_info *vi=ov_info(&ov,i);
  98.       if(vi->channels!=2){
  99.     printf("Sorry; right now seeking_test can only use Vorbis files\n"
  100.            "that are entirely stereo.\n\n");
  101.     exit(1);
  102.       }
  103.     }
  104.     
  105.     /* because we want to do sample-level verification that the seek
  106.        does what it claimed, decode the entire file into memory */
  107.     fflush(stdout);
  108.     pcmlength=ov_pcm_total(&ov,-1);
  109.     bigassbuffer=malloc(pcmlength*2); /* w00t */
  110.     i=0;
  111.     while(i<pcmlength*2){
  112.       int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
  113.       if(ret<0)continue;
  114.       if(ret){
  115.     i+=ret;
  116.       }else{
  117.     pcmlength=i/2;
  118.       }
  119.       fprintf(stderr,"\rloading.... [%ld left]              ",
  120.           (long)(pcmlength*2-i));
  121.     }
  122.     
  123.     /* Exercise all the real seeking cases; ov_raw_seek,
  124.        ov_pcm_seek_page and ov_pcm_seek.  time seek is just a wrapper
  125.        on pcm_seek */
  126.     {
  127.       ogg_int64_t length=ov.end;
  128.       printf("\rtesting raw seeking to random places in %ld bytes....\n",
  129.          (long)length);
  130.     
  131.       for(i=0;i<1000;i++){
  132.     ogg_int64_t val=(double)rand()/RAND_MAX*length;
  133.     ogg_int64_t pos;
  134.     printf("\r\t%d [raw position %ld]...     ",i,(long)val);
  135.     fflush(stdout);
  136.     ret=ov_raw_seek(&ov,val);
  137.     if(ret<0){
  138.       printf("seek failed: %d\n",ret);
  139.       exit(1);
  140.     }
  141.  
  142.     _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
  143.  
  144.       }
  145.     }
  146.  
  147.     printf("\r");
  148.     {
  149.       printf("testing pcm page seeking to random places in %ld samples....\n",
  150.          (long)pcmlength);
  151.     
  152.       for(i=0;i<1000;i++){
  153.     ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
  154.     ogg_int64_t pos;
  155.     printf("\r\t%d [pcm position %ld]...     ",i,(long)val);
  156.     fflush(stdout);
  157.     ret=ov_pcm_seek_page(&ov,val);
  158.     if(ret<0){
  159.       printf("seek failed: %d\n",ret);
  160.       exit(1);
  161.     }
  162.  
  163.     _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
  164.  
  165.       }
  166.     }
  167.     
  168.     printf("\r");
  169.     {
  170.       ogg_int64_t length=ov.end;
  171.       printf("testing pcm exact seeking to random places in %ld samples....\n",
  172.          (long)pcmlength);
  173.     
  174.       for(i=0;i<1000;i++){
  175.     ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
  176.     ogg_int64_t pos;
  177.     printf("\r\t%d [pcm position %ld]...     ",i,(long)val);
  178.     fflush(stdout);
  179.     ret=ov_pcm_seek(&ov,val);
  180.     if(ret<0){
  181.       printf("seek failed: %d\n",ret);
  182.       exit(1);
  183.     }
  184.     if(ov_pcm_tell(&ov)!=val){
  185.       printf("Declared position didn't perfectly match request: %ld != %ld\n",
  186.          (long)val,(long)ov_pcm_tell(&ov));
  187.       exit(1);
  188.     }
  189.  
  190.     _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
  191.  
  192.       }
  193.     }
  194.     
  195.     printf("\r                                           \nOK.\n\n");
  196.  
  197.  
  198.   }else{
  199.     printf("Standard input was not seekable.\n");
  200.   }
  201.  
  202.   ov_clear(&ov);
  203.   return 0;
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.