analysis.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 https://xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: single-block PCM analysis mode dispatch
  13. ********************************************************************/
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <math.h>
  17. #include <ogg/ogg.h>
  18. #include "vorbis/codec.h"
  19. #include "codec_internal.h"
  20. #include "registry.h"
  21. #include "scales.h"
  22. #include "os.h"
  23. #include "misc.h"
  24. /* decides between modes, dispatches to the appropriate mapping. */
  25. int vorbis_analysis(vorbis_block *vb, ogg_packet *op){
  26. int ret,i;
  27. vorbis_block_internal *vbi=vb->internal;
  28. vb->glue_bits=0;
  29. vb->time_bits=0;
  30. vb->floor_bits=0;
  31. vb->res_bits=0;
  32. /* first things first. Make sure encode is ready */
  33. for(i=0;i<PACKETBLOBS;i++)
  34. oggpack_reset(vbi->packetblob[i]);
  35. /* we only have one mapping type (0), and we let the mapping code
  36. itself figure out what soft mode to use. This allows easier
  37. bitrate management */
  38. if((ret=_mapping_P[0]->forward(vb)))
  39. return(ret);
  40. if(op){
  41. if(vorbis_bitrate_managed(vb))
  42. /* The app is using a bitmanaged mode... but not using the
  43. bitrate management interface. */
  44. return(OV_EINVAL);
  45. op->packet=oggpack_get_buffer(&vb->opb);
  46. op->bytes=oggpack_bytes(&vb->opb);
  47. op->b_o_s=0;
  48. op->e_o_s=vb->eofflag;
  49. op->granulepos=vb->granulepos;
  50. op->packetno=vb->sequence; /* for sake of completeness */
  51. }
  52. return(0);
  53. }
  54. #ifdef ANALYSIS
  55. int analysis_noisy=1;
  56. /* there was no great place to put this.... */
  57. void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB,ogg_int64_t off){
  58. int j;
  59. FILE *of;
  60. char buffer[80];
  61. sprintf(buffer,"%s_%d.m",base,i);
  62. of=fopen(buffer,"w");
  63. if(!of)perror("failed to open data dump file");
  64. for(j=0;j<n;j++){
  65. if(bark){
  66. float b=toBARK((4000.f*j/n)+.25);
  67. fprintf(of,"%f ",b);
  68. }else
  69. if(off!=0)
  70. fprintf(of,"%f ",(double)(j+off)/8000.);
  71. else
  72. fprintf(of,"%f ",(double)j);
  73. if(dB){
  74. float val;
  75. if(v[j]==0.)
  76. val=-140.;
  77. else
  78. val=todB(v+j);
  79. fprintf(of,"%f\n",val);
  80. }else{
  81. fprintf(of,"%f\n",v[j]);
  82. }
  83. }
  84. fclose(of);
  85. }
  86. void _analysis_output(char *base,int i,float *v,int n,int bark,int dB,
  87. ogg_int64_t off){
  88. if(analysis_noisy)_analysis_output_always(base,i,v,n,bark,dB,off);
  89. }
  90. #endif