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.

204 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - 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. namespace
  20. {
  21. static forcedinline void pushInterpolationSample (float* lastInputSamples, const float newValue) noexcept
  22. {
  23. lastInputSamples[4] = lastInputSamples[3];
  24. lastInputSamples[3] = lastInputSamples[2];
  25. lastInputSamples[2] = lastInputSamples[1];
  26. lastInputSamples[1] = lastInputSamples[0];
  27. lastInputSamples[0] = newValue;
  28. }
  29. static forcedinline void pushInterpolationSamples (float* lastInputSamples, const float* input, int numOut) noexcept
  30. {
  31. if (numOut >= 5)
  32. {
  33. for (int i = 0; i < 5; ++i)
  34. lastInputSamples[i] = input[--numOut];
  35. }
  36. else
  37. {
  38. for (int i = 0; i < numOut; ++i)
  39. pushInterpolationSample (lastInputSamples, input[i]);
  40. }
  41. }
  42. template <typename InterpolatorType>
  43. static int interpolate (float* lastInputSamples, double& subSamplePos, const double actualRatio,
  44. const float* in, float* out, const int numOut) noexcept
  45. {
  46. if (actualRatio == 1.0)
  47. {
  48. memcpy (out, in, (size_t) numOut * sizeof (float));
  49. pushInterpolationSamples (lastInputSamples, in, numOut);
  50. return numOut;
  51. }
  52. const float* const originalIn = in;
  53. double pos = subSamplePos;
  54. if (actualRatio < 1.0)
  55. {
  56. for (int i = numOut; --i >= 0;)
  57. {
  58. if (pos >= 1.0)
  59. {
  60. pushInterpolationSample (lastInputSamples, *in++);
  61. pos -= 1.0;
  62. }
  63. *out++ = InterpolatorType::valueAtOffset (lastInputSamples, (float) pos);
  64. pos += actualRatio;
  65. }
  66. }
  67. else
  68. {
  69. for (int i = numOut; --i >= 0;)
  70. {
  71. while (pos < actualRatio)
  72. {
  73. pushInterpolationSample (lastInputSamples, *in++);
  74. pos += 1.0;
  75. }
  76. pos -= actualRatio;
  77. *out++ = InterpolatorType::valueAtOffset (lastInputSamples, jmax (0.0f, 1.0f - (float) pos));
  78. }
  79. }
  80. subSamplePos = pos;
  81. return (int) (in - originalIn);
  82. }
  83. template <typename InterpolatorType>
  84. static int interpolateAdding (float* lastInputSamples, double& subSamplePos, const double actualRatio,
  85. const float* in, float* out, const int numOut, const float gain) noexcept
  86. {
  87. if (actualRatio == 1.0)
  88. {
  89. FloatVectorOperations::addWithMultiply (out, in, gain, numOut);
  90. pushInterpolationSamples (lastInputSamples, in, numOut);
  91. return numOut;
  92. }
  93. const float* const originalIn = in;
  94. double pos = subSamplePos;
  95. if (actualRatio < 1.0)
  96. {
  97. for (int i = numOut; --i >= 0;)
  98. {
  99. if (pos >= 1.0)
  100. {
  101. pushInterpolationSample (lastInputSamples, *in++);
  102. pos -= 1.0;
  103. }
  104. *out++ += gain * InterpolatorType::valueAtOffset (lastInputSamples, (float) pos);
  105. pos += actualRatio;
  106. }
  107. }
  108. else
  109. {
  110. for (int i = numOut; --i >= 0;)
  111. {
  112. while (pos < actualRatio)
  113. {
  114. pushInterpolationSample (lastInputSamples, *in++);
  115. pos += 1.0;
  116. }
  117. pos -= actualRatio;
  118. *out++ += gain * InterpolatorType::valueAtOffset (lastInputSamples, jmax (0.0f, 1.0f - (float) pos));
  119. }
  120. }
  121. subSamplePos = pos;
  122. return (int) (in - originalIn);
  123. }
  124. }
  125. //==============================================================================
  126. template <int k>
  127. struct LagrangeResampleHelper
  128. {
  129. static forcedinline void calc (float& a, float b) noexcept { a *= b * (1.0f / k); }
  130. };
  131. template<>
  132. struct LagrangeResampleHelper<0>
  133. {
  134. static forcedinline void calc (float&, float) noexcept {}
  135. };
  136. struct LagrangeAlgorithm
  137. {
  138. static forcedinline float valueAtOffset (const float* const inputs, const float offset) noexcept
  139. {
  140. return calcCoefficient<0> (inputs[4], offset)
  141. + calcCoefficient<1> (inputs[3], offset)
  142. + calcCoefficient<2> (inputs[2], offset)
  143. + calcCoefficient<3> (inputs[1], offset)
  144. + calcCoefficient<4> (inputs[0], offset);
  145. }
  146. template <int k>
  147. static forcedinline float calcCoefficient (float input, const float offset) noexcept
  148. {
  149. LagrangeResampleHelper<0 - k>::calc (input, -2.0f - offset);
  150. LagrangeResampleHelper<1 - k>::calc (input, -1.0f - offset);
  151. LagrangeResampleHelper<2 - k>::calc (input, 0.0f - offset);
  152. LagrangeResampleHelper<3 - k>::calc (input, 1.0f - offset);
  153. LagrangeResampleHelper<4 - k>::calc (input, 2.0f - offset);
  154. return input;
  155. }
  156. };
  157. LagrangeInterpolator::LagrangeInterpolator() noexcept { reset(); }
  158. LagrangeInterpolator::~LagrangeInterpolator() noexcept {}
  159. void LagrangeInterpolator::reset() noexcept
  160. {
  161. subSamplePos = 1.0;
  162. for (int i = 0; i < numElementsInArray (lastInputSamples); ++i)
  163. lastInputSamples[i] = 0;
  164. }
  165. int LagrangeInterpolator::process (double actualRatio, const float* in, float* out, int numOut) noexcept
  166. {
  167. return interpolate<LagrangeAlgorithm> (lastInputSamples, subSamplePos, actualRatio, in, out, numOut);
  168. }
  169. int LagrangeInterpolator::processAdding (double actualRatio, const float* in, float* out, int numOut, float gain) noexcept
  170. {
  171. return interpolateAdding<LagrangeAlgorithm> (lastInputSamples, subSamplePos, actualRatio, in, out, numOut, gain);
  172. }
  173. } // namespace juce