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.

189 lines
5.4KB

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