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.

624 lines
18KB

  1. /*
  2. * ZaMultiComp mono multiband compressor
  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 "ZaMultiCompPlugin.hpp"
  18. START_NAMESPACE_DISTRHO
  19. // -----------------------------------------------------------------------
  20. ZaMultiCompPlugin::ZaMultiCompPlugin()
  21. : Plugin(paramCount, 1, 0) // 1 program, 0 states
  22. {
  23. // set default values
  24. d_setProgram(0);
  25. }
  26. // -----------------------------------------------------------------------
  27. // Init
  28. void ZaMultiCompPlugin::d_initParameter(uint32_t index, Parameter& parameter)
  29. {
  30. switch (index)
  31. {
  32. case paramAttack:
  33. parameter.hints = PARAMETER_IS_AUTOMABLE;
  34. parameter.name = "Attack";
  35. parameter.symbol = "att";
  36. parameter.unit = "ms";
  37. parameter.ranges.def = 10.0f;
  38. parameter.ranges.min = 0.1f;
  39. parameter.ranges.max = 100.0f;
  40. break;
  41. case paramRelease:
  42. parameter.hints = PARAMETER_IS_AUTOMABLE;
  43. parameter.name = "Release";
  44. parameter.symbol = "rel";
  45. parameter.unit = "ms";
  46. parameter.ranges.def = 80.0f;
  47. parameter.ranges.min = 1.0f;
  48. parameter.ranges.max = 500.0f;
  49. break;
  50. case paramKnee:
  51. parameter.hints = PARAMETER_IS_AUTOMABLE;
  52. parameter.name = "Knee";
  53. parameter.symbol = "kn";
  54. parameter.unit = "dB";
  55. parameter.ranges.def = 0.0f;
  56. parameter.ranges.min = 0.0f;
  57. parameter.ranges.max = 9.0f;
  58. break;
  59. case paramRatio:
  60. parameter.hints = PARAMETER_IS_AUTOMABLE;
  61. parameter.name = "Ratio";
  62. parameter.symbol = "rat";
  63. parameter.unit = " ";
  64. parameter.ranges.def = 4.0f;
  65. parameter.ranges.min = 1.0f;
  66. parameter.ranges.max = 20.0f;
  67. break;
  68. case paramThresh:
  69. parameter.hints = PARAMETER_IS_AUTOMABLE;
  70. parameter.name = "Threshold";
  71. parameter.symbol = "thr";
  72. parameter.unit = "dB";
  73. parameter.ranges.def = -16.0f;
  74. parameter.ranges.min = -80.0f;
  75. parameter.ranges.max = 0.0f;
  76. break;
  77. case paramMakeup1:
  78. parameter.hints = PARAMETER_IS_AUTOMABLE;
  79. parameter.name = "Makeup 1";
  80. parameter.symbol = "mak1";
  81. parameter.unit = "dB";
  82. parameter.ranges.def = 0.0f;
  83. parameter.ranges.min = 0.0f;
  84. parameter.ranges.max = 30.0f;
  85. break;
  86. case paramMakeup2:
  87. parameter.hints = PARAMETER_IS_AUTOMABLE;
  88. parameter.name = "Makeup 2";
  89. parameter.symbol = "mak2";
  90. parameter.unit = "dB";
  91. parameter.ranges.def = 0.0f;
  92. parameter.ranges.min = 0.0f;
  93. parameter.ranges.max = 30.0f;
  94. break;
  95. case paramMakeup3:
  96. parameter.hints = PARAMETER_IS_AUTOMABLE;
  97. parameter.name = "Makeup 3";
  98. parameter.symbol = "mak3";
  99. parameter.unit = "dB";
  100. parameter.ranges.def = 0.0f;
  101. parameter.ranges.min = 0.0f;
  102. parameter.ranges.max = 30.0f;
  103. break;
  104. case paramGainR1:
  105. parameter.hints = PARAMETER_IS_OUTPUT;
  106. parameter.name = "Gain Reduction 1";
  107. parameter.symbol = "gr1";
  108. parameter.unit = "dB";
  109. parameter.ranges.def = 0.0f;
  110. parameter.ranges.min = 0.0f;
  111. parameter.ranges.max = 20.0f;
  112. break;
  113. case paramGainR2:
  114. parameter.hints = PARAMETER_IS_OUTPUT;
  115. parameter.name = "Gain Reduction 2";
  116. parameter.symbol = "gr2";
  117. parameter.unit = "dB";
  118. parameter.ranges.def = 0.0f;
  119. parameter.ranges.min = 0.0f;
  120. parameter.ranges.max = 20.0f;
  121. break;
  122. case paramGainR3:
  123. parameter.hints = PARAMETER_IS_OUTPUT;
  124. parameter.name = "Gain Reduction 3";
  125. parameter.symbol = "gr3";
  126. parameter.unit = "dB";
  127. parameter.ranges.def = 0.0f;
  128. parameter.ranges.min = 0.0f;
  129. parameter.ranges.max = 20.0f;
  130. break;
  131. case paramXover1:
  132. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  133. parameter.name = "Crossover freq 1";
  134. parameter.symbol = "xover1";
  135. parameter.unit = "Hz";
  136. parameter.ranges.def = 250.0f;
  137. parameter.ranges.min = 20.0f;
  138. parameter.ranges.max = 1400.0f;
  139. break;
  140. case paramXover2:
  141. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_LOGARITHMIC;
  142. parameter.name = "Crossover freq 2";
  143. parameter.symbol = "xover2";
  144. parameter.unit = "Hz";
  145. parameter.ranges.def = 1400.0f;
  146. parameter.ranges.min = 1400.0f;
  147. parameter.ranges.max = 14000.0f;
  148. break;
  149. case paramToggle1:
  150. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  151. parameter.name = "ZamComp 1 ON";
  152. parameter.symbol = "toggle1";
  153. parameter.unit = " ";
  154. parameter.ranges.def = 0.0f;
  155. parameter.ranges.min = 0.0f;
  156. parameter.ranges.max = 1.0f;
  157. break;
  158. case paramToggle2:
  159. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  160. parameter.name = "ZamComp 2 ON";
  161. parameter.symbol = "toggle2";
  162. parameter.unit = " ";
  163. parameter.ranges.def = 0.0f;
  164. parameter.ranges.min = 0.0f;
  165. parameter.ranges.max = 1.0f;
  166. break;
  167. case paramToggle3:
  168. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  169. parameter.name = "ZamComp 3 ON";
  170. parameter.symbol = "toggle3";
  171. parameter.unit = " ";
  172. parameter.ranges.def = 0.0f;
  173. parameter.ranges.min = 0.0f;
  174. parameter.ranges.max = 1.0f;
  175. break;
  176. case paramListen1:
  177. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  178. parameter.name = "Listen 1";
  179. parameter.symbol = "listen1";
  180. parameter.unit = " ";
  181. parameter.ranges.def = 0.0f;
  182. parameter.ranges.min = 0.0f;
  183. parameter.ranges.max = 1.0f;
  184. break;
  185. case paramListen2:
  186. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  187. parameter.name = "Listen 2";
  188. parameter.symbol = "listen2";
  189. parameter.unit = " ";
  190. parameter.ranges.def = 0.0f;
  191. parameter.ranges.min = 0.0f;
  192. parameter.ranges.max = 1.0f;
  193. break;
  194. case paramListen3:
  195. parameter.hints = PARAMETER_IS_AUTOMABLE | PARAMETER_IS_BOOLEAN;
  196. parameter.name = "Listen 3";
  197. parameter.symbol = "listen3";
  198. parameter.unit = " ";
  199. parameter.ranges.def = 0.0f;
  200. parameter.ranges.min = 0.0f;
  201. parameter.ranges.max = 1.0f;
  202. break;
  203. case paramGlobalGain:
  204. parameter.hints = PARAMETER_IS_AUTOMABLE;
  205. parameter.name = "Master Trim";
  206. parameter.symbol = "globalgain";
  207. parameter.unit = "dB";
  208. parameter.ranges.def = 0.0f;
  209. parameter.ranges.min = -12.0f;
  210. parameter.ranges.max = 12.0f;
  211. break;
  212. case paramOutputLevel:
  213. parameter.hints = PARAMETER_IS_OUTPUT;
  214. parameter.name = "Output Level";
  215. parameter.symbol = "outlevel";
  216. parameter.unit = "dB";
  217. parameter.ranges.def = -45.0f;
  218. parameter.ranges.min = -45.0f;
  219. parameter.ranges.max = 20.0f;
  220. break;
  221. }
  222. }
  223. void ZaMultiCompPlugin::d_initProgramName(uint32_t index, d_string& programName)
  224. {
  225. if (index != 0)
  226. return;
  227. programName = "Default";
  228. }
  229. // -----------------------------------------------------------------------
  230. // Internal data
  231. float ZaMultiCompPlugin::d_getParameterValue(uint32_t index) const
  232. {
  233. switch (index)
  234. {
  235. case paramAttack:
  236. return attack;
  237. break;
  238. case paramRelease:
  239. return release;
  240. break;
  241. case paramKnee:
  242. return knee;
  243. break;
  244. case paramRatio:
  245. return ratio;
  246. break;
  247. case paramThresh:
  248. return thresdb;
  249. break;
  250. case paramMakeup1:
  251. return makeup[0];
  252. break;
  253. case paramMakeup2:
  254. return makeup[1];
  255. break;
  256. case paramMakeup3:
  257. return makeup[2];
  258. break;
  259. case paramGainR1:
  260. return gainr[0];
  261. break;
  262. case paramGainR2:
  263. return gainr[1];
  264. break;
  265. case paramGainR3:
  266. return gainr[2];
  267. break;
  268. case paramXover1:
  269. return xover1;
  270. break;
  271. case paramXover2:
  272. return xover2;
  273. break;
  274. case paramToggle1:
  275. return toggle[0];
  276. break;
  277. case paramToggle2:
  278. return toggle[1];
  279. break;
  280. case paramToggle3:
  281. return toggle[2];
  282. break;
  283. case paramListen1:
  284. return listen[0];
  285. break;
  286. case paramListen2:
  287. return listen[1];
  288. break;
  289. case paramListen3:
  290. return listen[2];
  291. break;
  292. case paramGlobalGain:
  293. return globalgain;
  294. break;
  295. case paramOutputLevel:
  296. return outlevel;
  297. break;
  298. default:
  299. return 0.0f;
  300. }
  301. }
  302. void ZaMultiCompPlugin::d_setParameterValue(uint32_t index, float value)
  303. {
  304. switch (index)
  305. {
  306. case paramAttack:
  307. attack = value;
  308. break;
  309. case paramRelease:
  310. release = value;
  311. break;
  312. case paramKnee:
  313. knee = value;
  314. break;
  315. case paramRatio:
  316. ratio = value;
  317. break;
  318. case paramThresh:
  319. thresdb = value;
  320. break;
  321. case paramMakeup1:
  322. makeup[0] = value;
  323. break;
  324. case paramMakeup2:
  325. makeup[1] = value;
  326. break;
  327. case paramMakeup3:
  328. makeup[2] = value;
  329. break;
  330. case paramGainR1:
  331. gainr[0] = value;
  332. break;
  333. case paramGainR2:
  334. gainr[1] = value;
  335. break;
  336. case paramGainR3:
  337. gainr[2] = value;
  338. break;
  339. case paramXover1:
  340. xover1 = value;
  341. break;
  342. case paramXover2:
  343. xover2 = value;
  344. break;
  345. case paramToggle1:
  346. toggle[0] = value;
  347. if (value == 0.f)
  348. gainr[0] = 0.f;
  349. break;
  350. case paramToggle2:
  351. toggle[1] = value;
  352. if (value == 0.f)
  353. gainr[1] = 0.f;
  354. break;
  355. case paramToggle3:
  356. toggle[2] = value;
  357. if (value == 0.f)
  358. gainr[2] = 0.f;
  359. break;
  360. case paramListen1:
  361. listen[0] = value;
  362. if (value == 0.f)
  363. gainr[0] = 0.f;
  364. break;
  365. case paramListen2:
  366. listen[1] = value;
  367. if (value == 0.f)
  368. gainr[1] = 0.f;
  369. break;
  370. case paramListen3:
  371. listen[2] = value;
  372. if (value == 0.f)
  373. gainr[2] = 0.f;
  374. break;
  375. case paramGlobalGain:
  376. globalgain = value;
  377. break;
  378. case paramOutputLevel:
  379. outlevel = value;
  380. break;
  381. }
  382. }
  383. void ZaMultiCompPlugin::d_setProgram(uint32_t index)
  384. {
  385. if (index != 0)
  386. return;
  387. /* Default parameter values */
  388. attack = 10.0f;
  389. release = 80.0f;
  390. knee = 0.0f;
  391. ratio = 4.0f;
  392. thresdb = -16.0f;
  393. makeup[0] = 0.0f;
  394. makeup[1] = 0.0f;
  395. makeup[2] = 0.0f;
  396. gainr[0] = 0.0f;
  397. gainr[1] = 0.0f;
  398. gainr[2] = 0.0f;
  399. xover1 = 250.0f;
  400. xover2 = 1400.0f;
  401. toggle[0] = 0.0f;
  402. toggle[1] = 0.0f;
  403. toggle[2] = 0.0f;
  404. listen[0] = 0.0f;
  405. listen[1] = 0.0f;
  406. listen[2] = 0.0f;
  407. globalgain = 0.0f;
  408. outlevel = -45.0f;
  409. /* Default variable values */
  410. /* reset filter values */
  411. d_activate();
  412. }
  413. // -----------------------------------------------------------------------
  414. // Process
  415. void ZaMultiCompPlugin::d_activate()
  416. {
  417. int i;
  418. for (i = 0; i < MAX_COMP; i++) {
  419. old_yl[i]=old_y1[i]=0.f;
  420. }
  421. for (i = 0; i < MAX_FILT; i++) {
  422. a0[i] = a1[i] = a2[i] = 0.f;
  423. b1[i] = b2[i] = 0.f;
  424. w1[i] = w2[i] = 0.f;
  425. z1[i] = z2[i] = 0.f;
  426. }
  427. }
  428. float ZaMultiCompPlugin::run_filter(int i, float in)
  429. {
  430. in = sanitize_denormal(in);
  431. w1[i] = sanitize_denormal(w1[i]);
  432. w2[i] = sanitize_denormal(w2[i]);
  433. z1[i] = sanitize_denormal(z1[i]);
  434. z2[i] = sanitize_denormal(z2[i]);
  435. float out = in * a0[i] + w1[i] * a1[i] + w2[i] * a2[i]
  436. - z1[i] * b1[i] - z2[i] * b2[i];
  437. out = sanitize_denormal(out);
  438. w2[i] = w1[i];
  439. z2[i] = z1[i];
  440. w1[i] = in;
  441. z1[i] = out;
  442. return out;
  443. }
  444. void ZaMultiCompPlugin::set_lp_coeffs(float fc, float q, float sr, int i, float gain=1.0)
  445. {
  446. float omega=(float)(2.f*M_PI*fc/sr);
  447. float sn=sin(omega);
  448. float cs=cos(omega);
  449. float alpha=(float)(sn/(2.f*q));
  450. float inv=(float)(1.0/(1.0+alpha));
  451. a2[i] = a0[i] = (float)(gain*inv*(1.f - cs)*0.5f);
  452. a1[i] = a0[i] + a0[i];
  453. b1[i] = (float)(-2.f*cs*inv);
  454. b2[i] = (float)((1.f - alpha)*inv);
  455. }
  456. void ZaMultiCompPlugin::set_hp_coeffs(float fc, float q, float sr, int i, float gain=1.0)
  457. {
  458. float omega=(float)(2.f*M_PI*fc/sr);
  459. float sn=sin(omega);
  460. float cs=cos(omega);
  461. float alpha=(float)(sn/(2.f*q));
  462. float inv=(float)(1.f/(1.f+alpha));
  463. a0[i] = (float)(gain*inv*(1.f + cs)/2.f);
  464. a1[i] = -2.f * a0[i];
  465. a2[i] = a0[i];
  466. b1[i] = (float)(-2.f*cs*inv);
  467. b2[i] = (float)((1.f - alpha)*inv);
  468. }
  469. float ZaMultiCompPlugin::run_comp(int k, float in)
  470. {
  471. float srate = d_getSampleRate();
  472. float width=(knee-0.99f)*6.f;
  473. float attack_coeff = exp(-1000.f/(attack * srate));
  474. float release_coeff = exp(-1000.f/(release * srate));
  475. float cdb=0.f;
  476. float gain = 1.f;
  477. float xg, xl, yg, yl, y1;
  478. float out;
  479. yg=0.f;
  480. in = sanitize_denormal(in);
  481. xg = (in==0.f) ? -160.f : to_dB(fabs(in));
  482. xg = sanitize_denormal(xg);
  483. if (2.f*(xg-thresdb)<-width) {
  484. yg = xg;
  485. } else if (2.f*fabs(xg-thresdb)<=width) {
  486. yg = xg + (1.f/ratio-1.f)*(xg-thresdb+width/2.f)*(xg-thresdb+width/2.f)/(2.f*width);
  487. } else if (2.f*(xg-thresdb)>width) {
  488. yg = thresdb + (xg-thresdb)/ratio;
  489. }
  490. yg = sanitize_denormal(yg);
  491. xl = xg - yg;
  492. old_y1[k] = sanitize_denormal(old_y1[k]);
  493. old_yl[k] = sanitize_denormal(old_yl[k]);
  494. y1 = fmaxf(xl, release_coeff * old_y1[k]+(1.f-release_coeff)*xl);
  495. yl = attack_coeff * old_yl[k]+(1.f-attack_coeff)*y1;
  496. y1 = sanitize_denormal(y1);
  497. yl = sanitize_denormal(yl);
  498. cdb = -yl;
  499. gain = from_dB(cdb);
  500. gainr[k] = yl;
  501. out = in;
  502. out *= gain;
  503. old_yl[k] = yl;
  504. old_y1[k] = y1;
  505. return sanitize_denormal(out);
  506. }
  507. void ZaMultiCompPlugin::d_run(const float** inputs, float** outputs, uint32_t frames)
  508. {
  509. float srate = d_getSampleRate();
  510. float max = 0.f;
  511. int tog1 = (toggle[0] > 0.5f) ? 1 : 0;
  512. int tog2 = (toggle[1] > 0.5f) ? 1 : 0;
  513. int tog3 = (toggle[2] > 0.5f) ? 1 : 0;
  514. int listen1 = (listen[0] > 0.5f) ? 1 : 0;
  515. int listen2 = (listen[1] > 0.5f) ? 1 : 0;
  516. int listen3 = (listen[2] > 0.5f) ? 1 : 0;
  517. set_lp_coeffs(xover1, ONEOVERROOT2, srate, 0);
  518. set_lp_coeffs(xover1, ONEOVERROOT2, srate, 1);
  519. set_hp_coeffs(xover1, ONEOVERROOT2, srate, 2);
  520. set_hp_coeffs(xover1, ONEOVERROOT2, srate, 3);
  521. set_lp_coeffs(xover2, ONEOVERROOT2, srate, 4);
  522. set_lp_coeffs(xover2, ONEOVERROOT2, srate, 5);
  523. set_hp_coeffs(xover2, ONEOVERROOT2, srate, 6);
  524. set_hp_coeffs(xover2, ONEOVERROOT2, srate, 7);
  525. for (uint32_t i = 0; i < frames; ++i) {
  526. float tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, fil1, fil2, fil3, fil4;
  527. float cmp1, cmp2, cmp3, in;
  528. int listenmode = 0;
  529. in = sanitize_denormal(inputs[0][i]);
  530. in = (fabs(in) < DANGER) ? in : 0.f;
  531. fil1 = run_filter(0, in);
  532. tmp1 = run_filter(1, fil1);
  533. cmp1 = (!tog1 && !listen1) ? 0.f : run_comp(0, tmp1);
  534. tmp2 = tog1 ? cmp1 * from_dB(makeup[0]) : tmp1;
  535. fil2 = run_filter(2, in);
  536. tmp3 = run_filter(3, fil2);
  537. fil3 = run_filter(4, tmp3);
  538. tmp4 = run_filter(5, fil3);
  539. cmp2 = (!tog2 && !listen2) ? 0.f : run_comp(1, tmp4);
  540. tmp3 = (tog2) ? cmp2 * from_dB(makeup[1]) : tmp4;
  541. fil4 = run_filter(6, in);
  542. tmp5 = run_filter(7, fil4);
  543. cmp3 = (!tog3 && !listen3) ? 0.f : run_comp(2, tmp5);
  544. tmp6 = (tog3) ? cmp3 * from_dB(makeup[2]) : tmp5;
  545. outputs[0][i] = 0.f;
  546. if (listen1) {
  547. listenmode = 1;
  548. outputs[0][i] += cmp1 * tog1*from_dB(makeup[0])
  549. + (1.-tog1) * tmp1;
  550. }
  551. if (listen2) {
  552. listenmode = 1;
  553. outputs[0][i] += cmp2 * tog2*from_dB(makeup[1])
  554. + (1.-tog2) * tmp4;
  555. }
  556. if (listen3) {
  557. listenmode = 1;
  558. outputs[0][i] += cmp3 * tog3*from_dB(makeup[2])
  559. + (1.-tog3) * tmp5;
  560. }
  561. if (!listenmode) {
  562. outputs[0][i] = 0.f;
  563. outputs[0][i] += tmp2;
  564. outputs[0][i] += tmp3;
  565. outputs[0][i] += tmp6;
  566. }
  567. outputs[0][i] = sanitize_denormal(outputs[0][i]);
  568. outputs[0][i] *= from_dB(globalgain);
  569. max = (fabsf(outputs[0][i]) > max) ? fabsf(outputs[0][i]) : sanitize_denormal(max);
  570. }
  571. outlevel = (max == 0.f) ? -45.f : to_dB(max);
  572. }
  573. // -----------------------------------------------------------------------
  574. Plugin* createPlugin()
  575. {
  576. return new ZaMultiCompPlugin();
  577. }
  578. // -----------------------------------------------------------------------
  579. END_NAMESPACE_DISTRHO