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.

499 lines
14KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Phaser.cpp - Phasing and Approximate digital model of an analog JFET phaser.
  4. Analog modeling implemented by Ryan Billing aka Transmogrifox.
  5. DSP analog modeling theory & practice largely influenced by
  6. various CCRMA publications, particularly works by Julius O. Smith.
  7. Copyright (C) 2002-2005 Nasca Octavian Paul
  8. Copyright (C) 2009-2010 Ryan Billing
  9. Copyright (C) 2010-2010 Mark McCurry
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. */
  15. #include <cmath>
  16. #include <algorithm>
  17. #include <rtosc/ports.h>
  18. #include <rtosc/port-sugar.h>
  19. #include "../Misc/Allocator.h"
  20. #include "Phaser.h"
  21. using namespace std;
  22. #define rObject Phaser
  23. #define rBegin [](const char *msg, rtosc::RtData &d) {
  24. #define rEnd }
  25. #define ucharParamCb(pname) rBegin \
  26. rObject &p = *(rObject*)d.obj; \
  27. if(rtosc_narguments(msg)) \
  28. p.set##pname(rtosc_argument(msg, 0).i); \
  29. else \
  30. d.reply(d.loc, "i", p.P##pname); \
  31. rEnd
  32. #define rParamPhaser(name, ...) \
  33. {STRINGIFY(P##name) "::i", rProp(parameter) rMap(min, 0) rMap(max, 127) DOC(__VA_ARGS__), NULL, ucharParamCb(name)}
  34. rtosc::Ports Phaser::ports = {
  35. {"preset::i", rProp(parameter)
  36. rOptions(Phaser 1, Phaser 2, Phaser 3, Phaser 4,
  37. Phaser 5, Phaser 6,
  38. APhaser 1, APhaser 2, APhaser 3, APhaser 4,
  39. APhaser 5, APhaser 6)
  40. rDoc("Instrument Presets"), 0,
  41. rBegin;
  42. rObject *o = (rObject*)d.obj;
  43. if(rtosc_narguments(msg))
  44. o->setpreset(rtosc_argument(msg, 0).i);
  45. else
  46. d.reply(d.loc, "i", o->Ppreset);
  47. rEnd},
  48. //Pvolume/Ppanning are common
  49. rEffPar(lfo.Pfreq, 2, rShort("freq"), "LFO frequency"),
  50. rEffPar(lfo.Prandomness, 3, rShort("rnd."), "LFO randomness"),
  51. rEffPar(lfo.PLFOtype, 4, rShort("type"),
  52. rOptions(sine, tri), "lfo shape"),
  53. rEffPar(lfo.Pstereo, 5, rShort("stereo"), "Left/right channel phase shift"),
  54. rEffPar(Pdepth, 6, rShort("depth"), "LFP depth"),
  55. rEffPar(Pfb, 7, rShort("fb"), "Feedback"),
  56. rEffPar(Pstages, 8, rLinear(1,12), rShort("stages"), ""),
  57. rParamPhaser(lrcross, rShort("cross"), "Channel routing"),
  58. rParamPhaser(offset, rShort("off"), "Offset"),
  59. rEffParTF(Poutsub, 10, rShort("sub"), "Invert output"),
  60. rParamPhaser(phase, rShort("phase"), ""),
  61. rParamPhaser(width, rShort("width"), ""),
  62. rEffParTF(Phyper, 12, rShort("hyp."), "Square the LFO"),
  63. rEffPar(Pdistortion, 13, rShort("distort"), "Distortion"),
  64. rEffParTF(Panalog, 14, rShort("analog"), "Use analog phaser"),
  65. };
  66. #undef rBegin
  67. #undef rEnd
  68. #undef rObject
  69. #define PHASER_LFO_SHAPE 2
  70. #define ONE_ 0.99999f // To prevent LFO ever reaching 1.0f for filter stability purposes
  71. #define ZERO_ 0.00001f // Same idea as above.
  72. Phaser::Phaser(EffectParams pars)
  73. :Effect(pars), lfo(pars.srate, pars.bufsize), old(NULL), xn1(NULL),
  74. yn1(NULL), diff(0.0f), oldgain(0.0f), fb(0.0f)
  75. {
  76. analog_setup();
  77. setpreset(Ppreset);
  78. cleanup();
  79. }
  80. void Phaser::analog_setup()
  81. {
  82. //model mismatch between JFET devices
  83. offset[0] = -0.2509303f;
  84. offset[1] = 0.9408924f;
  85. offset[2] = 0.998f;
  86. offset[3] = -0.3486182f;
  87. offset[4] = -0.2762545f;
  88. offset[5] = -0.5215785f;
  89. offset[6] = 0.2509303f;
  90. offset[7] = -0.9408924f;
  91. offset[8] = -0.998f;
  92. offset[9] = 0.3486182f;
  93. offset[10] = 0.2762545f;
  94. offset[11] = 0.5215785f;
  95. barber = 0; //Deactivate barber pole phasing by default
  96. mis = 1.0f;
  97. Rmin = 625.0f; // 2N5457 typical on resistance at Vgs = 0
  98. Rmax = 22000.0f; // Resistor parallel to FET
  99. Rmx = Rmin / Rmax;
  100. Rconst = 1.0f + Rmx; // Handle parallel resistor relationship
  101. C = 0.00000005f; // 50 nF
  102. CFs = 2.0f * samplerate_f * C;
  103. invperiod = 1.0f / buffersize_f;
  104. }
  105. Phaser::~Phaser()
  106. {
  107. memory.devalloc(old.l);
  108. memory.devalloc(old.r);
  109. memory.devalloc(xn1.l);
  110. memory.devalloc(xn1.r);
  111. memory.devalloc(yn1.l);
  112. memory.devalloc(yn1.r);
  113. }
  114. /*
  115. * Effect output
  116. */
  117. void Phaser::out(const Stereo<float *> &input)
  118. {
  119. if(Panalog)
  120. AnalogPhase(input);
  121. else
  122. normalPhase(input);
  123. }
  124. void Phaser::AnalogPhase(const Stereo<float *> &input)
  125. {
  126. Stereo<float> gain(0.0f), lfoVal(0.0f), mod(0.0f), g(0.0f), b(0.0f), hpf(
  127. 0.0f);
  128. lfo.effectlfoout(&lfoVal.l, &lfoVal.r);
  129. mod.l = lfoVal.l * width + (depth - 0.5f);
  130. mod.r = lfoVal.r * width + (depth - 0.5f);
  131. mod.l = limit(mod.l, ZERO_, ONE_);
  132. mod.r = limit(mod.r, ZERO_, ONE_);
  133. if(Phyper) {
  134. //Triangle wave squared is approximately sin on bottom, tri on top
  135. //Result is exponential sweep more akin to filter in synth with
  136. //exponential generator circuitry.
  137. mod.l *= mod.l;
  138. mod.r *= mod.r;
  139. }
  140. //g.l,g.r is Vp - Vgs. Typical FET drain-source resistance follows constant/[1-sqrt(Vp - Vgs)]
  141. mod.l = sqrtf(1.0f - mod.l);
  142. mod.r = sqrtf(1.0f - mod.r);
  143. diff.r = (mod.r - oldgain.r) * invperiod;
  144. diff.l = (mod.l - oldgain.l) * invperiod;
  145. g = oldgain;
  146. oldgain = mod;
  147. for(int i = 0; i < buffersize; ++i) {
  148. g.l += diff.l; // Linear interpolation between LFO samples
  149. g.r += diff.r;
  150. Stereo<float> xn(input.l[i] * pangainL, input.r[i] * pangainR);
  151. if(barber) {
  152. g.l += 0.25;
  153. g.l -= floorf(g.l);
  154. g.r += 0.25;
  155. g.r -= floorf(g.r);
  156. }
  157. xn.l = applyPhase(xn.l, g.l, fb.l, hpf.l, yn1.l, xn1.l);
  158. xn.r = applyPhase(xn.r, g.r, fb.r, hpf.r, yn1.r, xn1.r);
  159. fb.l = xn.l * feedback;
  160. fb.r = xn.r * feedback;
  161. efxoutl[i] = xn.l;
  162. efxoutr[i] = xn.r;
  163. }
  164. if(Poutsub) {
  165. invSignal(efxoutl, buffersize);
  166. invSignal(efxoutr, buffersize);
  167. }
  168. }
  169. float Phaser::applyPhase(float x, float g, float fb,
  170. float &hpf, float *yn1, float *xn1)
  171. {
  172. for(int j = 0; j < Pstages; ++j) { //Phasing routine
  173. mis = 1.0f + offsetpct * offset[j];
  174. //This is symmetrical.
  175. //FET is not, so this deviates slightly, however sym dist. is
  176. //better sounding than a real FET.
  177. float d = (1.0f + 2.0f * (0.25f + g) * hpf * hpf * distortion) * mis;
  178. Rconst = 1.0f + mis * Rmx;
  179. // This is 1/R. R is being modulated to control filter fc.
  180. float b = (Rconst - g) / (d * Rmin);
  181. float gain = (CFs - b) / (CFs + b);
  182. yn1[j] = gain * (x + yn1[j]) - xn1[j];
  183. //high pass filter:
  184. //Distortion depends on the high-pass part of the AP stage.
  185. hpf = yn1[j] + (1.0f - gain) * xn1[j];
  186. xn1[j] = x;
  187. x = yn1[j];
  188. if(j == 1)
  189. x += fb; //Insert feedback after first phase stage
  190. }
  191. return x;
  192. }
  193. void Phaser::normalPhase(const Stereo<float *> &input)
  194. {
  195. Stereo<float> gain(0.0f), lfoVal(0.0f);
  196. lfo.effectlfoout(&lfoVal.l, &lfoVal.r);
  197. gain.l =
  198. (expf(lfoVal.l
  199. * PHASER_LFO_SHAPE) - 1) / (expf(PHASER_LFO_SHAPE) - 1.0f);
  200. gain.r =
  201. (expf(lfoVal.r
  202. * PHASER_LFO_SHAPE) - 1) / (expf(PHASER_LFO_SHAPE) - 1.0f);
  203. gain.l = 1.0f - phase * (1.0f - depth) - (1.0f - phase) * gain.l * depth;
  204. gain.r = 1.0f - phase * (1.0f - depth) - (1.0f - phase) * gain.r * depth;
  205. gain.l = limit(gain.l, ZERO_, ONE_);
  206. gain.r = limit(gain.r, ZERO_, ONE_);
  207. for(int i = 0; i < buffersize; ++i) {
  208. float x = (float) i / buffersize_f;
  209. float x1 = 1.0f - x;
  210. //TODO think about making panning an external feature
  211. Stereo<float> xn(input.l[i] * pangainL + fb.l,
  212. input.r[i] * pangainR + fb.r);
  213. Stereo<float> g(gain.l * x + oldgain.l * x1,
  214. gain.r * x + oldgain.r * x1);
  215. xn.l = applyPhase(xn.l, g.l, old.l);
  216. xn.r = applyPhase(xn.r, g.r, old.r);
  217. //Left/Right crossing
  218. crossover(xn.l, xn.r, lrcross);
  219. fb.l = xn.l * feedback;
  220. fb.r = xn.r * feedback;
  221. efxoutl[i] = xn.l;
  222. efxoutr[i] = xn.r;
  223. }
  224. oldgain = gain;
  225. if(Poutsub) {
  226. invSignal(efxoutl, buffersize);
  227. invSignal(efxoutr, buffersize);
  228. }
  229. }
  230. float Phaser::applyPhase(float x, float g, float *old)
  231. {
  232. for(int j = 0; j < Pstages * 2; ++j) { //Phasing routine
  233. float tmp = old[j];
  234. old[j] = g * tmp + x;
  235. x = tmp - g * old[j];
  236. }
  237. return x;
  238. }
  239. /*
  240. * Cleanup the effect
  241. */
  242. void Phaser::cleanup()
  243. {
  244. fb = oldgain = Stereo<float>(0.0f);
  245. for(int i = 0; i < Pstages * 2; ++i) {
  246. old.l[i] = 0.0f;
  247. old.r[i] = 0.0f;
  248. }
  249. for(int i = 0; i < Pstages; ++i) {
  250. xn1.l[i] = 0.0f;
  251. yn1.l[i] = 0.0f;
  252. xn1.r[i] = 0.0f;
  253. yn1.r[i] = 0.0f;
  254. }
  255. }
  256. /*
  257. * Parameter control
  258. */
  259. void Phaser::setwidth(unsigned char Pwidth)
  260. {
  261. this->Pwidth = Pwidth;
  262. width = ((float)Pwidth / 127.0f);
  263. }
  264. void Phaser::setfb(unsigned char Pfb)
  265. {
  266. this->Pfb = Pfb;
  267. feedback = (float) (Pfb - 64) / 64.2f;
  268. }
  269. void Phaser::setvolume(unsigned char Pvolume)
  270. {
  271. this->Pvolume = Pvolume;
  272. outvolume = Pvolume / 127.0f;
  273. if(insertion == 0)
  274. volume = 1.0f;
  275. else
  276. volume = outvolume;
  277. }
  278. void Phaser::setdistortion(unsigned char Pdistortion)
  279. {
  280. this->Pdistortion = Pdistortion;
  281. distortion = (float)Pdistortion / 127.0f;
  282. }
  283. void Phaser::setoffset(unsigned char Poffset)
  284. {
  285. this->Poffset = Poffset;
  286. offsetpct = (float)Poffset / 127.0f;
  287. }
  288. void Phaser::setstages(unsigned char Pstages_)
  289. {
  290. memory.devalloc(old.l);
  291. memory.devalloc(old.r);
  292. memory.devalloc(xn1.l);
  293. memory.devalloc(xn1.r);
  294. memory.devalloc(yn1.l);
  295. memory.devalloc(yn1.r);
  296. Pstages = limit<int>(Pstages_, 1, MAX_PHASER_STAGES);
  297. old = Stereo<float *>(memory.valloc<float>(Pstages * 2),
  298. memory.valloc<float>(Pstages * 2));
  299. xn1 = Stereo<float *>(memory.valloc<float>(Pstages),
  300. memory.valloc<float>(Pstages));
  301. yn1 = Stereo<float *>(memory.valloc<float>(Pstages),
  302. memory.valloc<float>(Pstages));
  303. cleanup();
  304. }
  305. void Phaser::setphase(unsigned char Pphase)
  306. {
  307. this->Pphase = Pphase;
  308. phase = (Pphase / 127.0f);
  309. }
  310. void Phaser::setdepth(unsigned char Pdepth)
  311. {
  312. this->Pdepth = Pdepth;
  313. depth = (float)(Pdepth) / 127.0f;
  314. }
  315. void Phaser::setpreset(unsigned char npreset)
  316. {
  317. const int PRESET_SIZE = 15;
  318. const int NUM_PRESETS = 12;
  319. unsigned char presets[NUM_PRESETS][PRESET_SIZE] = {
  320. //Phaser
  321. //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  322. {64, 64, 36, 0, 0, 64, 110, 64, 1, 0, 0, 20,
  323. 0, 0,
  324. 0 },
  325. {64, 64, 35, 0, 0, 88, 40, 64, 3, 0, 0, 20, 0, 0,
  326. 0 },
  327. {64, 64, 31, 0, 0, 66, 68, 107, 2, 0, 0, 20, 0, 0,
  328. 0 },
  329. {39, 64, 22, 0, 0, 66, 67, 10, 5, 0, 1, 20, 0, 0,
  330. 0 },
  331. {64, 64, 20, 0, 1, 110, 67, 78, 10, 0, 0, 20, 0, 0,
  332. 0 },
  333. {64, 64, 53, 100, 0, 58, 37, 78, 3, 0, 0, 20, 0, 0,
  334. 0 },
  335. //APhaser
  336. //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  337. {64, 64, 14, 0, 1, 64, 64, 40, 4, 10, 0, 110,1, 20,
  338. 1 },
  339. {64, 64, 14, 5, 1, 64, 70, 40, 6, 10, 0, 110,1, 20,
  340. 1 },
  341. {64, 64, 9, 0, 0, 64, 60, 40, 8, 10, 0, 40, 0, 20,
  342. 1 },
  343. {64, 64, 14, 10, 0, 64, 45, 80, 7, 10, 1, 110,1, 20,
  344. 1 },
  345. {25, 64, 127, 10, 0, 64, 25, 16, 8, 100, 0, 25, 0, 20,
  346. 1 },
  347. {64, 64, 1, 10, 1, 64, 70, 40, 12, 10, 0, 110,1, 20,
  348. 1 }
  349. };
  350. if(npreset >= NUM_PRESETS)
  351. npreset = NUM_PRESETS - 1;
  352. for(int n = 0; n < PRESET_SIZE; ++n)
  353. changepar(n, presets[npreset][n]);
  354. Ppreset = npreset;
  355. }
  356. void Phaser::changepar(int npar, unsigned char value)
  357. {
  358. switch(npar) {
  359. case 0:
  360. setvolume(value);
  361. break;
  362. case 1:
  363. setpanning(value);
  364. break;
  365. case 2:
  366. lfo.Pfreq = value;
  367. lfo.updateparams();
  368. break;
  369. case 3:
  370. lfo.Prandomness = value;
  371. lfo.updateparams();
  372. break;
  373. case 4:
  374. lfo.PLFOtype = value;
  375. lfo.updateparams();
  376. barber = (2 == value);
  377. break;
  378. case 5:
  379. lfo.Pstereo = value;
  380. lfo.updateparams();
  381. break;
  382. case 6:
  383. setdepth(value);
  384. break;
  385. case 7:
  386. setfb(value);
  387. break;
  388. case 8:
  389. setstages(value);
  390. break;
  391. case 9:
  392. setlrcross(value);
  393. setoffset(value);
  394. break;
  395. case 10:
  396. Poutsub = min((int)value, 1);
  397. break;
  398. case 11:
  399. setphase(value);
  400. setwidth(value);
  401. break;
  402. case 12:
  403. Phyper = min((int)value, 1);
  404. break;
  405. case 13:
  406. setdistortion(value);
  407. break;
  408. case 14:
  409. Panalog = value;
  410. break;
  411. }
  412. }
  413. unsigned char Phaser::getpar(int npar) const
  414. {
  415. switch(npar) {
  416. case 0: return Pvolume;
  417. case 1: return Ppanning;
  418. case 2: return lfo.Pfreq;
  419. case 3: return lfo.Prandomness;
  420. case 4: return lfo.PLFOtype;
  421. case 5: return lfo.Pstereo;
  422. case 6: return Pdepth;
  423. case 7: return Pfb;
  424. case 8: return Pstages;
  425. case 9: return Plrcross;
  426. return Poffset; //same
  427. case 10: return Poutsub;
  428. case 11: return Pphase;
  429. return Pwidth; //same
  430. case 12: return Phyper;
  431. case 13: return Pdistortion;
  432. case 14: return Panalog;
  433. default: return 0;
  434. }
  435. }