Merge branch_beta3 onto the mainline.
[platform/upstream/libvorbis.git] / doc / vorbisfile / seekexample.html
1 <html>
2
3 <head>
4 <title>vorbisfile - Example Code</title>
5 <link rel=stylesheet href="style.css" type="text/css">
6 </head>
7
8 <body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
9 <table border=0 width=100%>
10 <tr>
11 <td><p class=tiny>vorbisfile documentation</p></td>
12 <td align=right><p class=tiny>vorbisfile version 1.25 - 20000615</p></td>
13 </tr>
14 </table>
15
16 <h1>Example Code</h1>
17
18 <p>
19 The following is a run-through of the seeking example program supplied
20 with vorbisfile - <a href="seeking_test_c.html">seeking_test.c</a>.  
21 This program tests the vorbisfile <a href="ov_time_seek.html">ov_time_seek</a> function by seeking to random points within the file.
22
23 <p>
24 First, relevant headers, including vorbis-specific "codec.h" and "vorbisfile.h" have to be included.
25
26 <br><br>
27 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
28 <tr bgcolor=#cccccc>
29         <td>
30 <pre><b>
31 #include &lt;stdlib.h>
32 #include &lt;stdio.h>
33 #include "vorbis/codec.h"
34 #include "vorbis/vorbisfile.h"
35 #include "../lib/misc.h"
36 </b></pre>
37         </td>
38 </tr>
39 </table>
40
41 <p>Inside main(), we declare our primary OggVorbis_File structure.  We also declare a other helpful variables to track our progress within the file.
42 <br><br>
43 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
44 <tr bgcolor=#cccccc>
45         <td>
46 <pre><b>
47 int main(){
48   OggVorbis_File ov;
49   int i;
50 </b></pre>
51         </td>
52 </tr>
53 </table>
54
55 <p><a href="ov_open.html">ov_open()</a> must be
56 called to initialize the <a href="OggVorbis_File.html">OggVorbis_File</a> structure with default values.  
57 <a href="ov_open.html">ov_open()</a> also checks to ensure that we're reading Vorbis format and not something else.
58
59 <br><br>
60 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
61 <tr bgcolor=#cccccc>
62         <td>
63 <pre><b>
64   if(ov_open(stdin,&ov,NULL,-1)<0){
65     printf("Could not open input as an OggVorbis file.\n\n");
66     exit(1);
67   }
68
69 </b></pre>
70         </td>
71 </tr>
72 </table>
73
74 <p>
75 First we check to make sure the stream is seekable using <a href="ov_seekable.html">ov_seekable</a>.
76
77 <p>Then we seek to 100 random spots in the bitstream using <a href="ov_time_seek.html">ov_time_seek</a> with randomly generated values.
78
79 <br><br>
80 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
81 <tr bgcolor=#cccccc>
82         <td>
83 <pre><b>
84   
85   /* print details about each logical bitstream in the input */
86   if(ov_seekable(&ov)){
87     double length=ov_time_total(&ov,-1);
88     printf("testing seeking to random places in %g seconds....\n",length);
89     for(i=0;i<100;i++){
90       double val=(double)rand()/RAND_MAX*length;
91       ov_time_seek(&ov,val);
92       printf("\r\t%d [%gs]...     ",i,val);
93       fflush(stdout);
94     }
95     
96     printf("\r                                   \nOK.\n\n");
97   }else{
98     printf("Standard input was not seekable.\n");
99   }
100   
101 </b></pre>
102         </td>
103 </tr>
104 </table>
105 <p>
106 When we're done seeking, we need to call <a href="ov_clear.html">ov_clear()</a> to release the bitstream.
107
108 <br><br>
109 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
110 <tr bgcolor=#cccccc>
111         <td>
112 <pre><b>
113   ov_clear(&ov);
114   return 0;
115 }
116 </b></pre>
117         </td>
118 </tr>
119 </table>
120
121 <p>
122 The full source for seeking_test.c can be found with the vorbis
123 distribution in <a href="seeking_test_c.html">seeking_test.c</a>.
124
125 <br><br>
126 <hr noshade>
127 <table border=0 width=100%>
128 <tr valign=top>
129 <td><p class=tiny>copyright &copy; 2000 vorbis team</p></td>
130 <td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a><br><a href="mailto:team@vorbis.org">team@vorbis.org</a></p></td>
131 </tr><tr>
132 <td><p class=tiny>vorbisfile documentation</p></td>
133 <td align=right><p class=tiny>vorbisfile version 1.25 - 20000615</p></td>
134 </tr>
135 </table>
136
137 </body>
138
139 </html>