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.

VexADSR.h 4.9KB

11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. @tweaker falkTX
  23. ==============================================================================
  24. */
  25. #ifndef DISTRHO_VEX_ADSR_HEADER_INCLUDED
  26. #define DISTRHO_VEX_ADSR_HEADER_INCLUDED
  27. #ifndef CARLA_EXPORT
  28. #define CARLA_EXPORT
  29. #endif
  30. #ifdef CARLA_EXPORT
  31. #include "juce_audio_basics.h"
  32. #else
  33. #include "../StandardHeader.h"
  34. #endif
  35. //Process every sample in a buffer
  36. class VexADSR
  37. {
  38. public:
  39. enum State {
  40. kStateDone = 0,
  41. kStateAttack,
  42. kStateDecay,
  43. kStateSustain,
  44. kStateRelease,
  45. kStateQRelease,
  46. kStateCount
  47. };
  48. typedef void (VexADSR::*StateFunc)();
  49. VexADSR()
  50. : state(kStateAttack),
  51. attackRate(0.1f),
  52. decayRate(0.3f),
  53. sustainLevel(0.5f),
  54. releaseRate(0.1f),
  55. preCount(0),
  56. postCount(0),
  57. value(0.0f)
  58. {
  59. stateFn[kStateDone] = &VexADSR::fn_done;
  60. stateFn[kStateAttack] = &VexADSR::fn_attack;
  61. stateFn[kStateDecay] = &VexADSR::fn_decay;
  62. stateFn[kStateSustain] = &VexADSR::fn_sustain;
  63. stateFn[kStateRelease] = &VexADSR::fn_release;
  64. stateFn[kStateQRelease] = &VexADSR::fn_qrelease;
  65. }
  66. // Process a buffer
  67. void doProcess(float* BufferL, float* BufferR, int bSize)
  68. {
  69. for(int i = 0; i < bSize; i++)
  70. {
  71. (this->*stateFn[state])();
  72. BufferL[i] *= value * value;
  73. BufferR[i] *= value * value;
  74. }
  75. }
  76. // Process a Sample
  77. float getSample()
  78. {
  79. (this->*stateFn[state])();
  80. return value * value;
  81. }
  82. void reset(int p)
  83. {
  84. preCount = p;
  85. state = kStateAttack;
  86. value = 0.0f;
  87. }
  88. void release(int p)
  89. {
  90. postCount = p;
  91. state = kStateRelease;
  92. }
  93. void quickRelease()
  94. {
  95. state = kStateQRelease;
  96. }
  97. bool getState()
  98. {
  99. return (state != kStateDone);
  100. }
  101. void kill()
  102. {
  103. state = kStateDone;
  104. }
  105. void setADSR(double a, double d, double s, double r, double sr)
  106. {
  107. double rate = sr * 5.0;
  108. attackRate = float(1.0f / (rate * jmax(a * a, 0.001)));
  109. decayRate = float(1.0f / (rate * jmax(d * d, 0.005)));
  110. releaseRate = float(1.0f / (rate * jmax(r * r, 0.0002)));
  111. sustainLevel = (float)s;
  112. }
  113. private:
  114. void fn_done()
  115. {
  116. }
  117. void fn_attack()
  118. {
  119. --preCount;
  120. value += attackRate * float(preCount < 1);
  121. if (value >= 1.0f)
  122. {
  123. state = kStateDecay;
  124. value = 1.0f;
  125. }
  126. }
  127. void fn_decay()
  128. {
  129. value -= decayRate;
  130. if (value <= sustainLevel)
  131. state = kStateSustain;
  132. }
  133. void fn_sustain()
  134. {
  135. }
  136. void fn_release()
  137. {
  138. --postCount;
  139. value -= releaseRate * bool(postCount < 1);
  140. postCount -= int(postCount > 0);
  141. if (value <= 0.0f)
  142. {
  143. state = kStateDone;
  144. value = 0.0f;
  145. }
  146. }
  147. void fn_qrelease()
  148. {
  149. value -= 0.006f;
  150. if (value <= 0.0f)
  151. {
  152. state = kStateDone;
  153. value = 0.0f;
  154. }
  155. }
  156. State state;
  157. StateFunc stateFn[kStateCount]; //Function pointer array for the state functions
  158. float attackRate, decayRate, sustainLevel, releaseRate;
  159. int preCount, postCount; //counter to delay release to sync with the sampleframe
  160. float value; //this is the current value of the envelope
  161. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VexADSR)
  162. };
  163. #endif // DISTRHO_VEX_ADSR_HEADER_INCLUDED