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.

LFO.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, const AbsTime &t)
  24. :delayTime(t, lfopars.Pdelay / 127.0f * 4.0f), //0..4 sec
  25. waveShape(lfopars.PLFOtype),
  26. deterministic(!lfopars.Pfreqrand)
  27. {
  28. int stretch = lfopars.Pstretch;
  29. if(stretch == 0)
  30. stretch = 1;
  31. //max 2x/octave
  32. const float lfostretch = powf(basefreq / 440.0f, (stretch - 64.0f) / 63.0f);
  33. const float lfofreq =
  34. (powf(2, lfopars.Pfreq * 10.0f) - 1.0f) / 12.0f * lfostretch;
  35. phaseInc = fabs(lfofreq) * t.dt();
  36. if(!lfopars.Pcontinous) {
  37. if(lfopars.Pstartphase == 0)
  38. phase = RND;
  39. else
  40. phase = fmod((lfopars.Pstartphase - 64.0f) / 127.0f + 1.0f, 1.0f);
  41. }
  42. else {
  43. const float tmp = fmod(t.time() * phaseInc, 1.0f);
  44. phase = fmod((lfopars.Pstartphase - 64.0f) / 127.0f + 1.0f + tmp, 1.0f);
  45. }
  46. //Limit the Frequency(or else...)
  47. if(phaseInc > 0.49999999f)
  48. phaseInc = 0.499999999f;
  49. lfornd = limit(lfopars.Prandomness / 127.0f, 0.0f, 1.0f);
  50. lfofreqrnd = powf(lfopars.Pfreqrand / 127.0f, 2.0f) * 4.0f;
  51. switch(lfopars.fel) {
  52. case 1:
  53. lfointensity = lfopars.Pintensity / 127.0f;
  54. break;
  55. case 2:
  56. lfointensity = lfopars.Pintensity / 127.0f * 4.0f;
  57. break; //in octave
  58. default:
  59. lfointensity = powf(2, lfopars.Pintensity / 127.0f * 11.0f) - 1.0f; //in centi
  60. phase -= 0.25f; //chance the starting phase
  61. break;
  62. }
  63. amp1 = (1 - lfornd) + lfornd * RND;
  64. amp2 = (1 - lfornd) + lfornd * RND;
  65. incrnd = nextincrnd = 1.0f;
  66. computeNextFreqRnd();
  67. computeNextFreqRnd(); //twice because I want incrnd & nextincrnd to be random
  68. }
  69. LFO::~LFO()
  70. {}
  71. float LFO::baseOut(const char waveShape, const float phase) const
  72. {
  73. switch(waveShape) {
  74. case LFO_TRIANGLE:
  75. if(phase >= 0.0f && phase < 0.25f)
  76. return 4.0f * phase;
  77. else if(phase > 0.25f && phase < 0.75f)
  78. return 2 - 4 * phase;
  79. else
  80. return 4.0f * phase - 4.0f;
  81. break;
  82. case LFO_SQUARE:
  83. if(phase < 0.5f)
  84. return -1;
  85. else
  86. return 1;
  87. break;
  88. case LFO_RAMPUP: return (phase - 0.5f) * 2.0f;
  89. case LFO_RAMPDOWN: return (0.5f - phase) * 2.0f;
  90. case LFO_EXP_DOWN1: return powf(0.05f, phase) * 2.0f - 1.0f;
  91. case LFO_EXP_DOWN2: return powf(0.001f, phase) * 2.0f - 1.0f;
  92. default: return cosf(phase * 2.0f * PI); //LFO_SINE
  93. }
  94. }
  95. float LFO::lfoout()
  96. {
  97. float out = baseOut(waveShape, phase);
  98. if(waveShape == LFO_SINE || waveShape == LFO_TRIANGLE)
  99. out *= lfointensity * (amp1 + phase * (amp2 - amp1));
  100. else
  101. out *= lfointensity * amp2;
  102. if(delayTime.inFuture())
  103. return out;
  104. //Start oscillating
  105. if(deterministic)
  106. phase += phaseInc;
  107. else {
  108. const float tmp = (incrnd * (1.0f - phase) + nextincrnd * phase);
  109. phase += phaseInc * limit(tmp, 0.0f, 1.0f);
  110. }
  111. if(phase >= 1) {
  112. phase = fmod(phase, 1.0f);
  113. amp1 = amp2;
  114. amp2 = (1 - lfornd) + lfornd * RND;
  115. computeNextFreqRnd();
  116. }
  117. return out;
  118. }
  119. /*
  120. * LFO out (for amplitude)
  121. */
  122. float LFO::amplfoout()
  123. {
  124. return limit(1.0f - lfointensity + lfoout(), -1.0f, 1.0f);
  125. }
  126. void LFO::computeNextFreqRnd()
  127. {
  128. if(deterministic)
  129. return;
  130. incrnd = nextincrnd;
  131. nextincrnd = powf(0.5f, lfofreqrnd) + RND * (powf(2.0f, lfofreqrnd) - 1.0f);
  132. }