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.

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