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.

207 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. namespace
  24. {
  25. static forcedinline void pushInterpolationSample (float* lastInputSamples, const float newValue) noexcept
  26. {
  27. lastInputSamples[4] = lastInputSamples[3];
  28. lastInputSamples[3] = lastInputSamples[2];
  29. lastInputSamples[2] = lastInputSamples[1];
  30. lastInputSamples[1] = lastInputSamples[0];
  31. lastInputSamples[0] = newValue;
  32. }
  33. static forcedinline void pushInterpolationSamples (float* lastInputSamples, const float* input, int numOut) noexcept
  34. {
  35. if (numOut >= 5)
  36. {
  37. for (int i = 0; i < 5; ++i)
  38. lastInputSamples[i] = input[--numOut];
  39. }
  40. else
  41. {
  42. for (int i = 0; i < numOut; ++i)
  43. pushInterpolationSample (lastInputSamples, input[i]);
  44. }
  45. }
  46. template <typename InterpolatorType>
  47. static int interpolate (float* lastInputSamples, double& subSamplePos, const double actualRatio,
  48. const float* in, float* out, const int numOut) noexcept
  49. {
  50. if (actualRatio == 1.0)
  51. {
  52. memcpy (out, in, (size_t) numOut * sizeof (float));
  53. pushInterpolationSamples (lastInputSamples, in, numOut);
  54. return numOut;
  55. }
  56. const float* const originalIn = in;
  57. double pos = subSamplePos;
  58. if (actualRatio < 1.0)
  59. {
  60. for (int i = numOut; --i >= 0;)
  61. {
  62. if (pos >= 1.0)
  63. {
  64. pushInterpolationSample (lastInputSamples, *in++);
  65. pos -= 1.0;
  66. }
  67. *out++ = InterpolatorType::valueAtOffset (lastInputSamples, (float) pos);
  68. pos += actualRatio;
  69. }
  70. }
  71. else
  72. {
  73. for (int i = numOut; --i >= 0;)
  74. {
  75. while (pos < actualRatio)
  76. {
  77. pushInterpolationSample (lastInputSamples, *in++);
  78. pos += 1.0;
  79. }
  80. pos -= actualRatio;
  81. *out++ = InterpolatorType::valueAtOffset (lastInputSamples, jmax (0.0f, 1.0f - (float) pos));
  82. }
  83. }
  84. subSamplePos = pos;
  85. return (int) (in - originalIn);
  86. }
  87. template <typename InterpolatorType>
  88. static int interpolateAdding (float* lastInputSamples, double& subSamplePos, const double actualRatio,
  89. const float* in, float* out, const int numOut, const float gain) noexcept
  90. {
  91. if (actualRatio == 1.0)
  92. {
  93. FloatVectorOperations::addWithMultiply (out, in, gain, numOut);
  94. pushInterpolationSamples (lastInputSamples, in, numOut);
  95. return numOut;
  96. }
  97. const float* const originalIn = in;
  98. double pos = subSamplePos;
  99. if (actualRatio < 1.0)
  100. {
  101. for (int i = numOut; --i >= 0;)
  102. {
  103. if (pos >= 1.0)
  104. {
  105. pushInterpolationSample (lastInputSamples, *in++);
  106. pos -= 1.0;
  107. }
  108. *out++ += gain * InterpolatorType::valueAtOffset (lastInputSamples, (float) pos);
  109. pos += actualRatio;
  110. }
  111. }
  112. else
  113. {
  114. for (int i = numOut; --i >= 0;)
  115. {
  116. while (pos < actualRatio)
  117. {
  118. pushInterpolationSample (lastInputSamples, *in++);
  119. pos += 1.0;
  120. }
  121. pos -= actualRatio;
  122. *out++ += gain * InterpolatorType::valueAtOffset (lastInputSamples, jmax (0.0f, 1.0f - (float) pos));
  123. }
  124. }
  125. subSamplePos = pos;
  126. return (int) (in - originalIn);
  127. }
  128. }
  129. //==============================================================================
  130. template <int k>
  131. struct LagrangeResampleHelper
  132. {
  133. static forcedinline void calc (float& a, float b) noexcept { a *= b * (1.0f / k); }
  134. };
  135. template<>
  136. struct LagrangeResampleHelper<0>
  137. {
  138. static forcedinline void calc (float&, float) noexcept {}
  139. };
  140. struct LagrangeAlgorithm
  141. {
  142. static forcedinline float valueAtOffset (const float* const inputs, const float offset) noexcept
  143. {
  144. return calcCoefficient<0> (inputs[4], offset)
  145. + calcCoefficient<1> (inputs[3], offset)
  146. + calcCoefficient<2> (inputs[2], offset)
  147. + calcCoefficient<3> (inputs[1], offset)
  148. + calcCoefficient<4> (inputs[0], offset);
  149. }
  150. template <int k>
  151. static forcedinline float calcCoefficient (float input, const float offset) noexcept
  152. {
  153. LagrangeResampleHelper<0 - k>::calc (input, -2.0f - offset);
  154. LagrangeResampleHelper<1 - k>::calc (input, -1.0f - offset);
  155. LagrangeResampleHelper<2 - k>::calc (input, 0.0f - offset);
  156. LagrangeResampleHelper<3 - k>::calc (input, 1.0f - offset);
  157. LagrangeResampleHelper<4 - k>::calc (input, 2.0f - offset);
  158. return input;
  159. }
  160. };
  161. LagrangeInterpolator::LagrangeInterpolator() noexcept { reset(); }
  162. LagrangeInterpolator::~LagrangeInterpolator() noexcept {}
  163. void LagrangeInterpolator::reset() noexcept
  164. {
  165. subSamplePos = 1.0;
  166. for (int i = 0; i < numElementsInArray (lastInputSamples); ++i)
  167. lastInputSamples[i] = 0;
  168. }
  169. int LagrangeInterpolator::process (double actualRatio, const float* in, float* out, int numOut) noexcept
  170. {
  171. return interpolate<LagrangeAlgorithm> (lastInputSamples, subSamplePos, actualRatio, in, out, numOut);
  172. }
  173. int LagrangeInterpolator::processAdding (double actualRatio, const float* in, float* out, int numOut, float gain) noexcept
  174. {
  175. return interpolateAdding<LagrangeAlgorithm> (lastInputSamples, subSamplePos, actualRatio, in, out, numOut, gain);
  176. }