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 12KB

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