ivorbisfile_example.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: simple example decoder using vorbisidec
  14. ********************************************************************/
  15. /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
  16. stdout using vorbisfile. Using vorbisfile is much simpler than
  17. dealing with libvorbis. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "ivorbiscodec.h"
  21. #include "ivorbisfile.h"
  22. #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
  23. #include <io.h>
  24. #include <fcntl.h>
  25. #endif
  26. char pcmout[4096]; /* take 4k out of the data segment, not the stack */
  27. int main(){
  28. OggVorbis_File vf;
  29. int eof=0;
  30. int current_section;
  31. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  32. /* Beware the evil ifdef. We avoid these where we can, but this one we
  33. cannot. Don't add any more, you'll probably go to hell if you do. */
  34. _setmode( _fileno( stdin ), _O_BINARY );
  35. _setmode( _fileno( stdout ), _O_BINARY );
  36. #endif
  37. if(ov_open(stdin, &vf, NULL, 0) < 0) {
  38. fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
  39. exit(1);
  40. }
  41. /* Throw the comments plus a few lines about the bitstream we're
  42. decoding */
  43. {
  44. char **ptr=ov_comment(&vf,-1)->user_comments;
  45. vorbis_info *vi=ov_info(&vf,-1);
  46. while(*ptr){
  47. fprintf(stderr,"%s\n",*ptr);
  48. ++ptr;
  49. }
  50. fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
  51. fprintf(stderr,"\nDecoded length: %ld samples\n",
  52. (long)ov_pcm_total(&vf,-1));
  53. fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
  54. }
  55. while(!eof){
  56. long ret=ov_read(&vf,pcmout,sizeof(pcmout),&current_section);
  57. if (ret == 0) {
  58. /* EOF */
  59. eof=1;
  60. } else if (ret < 0) {
  61. /* error in the stream. Not a problem, just reporting it in
  62. case we (the app) cares. In this case, we don't. */
  63. } else {
  64. /* we don't bother dealing with sample rate changes, etc, but
  65. you'll have to*/
  66. fwrite(pcmout,1,ret,stdout);
  67. }
  68. }
  69. /* cleanup */
  70. ov_clear(&vf);
  71. fprintf(stderr,"Done.\n");
  72. return(0);
  73. }