chainingexample.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <html>
  2. <head>
  3. <title>vorbisfile - Example Code</title>
  4. <link rel=stylesheet href="style.css" type="text/css">
  5. </head>
  6. <body bgcolor=white text=black link="#5555ff" alink="#5555ff" vlink="#5555ff">
  7. <table border=0 width=100%>
  8. <tr>
  9. <td><p class=tiny>Vorbisfile documentation</p></td>
  10. <td align=right><p class=tiny>vorbisfile version 1.2.0 - 20070723</p></td>
  11. </tr>
  12. </table>
  13. <h1>Chaining Example Code</h1>
  14. <p>
  15. The following is a run-through of the chaining example program supplied
  16. with vorbisfile - <a href="chaining_example_c.html">chaining_example.c</a>.
  17. This program demonstrates how to work with a chained bitstream.
  18. <p>
  19. First, relevant headers, including vorbis-specific "codec.h" and "vorbisfile.h" have to be included.
  20. <br><br>
  21. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  22. <tr bgcolor=#cccccc>
  23. <td>
  24. <pre><b>
  25. #include "vorbis/codec.h"
  26. #include "vorbis/vorbisfile.h"
  27. #include "../lib/misc.h"
  28. </b></pre>
  29. </td>
  30. </tr>
  31. </table>
  32. <p>Inside main(), we declare our primary OggVorbis_File structure. We also declare a other helpful variables to track our progress within the file.
  33. <br><br>
  34. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  35. <tr bgcolor=#cccccc>
  36. <td>
  37. <pre><b>
  38. int main(){
  39. OggVorbis_File ov;
  40. int i;
  41. </b></pre>
  42. </td>
  43. </tr>
  44. </table>
  45. <p>This example takes its input on stdin which is in 'text' mode by default under Windows; this will corrupt the input data unless set to binary mode. This applies only to Windows.
  46. <br><br>
  47. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  48. <tr bgcolor=#cccccc>
  49. <td>
  50. <pre><b>
  51. #ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
  52. _setmode( _fileno( stdin ), _O_BINARY );
  53. #endif
  54. </b></pre>
  55. </td>
  56. </tr>
  57. </table>
  58. <p>We call <a href="ov_open_callbacks.html">ov_open_callbacks()</a> to
  59. initialize the <a href="OggVorbis_File.html">OggVorbis_File</a>
  60. structure. <a href="ov_open_callbacks.html">ov_open_callbacks()</a>
  61. also checks to ensure that we're reading Vorbis format and not
  62. something else. The OV_CALLBACKS_NOCLOSE callbacks instruct
  63. libvorbisfile not to close stdin later during cleanup.<p>
  64. <br><br>
  65. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  66. <tr bgcolor=#cccccc>
  67. <td>
  68. <pre><b>
  69. if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){
  70. printf("Could not open input as an OggVorbis file.\n\n");
  71. exit(1);
  72. }
  73. </b></pre>
  74. </td>
  75. </tr>
  76. </table>
  77. <p>
  78. First we check to make sure the stream is seekable using <a href="ov_seekable.html">ov_seekable</a>.
  79. <p>Then we're going to find the number of logical bitstreams in the physical bitstream using <a href="ov_streams.html">ov_streams</a>.
  80. <p>We use <a href="ov_time_total.html">ov_time_total</a> to determine the total length of the physical bitstream. We specify that we want the entire bitstream by using the argument <tt>-1</tt>.
  81. <br><br>
  82. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  83. <tr bgcolor=#cccccc>
  84. <td>
  85. <pre><b>
  86. if(ov_seekable(&amp;ov)){
  87. printf("Input bitstream contained %ld logical bitstream section(s).\n",
  88. ov_streams(&amp;ov));
  89. printf("Total bitstream playing time: %ld seconds\n\n",
  90. (long)ov_time_total(&amp;ov,-1));
  91. }else{
  92. printf("Standard input was not seekable.\n"
  93. "First logical bitstream information:\n\n");
  94. }
  95. </b></pre>
  96. </td>
  97. </tr>
  98. </table>
  99. <p>Now we're going to iterate through each logical bitstream and print information about that bitstream.
  100. <p>We use <a href="ov_info.html">ov_info</a> to pull out the <a href="vorbis_info.html">vorbis_info</a> struct for each logical bitstream. This struct contains bitstream-specific info.
  101. <p><a href="ov_serialnumber.html">ov_serialnumber</a> retrieves the unique serial number for the logical bistream. <a href="ov_raw_total.html">ov_raw_total</a> gives the total compressed bytes for the logical bitstream, and <a href="ov_time_total.html">ov_time_total</a> gives the total time in the logical bitstream.
  102. <br><br>
  103. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  104. <tr bgcolor=#cccccc>
  105. <td>
  106. <pre><b>
  107. for(i=0;i&lt;ov_streams(&amp;ov);i++){
  108. vorbis_info *vi=ov_info(&amp;ov,i);
  109. printf("\tlogical bitstream section %d information:\n",i+1);
  110. printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
  111. vi-&gt;rate,vi-&gt;channels,ov_bitrate(&amp;ov,i)/1000,
  112. ov_serialnumber(&amp;ov,i));
  113. printf("\t\tcompressed length: %ld bytes ",(long)(ov_raw_total(&amp;ov,i)));
  114. printf(" play time: %lds\n",(long)ov_time_total(&amp;ov,i));
  115. }
  116. </b></pre>
  117. </td>
  118. </tr>
  119. </table>
  120. <p>
  121. When we're done with the entire physical bitstream, we need to call <a href="ov_clear.html">ov_clear()</a> to release the bitstream.
  122. <br><br>
  123. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  124. <tr bgcolor=#cccccc>
  125. <td>
  126. <pre><b>
  127. ov_clear(&amp;ov);
  128. return 0;
  129. }
  130. </b></pre>
  131. </td>
  132. </tr>
  133. </table>
  134. <p>
  135. The full source for chaining_example.c can be found with the vorbis
  136. distribution in <a href="chaining_example_c.html">chaining_example.c</a>.
  137. <br><br>
  138. <hr noshade>
  139. <table border=0 width=100%>
  140. <tr valign=top>
  141. <td><p class=tiny>copyright &copy; 2007 Xiph.org</p></td>
  142. <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>
  143. </tr><tr>
  144. <td><p class=tiny>Vorbisfile documentation</p></td>
  145. <td align=right><p class=tiny>vorbisfile version 1.2.0 - 20070723</p></td>
  146. </tr>
  147. </table>
  148. </body>
  149. </html>