1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
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 $
16 ********************************************************************/
20 #include "vorbis/codec.h"
21 #include "vorbis/vorbisfile.h"
23 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
28 void _verify(OggVorbis_File *ov,ogg_int64_t pos,
29 ogg_int64_t val,ogg_int64_t pcmval,
30 ogg_int64_t pcmlength,
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));
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));
49 if(pos<0 || pos>pcmlength){
50 printf("pcm position out of bounds: got %ld\n",(long)pos);
53 bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
55 if(buffer[j]!=bigassbuffer[j+pos*2]){
56 printf("data position after seek doesn't match pcm position\n");
59 FILE *f=fopen("a.m","w");
60 for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)buffer[j]);
63 for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)bigassbuffer[j+pos*2]);
75 ogg_int64_t pcmlength;
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 );
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");
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);
99 printf("Sorry; right now seeking_test can only use Vorbis files\n"
100 "that are entirely stereo.\n\n");
105 /* because we want to do sample-level verification that the seek
106 does what it claimed, decode the entire file into memory */
108 pcmlength=ov_pcm_total(&ov,-1);
109 bigassbuffer=malloc(pcmlength*2); /* w00t */
111 while(i<pcmlength*2){
112 int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
119 fprintf(stderr,"\rloading.... [%ld left] ",
120 (long)(pcmlength*2-i));
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
127 ogg_int64_t length=ov.end;
128 printf("\rtesting raw seeking to random places in %ld bytes....\n",
132 ogg_int64_t val=(double)rand()/RAND_MAX*length;
134 printf("\r\t%d [raw position %ld]... ",i,(long)val);
136 ret=ov_raw_seek(&ov,val);
138 printf("seek failed: %d\n",ret);
142 _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
149 printf("testing pcm page seeking to random places in %ld samples....\n",
153 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
155 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
157 ret=ov_pcm_seek_page(&ov,val);
159 printf("seek failed: %d\n",ret);
163 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
170 ogg_int64_t length=ov.end;
171 printf("testing pcm exact seeking to random places in %ld samples....\n",
175 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
177 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
179 ret=ov_pcm_seek(&ov,val);
181 printf("seek failed: %d\n",ret);
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));
190 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
195 printf("\r \nOK.\n\n");
199 printf("Standard input was not seekable.\n");