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.

juce_ADSR.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A very simple ADSR envelope class.
  22. To use it, call setSampleRate() with the current sample rate and give it some parameters
  23. with setParameters() then call getNextSample() to get the envelope value to be applied
  24. to each audio sample or applyEnvelopeToBuffer() to apply the envelope to a whole buffer.
  25. @tags{Audio}
  26. */
  27. class ADSR
  28. {
  29. public:
  30. //==============================================================================
  31. ADSR()
  32. {
  33. setSampleRate (44100.0);
  34. setParameters ({});
  35. }
  36. //==============================================================================
  37. /**
  38. Holds the parameters being used by an ADSR object.
  39. @tags{Audio}
  40. */
  41. struct Parameters
  42. {
  43. /** Attack time in seconds. */
  44. float attack = 0.1f;
  45. /** Decay time in seconds. */
  46. float decay = 0.1f;
  47. /** Sustain level. */
  48. float sustain = 1.0f;
  49. /** Release time in seconds. */
  50. float release = 0.1f;
  51. };
  52. /** Sets the parameters that will be used by an ADSR object.
  53. You must have called setSampleRate() with the correct sample rate before
  54. this otherwise the values may be incorrect!
  55. @see getParameters
  56. */
  57. void setParameters (const Parameters& newParameters)
  58. {
  59. currentParameters = newParameters;
  60. sustainLevel = newParameters.sustain;
  61. calculateRates (newParameters);
  62. if (currentState != State::idle)
  63. checkCurrentState();
  64. }
  65. /** Returns the parameters currently being used by an ADSR object.
  66. @see setParameters
  67. */
  68. const Parameters& getParameters() const { return currentParameters; }
  69. /** Returns true if the envelope is in its attack, decay, sustain or release stage. */
  70. bool isActive() const noexcept { return currentState != State::idle; }
  71. //==============================================================================
  72. /** Sets the sample rate that will be used for the envelope.
  73. This must be called before the getNextSample() or setParameters() methods.
  74. */
  75. void setSampleRate (double sampleRate)
  76. {
  77. jassert (sampleRate > 0.0);
  78. sr = sampleRate;
  79. }
  80. //==============================================================================
  81. /** Resets the envelope to an idle state. */
  82. void reset()
  83. {
  84. envelopeVal = 0.0f;
  85. currentState = State::idle;
  86. }
  87. /** Starts the attack phase of the envelope. */
  88. void noteOn()
  89. {
  90. if (attackRate > 0.0f)
  91. {
  92. currentState = State::attack;
  93. }
  94. else if (decayRate > 0.0f)
  95. {
  96. envelopeVal = 1.0f;
  97. currentState = State::decay;
  98. }
  99. else
  100. {
  101. currentState = State::sustain;
  102. }
  103. }
  104. /** Starts the release phase of the envelope. */
  105. void noteOff()
  106. {
  107. if (currentState != State::idle)
  108. {
  109. if (currentParameters.release > 0.0f)
  110. {
  111. releaseRate = static_cast<float> (envelopeVal / (currentParameters.release * sr));
  112. currentState = State::release;
  113. }
  114. else
  115. {
  116. reset();
  117. }
  118. }
  119. }
  120. //==============================================================================
  121. /** Returns the next sample value for an ADSR object.
  122. @see applyEnvelopeToBuffer
  123. */
  124. float getNextSample()
  125. {
  126. if (currentState == State::idle)
  127. return 0.0f;
  128. if (currentState == State::attack)
  129. {
  130. envelopeVal += attackRate;
  131. if (envelopeVal >= 1.0f)
  132. {
  133. envelopeVal = 1.0f;
  134. if (decayRate > 0.0f)
  135. currentState = State::decay;
  136. else
  137. currentState = State::sustain;
  138. }
  139. }
  140. else if (currentState == State::decay)
  141. {
  142. envelopeVal -= decayRate;
  143. if (envelopeVal <= sustainLevel)
  144. {
  145. envelopeVal = sustainLevel;
  146. currentState = State::sustain;
  147. }
  148. }
  149. else if (currentState == State::sustain)
  150. {
  151. envelopeVal = sustainLevel;
  152. }
  153. else if (currentState == State::release)
  154. {
  155. envelopeVal -= releaseRate;
  156. if (envelopeVal <= 0.0f)
  157. reset();
  158. }
  159. return envelopeVal;
  160. }
  161. /** This method will conveniently apply the next numSamples number of envelope values
  162. to an AudioBuffer.
  163. @see getNextSample
  164. */
  165. template<typename FloatType>
  166. void applyEnvelopeToBuffer (AudioBuffer<FloatType>& buffer, int startSample, int numSamples)
  167. {
  168. jassert (startSample + numSamples <= buffer.getNumSamples());
  169. auto numChannels = buffer.getNumChannels();
  170. while (--numSamples >= 0)
  171. {
  172. auto env = getNextSample();
  173. for (int i = 0; i < numChannels; ++i)
  174. buffer.getWritePointer (i)[startSample] *= env;
  175. ++startSample;
  176. }
  177. }
  178. private:
  179. //==============================================================================
  180. void calculateRates (const Parameters& parameters)
  181. {
  182. // need to call setSampleRate() first!
  183. jassert (sr > 0.0);
  184. attackRate = (parameters.attack > 0.0f ? static_cast<float> (1.0f / (parameters.attack * sr)) : -1.0f);
  185. decayRate = (parameters.decay > 0.0f ? static_cast<float> ((1.0f - sustainLevel) / (parameters.decay * sr)) : -1.0f);
  186. }
  187. void checkCurrentState()
  188. {
  189. if (currentState == State::attack && attackRate <= 0.0f) currentState = decayRate > 0.0f ? State::decay : State::sustain;
  190. else if (currentState == State::decay && decayRate <= 0.0f) currentState = State::sustain;
  191. else if (currentState == State::release && releaseRate <= 0.0f) reset();
  192. }
  193. //==============================================================================
  194. enum class State { idle, attack, decay, sustain, release };
  195. State currentState = State::idle;
  196. Parameters currentParameters;
  197. double sr = 0.0;
  198. float envelopeVal = 0.0f, sustainLevel = 0.0f, attackRate = 0.0f, decayRate = 0.0f, releaseRate = 0.0f;
  199. };
  200. } // namespace juce