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.

Phaser.cpp 15KB

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