Reverb.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. Reverb.cc
  3. Copyright 2002-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. three reverb units: JVRev, Plate and Plate2x2.
  6. the former is a rewrite of STK's JVRev, a traditional design.
  7. original comment:
  8. This is based on some of the famous
  9. Stanford CCRMA reverbs (NRev, KipRev)
  10. all based on the Chowning/Moorer/
  11. Schroeder reverberators, which use
  12. networks of simple allpass and comb
  13. delay filters.
  14. the algorithm is mostly unchanged in this implementation; the delay
  15. line lengths have been fiddled with to make the stereo field more
  16. evenly weighted, and denormal protection has been added.
  17. the latter two are based on the circuit discussed in Jon Dattorro's
  18. september 1997 JAES paper on effect design (part 1: reverb & filters).
  19. */
  20. /*
  21. This program is free software; you can redistribute it and/or
  22. modify it under the terms of the GNU General Public License
  23. as published by the Free Software Foundation; either version 2
  24. of the License, or (at your option) any later version.
  25. This program is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. GNU General Public License for more details.
  29. You should have received a copy of the GNU General Public License
  30. along with this program; if not, write to the Free Software
  31. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  32. 02111-1307, USA or point your web browser to http://www.gnu.org.
  33. */
  34. #include "basics.h"
  35. #include "Reverb.h"
  36. #include "Descriptor.h"
  37. int
  38. JVRev::default_length[9] = {
  39. #if 1 /* slightly modified, tg */
  40. 1777, 1847, 1993, 2137, 389, 127, 43, 211, 209
  41. #else
  42. 4799, 4999, 5399, 5801, 1051, 337, 113, 573, 487
  43. #endif
  44. };
  45. void
  46. JVRev::init()
  47. {
  48. memcpy (length, default_length, sizeof (length));
  49. if (fs != 44100)
  50. {
  51. double s = fs / 44100.;
  52. for (int i = 0; i < 9; ++i)
  53. {
  54. int v = (int) (s * length[i]);
  55. v |= 1;
  56. while (!DSP::isprime (v))
  57. v += 2;
  58. length[i] = v;
  59. }
  60. }
  61. for (int i = 0; i < 4; ++i)
  62. comb[i].init (length[i]);
  63. for (int i = 0; i < 3; ++i)
  64. allpass[i].init (length[i+4]);
  65. left.init (length[7]);
  66. right.init (length[8]);
  67. /* such a simple number, but i couldn't find a better one. */
  68. apc = .7;
  69. }
  70. void
  71. JVRev::set_t60 (sample_t t)
  72. {
  73. t60 = t;
  74. t = max (.00001, t);
  75. for (int i = 0; i < 4; ++i)
  76. comb[i].c = pow (10, (-3 * length[i] / (t * fs)));
  77. }
  78. void
  79. JVRev::activate()
  80. {
  81. for (int i = 0; i < 3; ++i)
  82. allpass[i].reset();
  83. for (int i = 0; i < 4; ++i)
  84. comb[i].reset();
  85. left.reset();
  86. right.reset();
  87. set_t60 (getport(1));
  88. }
  89. template <sample_func_t F>
  90. void
  91. JVRev::one_cycle (int frames)
  92. {
  93. sample_t * s = ports[0];
  94. if (t60 != *ports[1])
  95. set_t60 (getport(1));
  96. double wet = getport(2), dry = 1 - wet;
  97. sample_t * dl = ports[3];
  98. sample_t * dr = ports[4];
  99. for (int i = 0; i < frames; ++i)
  100. {
  101. sample_t x = s[i], a = x + normal;
  102. x *= dry;
  103. /* diffusors */
  104. a = allpass[0].process (a, -apc);
  105. a = allpass[1].process (a, -apc);
  106. a = allpass[2].process (a, -apc);
  107. /* tank */
  108. sample_t t = 0;
  109. a -= normal;
  110. for (int j = 0; j < 4; ++j)
  111. t += comb[j].process (a);
  112. F (dl, i, x + wet * left.putget (t), adding_gain);
  113. F (dr, i, x + wet * right.putget (t), adding_gain);
  114. }
  115. }
  116. /* //////////////////////////////////////////////////////////////////////// */
  117. PortInfo
  118. JVRev::port_info [] =
  119. {
  120. {
  121. "in",
  122. INPUT | AUDIO,
  123. {BOUNDED, -1, 1}
  124. }, {
  125. "t60 (s)",
  126. INPUT | CONTROL,
  127. {BOUNDED | DEFAULT_MID, 0, 4.6}
  128. }, {
  129. "blend",
  130. INPUT | CONTROL,
  131. {BOUNDED | DEFAULT_LOW, 0, .28}
  132. }, {
  133. "out:l",
  134. OUTPUT | AUDIO,
  135. {0}
  136. }, {
  137. "out:r",
  138. OUTPUT | AUDIO,
  139. {0}
  140. }
  141. };
  142. template <> void
  143. Descriptor<JVRev>::setup()
  144. {
  145. UniqueID = 1778;
  146. Label = "JVRev";
  147. Properties = HARD_RT;
  148. Name = CAPS "JVRev - Stanford-style reverb from STK";
  149. Maker = "Tim Goetze <tim@quitte.de>";
  150. Copyright = "GPL, 2004-7";
  151. /* fill port info and vtable */
  152. autogen();
  153. }
  154. /* //////////////////////////////////////////////////////////////////////// */
  155. void
  156. PlateStub::init()
  157. {
  158. f_lfo = -1;
  159. # define L(i) ((int) (l[i] * fs))
  160. static float l[] = {
  161. 0.004771345048889486, 0.0035953092974026408,
  162. 0.01273478713752898, 0.0093074829474816042,
  163. 0.022579886428547427, 0.030509727495715868,
  164. 0.14962534861059779, 0.060481838647894894, 0.12499579987231611,
  165. 0.14169550754342933, 0.089244313027116023, 0.10628003091293972
  166. };
  167. /* lh */
  168. input.lattice[0].init (L(0));
  169. input.lattice[1].init (L(1));
  170. /* rh */
  171. input.lattice[2].init (L(2));
  172. input.lattice[3].init (L(3));
  173. /* modulated, width about 12 samples @ 44.1 */
  174. tank.mlattice[0].init (L(4), (int) (0.00040322707570310132 * fs));
  175. tank.mlattice[1].init (L(5), (int) (0.00040322707570310132 * fs));
  176. /* lh */
  177. tank.delay[0].init (L(6));
  178. tank.lattice[0].init (L(7));
  179. tank.delay[1].init (L(8));
  180. /* rh */
  181. tank.delay[2].init (L(9));
  182. tank.lattice[1].init (L(10));
  183. tank.delay[3].init (L(11));
  184. # undef L
  185. # define T(i) ((int) (t[i] * fs))
  186. static float t[] = {
  187. 0.0089378717113000241, 0.099929437854910791, 0.064278754074123853,
  188. 0.067067638856221232, 0.066866032727394914, 0.006283391015086859,
  189. 0.01186116057928161, 0.12187090487550822, 0.041262054366452743,
  190. 0.089815530392123921, 0.070931756325392295, 0.011256342192802662
  191. };
  192. for (int i = 0; i < 12; ++i)
  193. tank.taps[i] = T(i);
  194. # undef T
  195. /* tuned for soft attack, ambience */
  196. indiff1 = .742;
  197. indiff2 = .712;
  198. dediff1 = .723;
  199. dediff2 = .729;
  200. }
  201. inline void
  202. PlateStub::process (sample_t x, sample_t decay, sample_t * _xl, sample_t * _xr)
  203. {
  204. x = input.bandwidth.process (x);
  205. /* lh */
  206. x = input.lattice[0].process (x, indiff1);
  207. x = input.lattice[1].process (x, indiff1);
  208. /* rh */
  209. x = input.lattice[2].process (x, indiff2);
  210. x = input.lattice[3].process (x, indiff2);
  211. /* summation point */
  212. register sample_t xl = x + decay * tank.delay[3].get();
  213. register sample_t xr = x + decay * tank.delay[1].get();
  214. /* lh */
  215. xl = tank.mlattice[0].process (xl, dediff1);
  216. xl = tank.delay[0].putget (xl);
  217. xl = tank.damping[0].process (xl);
  218. xl *= decay;
  219. xl = tank.lattice[0].process (xl, dediff2);
  220. tank.delay[1].put (xl);
  221. /* rh */
  222. xr = tank.mlattice[1].process (xr, dediff1);
  223. xr = tank.delay[2].putget (xr);
  224. xr = tank.damping[1].process (xr);
  225. xr *= decay;
  226. xr = tank.lattice[1].process (xr, dediff2);
  227. tank.delay[3].put (xr);
  228. /* gather output */
  229. xl = .6 * tank.delay[2] [tank.taps[0]];
  230. xl += .6 * tank.delay[2] [tank.taps[1]];
  231. xl -= .6 * tank.lattice[1] [tank.taps[2]];
  232. xl += .6 * tank.delay[3] [tank.taps[3]];
  233. xl -= .6 * tank.delay[0] [tank.taps[4]];
  234. xl += .6 * tank.lattice[0] [tank.taps[5]];
  235. xr = .6 * tank.delay[0] [tank.taps[6]];
  236. xr += .6 * tank.delay[0] [tank.taps[7]];
  237. xr -= .6 * tank.lattice[0] [tank.taps[8]];
  238. xr += .6 * tank.delay[1] [tank.taps[9]];
  239. xr -= .6 * tank.delay[2] [tank.taps[10]];
  240. xr += .6 * tank.lattice[1] [tank.taps[11]];
  241. *_xl = xl;
  242. *_xr = xr;
  243. }
  244. /* //////////////////////////////////////////////////////////////////////// */
  245. template <sample_func_t F>
  246. void
  247. Plate::one_cycle (int frames)
  248. {
  249. sample_t * s = ports[0];
  250. input.bandwidth.set (exp (-M_PI * (1. - getport(1))));
  251. sample_t decay = getport(2);
  252. double damp = exp (-M_PI * getport(3));
  253. tank.damping[0].set (damp);
  254. tank.damping[1].set (damp);
  255. sample_t blend = getport(4), dry = 1 - blend;
  256. sample_t * dl = ports[5];
  257. sample_t * dr = ports[6];
  258. /* the modulated lattices interpolate, which needs truncated float */
  259. DSP::FPTruncateMode _truncate;
  260. for (int i = 0; i < frames; ++i)
  261. {
  262. normal = -normal;
  263. sample_t x = s[i] + normal;
  264. sample_t xl, xr;
  265. PlateStub::process (x, decay, &xl, &xr);
  266. x = dry * s[i];
  267. F (dl, i, x + blend * xl, adding_gain);
  268. F (dr, i, x + blend * xr, adding_gain);
  269. }
  270. }
  271. /* //////////////////////////////////////////////////////////////////////// */
  272. PortInfo
  273. Plate::port_info [] =
  274. {
  275. {
  276. "in",
  277. INPUT | AUDIO,
  278. {BOUNDED, -1, 1}
  279. }, {
  280. "bandwidth",
  281. INPUT | CONTROL,
  282. {BOUNDED | DEFAULT_MID, 0.005, .999} /* .9995 */
  283. }, {
  284. "tail",
  285. INPUT | CONTROL,
  286. {BOUNDED | DEFAULT_MID, 0, .749} /* .5 */
  287. }, {
  288. "damping",
  289. INPUT | CONTROL,
  290. {BOUNDED | DEFAULT_LOW, .0005, 1} /* .0005 */
  291. }, {
  292. "blend",
  293. INPUT | CONTROL,
  294. {BOUNDED | DEFAULT_LOW, 0, 1}
  295. }, {
  296. "out:l",
  297. OUTPUT | AUDIO,
  298. {0}
  299. }, {
  300. "out:r",
  301. OUTPUT | AUDIO,
  302. {0}
  303. }
  304. };
  305. template <> void
  306. Descriptor<Plate>::setup()
  307. {
  308. UniqueID = 1779;
  309. Label = "Plate";
  310. Properties = HARD_RT;
  311. Name = CAPS "Plate - Versatile plate reverb";
  312. Maker = "Tim Goetze <tim@quitte.de>";
  313. Copyright = "GPL, 2004-7";
  314. /* fill port info and vtable */
  315. autogen();
  316. }
  317. /* //////////////////////////////////////////////////////////////////////// */
  318. template <sample_func_t F>
  319. void
  320. Plate2x2::one_cycle (int frames)
  321. {
  322. sample_t * sl = ports[0];
  323. sample_t * sr = ports[1];
  324. input.bandwidth.set (exp (-M_PI * (1. - getport(2))));
  325. sample_t decay = getport(3);
  326. double damp = exp (-M_PI * getport(4));
  327. tank.damping[0].set (damp);
  328. tank.damping[1].set (damp);
  329. sample_t blend = getport(5), dry = 1 - blend;
  330. sample_t * dl = ports[6];
  331. sample_t * dr = ports[7];
  332. /* the modulated lattices interpolate, which needs truncated float */
  333. DSP::FPTruncateMode _truncate;
  334. for (int i = 0; i < frames; ++i)
  335. {
  336. normal = -normal;
  337. sample_t x = (sl[i] + sr[i] + normal) * .5;
  338. sample_t xl, xr;
  339. PlateStub::process (x, decay, &xl, &xr);
  340. xl = blend * xl + dry * sl[i];
  341. xr = blend * xr + dry * sr[i];
  342. F (dl, i, xl, adding_gain);
  343. F (dr, i, xr, adding_gain);
  344. }
  345. }
  346. /* //////////////////////////////////////////////////////////////////////// */
  347. PortInfo
  348. Plate2x2::port_info [] =
  349. {
  350. {
  351. "in:l",
  352. INPUT | AUDIO,
  353. {BOUNDED, -1, 1}
  354. }, {
  355. "in:r",
  356. INPUT | AUDIO,
  357. {BOUNDED, -1, 1}
  358. }, {
  359. "bandwidth",
  360. INPUT | CONTROL,
  361. {BOUNDED | DEFAULT_MID, 0.005, .999} /* .9995 */
  362. }, {
  363. "tail",
  364. INPUT | CONTROL,
  365. {BOUNDED | DEFAULT_MID, 0, .749} /* .5 */
  366. }, {
  367. "damping",
  368. INPUT | CONTROL,
  369. {BOUNDED | DEFAULT_LOW, .0005, 1} /* .0005 */
  370. }, {
  371. "blend",
  372. INPUT | CONTROL,
  373. {BOUNDED | DEFAULT_LOW, 0, 1}
  374. }, {
  375. "out:l",
  376. OUTPUT | AUDIO,
  377. {0}
  378. }, {
  379. "out:r",
  380. OUTPUT | AUDIO,
  381. {0}
  382. }
  383. };
  384. template <> void
  385. Descriptor<Plate2x2>::setup()
  386. {
  387. UniqueID = 1795;
  388. Label = "Plate2x2";
  389. Properties = HARD_RT;
  390. Name = CAPS "Plate2x2 - Versatile plate reverb, stereo inputs";
  391. Maker = "Tim Goetze <tim@quitte.de>";
  392. Copyright = "GPL, 2004-7";
  393. /* fill port info and vtable */
  394. autogen();
  395. }