Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZamEQ2Plugin.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * ZamEQ2 2 band parametric equaliser
  3. * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "ZamEQ2Plugin.hpp"
  18. START_NAMESPACE_DISTRHO
  19. // -----------------------------------------------------------------------
  20. ZamEQ2Plugin::ZamEQ2Plugin()
  21. : Plugin(paramCount, 1, 0) // 1 program, 0 states
  22. {
  23. // set default values
  24. d_setProgram(0);
  25. }
  26. // -----------------------------------------------------------------------
  27. // Init
  28. void ZamEQ2Plugin::d_initParameter(uint32_t index, Parameter& parameter)
  29. {
  30. switch (index)
  31. {
  32. case paramGain1:
  33. parameter.hints = PARAMETER_IS_AUTOMABLE;
  34. parameter.name = "Boost/Cut 1";
  35. parameter.symbol = "boost1";
  36. parameter.unit = "dB";
  37. parameter.ranges.def = 0.0f;
  38. parameter.ranges.min = -50.0f;
  39. parameter.ranges.max = 20.0f;
  40. break;
  41. case paramQ1:
  42. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  43. parameter.name = "Bandwidth 1";
  44. parameter.symbol = "bw1";
  45. parameter.unit = " ";
  46. parameter.ranges.def = 2.0f;
  47. parameter.ranges.min = 0.1f;
  48. parameter.ranges.max = 6.0f;
  49. break;
  50. case paramFreq1:
  51. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  52. parameter.name = "Frequency 1";
  53. parameter.symbol = "f1";
  54. parameter.unit = "Hz";
  55. parameter.ranges.def = 500.0f;
  56. parameter.ranges.min = 20.0f;
  57. parameter.ranges.max = 14000.0f;
  58. break;
  59. case paramGain2:
  60. parameter.hints = PARAMETER_IS_AUTOMABLE;
  61. parameter.name = "Boost/Cut 2";
  62. parameter.symbol = "boost2";
  63. parameter.unit = "dB";
  64. parameter.ranges.def = 0.0f;
  65. parameter.ranges.min = -50.0f;
  66. parameter.ranges.max = 20.0f;
  67. break;
  68. case paramQ2:
  69. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  70. parameter.name = "Bandwidth 2";
  71. parameter.symbol = "bw2";
  72. parameter.unit = " ";
  73. parameter.ranges.def = 2.0f;
  74. parameter.ranges.min = 0.1f;
  75. parameter.ranges.max = 6.0f;
  76. break;
  77. case paramFreq2:
  78. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  79. parameter.name = "Frequency 2";
  80. parameter.symbol = "f2";
  81. parameter.unit = "Hz";
  82. parameter.ranges.def = 3000.0f;
  83. parameter.ranges.min = 20.0f;
  84. parameter.ranges.max = 14000.0f;
  85. break;
  86. case paramGainL:
  87. parameter.hints = PARAMETER_IS_AUTOMABLE;
  88. parameter.name = "Boost/Cut L";
  89. parameter.symbol = "boostl";
  90. parameter.unit = "dB";
  91. parameter.ranges.def = 0.0f;
  92. parameter.ranges.min = -50.0f;
  93. parameter.ranges.max = 20.0f;
  94. break;
  95. case paramFreqL:
  96. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  97. parameter.name = "Frequency L";
  98. parameter.symbol = "fl";
  99. parameter.unit = "Hz";
  100. parameter.ranges.def = 250.0f;
  101. parameter.ranges.min = 20.0f;
  102. parameter.ranges.max = 14000.0f;
  103. break;
  104. case paramGainH:
  105. parameter.hints = PARAMETER_IS_AUTOMABLE;
  106. parameter.name = "Boost/Cut H";
  107. parameter.symbol = "boosth";
  108. parameter.unit = "dB";
  109. parameter.ranges.def = 0.0f;
  110. parameter.ranges.min = -50.0f;
  111. parameter.ranges.max = 20.0f;
  112. break;
  113. case paramFreqH:
  114. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  115. parameter.name = "Frequency H";
  116. parameter.symbol = "fh";
  117. parameter.unit = "Hz";
  118. parameter.ranges.def = 8000.0f;
  119. parameter.ranges.min = 20.0f;
  120. parameter.ranges.max = 14000.0f;
  121. break;
  122. case paramMaster:
  123. parameter.hints = PARAMETER_IS_AUTOMABLE;
  124. parameter.name = "Master Gain";
  125. parameter.symbol = "master";
  126. parameter.unit = "dB";
  127. parameter.ranges.def = 0.0f;
  128. parameter.ranges.min = -12.0f;
  129. parameter.ranges.max = 12.0f;
  130. break;
  131. case paramTogglePeaks:
  132. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  133. parameter.name = "Peaks ON";
  134. parameter.symbol = "peaks";
  135. parameter.unit = " ";
  136. parameter.ranges.def = 0.0f;
  137. parameter.ranges.min = 0.0f;
  138. parameter.ranges.max = 1.0f;
  139. break;
  140. }
  141. }
  142. void ZamEQ2Plugin::d_initProgramName(uint32_t index, d_string& programName)
  143. {
  144. if (index != 0)
  145. return;
  146. programName = "Default";
  147. }
  148. // -----------------------------------------------------------------------
  149. // Internal data
  150. float ZamEQ2Plugin::d_getParameterValue(uint32_t index) const
  151. {
  152. switch (index)
  153. {
  154. case paramGain1:
  155. return gain1;
  156. break;
  157. case paramQ1:
  158. return q1;
  159. break;
  160. case paramFreq1:
  161. return freq1;
  162. break;
  163. case paramGain2:
  164. return gain2;
  165. break;
  166. case paramQ2:
  167. return q2;
  168. break;
  169. case paramFreq2:
  170. return freq2;
  171. break;
  172. case paramGainL:
  173. return gainl;
  174. break;
  175. case paramFreqL:
  176. return freql;
  177. break;
  178. case paramGainH:
  179. return gainh;
  180. break;
  181. case paramFreqH:
  182. return freqh;
  183. break;
  184. case paramMaster:
  185. return master;
  186. break;
  187. case paramTogglePeaks:
  188. return togglepeaks;
  189. break;
  190. default:
  191. return 0.0f;
  192. }
  193. }
  194. void ZamEQ2Plugin::d_setParameterValue(uint32_t index, float value)
  195. {
  196. switch (index)
  197. {
  198. case paramGain1:
  199. gain1 = value;
  200. break;
  201. case paramQ1:
  202. q1 = value;
  203. break;
  204. case paramFreq1:
  205. freq1 = value;
  206. break;
  207. case paramGain2:
  208. gain2 = value;
  209. break;
  210. case paramQ2:
  211. q2 = value;
  212. break;
  213. case paramFreq2:
  214. freq2 = value;
  215. break;
  216. case paramGainL:
  217. gainl = value;
  218. break;
  219. case paramFreqL:
  220. freql = value;
  221. break;
  222. case paramGainH:
  223. gainh = value;
  224. break;
  225. case paramFreqH:
  226. freqh = value;
  227. break;
  228. case paramMaster:
  229. master = value;
  230. break;
  231. case paramTogglePeaks:
  232. togglepeaks = value;
  233. break;
  234. }
  235. }
  236. void ZamEQ2Plugin::d_setProgram(uint32_t index)
  237. {
  238. if (index != 0)
  239. return;
  240. /* Default parameter values */
  241. gain1 = 0.0f;
  242. q1 = 1.0f;
  243. freq1 = 500.0f;
  244. gain2 = 0.0f;
  245. q2 = 1.0f;
  246. freq2 = 3000.0f;
  247. gainl = 0.0f;
  248. freql = 250.0f;
  249. gainh = 0.0f;
  250. freqh = 8000.0f;
  251. master = 0.f;
  252. togglepeaks = 0.f;
  253. /* Default variable values */
  254. /* reset filter values */
  255. d_activate();
  256. }
  257. // -----------------------------------------------------------------------
  258. // Process
  259. void ZamEQ2Plugin::d_activate()
  260. {
  261. int i;
  262. for (i = 0; i < MAX_FILT; ++i) {
  263. x1[0][i] = x2[0][i] = 0.f;
  264. y1[0][i] = y2[0][i] = 0.f;
  265. b0[0][i] = b1[0][i] = b2[0][i] = 0.f;
  266. a1[0][i] = a2[0][i] = 0.f;
  267. }
  268. }
  269. void ZamEQ2Plugin::lowshelf(int i, int ch, float srate, float fc, float g)
  270. {
  271. float k, v0;
  272. k = tanf(M_PI * fc / srate);
  273. v0 = powf(10., g / 20.);
  274. if (g < 0.f) {
  275. // LF cut
  276. float denom = v0 + sqrt(2. * v0)*k + k*k;
  277. b0[ch][i] = v0 * (1. + sqrt(2.)*k + k*k) / denom;
  278. b1[ch][i] = 2. * v0*(k*k - 1.) / denom;
  279. b2[ch][i] = v0 * (1. - sqrt(2.)*k + k*k) / denom;
  280. a1[ch][i] = 2. * (k*k - v0) / denom;
  281. a2[ch][i] = (v0 - sqrt(2. * v0)*k + k*k) / denom;
  282. } else {
  283. // LF boost
  284. float denom = 1. + sqrt(2.)*k + k*k;
  285. b0[ch][i] = (1. + sqrt(2. * v0)*k + v0*k*k) / denom;
  286. b1[ch][i] = 2. * (v0*k*k - 1.) / denom;
  287. b2[ch][i] = (1. - sqrt(2. * v0)*k + v0*k*k) / denom;
  288. a1[ch][i] = 2. * (k*k - 1.) / denom;
  289. a2[ch][i] = (1. - sqrt(2.)*k + k*k) / denom;
  290. }
  291. }
  292. void ZamEQ2Plugin::highshelf(int i, int ch, float srate, float fc, float g)
  293. {
  294. float k, v0;
  295. k = tanf(M_PI * fc / srate);
  296. v0 = powf(10., g / 20.);
  297. if (g < 0.f) {
  298. // HF cut
  299. float denom = 1. + sqrt(2. * v0)*k + v0*k*k;
  300. b0[ch][i] = v0*(1. + sqrt(2.)*k + k*k) / denom;
  301. b1[ch][i] = 2. * v0*(k*k - 1.) / denom;
  302. b2[ch][i] = v0*(1. - sqrt(2.)*k + k*k) / denom;
  303. a1[ch][i] = 2. * (v0*k*k - 1.) / denom;
  304. a2[ch][i] = (1. - sqrt(2. * v0)*k + v0*k*k) / denom;
  305. } else {
  306. // HF boost
  307. float denom = 1. + sqrt(2.)*k + k*k;
  308. b0[ch][i] = (v0 + sqrt(2. * v0)*k + k*k) / denom;
  309. b1[ch][i] = 2. * (k*k - v0) / denom;
  310. b2[ch][i] = (v0 - sqrt(2. * v0)*k + k*k) / denom;
  311. a1[ch][i] = 2. * (k*k - 1.) / denom;
  312. a2[ch][i] = (1. - sqrt(2.)*k + k*k) / denom;
  313. }
  314. }
  315. void ZamEQ2Plugin::peq(int i, int ch, float srate, float fc, float g, float bw)
  316. {
  317. float k, v0, q;
  318. k = tanf(M_PI * fc / srate);
  319. v0 = powf(10., g / 20.);
  320. q = powf(2., 1./bw)/(powf(2., bw) - 1.); //q from octave bw
  321. if (g < 0.f) {
  322. // cut
  323. float denom = 1. + k/(v0*q) + k*k;
  324. b0[ch][i] = (1. + k/q + k*k) / denom;
  325. b1[ch][i] = 2. * (k*k - 1.) / denom;
  326. b2[ch][i] = (1. - k/q + k*k) / denom;
  327. a1[ch][i] = b1[ch][i];
  328. a2[ch][i] = (1. - k/(v0*q) + k*k) / denom;
  329. } else {
  330. // boost
  331. float denom = 1. + k/q + k*k;
  332. b0[ch][i] = (1. + k*v0/q + k*k) / denom;
  333. b1[ch][i] = 2. * (k*k - 1.) / denom;
  334. b2[ch][i] = (1. - k*v0/q + k*k) / denom;
  335. a1[ch][i] = b1[ch][i];
  336. a2[ch][i] = (1. - k/q + k*k) / denom;
  337. }
  338. }
  339. float ZamEQ2Plugin::run_filter(int i, int ch, double in)
  340. {
  341. double out;
  342. in = sanitize_denormal(in);
  343. out = in * b0[ch][i] + x1[ch][i] * b1[ch][i]
  344. + x2[ch][i] * b2[ch][i]
  345. - y1[ch][i] * a1[ch][i]
  346. - y2[ch][i] * a2[ch][i] + 1e-20f;
  347. out = sanitize_denormal(out);
  348. x2[ch][i] = sanitize_denormal(x1[ch][i]);
  349. y2[ch][i] = sanitize_denormal(y1[ch][i]);
  350. x1[ch][i] = in;
  351. y1[ch][i] = out;
  352. return (float) out;
  353. }
  354. void ZamEQ2Plugin::d_run(const float** inputs, float** outputs, uint32_t frames)
  355. {
  356. float srate = d_getSampleRate();
  357. lowshelf(0, 0, srate, freql, gainl);
  358. peq(1, 0, srate, freq1, gain1, q1);
  359. peq(2, 0, srate, freq2, gain2, q2);
  360. highshelf(3, 0, srate, freqh, gainh);
  361. for (uint32_t i = 0; i < frames; i++) {
  362. double tmp,tmpl, tmph;
  363. double in = inputs[0][i];
  364. in = sanitize_denormal(in);
  365. //lowshelf
  366. tmpl = (gainl == 0.f) ? in : run_filter(0, 0, in);
  367. //highshelf
  368. tmph = (gainh == 0.f) ? tmpl : run_filter(3, 0, tmpl);
  369. //parametric1
  370. tmp = (gain1 == 0.f) ? tmph : run_filter(1, 0, tmph);
  371. //parametric2
  372. tmpl = (gain2 == 0.f) ? tmp : run_filter(2, 0, tmp);
  373. outputs[0][i] = inputs[0][i];
  374. outputs[0][i] = (float) tmpl;
  375. outputs[0][i] *= from_dB(master);
  376. }
  377. }
  378. // -----------------------------------------------------------------------
  379. Plugin* createPlugin()
  380. {
  381. return new ZamEQ2Plugin();
  382. }
  383. // -----------------------------------------------------------------------
  384. END_NAMESPACE_DISTRHO