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.

492 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. Reverb::Reverb(bool insertion_, float *efxoutl_, float *efxoutr_, unsigned int srate, int bufsize)
  23. :Effect(insertion_, efxoutl_, efxoutr_, NULL, 0, srate, bufsize),
  24. // defaults
  25. Pvolume(48),
  26. Ptime(64),
  27. Pidelay(40),
  28. Pidelayfb(0),
  29. Plpf(127),
  30. Phpf(0),
  31. Plohidamp(80),
  32. Ptype(1),
  33. Proomsize(64),
  34. Pbandwidth(30),
  35. roomsize(1.0f),
  36. rs(1.0f),
  37. bandwidth(NULL),
  38. idelay(NULL),
  39. lpf(NULL),
  40. hpf(NULL) // no filter
  41. {
  42. for(int i = 0; i < REV_COMBS * 2; ++i) {
  43. comblen[i] = 800 + (int)(RND * 1400.0f);
  44. combk[i] = 0;
  45. lpcomb[i] = 0;
  46. combfb[i] = -0.97f;
  47. comb[i] = NULL;
  48. }
  49. for(int i = 0; i < REV_APS * 2; ++i) {
  50. aplen[i] = 500 + (int)(RND * 500.0f);
  51. apk[i] = 0;
  52. ap[i] = NULL;
  53. }
  54. setpreset(Ppreset);
  55. cleanup(); //do not call this before the comb initialisation
  56. }
  57. Reverb::~Reverb()
  58. {
  59. delete [] idelay;
  60. delete hpf;
  61. delete lpf;
  62. for(int i = 0; i < REV_APS * 2; ++i)
  63. delete [] ap[i];
  64. for(int i = 0; i < REV_COMBS * 2; ++i)
  65. delete [] comb[i];
  66. if(bandwidth)
  67. delete bandwidth;
  68. }
  69. //Cleanup the effect
  70. void Reverb::cleanup(void)
  71. {
  72. int i, j;
  73. for(i = 0; i < REV_COMBS * 2; ++i) {
  74. lpcomb[i] = 0.0f;
  75. for(j = 0; j < comblen[i]; ++j)
  76. comb[i][j] = 0.0f;
  77. }
  78. for(i = 0; i < REV_APS * 2; ++i)
  79. for(j = 0; j < aplen[i]; ++j)
  80. ap[i][j] = 0.0f;
  81. if(idelay)
  82. for(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. if(idelay)
  201. delete [] idelay;
  202. idelay = NULL;
  203. idelaylen = (int) (samplerate_f * delay / 1000);
  204. if(idelaylen > 1) {
  205. idelayk = 0;
  206. idelay = new float[idelaylen];
  207. memset(idelay, 0, idelaylen * sizeof(float));
  208. }
  209. }
  210. void Reverb::setidelayfb(unsigned char _Pidelayfb)
  211. {
  212. Pidelayfb = _Pidelayfb;
  213. idelayfb = Pidelayfb / 128.0f;
  214. }
  215. void Reverb::sethpf(unsigned char _Phpf)
  216. {
  217. Phpf = _Phpf;
  218. if(Phpf == 0) { //No HighPass
  219. if(hpf)
  220. delete hpf;
  221. hpf = NULL;
  222. }
  223. else {
  224. float fr = expf(powf(Phpf / 127.0f, 0.5f) * logf(10000.0f)) + 20.0f;
  225. if(hpf == NULL)
  226. hpf = new AnalogFilter(3, fr, 1, 0, samplerate, buffersize);
  227. else
  228. hpf->setfreq(fr);
  229. }
  230. }
  231. void Reverb::setlpf(unsigned char _Plpf)
  232. {
  233. Plpf = _Plpf;
  234. if(Plpf == 127) { //No LowPass
  235. if(lpf)
  236. delete lpf;
  237. lpf = NULL;
  238. }
  239. else {
  240. float fr = expf(powf(Plpf / 127.0f, 0.5f) * logf(25000.0f)) + 40.0f;
  241. if(!lpf)
  242. lpf = new AnalogFilter(2, fr, 1, 0, samplerate, buffersize);
  243. else
  244. lpf->setfreq(fr);
  245. }
  246. }
  247. void Reverb::settype(unsigned char _Ptype)
  248. {
  249. Ptype = _Ptype;
  250. const int NUM_TYPES = 3;
  251. const int combtunings[NUM_TYPES][REV_COMBS] = {
  252. //this is unused (for random)
  253. {0, 0, 0, 0, 0, 0, 0, 0 },
  254. //Freeverb by Jezar at Dreampoint
  255. {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 },
  256. //duplicate of Freeverb by Jezar at Dreampoint
  257. {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 }
  258. };
  259. const int aptunings[NUM_TYPES][REV_APS] = {
  260. //this is unused (for random)
  261. {0, 0, 0, 0 },
  262. //Freeverb by Jezar at Dreampoint
  263. {225, 341, 441, 556 },
  264. //duplicate of Freeverb by Jezar at Dreampoint
  265. {225, 341, 441, 556 }
  266. };
  267. if(Ptype >= NUM_TYPES)
  268. Ptype = NUM_TYPES - 1;
  269. // adjust the combs according to the samplerate
  270. float samplerate_adjust = samplerate_f / 44100.0f;
  271. float tmp;
  272. for(int i = 0; i < REV_COMBS * 2; ++i) {
  273. if(Ptype == 0)
  274. tmp = 800.0f + (int)(RND * 1400.0f);
  275. else
  276. tmp = combtunings[Ptype][i % REV_COMBS];
  277. tmp *= roomsize;
  278. if(i > REV_COMBS)
  279. tmp += 23.0f;
  280. tmp *= samplerate_adjust; //adjust the combs according to the samplerate
  281. if(tmp < 10.0f)
  282. tmp = 10.0f;
  283. comblen[i] = (int) tmp;
  284. combk[i] = 0;
  285. lpcomb[i] = 0;
  286. if(comb[i])
  287. delete [] comb[i];
  288. comb[i] = new float[comblen[i]];
  289. }
  290. for(int i = 0; i < REV_APS * 2; ++i) {
  291. if(Ptype == 0)
  292. tmp = 500 + (int)(RND * 500.0f);
  293. else
  294. tmp = aptunings[Ptype][i % REV_APS];
  295. tmp *= roomsize;
  296. if(i > REV_APS)
  297. tmp += 23.0f;
  298. tmp *= samplerate_adjust; //adjust the combs according to the samplerate
  299. if(tmp < 10)
  300. tmp = 10;
  301. aplen[i] = (int) tmp;
  302. apk[i] = 0;
  303. if(ap[i])
  304. delete [] ap[i];
  305. ap[i] = new float[aplen[i]];
  306. }
  307. delete bandwidth;
  308. bandwidth = NULL;
  309. if(Ptype == 2) { //bandwidth
  310. //TODO the size of the unison buffer may be too small, though this has
  311. //not been verified yet.
  312. //As this cannot be resized in a RT context, a good upper bound should
  313. //be found
  314. bandwidth = new Unison(buffersize / 4 + 1, 2.0f, samplerate_f);
  315. bandwidth->setSize(50);
  316. bandwidth->setBaseFrequency(1.0f);
  317. }
  318. settime(Ptime);
  319. cleanup();
  320. }
  321. void Reverb::setroomsize(unsigned char _Proomsize)
  322. {
  323. Proomsize = _Proomsize;
  324. if(!Proomsize)
  325. this->Proomsize = 64; //this is because the older versions consider roomsize=0
  326. roomsize = (this->Proomsize - 64.0f) / 64.0f;
  327. if(roomsize > 0.0f)
  328. roomsize *= 2.0f;
  329. roomsize = powf(10.0f, roomsize);
  330. rs = sqrtf(roomsize);
  331. settype(Ptype);
  332. }
  333. void Reverb::setbandwidth(unsigned char _Pbandwidth)
  334. {
  335. Pbandwidth = _Pbandwidth;
  336. float v = Pbandwidth / 127.0f;
  337. if(bandwidth)
  338. bandwidth->setBandwidth(powf(v, 2.0f) * 200.0f);
  339. }
  340. void Reverb::setpreset(unsigned char npreset)
  341. {
  342. const int PRESET_SIZE = 13;
  343. const int NUM_PRESETS = 13;
  344. unsigned char presets[NUM_PRESETS][PRESET_SIZE] = {
  345. //Cathedral1
  346. {80, 64, 63, 24, 0, 0, 0, 85, 5, 83, 1, 64, 20},
  347. //Cathedral2
  348. {80, 64, 69, 35, 0, 0, 0, 127, 0, 71, 0, 64, 20},
  349. //Cathedral3
  350. {80, 64, 69, 24, 0, 0, 0, 127, 75, 78, 1, 85, 20},
  351. //Hall1
  352. {90, 64, 51, 10, 0, 0, 0, 127, 21, 78, 1, 64, 20},
  353. //Hall2
  354. {90, 64, 53, 20, 0, 0, 0, 127, 75, 71, 1, 64, 20},
  355. //Room1
  356. {100, 64, 33, 0, 0, 0, 0, 127, 0, 106, 0, 30, 20},
  357. //Room2
  358. {100, 64, 21, 26, 0, 0, 0, 62, 0, 77, 1, 45, 20},
  359. //Basement
  360. {110, 64, 14, 0, 0, 0, 0, 127, 5, 71, 0, 25, 20},
  361. //Tunnel
  362. {85, 80, 84, 20, 42, 0, 0, 51, 0, 78, 1, 105, 20},
  363. //Echoed1
  364. {95, 64, 26, 60, 71, 0, 0, 114, 0, 64, 1, 64, 20},
  365. //Echoed2
  366. {90, 64, 40, 88, 71, 0, 0, 114, 0, 88, 1, 64, 20},
  367. //VeryLong1
  368. {90, 64, 93, 15, 0, 0, 0, 114, 0, 77, 0, 95, 20},
  369. //VeryLong2
  370. {90, 64, 111, 30, 0, 0, 0, 114, 90, 74, 1, 80, 20}
  371. };
  372. if(npreset >= NUM_PRESETS)
  373. npreset = NUM_PRESETS - 1;
  374. for(int n = 0; n < PRESET_SIZE; ++n)
  375. changepar(n, presets[npreset][n]);
  376. if(insertion)
  377. changepar(0, presets[npreset][0] / 2); //lower the volume if reverb is insertion effect
  378. Ppreset = npreset;
  379. }
  380. void Reverb::changepar(int npar, unsigned char value)
  381. {
  382. switch(npar) {
  383. case 0:
  384. setvolume(value);
  385. break;
  386. case 1:
  387. setpanning(value);
  388. break;
  389. case 2:
  390. settime(value);
  391. break;
  392. case 3:
  393. setidelay(value);
  394. break;
  395. case 4:
  396. setidelayfb(value);
  397. break;
  398. // case 5:
  399. // setrdelay(value);
  400. // break;
  401. // case 6:
  402. // seterbalance(value);
  403. // break;
  404. case 7:
  405. setlpf(value);
  406. break;
  407. case 8:
  408. sethpf(value);
  409. break;
  410. case 9:
  411. setlohidamp(value);
  412. break;
  413. case 10:
  414. settype(value);
  415. break;
  416. case 11:
  417. setroomsize(value);
  418. break;
  419. case 12:
  420. setbandwidth(value);
  421. break;
  422. }
  423. }
  424. unsigned char Reverb::getpar(int npar) const
  425. {
  426. switch(npar) {
  427. case 0: return Pvolume;
  428. case 1: return Ppanning;
  429. case 2: return Ptime;
  430. case 3: return Pidelay;
  431. case 4: return Pidelayfb;
  432. case 7: return Plpf;
  433. case 8: return Phpf;
  434. case 9: return Plohidamp;
  435. case 10: return Ptype;
  436. case 11: return Proomsize;
  437. case 12: return Pbandwidth;
  438. default: return 0;
  439. }
  440. }