seekexample.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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>Example Code: seeking</h1>
  14. <p>
  15. The following is a run-through of the seeking example program supplied
  16. with vorbisfile - <a href="seeking_test_c.html">seeking_test.c</a>.
  17. This program tests the vorbisfile <a href="ov_time_seek.html">ov_time_seek</a> function by seeking to random points within the file.
  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 &lt;stdlib.h>
  26. #include &lt;stdio.h>
  27. #include "vorbis/codec.h"
  28. #include "vorbis/vorbisfile.h"
  29. </b></pre>
  30. </td>
  31. </tr>
  32. </table>
  33. <p>Inside main(), we declare our primary OggVorbis_File structure. We also declare other helpful variables to track our progress within the file.
  34. <br><br>
  35. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  36. <tr bgcolor=#cccccc>
  37. <td>
  38. <pre><b>
  39. int main(){
  40. OggVorbis_File ov;
  41. int i;
  42. </b></pre>
  43. </td>
  44. </tr>
  45. </table>
  46. <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.
  47. <br><br>
  48. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  49. <tr bgcolor=#cccccc>
  50. <td>
  51. <pre><b>
  52. #ifdef _WIN32 /* We need to set stdin to binary mode under Windows */
  53. _setmode( _fileno( stdin ), _O_BINARY );
  54. #endif
  55. </b></pre>
  56. </td>
  57. </tr>
  58. </table>
  59. <p><a href="ov_open_callbacks.html">ov_open()</a> must be
  60. called to initialize the <a href="OggVorbis_File.html">OggVorbis_File</a> structure with default values.
  61. <a href="ov_open_callbacks.html">ov_open_callbacks()</a> also checks to ensure that we're reading Vorbis format and not something else.
  62. <br><br>
  63. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  64. <tr bgcolor=#cccccc>
  65. <td>
  66. <pre><b>
  67. if(ov_open_callbacks(stdin,&ov,NULL,-1, OV_CALLBACKS_NOCLOSE)<0){
  68. printf("Could not open input as an OggVorbis file.\n\n");
  69. exit(1);
  70. }
  71. </b></pre>
  72. </td>
  73. </tr>
  74. </table>
  75. <p>
  76. First we check to make sure the stream is seekable using <a href="ov_seekable.html">ov_seekable</a>.
  77. <p>Then we seek to 100 random spots in the bitstream using <a href="ov_time_seek.html">ov_time_seek</a> with randomly generated values.
  78. <br><br>
  79. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  80. <tr bgcolor=#cccccc>
  81. <td>
  82. <pre><b>
  83. /* print details about each logical bitstream in the input */
  84. if(ov_seekable(&ov)){
  85. double length=ov_time_total(&ov,-1);
  86. printf("testing seeking to random places in %g seconds....\n",length);
  87. for(i=0;i<100;i++){
  88. double val=(double)rand()/RAND_MAX*length;
  89. ov_time_seek(&ov,val);
  90. printf("\r\t%d [%gs]... ",i,val);
  91. fflush(stdout);
  92. }
  93. printf("\r \nOK.\n\n");
  94. }else{
  95. printf("Standard input was not seekable.\n");
  96. }
  97. </b></pre>
  98. </td>
  99. </tr>
  100. </table>
  101. <p>
  102. When we're done seeking, we need to call <a href="ov_clear.html">ov_clear()</a> to release the bitstream.
  103. <br><br>
  104. <table border=0 width=100% color=black cellspacing=0 cellpadding=7>
  105. <tr bgcolor=#cccccc>
  106. <td>
  107. <pre><b>
  108. ov_clear(&ov);
  109. return 0;
  110. }
  111. </b></pre>
  112. </td>
  113. </tr>
  114. </table>
  115. <p>
  116. The full source for seeking_test.c can be found with the vorbis
  117. distribution in <a href="seeking_test_c.html">seeking_test.c</a>.
  118. <br><br>
  119. <hr noshade>
  120. <table border=0 width=100%>
  121. <tr valign=top>
  122. <td><p class=tiny>copyright &copy; 2007 Xiph.org</p></td>
  123. <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>
  124. </tr><tr>
  125. <td><p class=tiny>Vorbisfile documentation</p></td>
  126. <td align=right><p class=tiny>vorbisfile version 1.2.0 - 20070723</p></td>
  127. </tr>
  128. </table>
  129. </body>
  130. </html>