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_LagrangeInterpolator.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. namespace
  20. {
  21. static forcedinline void pushInterpolationSample (float* lastInputSamples, const float newValue) noexcept
  22. {
  23. lastInputSamples[4] = lastInputSamples[3];
  24. lastInputSamples[3] = lastInputSamples[2];
  25. lastInputSamples[2] = lastInputSamples[1];
  26. lastInputSamples[1] = lastInputSamples[0];
  27. lastInputSamples[0] = newValue;
  28. }
  29. static forcedinline void pushInterpolationSamples (float* lastInputSamples, const float* input, int numOut) noexcept
  30. {
  31. if (numOut >= 5)
  32. {
  33. for (int i = 0; i < 5; ++i)
  34. lastInputSamples[i] = input[--numOut];
  35. }
  36. else
  37. {
  38. for (int i = 0; i < numOut; ++i)
  39. pushInterpolationSample (lastInputSamples, input[i]);
  40. }
  41. }
  42. template <typename InterpolatorType>
  43. static int interpolate (float* lastInputSamples, double& subSamplePos, double actualRatio,
  44. const float* in, float* out, int numOut) noexcept
  45. {
  46. auto pos = subSamplePos;
  47. if (actualRatio == 1.0 && pos == 1.0)
  48. {
  49. memcpy (out, in, (size_t) numOut * sizeof (float));
  50. pushInterpolationSamples (lastInputSamples, in, numOut);
  51. return numOut;
  52. }
  53. int numUsed = 0;
  54. while (numOut > 0)
  55. {
  56. while (pos >= 1.0)
  57. {
  58. pushInterpolationSample (lastInputSamples, in[numUsed++]);
  59. pos -= 1.0;
  60. }
  61. *out++ = InterpolatorType::valueAtOffset (lastInputSamples, (float) pos);
  62. pos += actualRatio;
  63. --numOut;
  64. }
  65. subSamplePos = pos;
  66. return numUsed;
  67. }
  68. template <typename InterpolatorType>
  69. static int interpolateAdding (float* lastInputSamples, double& subSamplePos, double actualRatio,
  70. const float* in, float* out, int numOut, const float gain) noexcept
  71. {
  72. auto pos = subSamplePos;
  73. if (actualRatio == 1.0 && pos == 1.0)
  74. {
  75. FloatVectorOperations::addWithMultiply (out, in, gain, numOut);
  76. pushInterpolationSamples (lastInputSamples, in, numOut);
  77. return numOut;
  78. }
  79. int numUsed = 0;
  80. while (numOut > 0)
  81. {
  82. while (pos >= 1.0)
  83. {
  84. pushInterpolationSample (lastInputSamples, in[numUsed++]);
  85. pos -= 1.0;
  86. }
  87. *out++ += gain * InterpolatorType::valueAtOffset (lastInputSamples, (float) pos);
  88. pos += actualRatio;
  89. --numOut;
  90. }
  91. subSamplePos = pos;
  92. return numUsed;
  93. }
  94. }
  95. //==============================================================================
  96. template <int k>
  97. struct LagrangeResampleHelper
  98. {
  99. static forcedinline void calc (float& a, float b) noexcept { a *= b * (1.0f / k); }
  100. };
  101. template<>
  102. struct LagrangeResampleHelper<0>
  103. {
  104. static forcedinline void calc (float&, float) noexcept {}
  105. };
  106. struct LagrangeAlgorithm
  107. {
  108. static forcedinline float valueAtOffset (const float* const inputs, const float offset) noexcept
  109. {
  110. return calcCoefficient<0> (inputs[4], offset)
  111. + calcCoefficient<1> (inputs[3], offset)
  112. + calcCoefficient<2> (inputs[2], offset)
  113. + calcCoefficient<3> (inputs[1], offset)
  114. + calcCoefficient<4> (inputs[0], offset);
  115. }
  116. template <int k>
  117. static forcedinline float calcCoefficient (float input, const float offset) noexcept
  118. {
  119. LagrangeResampleHelper<0 - k>::calc (input, -2.0f - offset);
  120. LagrangeResampleHelper<1 - k>::calc (input, -1.0f - offset);
  121. LagrangeResampleHelper<2 - k>::calc (input, 0.0f - offset);
  122. LagrangeResampleHelper<3 - k>::calc (input, 1.0f - offset);
  123. LagrangeResampleHelper<4 - k>::calc (input, 2.0f - offset);
  124. return input;
  125. }
  126. };
  127. LagrangeInterpolator::LagrangeInterpolator() noexcept { reset(); }
  128. LagrangeInterpolator::~LagrangeInterpolator() noexcept {}
  129. void LagrangeInterpolator::reset() noexcept
  130. {
  131. subSamplePos = 1.0;
  132. for (auto& s : lastInputSamples)
  133. s = 0;
  134. }
  135. int LagrangeInterpolator::process (double actualRatio, const float* in, float* out, int numOut) noexcept
  136. {
  137. return interpolate<LagrangeAlgorithm> (lastInputSamples, subSamplePos, actualRatio, in, out, numOut);
  138. }
  139. int LagrangeInterpolator::processAdding (double actualRatio, const float* in, float* out, int numOut, float gain) noexcept
  140. {
  141. return interpolateAdding<LagrangeAlgorithm> (lastInputSamples, subSamplePos, actualRatio, in, out, numOut, gain);
  142. }
  143. } // namespace juce