analysis.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: single-block PCM analysis mode dispatch
  13. last mod: $Id: analysis.c,v 1.46.4.2 2001/11/22 06:21:07 xiphmont Exp $
  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. int analysis_noisy=1;
  25. /* decides between modes, dispatches to the appropriate mapping. */
  26. int vorbis_analysis(vorbis_block *vb, ogg_packet *op){
  27. vorbis_dsp_state *vd=vb->vd;
  28. backend_lookup_state *b=vd->backend_state;
  29. vorbis_info *vi=vd->vi;
  30. codec_setup_info *ci=vi->codec_setup;
  31. int type,ret;
  32. int mode=0;
  33. vb->glue_bits=0;
  34. vb->time_bits=0;
  35. vb->floor_bits=0;
  36. vb->res_bits=0;
  37. /* first things first. Make sure encode is ready */
  38. oggpack_reset(&vb->opb);
  39. /* Encode the packet type */
  40. oggpack_write(&vb->opb,0,1);
  41. /* currently lazy. Short block dispatches to 0, long to 1. */
  42. if(vb->W &&ci->modes>1)mode=1;
  43. type=ci->map_type[ci->mode_param[mode]->mapping];
  44. vb->mode=mode;
  45. /* Encode frame mode, pre,post windowsize, then dispatch */
  46. oggpack_write(&vb->opb,mode,b->modebits);
  47. if(vb->W){
  48. oggpack_write(&vb->opb,vb->lW,1);
  49. oggpack_write(&vb->opb,vb->nW,1);
  50. /*fprintf(stderr,"*");
  51. }else{
  52. fprintf(stderr,".");*/
  53. }
  54. if((ret=_mapping_P[type]->forward(vb,b->mode[mode])))
  55. return(ret);
  56. if(op){
  57. op->packet=oggpack_get_buffer(&vb->opb);
  58. op->bytes=oggpack_bytes(&vb->opb);
  59. op->b_o_s=0;
  60. op->e_o_s=vb->eofflag;
  61. op->granulepos=vb->granulepos;
  62. op->packetno=vb->sequence; /* for sake of completeness */
  63. }
  64. return(0);
  65. }
  66. /* there was no great place to put this.... */
  67. void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB){
  68. int j;
  69. FILE *of;
  70. char buffer[80];
  71. /* if(i==5870){*/
  72. sprintf(buffer,"%s_%d.m",base,i);
  73. of=fopen(buffer,"w");
  74. if(!of)perror("failed to open data dump file");
  75. for(j=0;j<n;j++){
  76. if(dB && v[j]==0)
  77. fprintf(of,"\n\n");
  78. else{
  79. if(bark)
  80. fprintf(of,"%g ",toBARK(22050.f*j/n));
  81. else
  82. fprintf(of,"%g ",(double)j);
  83. if(dB){
  84. fprintf(of,"%g\n",todB(v+j));
  85. }else{
  86. fprintf(of,"%g\n",v[j]);
  87. }
  88. }
  89. }
  90. fclose(of);
  91. /* } */
  92. }
  93. void _analysis_output(char *base,int i,float *v,int n,int bark,int dB){
  94. #ifdef ANALYSIS
  95. if(analysis_noisy)_analysis_output_always(base,i,v,n,bark,dB);
  96. #endif
  97. }