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.

494 lines
19KB

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