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.

258 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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. struct ADSRTests : public UnitTest
  20. {
  21. ADSRTests() : UnitTest ("ADSR", UnitTestCategories::audio) {}
  22. void runTest() override
  23. {
  24. constexpr double sampleRate = 44100.0;
  25. const ADSR::Parameters parameters { 0.1f, 0.1f, 0.5f, 0.1f };
  26. ADSR adsr;
  27. adsr.setSampleRate (sampleRate);
  28. adsr.setParameters (parameters);
  29. beginTest ("Idle");
  30. {
  31. adsr.reset();
  32. expect (! adsr.isActive());
  33. expectEquals (adsr.getNextSample(), 0.0f);
  34. }
  35. beginTest ("Attack");
  36. {
  37. adsr.reset();
  38. adsr.noteOn();
  39. expect (adsr.isActive());
  40. auto buffer = getTestBuffer (sampleRate, parameters.attack);
  41. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  42. expect (isIncreasing (buffer));
  43. }
  44. beginTest ("Decay");
  45. {
  46. adsr.reset();
  47. adsr.noteOn();
  48. advanceADSR (adsr, roundToInt (parameters.attack * sampleRate));
  49. auto buffer = getTestBuffer (sampleRate, parameters.decay);
  50. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  51. expect (isDecreasing (buffer));
  52. }
  53. beginTest ("Sustain");
  54. {
  55. adsr.reset();
  56. adsr.noteOn();
  57. advanceADSR (adsr, roundToInt ((parameters.attack + parameters.decay + 0.01) * sampleRate));
  58. auto random = getRandom();
  59. for (int numTests = 0; numTests < 100; ++numTests)
  60. {
  61. const auto sustainLevel = random.nextFloat();
  62. const auto sustainLength = jmax (0.1f, random.nextFloat());
  63. adsr.setParameters ({ parameters.attack, parameters.decay, sustainLevel, parameters.release });
  64. auto buffer = getTestBuffer (sampleRate, sustainLength);
  65. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  66. expect (isSustained (buffer, sustainLevel));
  67. }
  68. }
  69. beginTest ("Release");
  70. {
  71. adsr.reset();
  72. adsr.noteOn();
  73. advanceADSR (adsr, roundToInt ((parameters.attack + parameters.decay) * sampleRate));
  74. adsr.noteOff();
  75. auto buffer = getTestBuffer (sampleRate, parameters.release);
  76. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  77. expect (isDecreasing (buffer));
  78. }
  79. beginTest ("Zero-length attack jumps to decay");
  80. {
  81. adsr.reset();
  82. adsr.setParameters ({ 0.0f, parameters.decay, parameters.sustain, parameters.release });
  83. adsr.noteOn();
  84. auto buffer = getTestBuffer (sampleRate, parameters.decay);
  85. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  86. expect (isDecreasing (buffer));
  87. }
  88. beginTest ("Zero-length decay jumps to sustain");
  89. {
  90. adsr.reset();
  91. adsr.setParameters ({ parameters.attack, 0.0f, parameters.sustain, parameters.release });
  92. adsr.noteOn();
  93. advanceADSR (adsr, roundToInt (parameters.attack * sampleRate));
  94. adsr.getNextSample();
  95. expectEquals (adsr.getNextSample(), parameters.sustain);
  96. auto buffer = getTestBuffer (sampleRate, 1);
  97. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  98. expect (isSustained (buffer, parameters.sustain));
  99. }
  100. beginTest ("Zero-length attack and decay jumps to sustain");
  101. {
  102. adsr.reset();
  103. adsr.setParameters ({ 0.0f, 0.0f, parameters.sustain, parameters.release });
  104. adsr.noteOn();
  105. expectEquals (adsr.getNextSample(), parameters.sustain);
  106. auto buffer = getTestBuffer (sampleRate, 1);
  107. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  108. expect (isSustained (buffer, parameters.sustain));
  109. }
  110. beginTest ("Zero-length attack and decay releases correctly");
  111. {
  112. adsr.reset();
  113. adsr.setParameters ({ 0.0f, 0.0f, parameters.sustain, parameters.release });
  114. adsr.noteOn();
  115. adsr.noteOff();
  116. auto buffer = getTestBuffer (sampleRate, parameters.release);
  117. adsr.applyEnvelopeToBuffer (buffer, 0, buffer.getNumSamples());
  118. expect (isDecreasing (buffer));
  119. }
  120. beginTest ("Zero-length release resets to idle");
  121. {
  122. adsr.reset();
  123. adsr.setParameters ({ parameters.attack, parameters.decay, parameters.sustain, 0.0f });
  124. adsr.noteOn();
  125. advanceADSR (adsr, roundToInt ((parameters.attack + parameters.decay) * sampleRate));
  126. adsr.noteOff();
  127. expect (! adsr.isActive());
  128. }
  129. }
  130. static void advanceADSR (ADSR& adsr, int numSamplesToAdvance)
  131. {
  132. while (--numSamplesToAdvance >= 0)
  133. adsr.getNextSample();
  134. }
  135. static AudioBuffer<float> getTestBuffer (double sampleRate, float lengthInSeconds)
  136. {
  137. AudioBuffer<float> buffer { 2, roundToInt (lengthInSeconds * sampleRate) };
  138. for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
  139. for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
  140. buffer.setSample (channel, sample, 1.0f);
  141. return buffer;
  142. }
  143. static bool isIncreasing (const AudioBuffer<float>& b)
  144. {
  145. jassert (b.getNumChannels() > 0 && b.getNumSamples() > 0);
  146. for (int channel = 0; channel < b.getNumChannels(); ++channel)
  147. {
  148. float previousSample = -1.0f;
  149. for (int sample = 0; sample < b.getNumSamples(); ++sample)
  150. {
  151. const auto currentSample = b.getSample (channel, sample);
  152. if (currentSample <= previousSample)
  153. return false;
  154. previousSample = currentSample;
  155. }
  156. }
  157. return true;
  158. }
  159. static bool isDecreasing (const AudioBuffer<float>& b)
  160. {
  161. jassert (b.getNumChannels() > 0 && b.getNumSamples() > 0);
  162. for (int channel = 0; channel < b.getNumChannels(); ++channel)
  163. {
  164. float previousSample = std::numeric_limits<float>::max();
  165. for (int sample = 0; sample < b.getNumSamples(); ++sample)
  166. {
  167. const auto currentSample = b.getSample (channel, sample);
  168. if (currentSample >= previousSample)
  169. return false;
  170. previousSample = currentSample;
  171. }
  172. }
  173. return true;
  174. }
  175. static bool isSustained (const AudioBuffer<float>& b, float sustainLevel)
  176. {
  177. jassert (b.getNumChannels() > 0 && b.getNumSamples() > 0);
  178. for (int channel = 0; channel < b.getNumChannels(); ++channel)
  179. if (b.findMinMax (channel, 0, b.getNumSamples()) != Range<float> { sustainLevel, sustainLevel })
  180. return false;
  181. return true;
  182. }
  183. };
  184. static ADSRTests adsrTests;
  185. } // namespace juce