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.

271 lines
8.1KB

  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 JUCE_API ADSR
  28. {
  29. public:
  30. //==============================================================================
  31. ADSR()
  32. {
  33. recalculateRates();
  34. }
  35. //==============================================================================
  36. /**
  37. Holds the parameters being used by an ADSR object.
  38. @tags{Audio}
  39. */
  40. struct JUCE_API Parameters
  41. {
  42. Parameters() = default;
  43. Parameters (float attackTimeSeconds,
  44. float decayTimeSeconds,
  45. float sustainLevel,
  46. float releaseTimeSeconds)
  47. : attack (attackTimeSeconds),
  48. decay (decayTimeSeconds),
  49. sustain (sustainLevel),
  50. release (releaseTimeSeconds)
  51. {
  52. }
  53. float attack = 0.1f, decay = 0.1f, sustain = 1.0f, release = 0.1f;
  54. };
  55. /** Sets the parameters that will be used by an ADSR object.
  56. You must have called setSampleRate() with the correct sample rate before
  57. this otherwise the values may be incorrect!
  58. @see getParameters
  59. */
  60. void setParameters (const Parameters& newParameters)
  61. {
  62. // need to call setSampleRate() first!
  63. jassert (sampleRate > 0.0);
  64. parameters = newParameters;
  65. recalculateRates();
  66. }
  67. /** Returns the parameters currently being used by an ADSR object.
  68. @see setParameters
  69. */
  70. const Parameters& getParameters() const noexcept { return parameters; }
  71. /** Returns true if the envelope is in its attack, decay, sustain or release stage. */
  72. bool isActive() const noexcept { return state != State::idle; }
  73. //==============================================================================
  74. /** Sets the sample rate that will be used for the envelope.
  75. This must be called before the getNextSample() or setParameters() methods.
  76. */
  77. void setSampleRate (double newSampleRate) noexcept
  78. {
  79. jassert (newSampleRate > 0.0);
  80. sampleRate = newSampleRate;
  81. }
  82. //==============================================================================
  83. /** Resets the envelope to an idle state. */
  84. void reset() noexcept
  85. {
  86. envelopeVal = 0.0f;
  87. state = State::idle;
  88. }
  89. /** Starts the attack phase of the envelope. */
  90. void noteOn() noexcept
  91. {
  92. if (attackRate > 0.0f)
  93. {
  94. state = State::attack;
  95. }
  96. else if (decayRate > 0.0f)
  97. {
  98. envelopeVal = 1.0f;
  99. state = State::decay;
  100. }
  101. else
  102. {
  103. envelopeVal = parameters.sustain;
  104. state = State::sustain;
  105. }
  106. }
  107. /** Starts the release phase of the envelope. */
  108. void noteOff() noexcept
  109. {
  110. if (state != State::idle)
  111. {
  112. if (parameters.release > 0.0f)
  113. {
  114. releaseRate = (float) (envelopeVal / (parameters.release * sampleRate));
  115. state = State::release;
  116. }
  117. else
  118. {
  119. reset();
  120. }
  121. }
  122. }
  123. //==============================================================================
  124. /** Returns the next sample value for an ADSR object.
  125. @see applyEnvelopeToBuffer
  126. */
  127. float getNextSample() noexcept
  128. {
  129. if (state == State::idle)
  130. return 0.0f;
  131. if (state == State::attack)
  132. {
  133. envelopeVal += attackRate;
  134. if (envelopeVal >= 1.0f)
  135. {
  136. envelopeVal = 1.0f;
  137. goToNextState();
  138. }
  139. }
  140. else if (state == State::decay)
  141. {
  142. envelopeVal -= decayRate;
  143. if (envelopeVal <= parameters.sustain)
  144. {
  145. envelopeVal = parameters.sustain;
  146. goToNextState();
  147. }
  148. }
  149. else if (state == State::sustain)
  150. {
  151. envelopeVal = parameters.sustain;
  152. }
  153. else if (state == State::release)
  154. {
  155. envelopeVal -= releaseRate;
  156. if (envelopeVal <= 0.0f)
  157. goToNextState();
  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. if (state == State::idle)
  170. {
  171. buffer.clear (startSample, numSamples);
  172. return;
  173. }
  174. if (state == State::sustain)
  175. {
  176. buffer.applyGain (startSample, numSamples, parameters.sustain);
  177. return;
  178. }
  179. auto numChannels = buffer.getNumChannels();
  180. while (--numSamples >= 0)
  181. {
  182. auto env = getNextSample();
  183. for (int i = 0; i < numChannels; ++i)
  184. buffer.getWritePointer (i)[startSample] *= env;
  185. ++startSample;
  186. }
  187. }
  188. private:
  189. //==============================================================================
  190. void recalculateRates() noexcept
  191. {
  192. auto getRate = [] (float distance, float timeInSeconds, double sr)
  193. {
  194. return timeInSeconds > 0.0f ? (float) (distance / (timeInSeconds * sr)) : -1.0f;
  195. };
  196. attackRate = getRate (1.0f, parameters.attack, sampleRate);
  197. decayRate = getRate (1.0f - parameters.sustain, parameters.decay, sampleRate);
  198. releaseRate = getRate (parameters.sustain, parameters.release, sampleRate);
  199. if ((state == State::attack && attackRate <= 0.0f)
  200. || (state == State::decay && (decayRate <= 0.0f || envelopeVal <= parameters.sustain))
  201. || (state == State::release && releaseRate <= 0.0f))
  202. {
  203. goToNextState();
  204. }
  205. }
  206. void goToNextState() noexcept
  207. {
  208. if (state == State::attack)
  209. state = (decayRate > 0.0f ? State::decay : State::sustain);
  210. else if (state == State::decay)
  211. state = State::sustain;
  212. else if (state == State::release)
  213. reset();
  214. }
  215. //==============================================================================
  216. enum class State { idle, attack, decay, sustain, release };
  217. State state = State::idle;
  218. Parameters parameters;
  219. double sampleRate = 44100.0;
  220. float envelopeVal = 0.0f, attackRate = 0.0f, decayRate = 0.0f, releaseRate = 0.0f;
  221. };
  222. } // namespace juce