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.

197 lines
5.7KB

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