annotate src/libvorbis-1.3.3/doc/vorbisfile/example.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.3.2 - 20101101</p></td>
Chris@1 13 </tr>
Chris@1 14 </table>
Chris@1 15
Chris@1 16 <h1>Decoding 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 libvorbisfile, <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 "vorbis/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 "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 _setmode( _fileno( stdout ), _O_BINARY );
Chris@1 84 #endif
Chris@1 85 </b></pre>
Chris@1 86 </td>
Chris@1 87 </tr>
Chris@1 88 </table>
Chris@1 89
Chris@1 90 <p>We call <a href="ov_open_callbacks.html">ov_open_callbacks()</a> to
Chris@1 91 initialize the <b>OggVorbis_File</b> structure with default values.
Chris@1 92 <a href="ov_open_callbacks.html">ov_open_callbacks()</a> also checks
Chris@1 93 to ensure that we're reading Vorbis format and not something else. The
Chris@1 94 OV_CALLBACKS_NOCLOSE callbacks instruct libvorbisfile not to close
Chris@1 95 stdin later during cleanup.
Chris@1 96
Chris@1 97 <br><br>
Chris@1 98 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 99 <tr bgcolor=#cccccc>
Chris@1 100 <td>
Chris@1 101 <pre><b>
Chris@1 102 if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
Chris@1 103 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
Chris@1 104 exit(1);
Chris@1 105 }
Chris@1 106
Chris@1 107 </b></pre>
Chris@1 108 </td>
Chris@1 109 </tr>
Chris@1 110 </table>
Chris@1 111
Chris@1 112 <p>
Chris@1 113 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 114 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 115
Chris@1 116 <br><br>
Chris@1 117 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 118 <tr bgcolor=#cccccc>
Chris@1 119 <td>
Chris@1 120 <pre><b>
Chris@1 121 {
Chris@1 122 char **ptr=ov_comment(&vf,-1)->user_comments;
Chris@1 123 vorbis_info *vi=ov_info(&vf,-1);
Chris@1 124 while(*ptr){
Chris@1 125 fprintf(stderr,"%s\n",*ptr);
Chris@1 126 ++ptr;
Chris@1 127 }
Chris@1 128 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
Chris@1 129 fprintf(stderr,"\nDecoded length: %ld samples\n",
Chris@1 130 (long)ov_pcm_total(&vf,-1));
Chris@1 131 fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
Chris@1 132 }
Chris@1 133
Chris@1 134 </b></pre>
Chris@1 135 </td>
Chris@1 136 </tr>
Chris@1 137 </table>
Chris@1 138
Chris@1 139 <p>
Chris@1 140 Here's the read loop:
Chris@1 141
Chris@1 142 <br><br>
Chris@1 143 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 144 <tr bgcolor=#cccccc>
Chris@1 145 <td>
Chris@1 146 <pre><b>
Chris@1 147
Chris@1 148 while(!eof){
Chris@1 149 long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,&current_section);
Chris@1 150 if (ret == 0) {
Chris@1 151 /* EOF */
Chris@1 152 eof=1;
Chris@1 153 } else if (ret < 0) {
Chris@1 154 /* error in the stream. Not a problem, just reporting it in
Chris@1 155 case we (the app) cares. In this case, we don't. */
Chris@1 156 } else {
Chris@1 157 /* we don't bother dealing with sample rate changes, etc, but
Chris@1 158 you'll have to*/
Chris@1 159 fwrite(pcmout,1,ret,stdout);
Chris@1 160 }
Chris@1 161 }
Chris@1 162
Chris@1 163
Chris@1 164 </b></pre>
Chris@1 165 </td>
Chris@1 166 </tr>
Chris@1 167 </table>
Chris@1 168
Chris@1 169 <p>
Chris@1 170 The code is reading blocks of data using <a href="ov_read.html">ov_read()</a>.
Chris@1 171 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 172
Chris@1 173 <p>
Chris@1 174 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 175
Chris@1 176 <br><br>
Chris@1 177 <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
Chris@1 178 <tr bgcolor=#cccccc>
Chris@1 179 <td>
Chris@1 180 <pre><b>
Chris@1 181
Chris@1 182 ov_clear(&vf);
Chris@1 183
Chris@1 184 fprintf(stderr,"Done.\n");
Chris@1 185 return(0);
Chris@1 186 }
Chris@1 187 </b></pre>
Chris@1 188 </td>
Chris@1 189 </tr>
Chris@1 190 </table>
Chris@1 191
Chris@1 192 <p>
Chris@1 193
Chris@1 194 <br><br>
Chris@1 195 <hr noshade>
Chris@1 196 <table border=0 width=100%>
Chris@1 197 <tr valign=top>
Chris@1 198 <td><p class=tiny>copyright &copy; 2000-2010 Xiph.Org</p></td>
Chris@1 199 <td align=right><p class=tiny><a href="http://www.xiph.org/ogg/vorbis/">Ogg Vorbis</a></p></td>
Chris@1 200 </tr><tr>
Chris@1 201 <td><p class=tiny>Vorbisfile documentation</p></td>
Chris@1 202 <td align=right><p class=tiny>vorbisfile version 1.3.2 - 20101101</p></td>
Chris@1 203 </tr>
Chris@1 204 </table>
Chris@1 205
Chris@1 206 </body>
Chris@1 207
Chris@1 208 </html>