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.

246 lines
8.7KB

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