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.

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