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