annotate src/libvorbis-1.3.3/doc/vorbisfile/seekingexample.html @ 86:98c1576536ae

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