Eq.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. Eq.cc
  3. Copyright 2002-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. 10-band octave-spread equalizer.
  6. */
  7. /*
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. 02111-1307, USA or point your web browser to http://www.gnu.org.
  20. */
  21. #include "basics.h"
  22. #include <stdio.h>
  23. #include "Eq.h"
  24. #include "Descriptor.h"
  25. /* slight adjustments to gain to keep response optimally flat at
  26. * 0 dB gain in all bands */
  27. inline static double
  28. adjust_gain (int i, double g)
  29. {
  30. static float adjust[] = {
  31. 0.69238604707174034, 0.67282771124180096,
  32. 0.67215187672467813, 0.65768648447259315,
  33. 0.65988083755898952, 0.66359580101701909,
  34. 0.66485139160960427, 0.65890297086039662,
  35. 0.6493229390740376, 0.82305724539749325
  36. };
  37. return g * adjust[i];
  38. }
  39. #define Q 1.414
  40. void
  41. Eq::init()
  42. {
  43. eq.init (fs, Q);
  44. }
  45. void
  46. Eq::activate()
  47. {
  48. for (int i = 0; i < 10; ++i)
  49. {
  50. gain[i] = getport (1 + i);
  51. eq.gain[i] = adjust_gain (i, DSP::db2lin (gain[i]));
  52. eq.gf[i] = 1;
  53. }
  54. }
  55. template <sample_func_t F>
  56. void
  57. Eq::one_cycle (int frames)
  58. {
  59. sample_t * s = ports[0];
  60. /* evaluate band gain changes and compute recursion factor to prevent
  61. * zipper noise */
  62. double one_over_n = frames > 0 ? 1. / frames : 1;
  63. for (int i = 0; i < 10; ++i)
  64. {
  65. sample_t g = getport (1 + i);
  66. if (g == gain[i])
  67. {
  68. /* no gain factoring */
  69. eq.gf[i] = 1;
  70. continue;
  71. }
  72. gain[i] = g;
  73. double want = adjust_gain (i, DSP::db2lin (g));
  74. eq.gf[i] = pow (want / eq.gain[i], one_over_n);
  75. }
  76. sample_t * d = ports[11];
  77. for (int i = 0; i < frames; ++i)
  78. {
  79. sample_t x = s[i];
  80. x = eq.process (x);
  81. F (d, i, x, adding_gain);
  82. }
  83. eq.normal = -normal;
  84. eq.flush_0();
  85. }
  86. /* //////////////////////////////////////////////////////////////////////// */
  87. PortInfo
  88. Eq::port_info [] =
  89. {
  90. {
  91. "in",
  92. INPUT | AUDIO,
  93. {0, -1, 1}
  94. }, {
  95. "31 Hz",
  96. INPUT | CONTROL,
  97. {BOUNDED | DEFAULT_0, -48, 24}
  98. }, {
  99. "63 Hz",
  100. INPUT | CONTROL,
  101. {BOUNDED | DEFAULT_0, -48, 24}
  102. }, {
  103. "125 Hz",
  104. INPUT | CONTROL,
  105. {BOUNDED | DEFAULT_0, -48, 24}
  106. }, {
  107. "250 Hz",
  108. INPUT | CONTROL,
  109. {BOUNDED | DEFAULT_0, -48, 24}
  110. }, {
  111. "500 Hz",
  112. INPUT | CONTROL,
  113. {BOUNDED | DEFAULT_0, -48, 24}
  114. }, {
  115. "1 kHz",
  116. INPUT | CONTROL,
  117. {BOUNDED | DEFAULT_0, -48, 24}
  118. }, {
  119. "2 kHz",
  120. INPUT | CONTROL,
  121. {BOUNDED | DEFAULT_0, -48, 24}
  122. }, {
  123. "4 kHz",
  124. INPUT | CONTROL,
  125. {BOUNDED | DEFAULT_0, -48, 24}
  126. }, {
  127. "8 kHz",
  128. INPUT | CONTROL,
  129. {BOUNDED | DEFAULT_0, -48, 24}
  130. }, {
  131. "16 kHz",
  132. INPUT | CONTROL,
  133. {BOUNDED | DEFAULT_0, -48, 24}
  134. }, {
  135. "out",
  136. OUTPUT | AUDIO,
  137. {0}
  138. }
  139. };
  140. template <> void
  141. Descriptor<Eq>::setup()
  142. {
  143. UniqueID = 1773;
  144. Label = "Eq";
  145. Properties = HARD_RT;
  146. Name = CAPS "Eq - 10-band equalizer";
  147. Maker = "Tim Goetze <tim@quitte.de>";
  148. Copyright = "GPL, 2004-7";
  149. /* fill port info and vtable */
  150. autogen();
  151. }
  152. /* //////////////////////////////////////////////////////////////////////// */
  153. void
  154. Eq2x2::init()
  155. {
  156. for (int c = 0; c < 2; ++c)
  157. eq[c].init (fs, Q);
  158. }
  159. void
  160. Eq2x2::activate()
  161. {
  162. /* Fetch current parameter settings so we won't sweep band gains in the
  163. * first block to process.
  164. */
  165. for (int i = 0; i < 10; ++i)
  166. {
  167. gain[i] = getport (2 + i);
  168. double a = adjust_gain (i, DSP::db2lin (gain[i]));
  169. for (int c = 0; c < 2; ++c)
  170. eq[c].gf[i] = 1,
  171. eq[c].gain[i] = a;
  172. }
  173. }
  174. template <sample_func_t F>
  175. void
  176. Eq2x2::one_cycle (int frames)
  177. {
  178. /* evaluate band gain changes and compute recursion factor to prevent
  179. * zipper noise */
  180. double one_over_n = frames > 0 ? 1. / frames : 1;
  181. for (int i = 0; i < 10; ++i)
  182. {
  183. double a;
  184. if (*ports [2 + i] == gain[i])
  185. /* still same value, no gain fade */
  186. a = 1;
  187. else
  188. {
  189. gain[i] = getport (2 + i);
  190. /* prepare factor for logarithmic gain fade */
  191. a = adjust_gain (i, DSP::db2lin (gain[i]));
  192. a = pow (a / eq[0].gain[i], one_over_n);
  193. }
  194. for (int c = 0; c < 2; ++c)
  195. eq[c].gf[i] = a;
  196. }
  197. for (int c = 0; c < 2; ++c)
  198. {
  199. sample_t
  200. * s = ports[c],
  201. * d = ports[12 + c];
  202. for (int i = 0; i < frames; ++i)
  203. {
  204. sample_t x = s[i];
  205. x = eq[c].process (x);
  206. F (d, i, x, adding_gain);
  207. }
  208. }
  209. /* flip 'renormal' values */
  210. for (int c = 0; c < 2; ++c)
  211. {
  212. eq[c].normal = normal;
  213. eq[c].flush_0();
  214. }
  215. }
  216. PortInfo
  217. Eq2x2::port_info [] =
  218. {
  219. {
  220. "in:l",
  221. INPUT | AUDIO,
  222. {0, -1, 1}
  223. }, {
  224. "in:r",
  225. INPUT | AUDIO,
  226. {0, -1, 1}
  227. }, {
  228. "31 Hz",
  229. INPUT | CONTROL,
  230. {BOUNDED | DEFAULT_0, -48, 24}
  231. }, {
  232. "63 Hz",
  233. INPUT | CONTROL,
  234. {BOUNDED | DEFAULT_0, -48, 24}
  235. }, {
  236. "125 Hz",
  237. INPUT | CONTROL,
  238. {BOUNDED | DEFAULT_0, -48, 24}
  239. }, {
  240. "250 Hz",
  241. INPUT | CONTROL,
  242. {BOUNDED | DEFAULT_0, -48, 24}
  243. }, {
  244. "500 Hz",
  245. INPUT | CONTROL,
  246. {BOUNDED | DEFAULT_0, -48, 24}
  247. }, {
  248. "1 kHz",
  249. INPUT | CONTROL,
  250. {BOUNDED | DEFAULT_0, -48, 24}
  251. }, {
  252. "2 kHz",
  253. INPUT | CONTROL,
  254. {BOUNDED | DEFAULT_0, -48, 24}
  255. }, {
  256. "4 kHz",
  257. INPUT | CONTROL,
  258. {BOUNDED | DEFAULT_0, -48, 24}
  259. }, {
  260. "8 kHz",
  261. INPUT | CONTROL,
  262. {BOUNDED | DEFAULT_0, -48, 24}
  263. }, {
  264. "16 kHz",
  265. INPUT | CONTROL,
  266. {BOUNDED | DEFAULT_0, -48, 24}
  267. }, {
  268. "out:l",
  269. OUTPUT | AUDIO,
  270. {0}
  271. }, {
  272. "out:r",
  273. OUTPUT | AUDIO,
  274. {0}
  275. }
  276. };
  277. template <> void
  278. Descriptor<Eq2x2>::setup()
  279. {
  280. UniqueID = 2594;
  281. Label = "Eq2x2";
  282. Properties = HARD_RT;
  283. Name = CAPS "Eq2x2 - stereo 10-band equalizer";
  284. Maker = "Tim Goetze <tim@quitte.de>";
  285. Copyright = "GPL, 2004-7";
  286. /* fill port info and vtable */
  287. autogen();
  288. }
  289. /* //////////////////////////////////////////////////////////////////////// */
  290. /*
  291. todo: parametric -- 20-400, 60-1k, 150-2.5k, 500-8k, 1k-20k
  292. bandwidth 0-2 octaves
  293. */