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.

297 lines
10.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2018 - 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. #if JUCE_UNIT_TESTS
  20. template <class SmoothedValueType>
  21. class CommonSmoothedValueTests : public UnitTest
  22. {
  23. public:
  24. CommonSmoothedValueTests()
  25. : UnitTest ("CommonSmoothedValueTests", "SmoothedValues")
  26. {}
  27. void runTest() override
  28. {
  29. beginTest ("Initial state");
  30. {
  31. SmoothedValueType sv;
  32. auto value = sv.getCurrentValue();
  33. expectEquals (sv.getTargetValue(), value);
  34. sv.getNextValue();
  35. expectEquals (sv.getCurrentValue(), value);
  36. expect (! sv.isSmoothing());
  37. }
  38. beginTest ("Resetting");
  39. {
  40. auto initialValue = 15.0f;
  41. SmoothedValueType sv (initialValue);
  42. sv.reset (3);
  43. expectEquals (sv.getCurrentValue(), initialValue);
  44. auto targetValue = initialValue + 1.0f;
  45. sv.setTargetValue (targetValue);
  46. expectEquals (sv.getTargetValue(), targetValue);
  47. expectEquals (sv.getCurrentValue(), initialValue);
  48. expect (sv.isSmoothing());
  49. auto currentValue = sv.getNextValue();
  50. expect (currentValue > initialValue);
  51. expectEquals (sv.getCurrentValue(), currentValue);
  52. expectEquals (sv.getTargetValue(), targetValue);
  53. expect (sv.isSmoothing());
  54. sv.reset (5);
  55. expectEquals (sv.getCurrentValue(), targetValue);
  56. expectEquals (sv.getTargetValue(), targetValue);
  57. expect (! sv.isSmoothing());
  58. sv.getNextValue();
  59. expectEquals (sv.getCurrentValue(), targetValue);
  60. sv.setTargetValue (1.5f);
  61. sv.getNextValue();
  62. float newStart = 0.2f;
  63. sv.setCurrentAndTargetValue (newStart);
  64. expectEquals (sv.getNextValue(), newStart);
  65. expectEquals (sv.getTargetValue(), newStart);
  66. expectEquals (sv.getCurrentValue(), newStart);
  67. expect (! sv.isSmoothing());
  68. }
  69. beginTest ("Sample rate");
  70. {
  71. SmoothedValueType svSamples { 3.0f };
  72. auto svTime = svSamples;
  73. auto numSamples = 12;
  74. svSamples.reset (numSamples);
  75. svTime.reset (numSamples * 2, 1.0);
  76. for (int i = 0; i < numSamples; ++i)
  77. {
  78. svTime.skip (1);
  79. expectWithinAbsoluteError (svSamples.getNextValue(),
  80. svTime.getNextValue(),
  81. 1.0e-7f);
  82. }
  83. }
  84. beginTest ("Block processing");
  85. {
  86. SmoothedValueType sv (1.0f);
  87. sv.reset (12);
  88. sv.setTargetValue (2.0f);
  89. const auto numSamples = 15;
  90. AudioBuffer<float> referenceData (1, numSamples);
  91. for (int i = 0; i < numSamples; ++i)
  92. referenceData.setSample (0, i, sv.getNextValue());
  93. expect (referenceData.getSample (0, 0) > 0);
  94. expect (referenceData.getSample (0, 10) < sv.getTargetValue());
  95. expectWithinAbsoluteError (referenceData.getSample (0, 11),
  96. sv.getTargetValue(),
  97. 1.0e-7f);
  98. auto getUnitData = [] (int numSamplesToGenerate)
  99. {
  100. AudioBuffer<float> result (1, numSamplesToGenerate);
  101. for (int i = 0; i < numSamplesToGenerate; ++i)
  102. result.setSample (0, i, 1.0f);
  103. return result;
  104. };
  105. auto compareData = [this](const AudioBuffer<float>& test,
  106. const AudioBuffer<float>& reference)
  107. {
  108. for (int i = 0; i < test.getNumSamples(); ++i)
  109. expectWithinAbsoluteError (test.getSample (0, i),
  110. reference.getSample (0, i),
  111. 1.0e-7f);
  112. };
  113. auto testData = getUnitData (numSamples);
  114. sv.setCurrentAndTargetValue (1.0f);
  115. sv.setTargetValue (2.0f);
  116. sv.applyGain (testData.getWritePointer (0), numSamples);
  117. compareData (testData, referenceData);
  118. testData = getUnitData (numSamples);
  119. AudioBuffer<float> destData (1, numSamples);
  120. sv.setCurrentAndTargetValue (1.0f);
  121. sv.setTargetValue (2.0f);
  122. sv.applyGain (destData.getWritePointer (0),
  123. testData.getReadPointer (0),
  124. numSamples);
  125. compareData (destData, referenceData);
  126. compareData (testData, getUnitData (numSamples));
  127. testData = getUnitData (numSamples);
  128. sv.setCurrentAndTargetValue (1.0f);
  129. sv.setTargetValue (2.0f);
  130. sv.applyGain (testData, numSamples);
  131. compareData (testData, referenceData);
  132. }
  133. beginTest ("Skip");
  134. {
  135. SmoothedValueType sv;
  136. sv.reset (12);
  137. sv.setCurrentAndTargetValue (1.0f);
  138. sv.setTargetValue (2.0f);
  139. Array<float> reference;
  140. for (int i = 0; i < 15; ++i)
  141. reference.add (sv.getNextValue());
  142. sv.setCurrentAndTargetValue (1.0f);
  143. sv.setTargetValue (2.0f);
  144. expectWithinAbsoluteError (sv.skip (1), reference[0], 1.0e-6f);
  145. expectWithinAbsoluteError (sv.skip (1), reference[1], 1.0e-6f);
  146. expectWithinAbsoluteError (sv.skip (2), reference[3], 1.0e-6f);
  147. sv.skip (3);
  148. expectWithinAbsoluteError (sv.getCurrentValue(), reference[6], 1.0e-6f);
  149. expectEquals (sv.skip (300), sv.getTargetValue());
  150. expectEquals (sv.getCurrentValue(), sv.getTargetValue());
  151. }
  152. beginTest ("Negative");
  153. {
  154. SmoothedValueType sv;
  155. auto start = -1.0f, end = -2.0f;
  156. auto numValues = 12;
  157. sv.reset (numValues);
  158. sv.setCurrentAndTargetValue (start);
  159. sv.setTargetValue (end);
  160. auto val = sv.skip (3);
  161. expect (val < start && val > end);
  162. auto nextVal = sv.getNextValue();
  163. expect (nextVal < val);
  164. auto endVal = sv.skip (500);
  165. expectEquals (endVal, end);
  166. expectEquals (sv.getNextValue(), end);
  167. expectEquals (sv.getCurrentValue(), end);
  168. sv.setCurrentAndTargetValue (start);
  169. sv.reset (numValues);
  170. sv.setTargetValue (end);
  171. SmoothedValueType positiveSv { -start };
  172. positiveSv.reset (numValues);
  173. positiveSv.setTargetValue (-end);
  174. for (int i = 0; i < numValues + 2; ++i)
  175. expectEquals (sv.getNextValue(), -positiveSv.getNextValue());
  176. }
  177. }
  178. };
  179. static CommonSmoothedValueTests <SmoothedValue<float, ValueSmoothingTypes::Linear>> commonLinearSmoothedValueTests;
  180. static CommonSmoothedValueTests <SmoothedValue<float, ValueSmoothingTypes::Multiplicative>> commonMultiplicativeSmoothedValueTests;
  181. class SmoothedValueTests : public UnitTest
  182. {
  183. public:
  184. SmoothedValueTests()
  185. : UnitTest ("SmoothedValueTests", "SmoothedValues")
  186. {}
  187. void runTest() override
  188. {
  189. beginTest ("Linear moving target");
  190. {
  191. SmoothedValue<float, ValueSmoothingTypes::Linear> sv;
  192. sv.reset (12);
  193. float initialValue = 0.0f;
  194. sv.setCurrentAndTargetValue (initialValue);
  195. sv.setTargetValue (1.0f);
  196. auto delta = sv.getNextValue() - initialValue;
  197. sv.skip (6);
  198. auto newInitialValue = sv.getCurrentValue();
  199. sv.setTargetValue (newInitialValue + 2.0f);
  200. auto doubleDelta = sv.getNextValue() - newInitialValue;
  201. expectWithinAbsoluteError (doubleDelta, delta * 2.0f, 1.0e-7f);
  202. }
  203. beginTest ("Multiplicative curve");
  204. {
  205. SmoothedValue<double, ValueSmoothingTypes::Multiplicative> sv;
  206. auto numSamples = 12;
  207. AudioBuffer<double> values (2, numSamples + 1);
  208. sv.reset (numSamples);
  209. sv.setCurrentAndTargetValue (1.0);
  210. sv.setTargetValue (2.0f);
  211. values.setSample (0, 0, sv.getCurrentValue());
  212. for (int i = 1; i < values.getNumSamples(); ++i)
  213. values.setSample (0, i, sv.getNextValue());
  214. sv.setTargetValue (1.0f);
  215. values.setSample (1, values.getNumSamples() - 1, sv.getCurrentValue());
  216. for (int i = values.getNumSamples() - 2; i >= 0 ; --i)
  217. values.setSample (1, i, sv.getNextValue());
  218. for (int i = 0; i < values.getNumSamples(); ++i)
  219. expectWithinAbsoluteError (values.getSample (0, i), values.getSample (1, i), 1.0e-9);
  220. }
  221. }
  222. };
  223. static SmoothedValueTests smoothedValueTests;
  224. #endif
  225. } // namespace juce