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.

229 lines
8.0KB

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