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.

174 lines
4.5KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. LFO.cpp - LFO implementation
  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 modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "LFO.h"
  18. #include "../Params/LFOParams.h"
  19. #include "../Misc/Util.h"
  20. #include <cstdlib>
  21. #include <cstdio>
  22. #include <cmath>
  23. LFO::LFO(const LFOParams &lfopars, float basefreq, float dt_)
  24. :dt(dt_)
  25. {
  26. int stretch = lfopars.Pstretch;
  27. if(stretch == 0)
  28. stretch = 1;
  29. //max 2x/octave
  30. const float lfostretch = powf(basefreq / 440.0f, (stretch - 64.0f) / 63.0f);
  31. float lfofreq =
  32. (powf(2, lfopars.Pfreq * 10.0f) - 1.0f) / 12.0f * lfostretch;
  33. incx = fabs(lfofreq) * dt;
  34. if(!lfopars.Pcontinous) {
  35. if(lfopars.Pstartphase == 0)
  36. x = RND;
  37. else
  38. x = fmod((lfopars.Pstartphase - 64.0f) / 127.0f + 1.0f, 1.0f);
  39. }
  40. else {
  41. const float tmp = fmod(lfopars.time * incx, 1.0f);
  42. x = fmod((lfopars.Pstartphase - 64.0f) / 127.0f + 1.0f + tmp, 1.0f);
  43. }
  44. //Limit the Frequency(or else...)
  45. if(incx > 0.49999999f)
  46. incx = 0.499999999f;
  47. lfornd = limit(lfopars.Prandomness / 127.0f, 0.0f, 1.0f);
  48. lfofreqrnd = powf(lfopars.Pfreqrand / 127.0f, 2.0f) * 4.0f;
  49. switch(lfopars.fel) {
  50. case 1:
  51. lfointensity = lfopars.Pintensity / 127.0f;
  52. break;
  53. case 2:
  54. lfointensity = lfopars.Pintensity / 127.0f * 4.0f;
  55. break; //in octave
  56. default:
  57. lfointensity = powf(2, lfopars.Pintensity / 127.0f * 11.0f) - 1.0f; //in centi
  58. x -= 0.25f; //chance the starting phase
  59. break;
  60. }
  61. amp1 = (1 - lfornd) + lfornd * RND;
  62. amp2 = (1 - lfornd) + lfornd * RND;
  63. lfotype = lfopars.PLFOtype;
  64. lfodelay = lfopars.Pdelay / 127.0f * 4.0f; //0..4 sec
  65. incrnd = nextincrnd = 1.0f;
  66. freqrndenabled = (lfopars.Pfreqrand != 0);
  67. computenextincrnd();
  68. computenextincrnd(); //twice because I want incrnd & nextincrnd to be random
  69. }
  70. LFO::~LFO()
  71. {}
  72. /*
  73. * LFO out
  74. */
  75. float LFO::lfoout()
  76. {
  77. float out;
  78. switch(lfotype) {
  79. case 1: //LFO_TRIANGLE
  80. if((x >= 0.0f) && (x < 0.25f))
  81. out = 4.0f * x;
  82. else
  83. if((x > 0.25f) && (x < 0.75f))
  84. out = 2 - 4 * x;
  85. else
  86. out = 4.0f * x - 4.0f;
  87. break;
  88. case 2: //LFO_SQUARE
  89. if(x < 0.5f)
  90. out = -1;
  91. else
  92. out = 1;
  93. break;
  94. case 3: //LFO_RAMPUP
  95. out = (x - 0.5f) * 2.0f;
  96. break;
  97. case 4: //LFO_RAMPDOWN
  98. out = (0.5f - x) * 2.0f;
  99. break;
  100. case 5: //LFO_EXP_DOWN 1
  101. out = powf(0.05f, x) * 2.0f - 1.0f;
  102. break;
  103. case 6: //LFO_EXP_DOWN 2
  104. out = powf(0.001f, x) * 2.0f - 1.0f;
  105. break;
  106. default:
  107. out = cosf(x * 2.0f * PI); //LFO_SINE
  108. }
  109. if((lfotype == 0) || (lfotype == 1))
  110. out *= lfointensity * (amp1 + x * (amp2 - amp1));
  111. else
  112. out *= lfointensity * amp2;
  113. if(lfodelay < 0.00001f) {
  114. if(freqrndenabled == 0)
  115. x += incx;
  116. else {
  117. float tmp = (incrnd * (1.0f - x) + nextincrnd * x);
  118. if(tmp > 1.0f)
  119. tmp = 1.0f;
  120. else
  121. if(tmp < 0.0f)
  122. tmp = 0.0f;
  123. x += incx * tmp;
  124. }
  125. if(x >= 1) {
  126. x = fmod(x, 1.0f);
  127. amp1 = amp2;
  128. amp2 = (1 - lfornd) + lfornd * RND;
  129. computenextincrnd();
  130. }
  131. }
  132. else
  133. lfodelay -= dt;
  134. return out;
  135. }
  136. /*
  137. * LFO out (for amplitude)
  138. */
  139. float LFO::amplfoout()
  140. {
  141. return limit(1.0f - lfointensity + lfoout(), -1.0f, 1.0f);
  142. }
  143. void LFO::computenextincrnd()
  144. {
  145. if(freqrndenabled == 0)
  146. return;
  147. incrnd = nextincrnd;
  148. nextincrnd = powf(0.5f, lfofreqrnd) + RND * (powf(2.0f, lfofreqrnd) - 1.0f);
  149. }