oggdecoder.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // oggdecoder.c
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <vorbis/vorbisfile.h>
  7. static int quiet = 0;
  8. static int bits = 16;
  9. #if __APPLE__ && __BIG_ENDIAN__
  10. static int endian = 1;
  11. #else
  12. static int endian = 0;
  13. #endif
  14. static int raw = 0;
  15. static int sign = 1;
  16. typedef struct oggio oggio;
  17. struct oggio
  18. {
  19. OggVorbis_File vf;
  20. ov_callbacks cb;
  21. };
  22. /*
  23. void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB,ogg_int64_t off)
  24. {
  25. };
  26. */
  27. void *Decode_Ogg(void *stream,void *oread,void *oseek,void *oclose,void *otell,int *samples,int *channels,int *freq)
  28. {
  29. oggio *ogg;
  30. int res;
  31. ogg_int64_t samples64;
  32. *samples=-1;
  33. ogg=(oggio*)malloc(sizeof(oggio));
  34. ogg->cb.read_func=oread;
  35. ogg->cb.seek_func=oseek;
  36. ogg->cb.close_func=oclose;
  37. ogg->cb.tell_func=otell;
  38. res=ov_open_callbacks(stream,&ogg->vf,0,0,ogg->cb);
  39. if (res<0) {free(ogg);return 0;}
  40. samples64=ov_pcm_total(&ogg->vf,0);
  41. *samples=(int)samples64;
  42. *channels=ov_info(&ogg->vf,-1)->channels;
  43. *freq=ov_info(&ogg->vf,-1)->rate;
  44. return ogg;
  45. }
  46. int Read_Ogg(oggio *ogg,char *buf,int bytes) // null buffer to close
  47. {
  48. int res,bs;
  49. if (buf==0) return ov_clear(&ogg->vf);
  50. while (bytes>0)
  51. {
  52. res=ov_read(&ogg->vf,buf,bytes,endian,bits/8,sign,&bs);
  53. if (res<0)
  54. {
  55. if (bs) return -1; // Only one logical bitstream currently supported
  56. return -2; // Warning: hole in data
  57. }
  58. buf+=res;
  59. bytes-=res;
  60. }
  61. return 0;
  62. }