1
0

eq10dsp.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*****************************************
  2. EQ10 library version 1.0
  3. Copyright (C)2002 4Front Technologies
  4. Written by George Yohng
  5. http://www.opensound.com
  6. Proprietary software.
  7. *****************************************/
  8. #include "main.h"
  9. #include "eq10dsp.h"
  10. //#include <stdio.h>
  11. //#include <string.h>
  12. #include <math.h>
  13. #include "WinampAttributes.h"
  14. #define DENORMAL_FIX // comment this for no denormal fixes
  15. char _eq10_copyright[]=
  16. "EQ10 Library version 1.0\n"
  17. "Copyright (C)2002 4Front Technologies http://www.opensound.com\n"
  18. "Copyright (C)2001-2002 by George Yohng http://www.yohng.com\n\0"
  19. "EQ10 ENGINE";
  20. static double eq10_freq[EQ10_NOFBANDS]={ 70, 180, 320, 600, 1000, 3000, 6000, 12000, 14000, 16000 }; // winamp style frequency table;
  21. static double eq10_freq_iso[EQ10_NOFBANDS]={31,62,125,250,500,1000,2000,4000,8000,16000}; // ISO frequency table
  22. #ifdef EQ10_DQ
  23. static double eq10_q[EQ10_NOFBANDS]=EQ10_DQ;
  24. #endif
  25. static void eq10_bsetup2(int u,double rate,eq10band_t *band,double freq,double Q)
  26. {
  27. double angle;
  28. double a0,/*a1,a2,*/b0,b1,b2,alpha;
  29. if (rate<4000.0) rate=4000.0;
  30. if (rate>384000.0) rate=384000.0;
  31. if (freq<20.0) freq=20.0;
  32. if (freq>=(rate*0.499)) {band->ua0=band->da0=0;return;}
  33. angle = 2.0*3.1415926535897932384626433832795*freq/rate;
  34. alpha = sin(angle)/(2.0*Q);
  35. b0 = 1.0/(1.0+alpha);
  36. a0 = b0*alpha;
  37. b1 = b0*2*cos(angle);
  38. b2 = b0*(alpha-1);
  39. if (u>0)
  40. {
  41. band->ua0=a0;
  42. band->ub1=b1;
  43. band->ub2=b2;
  44. }
  45. else
  46. {
  47. band->da0=a0;
  48. band->db1=b1;
  49. band->db2=b2;
  50. }
  51. }
  52. static void eq10_bsetup(double rate,eq10band_t *band,double freq,double Q)
  53. {
  54. memset(band,0,sizeof(*band));
  55. eq10_bsetup2(-1,rate,band,freq,Q*0.5);
  56. eq10_bsetup2(1,rate,band,freq,Q*2.0);
  57. #ifdef EQ10_DETECTOR_CODE
  58. /* release of detector */
  59. band->detectdecay=pow(0.001,1.0/(rate*EQ10_DETECTOR_RELEASE));
  60. #endif
  61. }
  62. void eq10_setup(eq10_t *eq, int eqs, double rate)
  63. {
  64. int t,k;
  65. for(k=0;k<eqs;k++,eq++)
  66. {
  67. for(t=0;t<EQ10_NOFBANDS;t++)
  68. #ifndef EQ10_DQ
  69. eq10_bsetup(rate,&eq->band[t],(config_eq_frequencies==EQ_FREQUENCIES_WINAMP)?eq10_freq[t]:eq10_freq_iso[t],EQ10_Q);
  70. #else
  71. eq10_bsetup(rate,&eq->band[t],(config_eq_frequencies==EQ_FREQUENCIES_WINAMP)?eq10_freq[t]:eq10_freq_iso[t],eq10_q[t]);
  72. #endif
  73. eq->detect=0;
  74. /* release of trimmer */
  75. eq->detectdecay=pow(0.001,1.0/(rate*EQ10_TRIM_RELEASE));
  76. }
  77. }
  78. void eq10_processf(eq10_t *eq,float *buf,float *outbuf,int sz,int idx,int step)
  79. {
  80. int t,k;
  81. float *in,*out;
  82. if (!eq) return;
  83. buf+=idx;
  84. outbuf+=idx;
  85. in=buf;
  86. for(k=0;k<EQ10_NOFBANDS;k++)
  87. {
  88. double a0,b1,b2;
  89. double x1 = eq->band[k].x1;
  90. double x2 = eq->band[k].x2;
  91. double y1 = eq->band[k].y1;
  92. double y2 = eq->band[k].y2;
  93. double gain = eq->band[k].gain;
  94. #ifdef EQ10_DETECTOR_CODE
  95. double detect = eq->band[k].detect;
  96. double detectdecay = eq->band[k].detectdecay;
  97. #endif
  98. out = outbuf;
  99. if (gain>0.0)
  100. {
  101. a0 = eq->band[k].ua0*gain;
  102. b1 = eq->band[k].ub1;
  103. b2 = eq->band[k].ub2;
  104. }
  105. else
  106. {
  107. a0 = eq->band[k].da0*gain;
  108. b1 = eq->band[k].db1;
  109. b2 = eq->band[k].db2;
  110. }
  111. if (a0==0.0) continue;
  112. for(t=0;t<sz;t++,in+=step,out+=step)
  113. {
  114. double y0 = (in[0]-x2)*a0 + y1*b1 + y2*b2
  115. #ifdef DENORMAL_FIX
  116. + 1e-30;
  117. #else
  118. ;
  119. #endif
  120. #ifdef EQ10_DETECTOR_CODE
  121. if (fabs(y0)>detect) detect=fabs(y0);
  122. detect*=detectdecay;
  123. #ifdef DENORMAL_FIX
  124. detect+=1e-30;
  125. #endif
  126. #endif
  127. x2=x1; x1=in[0]; y2=y1; y1=y0;
  128. out[0] = (float)(y0 + in[0]);
  129. }
  130. in=outbuf;
  131. eq->band[k].x1=x1;
  132. eq->band[k].x2=x2;
  133. eq->band[k].y1=y1;
  134. eq->band[k].y2=y2;
  135. #ifdef EQ10_DETECTOR_CODE
  136. eq->band[k].detect=detect;
  137. #endif
  138. }
  139. if (config_eq_limiter)
  140. {
  141. double detect=eq->detect;
  142. double detectdecay=eq->detectdecay;
  143. out=outbuf;
  144. for(t=0;t<sz;t++,in+=step,out+=step)
  145. {
  146. /* *0.99 - reserve */
  147. if (fabs(in[0])>detect) detect=fabs(in[0]);
  148. if (detect>EQ10_TRIM_CODE)
  149. out[0]=in[0]*(float)(EQ10_TRIM_CODE/detect);
  150. else
  151. out[0]=in[0];
  152. detect*=detectdecay;
  153. #ifdef DENORMAL_FIX
  154. detect+=1e-30;
  155. #endif
  156. }
  157. eq->detect=detect;
  158. }
  159. else if ((in==buf)&&(buf!=outbuf))
  160. {
  161. out=outbuf;
  162. for(t=0;t<sz;t++,in+=step,out+=step) out[0]=in[0];
  163. }
  164. }
  165. double eq10_db2gain(double gain_dB)
  166. {
  167. return pow(10.0,gain_dB/20.0)-1.0;
  168. }
  169. double eq10_gain2db(double gain)
  170. {
  171. return 20.0*log10(gain+1.0);
  172. }
  173. void eq10_setgain(eq10_t *eq,int eqs,int bandnr,double gain_dB)
  174. {
  175. double realgain;
  176. int k;
  177. if (!eq)
  178. return;
  179. realgain=eq10_db2gain(gain_dB);
  180. for(k=0;k<eqs;k++,eq++)
  181. eq->band[bandnr].gain=realgain;
  182. }
  183. double eq10_getgain(eq10_t *eq,int bandnr)
  184. {
  185. return eq10_gain2db(eq->band[bandnr].gain);
  186. }
  187. double eq10_detect(eq10_t *eq,int bandnr)
  188. {
  189. #ifdef EQ10_DETECTOR_CODE
  190. return eq10_gain2db(eq->band[bandnr].detect);
  191. #else
  192. return 0;
  193. #endif
  194. }
  195. #ifdef TESTCASE
  196. eq10_t eq;
  197. float buf1[4096] = {0};
  198. float buf[4096] = {0};
  199. int main()
  200. {
  201. int t,k;
  202. eq10_setup(&eq,1,44100);
  203. for(t=0;t<4096;t++)
  204. {
  205. buf1[t]=warand()*(1.0/16384);
  206. }
  207. for(t=0;t<EQ10_NOFBANDS;t++) eq.band[t].gain=-0.874107;
  208. for(t=0;t<10000;t++)
  209. {
  210. eq10_processf(&eq,buf1,buf,4096,0,1);
  211. }
  212. return 0;
  213. }
  214. #endif