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.

488 lines
13KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Reverb.cpp - Reverberation effect
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "Reverb.h"
  18. #include "../Misc/Util.h"
  19. #include "../Misc/Allocator.h"
  20. #include "../DSP/AnalogFilter.h"
  21. #include "../DSP/Unison.h"
  22. #include <cmath>
  23. Reverb::Reverb(EffectParams pars)
  24. :Effect(pars),
  25. // defaults
  26. Pvolume(48),
  27. Ptime(64),
  28. Pidelay(40),
  29. Pidelayfb(0),
  30. Plpf(127),
  31. Phpf(0),
  32. Plohidamp(80),
  33. Ptype(1),
  34. Proomsize(64),
  35. Pbandwidth(30),
  36. idelaylen(0),
  37. roomsize(1.0f),
  38. rs(1.0f),
  39. bandwidth(NULL),
  40. idelay(NULL),
  41. lpf(NULL),
  42. hpf(NULL) // no filter
  43. {
  44. for(int i = 0; i < REV_COMBS * 2; ++i) {
  45. comblen[i] = 800 + (int)(RND * 1400.0f);
  46. combk[i] = 0;
  47. lpcomb[i] = 0;
  48. combfb[i] = -0.97f;
  49. comb[i] = NULL;
  50. }
  51. for(int i = 0; i < REV_APS * 2; ++i) {
  52. aplen[i] = 500 + (int)(RND * 500.0f);
  53. apk[i] = 0;
  54. ap[i] = NULL;
  55. }
  56. setpreset(Ppreset);
  57. cleanup(); //do not call this before the comb initialisation
  58. }
  59. Reverb::~Reverb()
  60. {
  61. memory.devalloc(idelay);
  62. memory.dealloc(hpf);
  63. memory.dealloc(lpf);
  64. for(int i = 0; i < REV_APS * 2; ++i)
  65. memory.devalloc(ap[i]);
  66. for(int i = 0; i < REV_COMBS * 2; ++i)
  67. memory.devalloc(comb[i]);
  68. memory.dealloc(bandwidth);
  69. }
  70. //Cleanup the effect
  71. void Reverb::cleanup(void)
  72. {
  73. for(int i = 0; i < REV_COMBS * 2; ++i) {
  74. lpcomb[i] = 0.0f;
  75. for(int j = 0; j < comblen[i]; ++j)
  76. comb[i][j] = 0.0f;
  77. }
  78. for(int i = 0; i < REV_APS * 2; ++i)
  79. for(int j = 0; j < aplen[i]; ++j)
  80. ap[i][j] = 0.0f;
  81. if(idelay)
  82. for(int i = 0; i < idelaylen; ++i)
  83. idelay[i] = 0.0f;
  84. if(hpf)
  85. hpf->cleanup();
  86. if(lpf)
  87. lpf->cleanup();
  88. }
  89. //Process one channel; 0=left, 1=right
  90. void Reverb::processmono(int ch, float *output, float *inputbuf)
  91. {
  92. //todo: implement the high part from lohidamp
  93. for(int j = REV_COMBS * ch; j < REV_COMBS * (ch + 1); ++j) {
  94. int &ck = combk[j];
  95. const int comblength = comblen[j];
  96. float &lpcombj = lpcomb[j];
  97. for(int i = 0; i < buffersize; ++i) {
  98. float fbout = comb[j][ck] * combfb[j];
  99. fbout = fbout * (1.0f - lohifb) + lpcombj * lohifb;
  100. lpcombj = fbout;
  101. comb[j][ck] = inputbuf[i] + fbout;
  102. output[i] += fbout;
  103. if((++ck) >= comblength)
  104. ck = 0;
  105. }
  106. }
  107. for(int j = REV_APS * ch; j < REV_APS * (1 + ch); ++j) {
  108. int &ak = apk[j];
  109. const int aplength = aplen[j];
  110. for(int i = 0; i < buffersize; ++i) {
  111. float tmp = ap[j][ak];
  112. ap[j][ak] = 0.7f * tmp + output[i];
  113. output[i] = tmp - 0.7f * ap[j][ak];
  114. if((++ak) >= aplength)
  115. ak = 0;
  116. }
  117. }
  118. }
  119. //Effect output
  120. void Reverb::out(const Stereo<float *> &smp)
  121. {
  122. if(!Pvolume && insertion)
  123. return;
  124. float inputbuf[buffersize];
  125. for(int i = 0; i < buffersize; ++i)
  126. inputbuf[i] = (smp.l[i] + smp.r[i]) / 2.0f;
  127. if(idelay)
  128. for(int i = 0; i < buffersize; ++i) {
  129. //Initial delay r
  130. float tmp = inputbuf[i] + idelay[idelayk] * idelayfb;
  131. inputbuf[i] = idelay[idelayk];
  132. idelay[idelayk] = tmp;
  133. idelayk++;
  134. if(idelayk >= idelaylen)
  135. idelayk = 0;
  136. }
  137. if(bandwidth)
  138. bandwidth->process(buffersize, inputbuf);
  139. if(lpf)
  140. lpf->filterout(inputbuf);
  141. if(hpf)
  142. hpf->filterout(inputbuf);
  143. processmono(0, efxoutl, inputbuf); //left
  144. processmono(1, efxoutr, inputbuf); //right
  145. float lvol = rs / REV_COMBS * pangainL;
  146. float rvol = rs / REV_COMBS * pangainR;
  147. if(insertion != 0) {
  148. lvol *= 2.0f;
  149. rvol *= 2.0f;
  150. }
  151. for(int i = 0; i < buffersize; ++i) {
  152. efxoutl[i] *= lvol;
  153. efxoutr[i] *= rvol;
  154. }
  155. }
  156. //Parameter control
  157. void Reverb::setvolume(unsigned char _Pvolume)
  158. {
  159. Pvolume = _Pvolume;
  160. if(!insertion) {
  161. outvolume = powf(0.01f, (1.0f - Pvolume / 127.0f)) * 4.0f;
  162. volume = 1.0f;
  163. }
  164. else {
  165. volume = outvolume = Pvolume / 127.0f;
  166. if(Pvolume == 0)
  167. cleanup();
  168. }
  169. }
  170. void Reverb::settime(unsigned char _Ptime)
  171. {
  172. Ptime = _Ptime;
  173. float t = powf(60.0f, Ptime / 127.0f) - 0.97f;
  174. for(int i = 0; i < REV_COMBS * 2; ++i)
  175. combfb[i] =
  176. -expf((float)comblen[i] / samplerate_f * logf(0.001f) / t);
  177. //the feedback is negative because it removes the DC
  178. }
  179. void Reverb::setlohidamp(unsigned char _Plohidamp)
  180. {
  181. Plohidamp = (_Plohidamp < 64) ? 64 : _Plohidamp;
  182. //remove this when the high part from lohidamp is added
  183. if(Plohidamp == 64) {
  184. lohidamptype = 0;
  185. lohifb = 0.0f;
  186. }
  187. else {
  188. if(Plohidamp < 64)
  189. lohidamptype = 1;
  190. if(Plohidamp > 64)
  191. lohidamptype = 2;
  192. float x = fabsf((float)(Plohidamp - 64) / 64.1f);
  193. lohifb = x * x;
  194. }
  195. }
  196. void Reverb::setidelay(unsigned char _Pidelay)
  197. {
  198. Pidelay = _Pidelay;
  199. float delay = powf(50.0f * Pidelay / 127.0f, 2.0f) - 1.0f;
  200. int newDelayLen = (int) (samplerate_f * delay / 1000);
  201. if(newDelayLen == idelaylen)
  202. return;
  203. memory.devalloc(idelay);
  204. idelaylen = newDelayLen;
  205. if(idelaylen > 1) {
  206. idelayk = 0;
  207. idelay = memory.valloc<float>(idelaylen);
  208. memset(idelay, 0, idelaylen * sizeof(float));
  209. }
  210. }
  211. void Reverb::setidelayfb(unsigned char _Pidelayfb)
  212. {
  213. Pidelayfb = _Pidelayfb;
  214. idelayfb = Pidelayfb / 128.0f;
  215. }
  216. void Reverb::sethpf(unsigned char _Phpf)
  217. {
  218. Phpf = _Phpf;
  219. if(Phpf == 0) { //No HighPass
  220. memory.dealloc(hpf);
  221. } else {
  222. float fr = expf(sqrtf(Phpf / 127.0f) * logf(10000.0f)) + 20.0f;
  223. if(hpf == NULL)
  224. hpf = memory.alloc<AnalogFilter>(3, fr, 1, 0, samplerate, buffersize);
  225. else
  226. hpf->setfreq(fr);
  227. }
  228. }
  229. void Reverb::setlpf(unsigned char _Plpf)
  230. {
  231. Plpf = _Plpf;
  232. if(Plpf == 127) { //No LowPass
  233. memory.dealloc(lpf);
  234. } else {
  235. float fr = expf(sqrtf(Plpf / 127.0f) * logf(25000.0f)) + 40.0f;
  236. if(!lpf)
  237. lpf = memory.alloc<AnalogFilter>(2, fr, 1, 0, samplerate, buffersize);
  238. else
  239. lpf->setfreq(fr);
  240. }
  241. }
  242. void Reverb::settype(unsigned char _Ptype)
  243. {
  244. Ptype = _Ptype;
  245. const int NUM_TYPES = 3;
  246. const int combtunings[NUM_TYPES][REV_COMBS] = {
  247. //this is unused (for random)
  248. {0, 0, 0, 0, 0, 0, 0, 0 },
  249. //Freeverb by Jezar at Dreampoint
  250. {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 },
  251. //duplicate of Freeverb by Jezar at Dreampoint
  252. {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 }
  253. };
  254. const int aptunings[NUM_TYPES][REV_APS] = {
  255. //this is unused (for random)
  256. {0, 0, 0, 0 },
  257. //Freeverb by Jezar at Dreampoint
  258. {225, 341, 441, 556 },
  259. //duplicate of Freeverb by Jezar at Dreampoint
  260. {225, 341, 441, 556 }
  261. };
  262. if(Ptype >= NUM_TYPES)
  263. Ptype = NUM_TYPES - 1;
  264. // adjust the combs according to the samplerate
  265. float samplerate_adjust = samplerate_f / 44100.0f;
  266. float tmp;
  267. for(int i = 0; i < REV_COMBS * 2; ++i) {
  268. if(Ptype == 0)
  269. tmp = 800.0f + (int)(RND * 1400.0f);
  270. else
  271. tmp = combtunings[Ptype][i % REV_COMBS];
  272. tmp *= roomsize;
  273. if(i > REV_COMBS)
  274. tmp += 23.0f;
  275. tmp *= samplerate_adjust; //adjust the combs according to the samplerate
  276. if(tmp < 10.0f)
  277. tmp = 10.0f;
  278. combk[i] = 0;
  279. lpcomb[i] = 0;
  280. if(comblen[i] != (int)tmp || comb[i] == NULL) {
  281. comblen[i] = (int) tmp;
  282. memory.devalloc(comb[i]);
  283. comb[i] = memory.valloc<float>(comblen[i]);
  284. }
  285. }
  286. for(int i = 0; i < REV_APS * 2; ++i) {
  287. if(Ptype == 0)
  288. tmp = 500 + (int)(RND * 500.0f);
  289. else
  290. tmp = aptunings[Ptype][i % REV_APS];
  291. tmp *= roomsize;
  292. if(i > REV_APS)
  293. tmp += 23.0f;
  294. tmp *= samplerate_adjust; //adjust the combs according to the samplerate
  295. if(tmp < 10)
  296. tmp = 10;
  297. apk[i] = 0;
  298. if(aplen[i] != (int)tmp || ap[i] == NULL) {
  299. aplen[i] = (int) tmp;
  300. memory.devalloc(ap[i]);
  301. ap[i] = memory.valloc<float>(aplen[i]);
  302. }
  303. }
  304. memory.dealloc(bandwidth);
  305. if(Ptype == 2) { //bandwidth
  306. //TODO the size of the unison buffer may be too small, though this has
  307. //not been verified yet.
  308. //As this cannot be resized in a RT context, a good upper bound should
  309. //be found
  310. bandwidth = memory.alloc<Unison>(&memory, buffersize / 4 + 1, 2.0f, samplerate_f);
  311. bandwidth->setSize(50);
  312. bandwidth->setBaseFrequency(1.0f);
  313. }
  314. settime(Ptime);
  315. cleanup();
  316. }
  317. void Reverb::setroomsize(unsigned char _Proomsize)
  318. {
  319. Proomsize = _Proomsize;
  320. if(!Proomsize)
  321. this->Proomsize = 64; //this is because the older versions consider roomsize=0
  322. roomsize = (this->Proomsize - 64.0f) / 64.0f;
  323. if(roomsize > 0.0f)
  324. roomsize *= 2.0f;
  325. roomsize = powf(10.0f, roomsize);
  326. rs = sqrtf(roomsize);
  327. settype(Ptype);
  328. }
  329. void Reverb::setbandwidth(unsigned char _Pbandwidth)
  330. {
  331. Pbandwidth = _Pbandwidth;
  332. float v = Pbandwidth / 127.0f;
  333. if(bandwidth)
  334. bandwidth->setBandwidth(powf(v, 2.0f) * 200.0f);
  335. }
  336. void Reverb::setpreset(unsigned char npreset)
  337. {
  338. const int PRESET_SIZE = 13;
  339. const int NUM_PRESETS = 13;
  340. unsigned char presets[NUM_PRESETS][PRESET_SIZE] = {
  341. //Cathedral1
  342. {80, 64, 63, 24, 0, 0, 0, 85, 5, 83, 1, 64, 20},
  343. //Cathedral2
  344. {80, 64, 69, 35, 0, 0, 0, 127, 0, 71, 0, 64, 20},
  345. //Cathedral3
  346. {80, 64, 69, 24, 0, 0, 0, 127, 75, 78, 1, 85, 20},
  347. //Hall1
  348. {90, 64, 51, 10, 0, 0, 0, 127, 21, 78, 1, 64, 20},
  349. //Hall2
  350. {90, 64, 53, 20, 0, 0, 0, 127, 75, 71, 1, 64, 20},
  351. //Room1
  352. {100, 64, 33, 0, 0, 0, 0, 127, 0, 106, 0, 30, 20},
  353. //Room2
  354. {100, 64, 21, 26, 0, 0, 0, 62, 0, 77, 1, 45, 20},
  355. //Basement
  356. {110, 64, 14, 0, 0, 0, 0, 127, 5, 71, 0, 25, 20},
  357. //Tunnel
  358. {85, 80, 84, 20, 42, 0, 0, 51, 0, 78, 1, 105, 20},
  359. //Echoed1
  360. {95, 64, 26, 60, 71, 0, 0, 114, 0, 64, 1, 64, 20},
  361. //Echoed2
  362. {90, 64, 40, 88, 71, 0, 0, 114, 0, 88, 1, 64, 20},
  363. //VeryLong1
  364. {90, 64, 93, 15, 0, 0, 0, 114, 0, 77, 0, 95, 20},
  365. //VeryLong2
  366. {90, 64, 111, 30, 0, 0, 0, 114, 90, 74, 1, 80, 20}
  367. };
  368. if(npreset >= NUM_PRESETS)
  369. npreset = NUM_PRESETS - 1;
  370. for(int n = 0; n < PRESET_SIZE; ++n)
  371. changepar(n, presets[npreset][n]);
  372. if(insertion)
  373. changepar(0, presets[npreset][0] / 2); //lower the volume if reverb is insertion effect
  374. Ppreset = npreset;
  375. }
  376. void Reverb::changepar(int npar, unsigned char value)
  377. {
  378. switch(npar) {
  379. case 0:
  380. setvolume(value);
  381. break;
  382. case 1:
  383. setpanning(value);
  384. break;
  385. case 2:
  386. settime(value);
  387. break;
  388. case 3:
  389. setidelay(value);
  390. break;
  391. case 4:
  392. setidelayfb(value);
  393. break;
  394. // case 5:
  395. // setrdelay(value);
  396. // break;
  397. // case 6:
  398. // seterbalance(value);
  399. // break;
  400. case 7:
  401. setlpf(value);
  402. break;
  403. case 8:
  404. sethpf(value);
  405. break;
  406. case 9:
  407. setlohidamp(value);
  408. break;
  409. case 10:
  410. settype(value);
  411. break;
  412. case 11:
  413. setroomsize(value);
  414. break;
  415. case 12:
  416. setbandwidth(value);
  417. break;
  418. }
  419. }
  420. unsigned char Reverb::getpar(int npar) const
  421. {
  422. switch(npar) {
  423. case 0: return Pvolume;
  424. case 1: return Ppanning;
  425. case 2: return Ptime;
  426. case 3: return Pidelay;
  427. case 4: return Pidelayfb;
  428. case 7: return Plpf;
  429. case 8: return Phpf;
  430. case 9: return Plohidamp;
  431. case 10: return Ptype;
  432. case 11: return Proomsize;
  433. case 12: return Pbandwidth;
  434. default: return 0;
  435. }
  436. }