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.

387 lines
16KB

  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. An interpolator base class for resampling streams of floats.
  21. Note that the resamplers are stateful, so when there's a break in the continuity
  22. of the input stream you're feeding it, you should call reset() before feeding
  23. it any new data. And like with any other stateful filter, if you're resampling
  24. multiple channels, make sure each one uses its own interpolator object.
  25. @see LagrangeInterpolator, CatmullRomInterpolator, WindowedSincInterpolator,
  26. LinearInterpolator, ZeroOrderHoldInterpolator
  27. @tags{Audio}
  28. */
  29. template <class InterpolatorTraits, int memorySize>
  30. class JUCE_API GenericInterpolator
  31. {
  32. static auto processReplacingCallback()
  33. {
  34. return [] (auto, auto newValue) { return newValue; };
  35. }
  36. static auto processAddingCallback (float gain)
  37. {
  38. return [gain] (auto oldValue, auto newValue) { return oldValue + gain * newValue; };
  39. }
  40. public:
  41. GenericInterpolator() noexcept { reset(); }
  42. GenericInterpolator (GenericInterpolator&&) noexcept = default;
  43. GenericInterpolator& operator= (GenericInterpolator&&) noexcept = default;
  44. /** Returns the latency of the interpolation algorithm in isolation.
  45. In the context of resampling the total latency of a process using
  46. the interpolator is the base latency divided by the speed ratio.
  47. */
  48. static constexpr float getBaseLatency() noexcept
  49. {
  50. return InterpolatorTraits::algorithmicLatency;
  51. }
  52. /** Resets the state of the interpolator.
  53. Call this when there's a break in the continuity of the input data stream.
  54. */
  55. void reset() noexcept
  56. {
  57. indexBuffer = 0;
  58. subSamplePos = 1.0;
  59. std::fill (std::begin (lastInputSamples), std::end (lastInputSamples), 0.0f);
  60. }
  61. /** Resamples a stream of samples.
  62. @param speedRatio the number of input samples to use for each output sample
  63. @param inputSamples the source data to read from. This must contain at
  64. least (speedRatio * numOutputSamplesToProduce) samples.
  65. @param outputSamples the buffer to write the results into
  66. @param numOutputSamplesToProduce the number of output samples that should be created
  67. @returns the actual number of input samples that were used
  68. */
  69. int process (double speedRatio,
  70. const float* inputSamples,
  71. float* outputSamples,
  72. int numOutputSamplesToProduce) noexcept
  73. {
  74. return interpolateImpl (speedRatio,
  75. inputSamples,
  76. outputSamples,
  77. numOutputSamplesToProduce,
  78. processReplacingCallback());
  79. }
  80. /** Resamples a stream of samples.
  81. @param speedRatio the number of input samples to use for each output sample
  82. @param inputSamples the source data to read from. This must contain at
  83. least (speedRatio * numOutputSamplesToProduce) samples.
  84. @param outputSamples the buffer to write the results into
  85. @param numOutputSamplesToProduce the number of output samples that should be created
  86. @param numInputSamplesAvailable the number of available input samples. If it needs more samples
  87. than available, it either wraps back for wrapAround samples, or
  88. it feeds zeroes
  89. @param wrapAround if the stream exceeds available samples, it wraps back for
  90. wrapAround samples. If wrapAround is set to 0, it will feed zeroes.
  91. @returns the actual number of input samples that were used
  92. */
  93. int process (double speedRatio,
  94. const float* inputSamples,
  95. float* outputSamples,
  96. int numOutputSamplesToProduce,
  97. int numInputSamplesAvailable,
  98. int wrapAround) noexcept
  99. {
  100. return interpolateImpl (speedRatio,
  101. inputSamples,
  102. outputSamples,
  103. numOutputSamplesToProduce,
  104. numInputSamplesAvailable,
  105. wrapAround,
  106. processReplacingCallback());
  107. }
  108. /** Resamples a stream of samples, adding the results to the output data
  109. with a gain.
  110. @param speedRatio the number of input samples to use for each output sample
  111. @param inputSamples the source data to read from. This must contain at
  112. least (speedRatio * numOutputSamplesToProduce) samples.
  113. @param outputSamples the buffer to write the results to - the result values will be added
  114. to any pre-existing data in this buffer after being multiplied by
  115. the gain factor
  116. @param numOutputSamplesToProduce the number of output samples that should be created
  117. @param gain a gain factor to multiply the resulting samples by before
  118. adding them to the destination buffer
  119. @returns the actual number of input samples that were used
  120. */
  121. int processAdding (double speedRatio,
  122. const float* inputSamples,
  123. float* outputSamples,
  124. int numOutputSamplesToProduce,
  125. float gain) noexcept
  126. {
  127. return interpolateImpl (speedRatio,
  128. inputSamples,
  129. outputSamples,
  130. numOutputSamplesToProduce,
  131. processAddingCallback (gain));
  132. }
  133. /** Resamples a stream of samples, adding the results to the output data
  134. with a gain.
  135. @param speedRatio the number of input samples to use for each output sample
  136. @param inputSamples the source data to read from. This must contain at
  137. least (speedRatio * numOutputSamplesToProduce) samples.
  138. @param outputSamples the buffer to write the results to - the result values will be added
  139. to any pre-existing data in this buffer after being multiplied by
  140. the gain factor
  141. @param numOutputSamplesToProduce the number of output samples that should be created
  142. @param numInputSamplesAvailable the number of available input samples. If it needs more samples
  143. than available, it either wraps back for wrapAround samples, or
  144. it feeds zeroes
  145. @param wrapAround if the stream exceeds available samples, it wraps back for
  146. wrapAround samples. If wrapAround is set to 0, it will feed zeroes.
  147. @param gain a gain factor to multiply the resulting samples by before
  148. adding them to the destination buffer
  149. @returns the actual number of input samples that were used
  150. */
  151. int processAdding (double speedRatio,
  152. const float* inputSamples,
  153. float* outputSamples,
  154. int numOutputSamplesToProduce,
  155. int numInputSamplesAvailable,
  156. int wrapAround,
  157. float gain) noexcept
  158. {
  159. return interpolateImpl (speedRatio,
  160. inputSamples,
  161. outputSamples,
  162. numOutputSamplesToProduce,
  163. numInputSamplesAvailable,
  164. wrapAround,
  165. processAddingCallback (gain));
  166. }
  167. private:
  168. //==============================================================================
  169. forcedinline void pushInterpolationSample (float newValue) noexcept
  170. {
  171. lastInputSamples[indexBuffer] = newValue;
  172. if (++indexBuffer == memorySize)
  173. indexBuffer = 0;
  174. }
  175. forcedinline void pushInterpolationSamples (const float* input,
  176. int numOutputSamplesToProduce) noexcept
  177. {
  178. if (numOutputSamplesToProduce >= memorySize)
  179. {
  180. const auto* const offsetInput = input + (numOutputSamplesToProduce - memorySize);
  181. for (int i = 0; i < memorySize; ++i)
  182. pushInterpolationSample (offsetInput[i]);
  183. }
  184. else
  185. {
  186. for (int i = 0; i < numOutputSamplesToProduce; ++i)
  187. pushInterpolationSample (input[i]);
  188. }
  189. }
  190. forcedinline void pushInterpolationSamples (const float* input,
  191. int numOutputSamplesToProduce,
  192. int numInputSamplesAvailable,
  193. int wrapAround) noexcept
  194. {
  195. if (numOutputSamplesToProduce >= memorySize)
  196. {
  197. if (numInputSamplesAvailable >= memorySize)
  198. {
  199. pushInterpolationSamples (input,
  200. numOutputSamplesToProduce);
  201. }
  202. else
  203. {
  204. pushInterpolationSamples (input + ((numOutputSamplesToProduce - numInputSamplesAvailable) - 1),
  205. numInputSamplesAvailable);
  206. if (wrapAround > 0)
  207. {
  208. numOutputSamplesToProduce -= wrapAround;
  209. pushInterpolationSamples (input + ((numOutputSamplesToProduce - (memorySize - numInputSamplesAvailable)) - 1),
  210. memorySize - numInputSamplesAvailable);
  211. }
  212. else
  213. {
  214. for (int i = numInputSamplesAvailable; i < memorySize; ++i)
  215. pushInterpolationSample (0.0f);
  216. }
  217. }
  218. }
  219. else
  220. {
  221. if (numOutputSamplesToProduce > numInputSamplesAvailable)
  222. {
  223. for (int i = 0; i < numInputSamplesAvailable; ++i)
  224. pushInterpolationSample (input[i]);
  225. const auto extraSamples = numOutputSamplesToProduce - numInputSamplesAvailable;
  226. if (wrapAround > 0)
  227. {
  228. const auto* const offsetInput = input + (numInputSamplesAvailable - wrapAround);
  229. for (int i = 0; i < extraSamples; ++i)
  230. pushInterpolationSample (offsetInput[i]);
  231. }
  232. else
  233. {
  234. for (int i = 0; i < extraSamples; ++i)
  235. pushInterpolationSample (0.0f);
  236. }
  237. }
  238. else
  239. {
  240. for (int i = 0; i < numOutputSamplesToProduce; ++i)
  241. pushInterpolationSample (input[i]);
  242. }
  243. }
  244. }
  245. //==============================================================================
  246. template <typename Process>
  247. int interpolateImpl (double speedRatio,
  248. const float* input,
  249. float* output,
  250. int numOutputSamplesToProduce,
  251. int numInputSamplesAvailable,
  252. int wrap,
  253. Process process)
  254. {
  255. auto originalIn = input;
  256. bool exceeded = false;
  257. const auto pushSample = [&]
  258. {
  259. if (exceeded)
  260. {
  261. pushInterpolationSample (0.0);
  262. }
  263. else
  264. {
  265. pushInterpolationSample (*input++);
  266. if (--numInputSamplesAvailable <= 0)
  267. {
  268. if (wrap > 0)
  269. {
  270. input -= wrap;
  271. numInputSamplesAvailable += wrap;
  272. }
  273. else
  274. {
  275. exceeded = true;
  276. }
  277. }
  278. }
  279. };
  280. interpolateImpl (speedRatio,
  281. output,
  282. numOutputSamplesToProduce,
  283. process,
  284. pushSample);
  285. if (wrap == 0)
  286. return (int) (input - originalIn);
  287. return ((int) (input - originalIn) + wrap) % wrap;
  288. }
  289. template <typename Process>
  290. int interpolateImpl (double speedRatio,
  291. const float* input,
  292. float* output,
  293. int numOutputSamplesToProduce,
  294. Process process)
  295. {
  296. int numUsed = 0;
  297. interpolateImpl (speedRatio,
  298. output,
  299. numOutputSamplesToProduce,
  300. process,
  301. [this, input, &numUsed] { pushInterpolationSample (input[numUsed++]); });
  302. return numUsed;
  303. }
  304. template <typename Process, typename PushSample>
  305. void interpolateImpl (double speedRatio,
  306. float* output,
  307. int numOutputSamplesToProduce,
  308. Process process,
  309. PushSample pushSample)
  310. {
  311. auto pos = subSamplePos;
  312. for (auto i = 0; i < numOutputSamplesToProduce; ++i)
  313. {
  314. while (pos >= 1.0)
  315. {
  316. pushSample();
  317. pos -= 1.0;
  318. }
  319. *output = process (*output, InterpolatorTraits::valueAtOffset (lastInputSamples, (float) pos, indexBuffer));
  320. ++output;
  321. pos += speedRatio;
  322. }
  323. subSamplePos = pos;
  324. }
  325. //==============================================================================
  326. float lastInputSamples[(size_t) memorySize];
  327. double subSamplePos = 1.0;
  328. int indexBuffer = 0;
  329. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GenericInterpolator)
  330. };
  331. } // namespace juce