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.

EffectLFO.cpp 2.6KB

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