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.

189 lines
8.0KB

  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. #if JUCE_UNIT_TESTS
  20. class InterpolatorTests : public UnitTest
  21. {
  22. public:
  23. InterpolatorTests()
  24. : UnitTest ("InterpolatorTests", UnitTestCategories::audio)
  25. {
  26. }
  27. private:
  28. template <typename InterpolatorType>
  29. void runInterplatorTests (const String& interpolatorName)
  30. {
  31. auto createGaussian = [] (std::vector<float>& destination, float scale, float centreInSamples, float width)
  32. {
  33. for (size_t i = 0; i < destination.size(); ++i)
  34. {
  35. auto x = (((float) i) - centreInSamples) * width;
  36. destination[i] = std::exp (-(x * x));
  37. }
  38. FloatVectorOperations::multiply (destination.data(), scale, (int) destination.size());
  39. };
  40. auto findGaussianPeak = [] (const std::vector<float>& input) -> float
  41. {
  42. auto max = std::max_element (std::begin (input), std::end (input));
  43. auto maxPrev = max - 1;
  44. jassert (maxPrev >= std::begin (input));
  45. auto maxNext = max + 1;
  46. jassert (maxNext < std::end (input));
  47. auto quadraticMaxLoc = (*maxPrev - *maxNext) / (2.0f * ((*maxNext + *maxPrev) - (2.0f * *max)));
  48. return quadraticMaxLoc + (float) std::distance (std::begin (input), max);
  49. };
  50. auto expectAllElementsWithin = [this] (const std::vector<float>& v1, const std::vector<float>& v2, float tolerance)
  51. {
  52. expectEquals ((int) v1.size(), (int) v2.size());
  53. for (size_t i = 0; i < v1.size(); ++i)
  54. expectWithinAbsoluteError (v1[i], v2[i], tolerance);
  55. };
  56. InterpolatorType interpolator;
  57. constexpr size_t inputSize = 1001;
  58. static_assert (inputSize > 800 + InterpolatorType::getBaseLatency(),
  59. "The test InterpolatorTests input buffer is too small");
  60. std::vector<float> input (inputSize);
  61. constexpr auto inputGaussianMidpoint = (float) (inputSize - 1) / 2.0f;
  62. constexpr auto inputGaussianValueAtEnds = 0.000001f;
  63. const auto inputGaussianWidth = std::sqrt (-std::log (inputGaussianValueAtEnds)) / inputGaussianMidpoint;
  64. createGaussian (input, 1.0f, inputGaussianMidpoint, inputGaussianWidth);
  65. for (auto speedRatio : { 0.4, 0.8263, 1.0, 1.05, 1.2384, 1.6 })
  66. {
  67. const auto expectedGaussianMidpoint = (inputGaussianMidpoint + InterpolatorType::getBaseLatency()) / (float) speedRatio;
  68. const auto expectedGaussianWidth = inputGaussianWidth * (float) speedRatio;
  69. const auto outputBufferSize = (size_t) std::floor ((float) input.size() / speedRatio);
  70. for (int numBlocks : { 1, 5 })
  71. {
  72. const auto inputBlockSize = (float) input.size() / (float) numBlocks;
  73. const auto outputBlockSize = (int) std::floor (inputBlockSize / speedRatio);
  74. std::vector<float> output (outputBufferSize, std::numeric_limits<float>::min());
  75. beginTest (interpolatorName + " process " + String (numBlocks) + " blocks ratio " + String (speedRatio));
  76. interpolator.reset();
  77. {
  78. auto* inputPtr = input.data();
  79. auto* outputPtr = output.data();
  80. for (int i = 0; i < numBlocks; ++i)
  81. {
  82. auto numInputSamplesRead = interpolator.process (speedRatio, inputPtr, outputPtr, outputBlockSize);
  83. inputPtr += numInputSamplesRead;
  84. outputPtr += outputBlockSize;
  85. }
  86. }
  87. expectWithinAbsoluteError (findGaussianPeak (output), expectedGaussianMidpoint, 0.1f);
  88. std::vector<float> expectedOutput (output.size());
  89. createGaussian (expectedOutput, 1.0f, expectedGaussianMidpoint, expectedGaussianWidth);
  90. expectAllElementsWithin (output, expectedOutput, 0.02f);
  91. beginTest (interpolatorName + " process adding " + String (numBlocks) + " blocks ratio " + String (speedRatio));
  92. interpolator.reset();
  93. constexpr float addingGain = 0.7384f;
  94. {
  95. auto* inputPtr = input.data();
  96. auto* outputPtr = output.data();
  97. for (int i = 0; i < numBlocks; ++i)
  98. {
  99. auto numInputSamplesRead = interpolator.processAdding (speedRatio, inputPtr, outputPtr, outputBlockSize, addingGain);
  100. inputPtr += numInputSamplesRead;
  101. outputPtr += outputBlockSize;
  102. }
  103. }
  104. expectWithinAbsoluteError (findGaussianPeak (output), expectedGaussianMidpoint, 0.1f);
  105. std::vector<float> additionalOutput (output.size());
  106. createGaussian (additionalOutput, addingGain, expectedGaussianMidpoint, expectedGaussianWidth);
  107. FloatVectorOperations::add (expectedOutput.data(), additionalOutput.data(), (int) additionalOutput.size());
  108. expectAllElementsWithin (output, expectedOutput, 0.02f);
  109. }
  110. beginTest (interpolatorName + " process wrap 0 ratio " + String (speedRatio));
  111. std::vector<float> doubleLengthOutput (2 * outputBufferSize, std::numeric_limits<float>::min());
  112. interpolator.reset();
  113. interpolator.process (speedRatio, input.data(), doubleLengthOutput.data(), (int) doubleLengthOutput.size(),
  114. (int) input.size(), 0);
  115. std::vector<float> expectedDoubleLengthOutput (doubleLengthOutput.size());
  116. createGaussian (expectedDoubleLengthOutput, 1.0f, expectedGaussianMidpoint, expectedGaussianWidth);
  117. expectAllElementsWithin (doubleLengthOutput, expectedDoubleLengthOutput, 0.02f);
  118. beginTest (interpolatorName + " process wrap double ratio " + String (speedRatio));
  119. interpolator.reset();
  120. interpolator.process (speedRatio, input.data(), doubleLengthOutput.data(), (int) doubleLengthOutput.size(),
  121. (int) input.size(), (int) input.size());
  122. std::vector<float> secondGaussian (doubleLengthOutput.size());
  123. createGaussian (secondGaussian, 1.0f, expectedGaussianMidpoint + (float) outputBufferSize, expectedGaussianWidth);
  124. FloatVectorOperations::add (expectedDoubleLengthOutput.data(), secondGaussian.data(), (int) expectedDoubleLengthOutput.size());
  125. expectAllElementsWithin (doubleLengthOutput, expectedDoubleLengthOutput, 0.02f);
  126. }
  127. }
  128. public:
  129. void runTest() override
  130. {
  131. runInterplatorTests<WindowedSincInterpolator> ("WindowedSincInterpolator");
  132. runInterplatorTests<LagrangeInterpolator> ("LagrangeInterpolator");
  133. runInterplatorTests<CatmullRomInterpolator> ("CatmullRomInterpolator");
  134. runInterplatorTests<LinearInterpolator> ("LinearInterpolator");
  135. }
  136. };
  137. static InterpolatorTests interpolatorTests;
  138. #endif
  139. } // namespace juce