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
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2008 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2008 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU Lesser General Public License, as published by the Free Software
  9. Foundation; either version 2 of the License, or (at your option) any later
  10. version.
  11. JUCE and JUCETICE are distributed in the hope that they will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  17. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. Boston, MA 02111-1307 USA
  19. ==============================================================================
  20. @author rockhardbuns
  21. @tweaker Lucio Asnaghi
  22. ==============================================================================
  23. */
  24. #ifndef __JUCETICE_VEXCADSR_HEADER__
  25. #define __JUCETICE_VEXCADSR_HEADER__
  26. #ifdef CARLA_EXPORT
  27. #include "juce_audio_basics.h"
  28. #else
  29. #include "../StandardHeader.h"
  30. #endif
  31. //Process every sample in a buffer
  32. class VexADSR
  33. {
  34. public:
  35. enum State {
  36. kStateDone = 0,
  37. kStateAttack,
  38. kStateDecay,
  39. kStateSustain,
  40. kStateRelease,
  41. kStateQRelease,
  42. kStateCount
  43. };
  44. typedef void (VexADSR::*StateFunc)();
  45. VexADSR()
  46. : state(kStateAttack),
  47. attackRate(0.1f),
  48. decayRate(0.3f),
  49. sustainLevel(0.5f),
  50. releaseRate(0.1f),
  51. preCount(0),
  52. postCount(0),
  53. value(0.0f)
  54. {
  55. stateFn[kStateDone] = &VexADSR::fn_done;
  56. stateFn[kStateAttack] = &VexADSR::fn_attack;
  57. stateFn[kStateDecay] = &VexADSR::fn_decay;
  58. stateFn[kStateSustain] = &VexADSR::fn_sustain;
  59. stateFn[kStateRelease] = &VexADSR::fn_release;
  60. stateFn[kStateQRelease] = &VexADSR::fn_qrelease;
  61. }
  62. // Process a buffer
  63. void doProcess(float* BufferL, float* BufferR, int bSize)
  64. {
  65. for(int i = 0; i < bSize; i++)
  66. {
  67. (this->*stateFn[state])();
  68. BufferL[i] *= value * value;
  69. BufferR[i] *= value * value;
  70. }
  71. }
  72. // Process a Sample
  73. float getSample()
  74. {
  75. (this->*stateFn[state])();
  76. return value * value;
  77. }
  78. void reset(int p)
  79. {
  80. preCount = p;
  81. state = kStateAttack;
  82. value = 0.0f;
  83. }
  84. void release(int p)
  85. {
  86. postCount = p;
  87. state = kStateRelease;
  88. }
  89. void quickRelease()
  90. {
  91. state = kStateQRelease;
  92. }
  93. bool getState()
  94. {
  95. return (state != kStateDone);
  96. }
  97. void kill()
  98. {
  99. state = kStateDone;
  100. }
  101. void setADSR(double a, double d, double s, double r, double sr)
  102. {
  103. double rate = sr * 5.0;
  104. attackRate = float(1.0f / (rate * jmax(a * a, 0.001)));
  105. decayRate = float(1.0f / (rate * jmax(d * d, 0.005)));
  106. releaseRate = float(1.0f / (rate * jmax(r * r, 0.0002)));
  107. sustainLevel = (float)s;
  108. }
  109. private:
  110. void fn_done()
  111. {
  112. }
  113. void fn_attack()
  114. {
  115. --preCount;
  116. value += attackRate * float(preCount < 1);
  117. if (value >= 1.0f)
  118. {
  119. state = kStateDecay;
  120. value = 1.0f;
  121. }
  122. }
  123. void fn_decay()
  124. {
  125. value -= decayRate;
  126. if (value <= sustainLevel)
  127. state = kStateSustain;
  128. }
  129. void fn_sustain()
  130. {
  131. }
  132. void fn_release()
  133. {
  134. --postCount;
  135. value -= releaseRate * bool(postCount < 1);
  136. postCount -= int(postCount > 0);
  137. if (value <= 0.0f)
  138. {
  139. state = kStateDone;
  140. value = 0.0f;
  141. }
  142. }
  143. void fn_qrelease()
  144. {
  145. value -= 0.006f;
  146. if (value <= 0.0f)
  147. {
  148. state = kStateDone;
  149. value = 0.0f;
  150. }
  151. }
  152. State state;
  153. StateFunc stateFn[kStateCount]; //Function pointer array for the state functions
  154. float attackRate, decayRate, sustainLevel, releaseRate;
  155. int preCount, postCount; //counter to delay release to sync with the sampleframe
  156. float value; //this is the current value of the envelope
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VexADSR)
  158. };
  159. #endif