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.

328 lines
14KB

  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. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::dsp
  19. {
  20. /**
  21. Class for efficiently approximating expensive arithmetic operations.
  22. The approximation is based on linear interpolation between pre-calculated values.
  23. The approximated function should be passed as a callable object to the constructor
  24. along with the number of data points to be pre-calculated. The accuracy of the
  25. approximation can be increased by using more points at the cost of a larger memory
  26. footprint.
  27. Consider using LookupTableTransform as an easy-to-use alternative.
  28. Example:
  29. LookupTable<float> lut ([] (size_t i) { return std::sqrt ((float) i); }, 64);
  30. auto outValue = lut[17];
  31. @see LookupTableTransform
  32. @tags{DSP}
  33. */
  34. template <typename FloatType>
  35. class LookupTable
  36. {
  37. public:
  38. /** Creates an uninitialised LookupTable object.
  39. You need to call initialise() before using the object. Prefer using the
  40. non-default constructor instead.
  41. @see initialise
  42. */
  43. LookupTable();
  44. /** Creates and initialises a LookupTable object.
  45. @param functionToApproximate The function to be approximated. This should be a
  46. mapping from the integer range [0, numPointsToUse - 1].
  47. @param numPointsToUse The number of pre-calculated values stored.
  48. */
  49. LookupTable (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
  50. /** Initialises or changes the parameters of a LookupTable object.
  51. This function can be used to change what function is approximated by an already
  52. constructed LookupTable along with the number of data points used. If the function
  53. to be approximated won't ever change, prefer using the non-default constructor.
  54. @param functionToApproximate The function to be approximated. This should be a
  55. mapping from the integer range [0, numPointsToUse - 1].
  56. @param numPointsToUse The number of pre-calculated values stored.
  57. */
  58. void initialise (const std::function<FloatType (size_t)>& functionToApproximate, size_t numPointsToUse);
  59. //==============================================================================
  60. /** Calculates the approximated value for the given index without range checking.
  61. Use this if you can guarantee that the index is non-negative and less than numPoints.
  62. Otherwise use get().
  63. @param index The approximation is calculated for this non-integer index.
  64. @return The approximated value at the given index.
  65. @see get, operator[]
  66. */
  67. FloatType getUnchecked (FloatType index) const noexcept
  68. {
  69. jassert (isInitialised()); // Use the non-default constructor or call initialise() before first use
  70. jassert (isPositiveAndBelow (index, FloatType (getNumPoints())));
  71. auto i = truncatePositiveToUnsignedInt (index);
  72. auto f = index - FloatType (i);
  73. jassert (isPositiveAndBelow (f, FloatType (1)));
  74. auto x0 = data.getUnchecked (static_cast<int> (i));
  75. auto x1 = data.getUnchecked (static_cast<int> (i + 1));
  76. return jmap (f, x0, x1);
  77. }
  78. //==============================================================================
  79. /** Calculates the approximated value for the given index with range checking.
  80. This can be called with any input indices. If the provided index is out-of-range
  81. either the bottom or the top element of the LookupTable is returned.
  82. If the index is guaranteed to be in range use the faster getUnchecked() instead.
  83. @param index The approximation is calculated for this non-integer index.
  84. @return The approximated value at the given index.
  85. @see getUnchecked, operator[]
  86. */
  87. FloatType get (FloatType index) const noexcept
  88. {
  89. if (index >= (FloatType) getNumPoints())
  90. index = static_cast<FloatType> (getGuardIndex());
  91. else if (index < 0)
  92. index = {};
  93. return getUnchecked (index);
  94. }
  95. //==============================================================================
  96. /** @see getUnchecked */
  97. FloatType operator[] (FloatType index) const noexcept { return getUnchecked (index); }
  98. /** Returns the size of the LookupTable, i.e., the number of pre-calculated data points. */
  99. size_t getNumPoints() const noexcept { return static_cast<size_t> (data.size()) - 1; }
  100. /** Returns true if the LookupTable is initialised and ready to be used. */
  101. bool isInitialised() const noexcept { return data.size() > 1; }
  102. private:
  103. //==============================================================================
  104. Array<FloatType> data;
  105. void prepare() noexcept;
  106. static size_t getRequiredBufferSize (size_t numPointsToUse) noexcept { return numPointsToUse + 1; }
  107. size_t getGuardIndex() const noexcept { return getRequiredBufferSize (getNumPoints()) - 1; }
  108. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LookupTable)
  109. };
  110. //==============================================================================
  111. /** Class for approximating expensive arithmetic operations.
  112. Once initialised, this class can be used just like the function it approximates
  113. via operator().
  114. Example:
  115. LookupTableTransform<float> tanhApprox ([] (float x) { return std::tanh (x); }, -5.0f, 5.0f, 64);
  116. auto outValue = tanhApprox (4.2f);
  117. Note: If you try to call the function with an input outside the provided
  118. range, it will return either the first or the last recorded LookupTable value.
  119. @see LookupTable
  120. @tags{DSP}
  121. */
  122. template <typename FloatType>
  123. class LookupTableTransform
  124. {
  125. public:
  126. //==============================================================================
  127. /** Creates an uninitialised LookupTableTransform object.
  128. You need to call initialise() before using the object. Prefer using the
  129. non-default constructor instead.
  130. @see initialise
  131. */
  132. LookupTableTransform() = default;
  133. //==============================================================================
  134. /** Creates and initialises a LookupTableTransform object.
  135. @param functionToApproximate The function to be approximated. This should be a
  136. mapping from a FloatType to FloatType.
  137. @param minInputValueToUse The lowest input value used. The approximation will
  138. fail for values lower than this.
  139. @param maxInputValueToUse The highest input value used. The approximation will
  140. fail for values higher than this.
  141. @param numPoints The number of pre-calculated values stored.
  142. */
  143. LookupTableTransform (const std::function<FloatType (FloatType)>& functionToApproximate,
  144. FloatType minInputValueToUse,
  145. FloatType maxInputValueToUse,
  146. size_t numPoints)
  147. {
  148. initialise (functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints);
  149. }
  150. //==============================================================================
  151. /** Initialises or changes the parameters of a LookupTableTransform object.
  152. @param functionToApproximate The function to be approximated. This should be a
  153. mapping from a FloatType to FloatType.
  154. @param minInputValueToUse The lowest input value used. The approximation will
  155. fail for values lower than this.
  156. @param maxInputValueToUse The highest input value used. The approximation will
  157. fail for values higher than this.
  158. @param numPoints The number of pre-calculated values stored.
  159. */
  160. void initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
  161. FloatType minInputValueToUse,
  162. FloatType maxInputValueToUse,
  163. size_t numPoints);
  164. //==============================================================================
  165. /** Calculates the approximated value for the given input value without range checking.
  166. Use this if you can guarantee that the input value is within the range specified
  167. in the constructor or initialise(), otherwise use processSample().
  168. @param value The approximation is calculated for this input value.
  169. @return The approximated value for the provided input value.
  170. @see processSample, operator(), operator[]
  171. */
  172. FloatType processSampleUnchecked (FloatType value) const noexcept
  173. {
  174. jassert (value >= minInputValue && value <= maxInputValue);
  175. return lookupTable[scaler * value + offset];
  176. }
  177. //==============================================================================
  178. /** Calculates the approximated value for the given input value with range checking.
  179. This can be called with any input values. Out-of-range input values will be
  180. clipped to the specified input range.
  181. If the index is guaranteed to be in range use the faster processSampleUnchecked()
  182. instead.
  183. @param value The approximation is calculated for this input value.
  184. @return The approximated value for the provided input value.
  185. @see processSampleUnchecked, operator(), operator[]
  186. */
  187. FloatType processSample (FloatType value) const noexcept
  188. {
  189. auto index = scaler * jlimit (minInputValue, maxInputValue, value) + offset;
  190. jassert (isPositiveAndBelow (index, FloatType (lookupTable.getNumPoints())));
  191. return lookupTable[index];
  192. }
  193. //==============================================================================
  194. /** @see processSampleUnchecked */
  195. FloatType operator[] (FloatType index) const noexcept { return processSampleUnchecked (index); }
  196. /** @see processSample */
  197. FloatType operator() (FloatType index) const noexcept { return processSample (index); }
  198. //==============================================================================
  199. /** Processes an array of input values without range checking
  200. @see process
  201. */
  202. void processUnchecked (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
  203. {
  204. for (size_t i = 0; i < numSamples; ++i)
  205. output[i] = processSampleUnchecked (input[i]);
  206. }
  207. //==============================================================================
  208. /** Processes an array of input values with range checking
  209. @see processUnchecked
  210. */
  211. void process (const FloatType* input, FloatType* output, size_t numSamples) const noexcept
  212. {
  213. for (size_t i = 0; i < numSamples; ++i)
  214. output[i] = processSample (input[i]);
  215. }
  216. //==============================================================================
  217. /** Calculates the maximum relative error of the approximation for the specified
  218. parameter set.
  219. The closer the returned value is to zero the more accurate the approximation
  220. is.
  221. This function compares the approximated output of this class to the function
  222. it approximates at a range of points and returns the maximum relative error.
  223. This can be used to determine if the approximation is suitable for the given
  224. problem. The accuracy of the approximation can generally be improved by
  225. increasing numPoints.
  226. @param functionToApproximate The approximated function. This should be a
  227. mapping from a FloatType to FloatType.
  228. @param minInputValue The lowest input value used.
  229. @param maxInputValue The highest input value used.
  230. @param numPoints The number of pre-calculated values stored.
  231. @param numTestPoints The number of input values used for error
  232. calculation. Higher numbers can increase the
  233. accuracy of the error calculation. If it's zero
  234. then 100 * numPoints will be used.
  235. */
  236. static double calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
  237. FloatType minInputValue,
  238. FloatType maxInputValue,
  239. size_t numPoints,
  240. size_t numTestPoints = 0);
  241. private:
  242. //==============================================================================
  243. static double calculateRelativeDifference (double, double) noexcept;
  244. //==============================================================================
  245. LookupTable<FloatType> lookupTable;
  246. FloatType minInputValue, maxInputValue;
  247. FloatType scaler, offset;
  248. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LookupTableTransform)
  249. };
  250. } // namespace juce::dsp