1
0

benskiQ.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "main.h"
  2. #include "EqBand.h"
  3. #include "WinampAttributes.h"
  4. #include <math.h>
  5. extern int filter_srate, filter_enabled, filter_top, filter_top2;
  6. extern float preamp_val;
  7. static int filter_nch=0;
  8. static bool init=false;
  9. static EqBand benskiQ[10];
  10. //#define BENSKIQ_Q 0.70710678118654752440084436210485
  11. #define BENSKIQ_Q 1.41
  12. static double benskiQ_freqs_iso[10]={31.5, 63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000}; // ISO standard equalizer frequency table
  13. static double benskiQ_freqs[10]={ 70, 180, 320, 600, 1000, 3000, 6000, 12000, 14000, 16000 }; // winamp style frequency table
  14. static CRITICAL_SECTION benskiQ_cs;
  15. void benskiQ_init()
  16. {
  17. InitializeCriticalSection(&benskiQ_cs);
  18. for (int x=0;x<10;x++)
  19. {
  20. benskiQ[x].set_parameters((config_eq_frequencies==EQ_FREQUENCIES_WINAMP)?benskiQ_freqs[x]:benskiQ_freqs_iso[x], 1.0, BENSKIQ_Q);
  21. }
  22. }
  23. static __inline double VALTODB(int v)
  24. {
  25. v -= 31;
  26. if (v < -31) v = -31;
  27. if (v > 32) v = 32;
  28. if (v > 0) return -12.0*(v / 32.0);
  29. else if (v < 0)
  30. {
  31. return -12.0*(v / 31.0);
  32. }
  33. return 0.0f;
  34. }
  35. static __inline double VALTOGAIN(int v)
  36. {
  37. return pow(10.0, VALTODB(v)/20.0);
  38. }
  39. void benskiQ_eq_set(char data[10])
  40. {
  41. if (!init)
  42. {
  43. init=true; benskiQ_init();
  44. }
  45. EnterCriticalSection(&benskiQ_cs);
  46. for (int x = 0; x < 10; x ++)
  47. {
  48. benskiQ[x].set_parameters((config_eq_frequencies==EQ_FREQUENCIES_WINAMP)?benskiQ_freqs[x]:benskiQ_freqs_iso[x], VALTOGAIN(data[x]), BENSKIQ_Q);
  49. }
  50. LeaveCriticalSection(&benskiQ_cs);
  51. }
  52. static void FillFloat(float **floatBuf, void *samples, size_t bps, size_t numSamples, size_t numChannels, float preamp)
  53. {
  54. switch (bps)
  55. {
  56. case 8:
  57. {
  58. preamp /= 256.0f;
  59. unsigned __int8 *samples8 = (unsigned __int8 *)samples;
  60. for (size_t c=0;c<numChannels;c++)
  61. for (size_t x = 0; x != numSamples; x ++)
  62. {
  63. floatBuf[c][x] = (float)(samples8[c+numChannels*x]-128) * preamp;
  64. }
  65. }
  66. break;
  67. case 16:
  68. {
  69. preamp/=32768.0f;
  70. short *samples16 = (short *)samples;
  71. for (size_t c=0;c<numChannels;c++)
  72. for (size_t x = 0; x != numSamples; x ++)
  73. {
  74. floatBuf[c][x] = (float)samples16[c+numChannels*x] * preamp;
  75. }
  76. }
  77. break;
  78. case 24:
  79. {
  80. preamp/=2147483648.0f;
  81. unsigned __int8 *samples8 = (unsigned __int8 *)samples;
  82. long temp;
  83. for (size_t x = 0; x != numSamples; x ++)
  84. for (size_t c=0;c<numChannels;c++)
  85. {
  86. temp = (((long)samples8[0]) << 8);
  87. temp = temp | (((long)samples8[1]) << 16);
  88. temp = temp | (((long)samples8[2]) << 24);
  89. floatBuf[c][x] = (float)temp * preamp;
  90. samples8+=3;
  91. }
  92. }
  93. break;
  94. case 32:
  95. {
  96. preamp /= 2147483648.0f;
  97. int32_t *samples32 = (int32_t *)samples;
  98. for (size_t x = 0; x != numSamples; x ++)
  99. for (size_t c=0;c<numChannels;c++)
  100. {
  101. floatBuf[c][x] = (float)samples32[c+x*numChannels] * preamp;
  102. }
  103. }
  104. break;
  105. }
  106. }
  107. static void FillSamples(void *samples, float **floatBuf, size_t bps, size_t numSamples, size_t numChannels)
  108. {
  109. switch (bps)
  110. {
  111. case 16:
  112. for (size_t i=0;i<numChannels;i++)
  113. Float32_To_Int16_Clip((char *)samples+i*(bps/8), (signed int)numChannels, floatBuf[i], 1, (unsigned int) numSamples);
  114. break;
  115. case 24:
  116. for (size_t i=0;i<numChannels;i++)
  117. Float32_To_Int24_Clip((char *)samples+i*(bps/8), (signed int)numChannels, floatBuf[i], 1, (unsigned int) numSamples);
  118. break;
  119. }
  120. }
  121. static int last_nch=0, last_numsamples=0;
  122. static float **last_sample=0;
  123. void DeleteSample(float **big, int nch)
  124. {
  125. for (int i=0;i<nch;i++)
  126. delete big[i];
  127. delete[]big;
  128. }
  129. float **MakeSample(int numsamples, int nch)
  130. {
  131. if (last_nch < nch || last_numsamples < numsamples)
  132. {
  133. DeleteSample(last_sample, last_nch);
  134. last_nch=max(nch, last_nch);
  135. last_numsamples=max(numsamples, last_numsamples);
  136. last_sample = new float*[last_nch];
  137. for (int i=0;i<last_nch;i++)
  138. last_sample [i]=new float[last_numsamples];
  139. }
  140. return last_sample;
  141. }
  142. void benskiQ_reset(int srate, int nch)
  143. {
  144. for (int i=0;i<10;i++)
  145. {
  146. benskiQ[i].SetSampleRate(srate);
  147. benskiQ[i].set_num_channels(nch);
  148. }
  149. int x;
  150. if (config_eq_frequencies == EQ_FREQUENCIES_WINAMP)
  151. for (x = 0; x < 10 && benskiQ_freqs[x]*2 <= srate; x++);
  152. else
  153. for (x = 0; x < 10 && benskiQ_freqs_iso[x]*2 <= srate; x++);
  154. filter_top = min(x, filter_top2);
  155. filter_srate=srate;
  156. filter_nch=nch;
  157. }
  158. static float NonReplayGainAdjust()
  159. {
  160. if (!(in_mod->UsesOutputPlug&IN_MODULE_FLAG_REPLAYGAIN) && config_replaygain.GetBool())
  161. return pow(10.0f, (float)config_replaygain_non_rg_gain/20.0f);
  162. else
  163. return 1.0f;
  164. }
  165. static float ReplayGainPreamp()
  166. {
  167. if (!(in_mod->UsesOutputPlug&IN_MODULE_FLAG_REPLAYGAIN_PREAMP) && config_replaygain.GetBool())
  168. return pow(10.0f, (float)config_replaygain_preamp/20.0f);
  169. else
  170. return 1.0f;
  171. }
  172. int benskiQ_eq_dosamples(short *samples, int numsamples, int bps, int nch, int srate)
  173. {
  174. if (filter_enabled && in_mod && !(in_mod->UsesOutputPlug&IN_MODULE_FLAG_EQ) && bps != 32)
  175. {
  176. if (srate !=filter_srate || nch != filter_nch)
  177. benskiQ_reset(srate, nch);
  178. if (!init)
  179. {
  180. init=true; benskiQ_init();
  181. }
  182. float **in = MakeSample(numsamples, nch);
  183. FillFloat(in, samples, bps, numsamples, nch, preamp_val*NonReplayGainAdjust()*ReplayGainPreamp());
  184. EnterCriticalSection(&benskiQ_cs);
  185. for (int x = 0; x < filter_top; x ++)
  186. {
  187. benskiQ[x].process(in, in, numsamples, nch);
  188. }
  189. LeaveCriticalSection(&benskiQ_cs);
  190. FillSamples(samples, in, bps, numsamples, nch);
  191. }
  192. else if (!(in_mod->UsesOutputPlug&IN_MODULE_FLAG_REPLAYGAIN) && config_replaygain.GetBool() && (config_replaygain_non_rg_gain.GetFloat() != 0) && bps != 32)
  193. {
  194. float **in = MakeSample(numsamples, nch);
  195. FillFloat(in, samples, bps, numsamples, nch, NonReplayGainAdjust()*ReplayGainPreamp());
  196. FillSamples(samples, in, bps, numsamples, nch);
  197. }
  198. else if (!(in_mod->UsesOutputPlug&IN_MODULE_FLAG_REPLAYGAIN_PREAMP) && config_replaygain.GetBool() && (config_replaygain_preamp.GetFloat() != 0) && bps != 32)
  199. {
  200. float **in = MakeSample(numsamples, nch);
  201. FillFloat(in, samples, bps, numsamples, nch, ReplayGainPreamp());
  202. FillSamples(samples, in, bps, numsamples, nch);
  203. }
  204. else
  205. filter_srate = 0;
  206. return dsp_dosamples(samples, numsamples, bps, nch, srate);
  207. }