envelope.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH 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: PCM data envelope analysis and manipulation
  14. last mod: $Id: envelope.c,v 1.15.4.1 2000/05/08 08:25:42 xiphmont Exp $
  15. Preecho calculation.
  16. ********************************************************************/
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include "vorbis/codec.h"
  22. #include "os.h"
  23. #include "mdct.h"
  24. #include "envelope.h"
  25. #include "bitwise.h"
  26. #include "window.h"
  27. void _ve_envelope_init(envelope_lookup *e,int samples_per){
  28. int i;
  29. e->winlen=samples_per;
  30. e->window=malloc(e->winlen*sizeof(double));
  31. mdct_init(&e->mdct,e->winlen);
  32. /* We just use a straight sin(x) window for this */
  33. for(i=0;i<e->winlen;i++)
  34. e->window[i]=sin((i+.5)/e->winlen*M_PI);
  35. }
  36. void _ve_envelope_clear(envelope_lookup *e){
  37. if(e->window)free(e->window);
  38. mdct_clear(&e->mdct);
  39. memset(e,0,sizeof(envelope_lookup));
  40. }
  41. /* use MDCT for spectral power estimation */
  42. static void _ve_deltas(double *deltas,double *pcm,int n,double *window,
  43. int winsize,mdct_lookup *m){
  44. int i,j;
  45. double *out=alloca(sizeof(double)*winsize);
  46. for(j=0;j<n;j++){
  47. double acc=0.;
  48. memcpy(out,pcm+j*winsize,winsize*sizeof(double));
  49. for(i=0;i<winsize;i++)
  50. out[i]*=window[i];
  51. mdct_forward(m,out,out);
  52. for(i=winsize/10;i<winsize/2;i++)
  53. acc+=fabs(out[i]);
  54. if(deltas[j]<acc)deltas[j]=acc;
  55. }
  56. }
  57. void _ve_envelope_deltas(vorbis_dsp_state *v){
  58. vorbis_info *vi=v->vi;
  59. int step=vi->envelopesa;
  60. int dtotal=v->pcm_current/vi->envelopesa;
  61. int dcurr=v->envelope_current;
  62. int pch;
  63. if(dtotal>dcurr){
  64. double *mult=v->multipliers+dcurr;
  65. memset(mult,0,sizeof(double)*(dtotal-dcurr));
  66. for(pch=0;pch<vi->channels;pch++){
  67. double *pcm=v->pcm[pch]+dcurr*step;
  68. _ve_deltas(mult,pcm,dtotal-dcurr,v->ve.window,v->ve.winlen,&v->ve.mdct);
  69. }
  70. v->envelope_current=dtotal;
  71. }
  72. }