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.

324 lines
14KB

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