vorbisfile_example.c 2.9 KB

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