The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

223 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. */
  26. class ADSR
  27. {
  28. public:
  29. //==============================================================================
  30. ADSR()
  31. {
  32. setSampleRate (44100.0);
  33. setParameters ({});
  34. }
  35. //==============================================================================
  36. /** Holds the parameters being used by an ADSR object. */
  37. struct Parameters
  38. {
  39. /** Attack time in seconds. */
  40. float attack = 0.1f;
  41. /** Decay time in seconds. */
  42. float decay = 0.1f;
  43. /** Sustain level. */
  44. float sustain = 1.0f;
  45. /** Release time in seconds. */
  46. float release = 0.1f;
  47. };
  48. /** Sets the parameters that will be used by an ADSR object.
  49. You must have called setSampleRate() with the correct sample rate before
  50. this otherwise the values may be incorrect!
  51. @see getParameters
  52. */
  53. void setParameters (const Parameters& newParameters)
  54. {
  55. currentParameters = newParameters;
  56. sustainLevel = newParameters.sustain;
  57. calculateRates (newParameters);
  58. }
  59. /** Returns the parameters currently being used by an ADSR object.
  60. @see setParameters
  61. */
  62. const Parameters& getParameters() const { return currentParameters; }
  63. /** Returns true if the envelope is in its attack, decay, sustain or release stage. */
  64. bool isActive() const noexcept { return currentState != State::idle; }
  65. //==============================================================================
  66. /** Sets the sample rate that will be used for the envelope.
  67. This must be called before the getNextSample() or setParameters() methods.
  68. */
  69. void setSampleRate (double sampleRate)
  70. {
  71. jassert (sampleRate > 0.0);
  72. sr = sampleRate;
  73. }
  74. //==============================================================================
  75. /** Resets the envelope to an idle state. */
  76. void reset()
  77. {
  78. envelopeVal = 0.0f;
  79. currentState = State::idle;
  80. }
  81. /** Starts the attack phase of the envelope. */
  82. void noteOn()
  83. {
  84. if (attackRate > 0.0f) currentState = State::attack;
  85. else if (decayRate > 0.0f) currentState = State::decay;
  86. else currentState = State::sustain;
  87. }
  88. /** Starts the release phase of the envelope. */
  89. void noteOff()
  90. {
  91. if (currentState != State::idle)
  92. {
  93. if (releaseRate > 0.0f)
  94. currentState = State::release;
  95. else
  96. reset();
  97. }
  98. }
  99. //==============================================================================
  100. /** Returns the next sample value for an ADSR object.
  101. @see applyEnvelopeToBuffer
  102. */
  103. float getNextSample()
  104. {
  105. if (currentState == State::idle)
  106. return 0.0f;
  107. if (currentState == State::attack)
  108. {
  109. envelopeVal += attackRate;
  110. if (envelopeVal >= 1.0f)
  111. {
  112. envelopeVal = 1.0f;
  113. if (decayRate > 0.0f)
  114. currentState = State::decay;
  115. else
  116. currentState = State::sustain;
  117. }
  118. }
  119. else if (currentState == State::decay)
  120. {
  121. envelopeVal -= decayRate;
  122. if (envelopeVal <= sustainLevel)
  123. {
  124. envelopeVal = sustainLevel;
  125. currentState = State::sustain;
  126. }
  127. }
  128. else if (currentState == State::sustain)
  129. {
  130. envelopeVal = sustainLevel;
  131. }
  132. else if (currentState == State::release)
  133. {
  134. envelopeVal -= releaseRate;
  135. if (envelopeVal <= 0.0f)
  136. reset();
  137. }
  138. return envelopeVal;
  139. }
  140. /** This method will conveniently apply the next numSamples number of envelope values
  141. to an AudioBuffer.
  142. @see getNextSample
  143. */
  144. template<typename FloatType>
  145. void applyEnvelopeToBuffer (AudioBuffer<FloatType>& buffer, int startSample, int numSamples)
  146. {
  147. jassert (startSample + numSamples <= buffer.getNumSamples());
  148. auto numChannels = buffer.getNumChannels();
  149. while (--numSamples >= 0)
  150. {
  151. auto env = getNextSample();
  152. for (int i = 0; i < numChannels; ++i)
  153. buffer.getWritePointer (i)[startSample] *= env;
  154. ++startSample;
  155. }
  156. }
  157. private:
  158. //==============================================================================
  159. void calculateRates (const Parameters& parameters)
  160. {
  161. // need to call setSampleRate() first!
  162. jassert (sr > 0.0);
  163. attackRate = (parameters.attack > 0.0f ? static_cast<float> (1.0f / (parameters.attack * sr)) : -1.0f);
  164. decayRate = (parameters.decay > 0.0f ? static_cast<float> ((1.0f - sustainLevel) / (parameters.decay * sr)) : -1.0f);
  165. releaseRate = (parameters.release > 0.0f ? static_cast<float> (sustainLevel / (parameters.release * sr)) : -1.0f);
  166. }
  167. //==============================================================================
  168. enum class State { idle, attack, decay, sustain, release };
  169. State currentState = State::idle;
  170. Parameters currentParameters;
  171. double sr = 0.0;
  172. float envelopeVal = 0.0f;
  173. float sustainLevel = 0.0f;
  174. float attackRate = 0.0f, decayRate = 0.0f, releaseRate = 0.0f;
  175. };
  176. } // namespace juce