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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace LagrangeHelpers
  18. {
  19. template <int k>
  20. struct ResampleHelper
  21. {
  22. static forcedinline void calc (float& a, float b) { a *= b * (1.0f / k); }
  23. };
  24. template<>
  25. struct ResampleHelper <0>
  26. {
  27. static forcedinline void calc (float&, float) {}
  28. };
  29. template <int k>
  30. static forcedinline float calcCoefficient (float input, const float offset) noexcept
  31. {
  32. ResampleHelper <0 - k>::calc (input, -2.0f - offset);
  33. ResampleHelper <1 - k>::calc (input, -1.0f - offset);
  34. ResampleHelper <2 - k>::calc (input, 0.0f - offset);
  35. ResampleHelper <3 - k>::calc (input, 1.0f - offset);
  36. ResampleHelper <4 - k>::calc (input, 2.0f - offset);
  37. return input;
  38. }
  39. static forcedinline float valueAtOffset (const float* const inputs, const float offset) noexcept
  40. {
  41. return calcCoefficient<0> (inputs[4], offset)
  42. + calcCoefficient<1> (inputs[3], offset)
  43. + calcCoefficient<2> (inputs[2], offset)
  44. + calcCoefficient<3> (inputs[1], offset)
  45. + calcCoefficient<4> (inputs[0], offset);
  46. }
  47. static forcedinline void push (float* inputs, const float newValue) noexcept
  48. {
  49. inputs[4] = inputs[3];
  50. inputs[3] = inputs[2];
  51. inputs[2] = inputs[1];
  52. inputs[1] = inputs[0];
  53. inputs[0] = newValue;
  54. }
  55. }
  56. //==============================================================================
  57. LagrangeInterpolator::LagrangeInterpolator() { reset(); }
  58. LagrangeInterpolator::~LagrangeInterpolator() {}
  59. void LagrangeInterpolator::reset() noexcept
  60. {
  61. subSamplePos = 1.0;
  62. for (int i = 0; i < numElementsInArray (lastInputSamples); ++i)
  63. lastInputSamples[i] = 0;
  64. }
  65. int LagrangeInterpolator::process (const double actualRatio, const float* in,
  66. float* out, const int numOut) noexcept
  67. {
  68. if (actualRatio == 1.0)
  69. {
  70. memcpy (out, in, (size_t) numOut * sizeof (float));
  71. if (numOut >= 4)
  72. {
  73. memcpy (lastInputSamples, in + (numOut - 4), 4 * sizeof (float));
  74. }
  75. else
  76. {
  77. for (int i = 0; i < numOut; ++i)
  78. LagrangeHelpers::push (lastInputSamples, in[i]);
  79. }
  80. return numOut;
  81. }
  82. const float* const originalIn = in;
  83. double pos = subSamplePos;
  84. if (actualRatio < 1.0)
  85. {
  86. for (int i = numOut; --i >= 0;)
  87. {
  88. if (pos >= 1.0)
  89. {
  90. LagrangeHelpers::push (lastInputSamples, *in++);
  91. pos -= 1.0;
  92. }
  93. *out++ = LagrangeHelpers::valueAtOffset (lastInputSamples, (float) pos);
  94. pos += actualRatio;
  95. }
  96. }
  97. else
  98. {
  99. for (int i = numOut; --i >= 0;)
  100. {
  101. while (pos < actualRatio)
  102. {
  103. LagrangeHelpers::push (lastInputSamples, *in++);
  104. pos += 1.0;
  105. }
  106. pos -= actualRatio;
  107. *out++ = LagrangeHelpers::valueAtOffset (lastInputSamples, 1.0f - (float) pos);
  108. }
  109. }
  110. subSamplePos = pos;
  111. return (int) (in - originalIn);
  112. }
  113. int LagrangeInterpolator::processAdding (const double actualRatio, const float* in,
  114. float* out, const int numOut, const float gain) noexcept
  115. {
  116. if (actualRatio == 1.0)
  117. {
  118. if (gain != 1.0f)
  119. {
  120. for (int i = 0; i < numOut; ++i)
  121. out[i] += in[i] * gain;
  122. }
  123. else
  124. {
  125. for (int i = 0; i < numOut; ++i)
  126. out[i] += in[i];
  127. }
  128. if (numOut >= 4)
  129. {
  130. memcpy (lastInputSamples, in + (numOut - 4), 4 * sizeof (float));
  131. }
  132. else
  133. {
  134. for (int i = 0; i < numOut; ++i)
  135. LagrangeHelpers::push (lastInputSamples, in[i]);
  136. }
  137. return numOut;
  138. }
  139. const float* const originalIn = in;
  140. double pos = subSamplePos;
  141. if (actualRatio < 1.0)
  142. {
  143. for (int i = numOut; --i >= 0;)
  144. {
  145. if (pos >= 1.0)
  146. {
  147. LagrangeHelpers::push (lastInputSamples, *in++);
  148. pos -= 1.0;
  149. }
  150. *out++ += gain * LagrangeHelpers::valueAtOffset (lastInputSamples, (float) pos);
  151. pos += actualRatio;
  152. }
  153. }
  154. else
  155. {
  156. for (int i = numOut; --i >= 0;)
  157. {
  158. while (pos < actualRatio)
  159. {
  160. LagrangeHelpers::push (lastInputSamples, *in++);
  161. pos += 1.0;
  162. }
  163. pos -= actualRatio;
  164. *out++ += gain * LagrangeHelpers::valueAtOffset (lastInputSamples, jmax (0.0f, 1.0f - (float) pos));
  165. }
  166. }
  167. subSamplePos = pos;
  168. return (int) (in - originalIn);
  169. }