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 SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: simple example decoder using vorbisfile
  14. last mod: $Id: vorbisfile_example.c,v 1.2.2.2 2000/11/04 06:21:39 xiphmont Exp $
  15. ********************************************************************/
  16. /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
  17. stdout using vorbisfile. Using vorbisfile is much simpler than
  18. dealing with libvorbis. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <vorbis/codec.h>
  23. #include <vorbis/vorbisfile.h>
  24. #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #endif
  28. char pcmout[4096]; /* take 4k out of the data segment, not the stack */
  29. int main(int argc, char **argv){
  30. OggVorbis_File vf;
  31. int eof=0;
  32. int current_section;
  33. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  34. /* Beware the evil ifdef. We avoid these where we can, but this one we
  35. cannot. Don't add any more, you'll probably go to hell if you do. */
  36. _setmode( _fileno( stdin ), _O_BINARY );
  37. _setmode( _fileno( stdout ), _O_BINARY );
  38. #endif
  39. if(ov_open(stdin, &vf, NULL, 0) < 0) {
  40. fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
  41. exit(1);
  42. }
  43. /* Throw the comments plus a few lines about the bitstream we're
  44. decoding */
  45. {
  46. char **ptr=ov_comment(&vf,-1)->user_comments;
  47. vorbis_info *vi=ov_info(&vf,-1);
  48. while(*ptr){
  49. fprintf(stderr,"%s\n",*ptr);
  50. ++ptr;
  51. }
  52. fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
  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. /* 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. }