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.

501 lines
19KB

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