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.

244 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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
  19. {
  20. /**
  21. A collection of different interpolators for resampling streams of floats.
  22. @see GenericInterpolator, WindowedSincInterpolator, LagrangeInterpolator,
  23. CatmullRomInterpolator, LinearInterpolator, ZeroOrderHoldInterpolator
  24. @tags{Audio}
  25. */
  26. class Interpolators
  27. {
  28. private:
  29. struct WindowedSincTraits
  30. {
  31. static constexpr float algorithmicLatency = 100.0f;
  32. static forcedinline float windowedSinc (float firstFrac, int index) noexcept
  33. {
  34. auto index2 = index + 1;
  35. auto frac = firstFrac;
  36. auto value1 = lookupTable[index];
  37. auto value2 = lookupTable[index2];
  38. return value1 + (frac * (value2 - value1));
  39. }
  40. static forcedinline float valueAtOffset (const float* const inputs, const float offset, int indexBuffer) noexcept
  41. {
  42. int numCrossings = 100;
  43. float result = 0.0f;
  44. auto samplePosition = indexBuffer;
  45. float firstFrac = 0.0f;
  46. float lastSincPosition = -1.0f;
  47. int index = 0, sign = -1;
  48. for (int i = -numCrossings; i <= numCrossings; ++i)
  49. {
  50. auto sincPosition = (1.0f - offset) + (float) i;
  51. if (i == -numCrossings || (sincPosition >= 0 && lastSincPosition < 0))
  52. {
  53. auto indexFloat = (sincPosition >= 0.f ? sincPosition : -sincPosition) * 100.0f;
  54. index = (int) std::floor (indexFloat);
  55. firstFrac = indexFloat - index;
  56. sign = (sincPosition < 0 ? -1 : 1);
  57. }
  58. if (sincPosition == 0.0f)
  59. result += inputs[samplePosition];
  60. else if (sincPosition < numCrossings && sincPosition > -numCrossings)
  61. result += inputs[samplePosition] * windowedSinc (firstFrac, index);
  62. if (++samplePosition == numCrossings * 2)
  63. samplePosition = 0;
  64. lastSincPosition = sincPosition;
  65. index += 100 * sign;
  66. }
  67. return result;
  68. }
  69. static const float lookupTable[10001];
  70. };
  71. struct LagrangeTraits
  72. {
  73. static constexpr float algorithmicLatency = 2.0f;
  74. static float valueAtOffset (const float*, float, int) noexcept;
  75. };
  76. struct CatmullRomTraits
  77. {
  78. //==============================================================================
  79. static constexpr float algorithmicLatency = 2.0f;
  80. static forcedinline float valueAtOffset (const float* const inputs, const float offset, int index) noexcept
  81. {
  82. auto y0 = inputs[index]; if (++index == 4) index = 0;
  83. auto y1 = inputs[index]; if (++index == 4) index = 0;
  84. auto y2 = inputs[index]; if (++index == 4) index = 0;
  85. auto y3 = inputs[index];
  86. auto halfY0 = 0.5f * y0;
  87. auto halfY3 = 0.5f * y3;
  88. return y1 + offset * ((0.5f * y2 - halfY0)
  89. + (offset * (((y0 + 2.0f * y2) - (halfY3 + 2.5f * y1))
  90. + (offset * ((halfY3 + 1.5f * y1) - (halfY0 + 1.5f * y2))))));
  91. }
  92. };
  93. struct LinearTraits
  94. {
  95. static constexpr float algorithmicLatency = 1.0f;
  96. static forcedinline float valueAtOffset (const float* const inputs, const float offset, int index) noexcept
  97. {
  98. auto y0 = inputs[index];
  99. auto y1 = inputs[index == 0 ? 1 : 0];
  100. return y1 * offset + y0 * (1.0f - offset);
  101. }
  102. };
  103. struct ZeroOrderHoldTraits
  104. {
  105. static constexpr float algorithmicLatency = 0.0f;
  106. static forcedinline float valueAtOffset (const float* const inputs, const float, int) noexcept
  107. {
  108. return inputs[0];
  109. }
  110. };
  111. public:
  112. using WindowedSinc = GenericInterpolator<WindowedSincTraits, 200>;
  113. using Lagrange = GenericInterpolator<LagrangeTraits, 5>;
  114. using CatmullRom = GenericInterpolator<CatmullRomTraits, 4>;
  115. using Linear = GenericInterpolator<LinearTraits, 2>;
  116. using ZeroOrderHold = GenericInterpolator<ZeroOrderHoldTraits, 1>;
  117. };
  118. //==============================================================================
  119. /**
  120. An interpolator for resampling a stream of floats using high order windowed
  121. (hann) sinc interpolation, recommended for high quality resampling.
  122. Note that the resampler is stateful, so when there's a break in the continuity
  123. of the input stream you're feeding it, you should call reset() before feeding
  124. it any new data. And like with any other stateful filter, if you're resampling
  125. multiple channels, make sure each one uses its own LinearInterpolator object.
  126. @see GenericInterpolator
  127. @see LagrangeInterpolator, CatmullRomInterpolator, LinearInterpolator,
  128. ZeroOrderHoldInterpolator
  129. @tags{Audio}
  130. */
  131. using WindowedSincInterpolator = Interpolators::WindowedSinc;
  132. /**
  133. An interpolator for resampling a stream of floats using 4-point lagrange interpolation.
  134. Note that the resampler is stateful, so when there's a break in the continuity
  135. of the input stream you're feeding it, you should call reset() before feeding
  136. it any new data. And like with any other stateful filter, if you're resampling
  137. multiple channels, make sure each one uses its own LagrangeInterpolator object.
  138. @see GenericInterpolator
  139. @see CatmullRomInterpolator, WindowedSincInterpolator, LinearInterpolator,
  140. ZeroOrderHoldInterpolator
  141. @tags{Audio}
  142. */
  143. using LagrangeInterpolator = Interpolators::Lagrange;
  144. /**
  145. An interpolator for resampling a stream of floats using Catmull-Rom interpolation.
  146. Note that the resampler is stateful, so when there's a break in the continuity
  147. of the input stream you're feeding it, you should call reset() before feeding
  148. it any new data. And like with any other stateful filter, if you're resampling
  149. multiple channels, make sure each one uses its own CatmullRomInterpolator object.
  150. @see GenericInterpolator
  151. @see LagrangeInterpolator, WindowedSincInterpolator, LinearInterpolator,
  152. ZeroOrderHoldInterpolator
  153. @tags{Audio}
  154. */
  155. using CatmullRomInterpolator = Interpolators::CatmullRom;
  156. /**
  157. An interpolator for resampling a stream of floats using linear interpolation.
  158. Note that the resampler is stateful, so when there's a break in the continuity
  159. of the input stream you're feeding it, you should call reset() before feeding
  160. it any new data. And like with any other stateful filter, if you're resampling
  161. multiple channels, make sure each one uses its own LinearInterpolator object.
  162. @see GenericInterpolator
  163. @see LagrangeInterpolator, CatmullRomInterpolator, WindowedSincInterpolator,
  164. ZeroOrderHoldInterpolator
  165. @tags{Audio}
  166. */
  167. using LinearInterpolator = Interpolators::Linear;
  168. /**
  169. An interpolator for resampling a stream of floats using zero order hold
  170. interpolation.
  171. Note that the resampler is stateful, so when there's a break in the continuity
  172. of the input stream you're feeding it, you should call reset() before feeding
  173. it any new data. And like with any other stateful filter, if you're resampling
  174. multiple channels, make sure each one uses its own ZeroOrderHoldInterpolator
  175. object.
  176. @see GenericInterpolator
  177. @see LagrangeInterpolator, CatmullRomInterpolator, WindowedSincInterpolator,
  178. LinearInterpolator
  179. @tags{Audio}
  180. */
  181. using ZeroOrderHoldInterpolator = Interpolators::ZeroOrderHold;
  182. } // namespace juce