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.

498 lines
19KB

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