]> git.jsancho.org Git - lugaru.git/blob - Dependencies/libvorbis/doc/vorbisfile/seekingexample.html
CMake: Purge all the bundled dependencies
[lugaru.git] / Dependencies / libvorbis / doc / vorbisfile / seekingexample.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 decoding example program supplied
20 with vorbisfile - <a href="vorbisfile_example_c.html">vorbisfile_example.c</a>.  
21 This program takes a vorbis bitstream from stdin and writes raw pcm to stdout.
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;stdio.h&gt;
32 #include &lt;stdlib.h&gt;
33 #include &lt;math.h&gt;
34 #include "vorbis/codec.h"
35 #include "vorbis/vorbisfile.h"
36 </b></pre>
37         </td>
38 </tr>
39 </table>
40 <p>
41 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.
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 #ifdef _WIN32
48 #include &lt;io.h&gt;
49 #include &lt;fcntl.h&gt;
50 #endif
51 </b></pre>
52         </td>
53 </tr>
54 </table>
55 <p>
56 Next, a buffer for the pcm audio output is declared.
57
58 <br><br>
59 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
60 <tr bgcolor=#cccccc>
61         <td>
62 <pre><b>
63 char pcmout[4096];
64 </b></pre>
65         </td>
66 </tr>
67 </table>
68
69 <p>Inside main(), we declare our primary OggVorbis_File structure.  We also declare a few other helpful variables to track out progress within the file.
70 Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.
71 <br><br>
72 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
73 <tr bgcolor=#cccccc>
74         <td>
75 <pre><b>
76 int main(int argc, char **argv){
77   OggVorbis_File vf;
78   int eof=0;
79   int current_section;
80
81 #ifdef _WIN32
82   _setmode( _fileno( stdin ), _O_BINARY );
83 #endif
84 </b></pre>
85         </td>
86 </tr>
87 </table>
88
89 <p><a href="ov_open_callbacks.html">ov_open_callbacks()</a> must be
90 called to initialize the <b>OggVorbis_File</b> structure with default values.  
91 <a href="ov_open_callbacks.html">ov_open_callbacks()</a> also checks to ensure that we're reading Vorbis format and not something else.
92
93 <br><br>
94 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
95 <tr bgcolor=#cccccc>
96         <td>
97 <pre><b>
98   if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
99       fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
100       exit(1);
101   }
102
103 </b></pre>
104         </td>
105 </tr>
106 </table>
107
108 <p>
109 We're going to pull the channel and bitrate info from the file using <a href="ov_info.html">ov_info()</a> and show them to the user.
110 We also want to pull out and show the user a comment attached to the file using <a href="ov_comment.html">ov_comment()</a>.
111
112 <br><br>
113 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
114 <tr bgcolor=#cccccc>
115         <td>
116 <pre><b>
117   {
118     char **ptr=ov_comment(&vf,-1)->user_comments;
119     vorbis_info *vi=ov_info(&vf,-1);
120     while(*ptr){
121       fprintf(stderr,"%s\n",*ptr);
122       ++ptr;
123     }
124     fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
125     fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
126   }
127   
128 </b></pre>
129         </td>
130 </tr>
131 </table>
132
133 <p>
134 Here's the read loop:
135
136 <br><br>
137 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
138 <tr bgcolor=#cccccc>
139         <td>
140 <pre><b>
141
142   while(!eof){
143     long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
144     switch(ret){
145     case 0:
146       /* EOF */
147       eof=1;
148       break;
149     case -1:
150       break;
151     default:
152       fwrite(pcmout,1,ret,stdout);
153       break;
154     }
155   }
156   
157 </b></pre>
158         </td>
159 </tr>
160 </table>
161
162 <p>
163 The code is reading blocks of data using <a href="ov_read.html">ov_read()</a>.
164 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.
165
166 <p>
167 Now that we've finished playing, we can pack up and go home.  It's important to call <a href="ov_clear.html">ov_clear()</a> when we're finished.
168
169 <br><br>
170 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
171 <tr bgcolor=#cccccc>
172         <td>
173 <pre><b>
174
175   ov_clear(&vf);
176     
177   fprintf(stderr,"Done.\n");
178   return(0);
179 }
180 </b></pre>
181         </td>
182 </tr>
183 </table>
184
185 <p>
186 The full source for vorbisfile_example.c can be found with the vorbis
187 distribution in <a href="vorbisfile_example_c.html">vorbisfile_example.c</a>.
188
189 <br><br>
190 <hr noshade>
191 <table border=0 width=100%>
192 <tr valign=top>
193 <td><p class=tiny>copyright &copy; 2000 vorbis team</p></td>
194 <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>
195 </tr><tr>
196 <td><p class=tiny>vorbisfile documentation</p></td>
197 <td align=right><p class=tiny>vorbisfile version 1.25 - 20000615</p></td>
198 </tr>
199 </table>
200
201 </body>
202
203 </html>