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.

515 lines
14KB

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