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.

reverb.cc 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // -----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2003-2010 Fons Adriaensen <fons@linuxaudio.org>
  4. // Modified by falkTX on Jan 2015 for inclusion in Carla
  5. //
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. //
  20. // -----------------------------------------------------------------------
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include "reverb.h"
  25. namespace REV1 {
  26. // -----------------------------------------------------------------------
  27. Diff1::Diff1 (void) :
  28. _size (0),
  29. _line (0)
  30. {
  31. }
  32. Diff1::~Diff1 (void)
  33. {
  34. fini ();
  35. }
  36. void Diff1::init (int size, float c)
  37. {
  38. _size = size;
  39. _line = new float [size];
  40. memset (_line, 0, size * sizeof (float));
  41. _i = 0;
  42. _c = c;
  43. }
  44. void Diff1::fini (void)
  45. {
  46. delete[] _line;
  47. _size = 0;
  48. _line = 0;
  49. }
  50. // -----------------------------------------------------------------------
  51. Delay::Delay (void) :
  52. _size (0),
  53. _line (0)
  54. {
  55. }
  56. Delay::~Delay (void)
  57. {
  58. fini ();
  59. }
  60. void Delay::init (int size)
  61. {
  62. _size = size;
  63. _line = new float [size];
  64. memset (_line, 0, size * sizeof (float));
  65. _i = 0;
  66. }
  67. void Delay::fini (void)
  68. {
  69. delete[] _line;
  70. _size = 0;
  71. _line = 0;
  72. }
  73. // -----------------------------------------------------------------------
  74. Vdelay::Vdelay (void) :
  75. _size (0),
  76. _line (0)
  77. {
  78. }
  79. Vdelay::~Vdelay (void)
  80. {
  81. fini ();
  82. }
  83. void Vdelay::init (int size)
  84. {
  85. _size = size;
  86. _line = new float [size];
  87. memset (_line, 0, size * sizeof (float));
  88. _ir = 0;
  89. _iw = 0;
  90. }
  91. void Vdelay::fini (void)
  92. {
  93. delete[] _line;
  94. _size = 0;
  95. _line = 0;
  96. }
  97. void Vdelay::set_delay (int del)
  98. {
  99. _ir = _iw - del;
  100. if (_ir < 0) _ir += _size;
  101. }
  102. // -----------------------------------------------------------------------
  103. void Filt1::set_params (float del, float tmf, float tlo, float wlo, float thi, float chi)
  104. {
  105. float g, t;
  106. _gmf = powf (0.001f, del / tmf);
  107. _glo = powf (0.001f, del / tlo) / _gmf - 1.0f;
  108. _wlo = wlo;
  109. g = powf (0.001f, del / thi) / _gmf;
  110. t = (1 - g * g) / (2 * g * g * chi);
  111. _whi = (sqrtf (1 + 4 * t) - 1) / (2 * t);
  112. }
  113. // -----------------------------------------------------------------------
  114. float Reverb::_tdiff1 [8] =
  115. {
  116. 20346e-6f,
  117. 24421e-6f,
  118. 31604e-6f,
  119. 27333e-6f,
  120. 22904e-6f,
  121. 29291e-6f,
  122. 13458e-6f,
  123. 19123e-6f
  124. };
  125. float Reverb::_tdelay [8] =
  126. {
  127. 153129e-6f,
  128. 210389e-6f,
  129. 127837e-6f,
  130. 256891e-6f,
  131. 174713e-6f,
  132. 192303e-6f,
  133. 125000e-6f,
  134. 219991e-6f
  135. };
  136. Reverb::Reverb (void)
  137. {
  138. }
  139. Reverb::~Reverb (void)
  140. {
  141. fini ();
  142. }
  143. void Reverb::init (float fsamp, bool ambis)
  144. {
  145. int i, k1, k2;
  146. _fsamp = fsamp;
  147. _ambis = ambis;
  148. _cntA1 = 1;
  149. _cntA2 = 0;
  150. _cntB1 = 1;
  151. _cntB2 = 0;
  152. _cntC1 = 1;
  153. _cntC2 = 0;
  154. _ipdel = 0.04f;
  155. _xover = 200.0f;
  156. _rtlow = 3.0f;
  157. _rtmid = 2.0f;
  158. _fdamp = 3e3f;
  159. _opmix = 0.5f;
  160. _rgxyz = 0.0f;
  161. _g0 = _d0 = 0;
  162. _g1 = _d1 = 0;
  163. _vdelay0.init ((int)(0.1f * _fsamp));
  164. _vdelay1.init ((int)(0.1f * _fsamp));
  165. for (i = 0; i < 8; i++)
  166. {
  167. k1 = (int)(floorf (_tdiff1 [i] * _fsamp + 0.5f));
  168. k2 = (int)(floorf (_tdelay [i] * _fsamp + 0.5f));
  169. _diff1 [i].init (k1, (i & 1) ? -0.6f : 0.6f);
  170. _delay [i].init (k2 - k1);
  171. }
  172. _pareq1.setfsamp (fsamp);
  173. _pareq2.setfsamp (fsamp);
  174. }
  175. void Reverb::fini (void)
  176. {
  177. for (int i = 0; i < 8; i++) _delay [i].fini ();
  178. }
  179. void Reverb::prepare (int nfram)
  180. {
  181. int a, b, c, i, k;
  182. float t0, t1, wlo, chi;
  183. a = _cntA1;
  184. b = _cntB1;
  185. c = _cntC1;
  186. _d0 = _d1 = 0;
  187. if (a != _cntA2)
  188. {
  189. k = (int)(floorf ((_ipdel - 0.020f) * _fsamp + 0.5f));
  190. _vdelay0.set_delay (k);
  191. _vdelay1.set_delay (k);
  192. _cntA2 = a;
  193. }
  194. if (b != _cntB2)
  195. {
  196. wlo = 6.2832f * _xover / _fsamp;
  197. if (_fdamp > 0.49f * _fsamp) chi = 2;
  198. else chi = 1 - cosf (6.2832f * _fdamp / _fsamp);
  199. for (i = 0; i < 8; i++)
  200. {
  201. _filt1 [i].set_params (_tdelay [i], _rtmid, _rtlow, wlo, 0.5f * _rtmid, chi);
  202. }
  203. _cntB2 = b;
  204. }
  205. if (c != _cntC2)
  206. {
  207. if (_ambis)
  208. {
  209. t0 = 1.0f / sqrtf (_rtmid);
  210. t1 = t0 * powf (10.0f, 0.05f * _rgxyz);
  211. }
  212. else
  213. {
  214. t0 = (1 - _opmix) * (1 + _opmix);
  215. t1 = 0.7f * _opmix * (2 - _opmix) / sqrtf (_rtmid);
  216. }
  217. _d0 = (t0 - _g0) / nfram;
  218. _d1 = (t1 - _g1) / nfram;
  219. _cntC2 = c;
  220. }
  221. _pareq1.prepare (nfram);
  222. _pareq2.prepare (nfram);
  223. }
  224. void Reverb::process (int nfram, float *inp [], float *out [])
  225. {
  226. int i, n;
  227. float *p0, *p1;
  228. float *q0, *q1, *q2, *q3;
  229. float t, g, x0, x1, x2, x3, x4, x5, x6, x7;
  230. g = sqrtf (0.125f);
  231. p0 = inp [0];
  232. p1 = inp [1];
  233. q0 = out [0];
  234. q1 = out [1];
  235. q2 = out [2];
  236. q3 = out [3];
  237. for (i = 0; i < nfram; i++)
  238. {
  239. _vdelay0.write (p0 [i]);
  240. _vdelay1.write (p1 [i]);
  241. t = 0.3f * _vdelay0.read ();
  242. x0 = _diff1 [0].process (_delay [0].read () + t);
  243. x1 = _diff1 [1].process (_delay [1].read () + t);
  244. x2 = _diff1 [2].process (_delay [2].read () - t);
  245. x3 = _diff1 [3].process (_delay [3].read () - t);
  246. t = 0.3f * _vdelay1.read ();
  247. x4 = _diff1 [4].process (_delay [4].read () + t);
  248. x5 = _diff1 [5].process (_delay [5].read () + t);
  249. x6 = _diff1 [6].process (_delay [6].read () - t);
  250. x7 = _diff1 [7].process (_delay [7].read () - t);
  251. t = x0 - x1; x0 += x1; x1 = t;
  252. t = x2 - x3; x2 += x3; x3 = t;
  253. t = x4 - x5; x4 += x5; x5 = t;
  254. t = x6 - x7; x6 += x7; x7 = t;
  255. t = x0 - x2; x0 += x2; x2 = t;
  256. t = x1 - x3; x1 += x3; x3 = t;
  257. t = x4 - x6; x4 += x6; x6 = t;
  258. t = x5 - x7; x5 += x7; x7 = t;
  259. t = x0 - x4; x0 += x4; x4 = t;
  260. t = x1 - x5; x1 += x5; x5 = t;
  261. t = x2 - x6; x2 += x6; x6 = t;
  262. t = x3 - x7; x3 += x7; x7 = t;
  263. if (_ambis)
  264. {
  265. _g0 += _d0;
  266. _g1 += _d1;
  267. q0 [i] = _g0 * x0;
  268. q1 [i] = _g1 * x1;
  269. q2 [i] = _g1 * x4;
  270. q3 [i] = _g1 * x2;
  271. }
  272. else
  273. {
  274. _g1 += _d1;
  275. q0 [i] = _g1 * (x1 + x2);
  276. q1 [i] = _g1 * (x1 - x2);
  277. }
  278. _delay [0].write (_filt1 [0].process (g * x0));
  279. _delay [1].write (_filt1 [1].process (g * x1));
  280. _delay [2].write (_filt1 [2].process (g * x2));
  281. _delay [3].write (_filt1 [3].process (g * x3));
  282. _delay [4].write (_filt1 [4].process (g * x4));
  283. _delay [5].write (_filt1 [5].process (g * x5));
  284. _delay [6].write (_filt1 [6].process (g * x6));
  285. _delay [7].write (_filt1 [7].process (g * x7));
  286. }
  287. n = _ambis ? 4 : 2;
  288. _pareq1.process (nfram, n, out);
  289. _pareq2.process (nfram, n, out);
  290. if (!_ambis)
  291. {
  292. for (i = 0; i < nfram; i++)
  293. {
  294. _g0 += _d0;
  295. q0 [i] += _g0 * p0 [i];
  296. q1 [i] += _g0 * p1 [i];
  297. }
  298. }
  299. }
  300. // -----------------------------------------------------------------------
  301. }