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.

juce_Interpolators.h 8.7KB

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