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.

111 lines
2.6KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. EffectLFO.cpp - Stereo LFO used by some effects
  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 "EffectLFO.h"
  12. #include "../Misc/Util.h"
  13. #include <cmath>
  14. #include "globals.h"
  15. namespace zyncarla {
  16. EffectLFO::EffectLFO(float srate_f, float bufsize_f)
  17. :Pfreq(40),
  18. Prandomness(0),
  19. PLFOtype(0),
  20. Pstereo(64),
  21. xl(0.0f),
  22. xr(0.0f),
  23. ampl1(RND),
  24. ampl2(RND),
  25. ampr1(RND),
  26. ampr2(RND),
  27. lfornd(0.0f),
  28. samplerate_f(srate_f),
  29. buffersize_f(bufsize_f)
  30. {
  31. updateparams();
  32. }
  33. EffectLFO::~EffectLFO() {}
  34. //Update the changed parameters
  35. void EffectLFO::updateparams(void)
  36. {
  37. float lfofreq = (powf(2.0f, Pfreq / 127.0f * 10.0f) - 1.0f) * 0.03f;
  38. incx = fabsf(lfofreq) * buffersize_f / samplerate_f;
  39. if(incx > 0.49999999f)
  40. incx = 0.499999999f; //Limit the Frequency
  41. lfornd = Prandomness / 127.0f;
  42. lfornd = (lfornd > 1.0f) ? 1.0f : lfornd;
  43. if(PLFOtype > 1)
  44. PLFOtype = 1; //this has to be updated if more lfo's are added
  45. lfotype = PLFOtype;
  46. xr = xl + (Pstereo - 64.0f) / 127.0f + 1.0f;
  47. xr -= floorf(xr);
  48. }
  49. //Compute the shape of the LFO
  50. float EffectLFO::getlfoshape(float x)
  51. {
  52. float out;
  53. switch(lfotype) {
  54. case 1: //EffectLFO_TRIANGLE
  55. if((x > 0.0f) && (x < 0.25f))
  56. out = 4.0f * x;
  57. else
  58. if((x > 0.25f) && (x < 0.75f))
  59. out = 2.0f - 4.0f * x;
  60. else
  61. out = 4.0f * x - 4.0f;
  62. break;
  63. //when adding more, ensure ::updateparams() gets updated
  64. default:
  65. out = cosf(x * 2.0f * PI); //EffectLFO_SINE // TODO: use M_PI ?
  66. }
  67. return out;
  68. }
  69. //LFO output
  70. void EffectLFO::effectlfoout(float *outl, float *outr)
  71. {
  72. float out;
  73. out = getlfoshape(xl);
  74. if((lfotype == 0) || (lfotype == 1))
  75. out *= (ampl1 + xl * (ampl2 - ampl1));
  76. xl += incx;
  77. if(xl > 1.0f) {
  78. xl -= 1.0f;
  79. ampl1 = ampl2;
  80. ampl2 = (1.0f - lfornd) + lfornd * RND;
  81. }
  82. *outl = (out + 1.0f) * 0.5f;
  83. out = getlfoshape(xr);
  84. if((lfotype == 0) || (lfotype == 1))
  85. out *= (ampr1 + xr * (ampr2 - ampr1));
  86. xr += incx;
  87. if(xr > 1.0f) {
  88. xr -= 1.0f;
  89. ampr1 = ampr2;
  90. ampr2 = (1.0f - lfornd) + lfornd * RND;
  91. }
  92. *outr = (out + 1.0f) * 0.5f;
  93. }
  94. }