vorbisfile_example.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ********************************************************************/
  14. /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
  15. stdout using vorbisfile. Using vorbisfile is much simpler than
  16. dealing with libvorbis. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <vorbis/codec.h>
  21. #include <vorbis/vorbisfile.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_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 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),0,2,1,&current_section);
  57. if (ret == 0) {
  58. /* EOF */
  59. eof=1;
  60. } else if (ret < 0) {
  61. if(ret==OV_EBADLINK){
  62. fprintf(stderr,"Corrupt bitstream section! Exiting.\n");
  63. exit(1);
  64. }
  65. /* some other error in the stream. Not a problem, just reporting it in
  66. case we (the app) cares. In this case, we don't. */
  67. } else {
  68. /* we don't bother dealing with sample rate changes, etc, but
  69. you'll have to*/
  70. fwrite(pcmout,1,ret,stdout);
  71. }
  72. }
  73. /* cleanup */
  74. ov_clear(&vf);
  75. fprintf(stderr,"Done.\n");
  76. return(0);
  77. }