annotate src/libvorbis-1.3.3/doc/vorbisfile/seekingexample.html @ 1:05aa0afa9217

Bring in flac, ogg, vorbis
author Chris Cannam
date Tue, 19 Mar 2013 17:37:49 +0000
parents
children
rev   line source
Chris@1 1 <html>
Chris@1 2
Chris@1 3 <head>
Chris@1 4 <title>vorbisfile - Example Code</title>
Chris@1 5 <link rel=stylesheet href="style.css" type="text/css">
Chris@1 6 </head>
Chris@1 7
Chris@1 8 <body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
Chris@1 9 <table border=0 width=100%>
Chris@1 10 <tr>
Chris@1 11 <td><p class=tiny>vorbisfile documentation</p></td>
Chris@1 12 <td align=right><p class=tiny>vorbisfile version 1.25 - 20000615</p></td>
Chris@1 13 </tr>
Chris@1 14 </table>
Chris@1 15
Chris@1 16 <h1>Example Code</h1>
Chris@1 17
Chris@1 18 <p>
Chris@1 19 The following is a run-through of the decoding example program supplied
Chris@1 20 with vorbisfile - <a href="vorbisfile_example_c.html">vorbisfile_example.c</a>.
Chris@1 21 This program takes a vorbis bitstream from stdin and writes raw pcm to stdout.
Chris@1 22
Chris@1 23 <p>
Chris@1 24 First, relevant headers, including vorbis-specific "codec.h" and "vorbisfile.h" have to be included.
Chris@1 25
Chris@1 26 <br><br>
Chris@1 27 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 28 <tr bgcolor=#cccccc>
Chris@1 29 <td>
Chris@1 30 <pre><b>
Chris@1 31 #include &lt;stdio.h&gt;
Chris@1 32 #include &lt;stdlib.h&gt;
Chris@1 33 #include &lt;math.h&gt;
Chris@1 34 #include "vorbis/codec.h"
Chris@1 35 #include "vorbis/vorbisfile.h"
Chris@1 36 </b></pre>
Chris@1 37 </td>
Chris@1 38 </tr>
Chris@1 39 </table>
Chris@1 40 <p>
Chris@1 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.
Chris@1 42 <br><br>
Chris@1 43 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 44 <tr bgcolor=#cccccc>
Chris@1 45 <td>
Chris@1 46 <pre><b>
Chris@1 47 #ifdef _WIN32
Chris@1 48 #include &lt;io.h&gt;
Chris@1 49 #include &lt;fcntl.h&gt;
Chris@1 50 #endif
Chris@1 51 </b></pre>
Chris@1 52 </td>
Chris@1 53 </tr>
Chris@1 54 </table>
Chris@1 55 <p>
Chris@1 56 Next, a buffer for the pcm audio output is declared.
Chris@1 57
Chris@1 58 <br><br>
Chris@1 59 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 60 <tr bgcolor=#cccccc>
Chris@1 61 <td>
Chris@1 62 <pre><b>
Chris@1 63 char pcmout[4096];
Chris@1 64 </b></pre>
Chris@1 65 </td>
Chris@1 66 </tr>
Chris@1 67 </table>
Chris@1 68
Chris@1 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.
Chris@1 70 Also, we make our final concession to Windows users by setting the stdin and stdout to binary mode.
Chris@1 71 <br><br>
Chris@1 72 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 73 <tr bgcolor=#cccccc>
Chris@1 74 <td>
Chris@1 75 <pre><b>
Chris@1 76 int main(int argc, char **argv){
Chris@1 77 OggVorbis_File vf;
Chris@1 78 int eof=0;
Chris@1 79 int current_section;
Chris@1 80
Chris@1 81 #ifdef _WIN32
Chris@1 82 _setmode( _fileno( stdin ), _O_BINARY );
Chris@1 83 #endif
Chris@1 84 </b></pre>
Chris@1 85 </td>
Chris@1 86 </tr>
Chris@1 87 </table>
Chris@1 88
Chris@1 89 <p><a href="ov_open_callbacks.html">ov_open_callbacks()</a> must be
Chris@1 90 called to initialize the <b>OggVorbis_File</b> structure with default values.
Chris@1 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.
Chris@1 92
Chris@1 93 <br><br>
Chris@1 94 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 95 <tr bgcolor=#cccccc>
Chris@1 96 <td>
Chris@1 97 <pre><b>
Chris@1 98 if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
Chris@1 99 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
Chris@1 100 exit(1);
Chris@1 101 }
Chris@1 102
Chris@1 103 </b></pre>
Chris@1 104 </td>
Chris@1 105 </tr>
Chris@1 106 </table>
Chris@1 107
Chris@1 108 <p>
Chris@1 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.
Chris@1 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>.
Chris@1 111
Chris@1 112 <br><br>
Chris@1 113 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 114 <tr bgcolor=#cccccc>
Chris@1 115 <td>
Chris@1 116 <pre><b>
Chris@1 117 {
Chris@1 118 char **ptr=ov_comment(&vf,-1)->user_comments;
Chris@1 119 vorbis_info *vi=ov_info(&vf,-1);
Chris@1 120 while(*ptr){
Chris@1 121 fprintf(stderr,"%s\n",*ptr);
Chris@1 122 ++ptr;
Chris@1 123 }
Chris@1 124 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
Chris@1 125 fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
Chris@1 126 }
Chris@1 127
Chris@1 128 </b></pre>
Chris@1 129 </td>
Chris@1 130 </tr>
Chris@1 131 </table>
Chris@1 132
Chris@1 133 <p>
Chris@1 134 Here's the read loop:
Chris@1 135
Chris@1 136 <br><br>
Chris@1 137 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 138 <tr bgcolor=#cccccc>
Chris@1 139 <td>
Chris@1 140 <pre><b>
Chris@1 141
Chris@1 142 while(!eof){
Chris@1 143 long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
Chris@1 144 switch(ret){
Chris@1 145 case 0:
Chris@1 146 /* EOF */
Chris@1 147 eof=1;
Chris@1 148 break;
Chris@1 149 case -1:
Chris@1 150 break;
Chris@1 151 default:
Chris@1 152 fwrite(pcmout,1,ret,stdout);
Chris@1 153 break;
Chris@1 154 }
Chris@1 155 }
Chris@1 156
Chris@1 157 </b></pre>
Chris@1 158 </td>
Chris@1 159 </tr>
Chris@1 160 </table>
Chris@1 161
Chris@1 162 <p>
Chris@1 163 The code is reading blocks of data using <a href="ov_read.html">ov_read()</a>.
Chris@1 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.
Chris@1 165
Chris@1 166 <p>
Chris@1 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.
Chris@1 168
Chris@1 169 <br><br>
Chris@1 170 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 171 <tr bgcolor=#cccccc>
Chris@1 172 <td>
Chris@1 173 <pre><b>
Chris@1 174
Chris@1 175 ov_clear(&vf);
Chris@1 176
Chris@1 177 fprintf(stderr,"Done.\n");
Chris@1 178 return(0);
Chris@1 179 }
Chris@1 180 </b></pre>
Chris@1 181 </td>
Chris@1 182 </tr>
Chris@1 183 </table>
Chris@1 184
Chris@1 185 <p>
Chris@1 186 The full source for vorbisfile_example.c can be found with the vorbis
Chris@1 187 distribution in <a href="vorbisfile_example_c.html">vorbisfile_example.c</a>.
Chris@1 188
Chris@1 189 <br><br>
Chris@1 190 <hr noshade>
Chris@1 191 <table border=0 width=100%>
Chris@1 192 <tr valign=top>
Chris@1 193 <td><p class=tiny>copyright &copy; 2000 vorbis team</p></td>
Chris@1 194 <td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a></p></td>
Chris@1 195 </tr><tr>
Chris@1 196 <td><p class=tiny>vorbisfile documentation</p></td>
Chris@1 197 <td align=right><p class=tiny>vorbisfile version 1.25 - 20000615</p></td>
Chris@1 198 </tr>
Chris@1 199 </table>
Chris@1 200
Chris@1 201 </body>
Chris@1 202
Chris@1 203 </html>