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.

185 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. #if JUCE_UNIT_TESTS
  16. class InterpolatorTests : public UnitTest
  17. {
  18. public:
  19. InterpolatorTests()
  20. : UnitTest ("InterpolatorTests", UnitTestCategories::audio)
  21. {
  22. }
  23. private:
  24. template<typename InterpolatorType>
  25. void runInterplatorTests (const String& interpolatorName)
  26. {
  27. auto createGaussian = [](std::vector<float>& destination, float scale, float centreInSamples, float width)
  28. {
  29. for (size_t i = 0; i < destination.size(); ++i)
  30. {
  31. auto x = (((float) i) - centreInSamples) * width;
  32. destination[i] = std::exp (-(x * x));
  33. }
  34. FloatVectorOperations::multiply (destination.data(), scale, (int) destination.size());
  35. };
  36. auto findGaussianPeak = [](const std::vector<float>& input) -> float
  37. {
  38. auto max = std::max_element (std::begin (input), std::end (input));
  39. auto maxPrev = max - 1;
  40. jassert (maxPrev >= std::begin (input));
  41. auto maxNext = max + 1;
  42. jassert (maxNext < std::end (input));
  43. auto quadraticMaxLoc = (*maxPrev - *maxNext) / (2.0f * ((*maxNext + *maxPrev) - (2.0f * *max)));
  44. return quadraticMaxLoc + (float) std::distance (std::begin (input), max);
  45. };
  46. auto expectAllElementsWithin = [this](const std::vector<float>& v1, const std::vector<float>& v2, float tolerance)
  47. {
  48. expectEquals ((int) v1.size(), (int) v2.size());
  49. for (size_t i = 0; i < v1.size(); ++i)
  50. expectWithinAbsoluteError (v1[i], v2[i], tolerance);
  51. };
  52. InterpolatorType interpolator;
  53. constexpr size_t inputSize = 1001;
  54. static_assert (inputSize > 800 + InterpolatorType::getBaseLatency(),
  55. "The test InterpolatorTests input buffer is too small");
  56. std::vector<float> input (inputSize);
  57. constexpr auto inputGaussianMidpoint = (float) (inputSize - 1) / 2.0f;
  58. constexpr auto inputGaussianValueAtEnds = 0.000001f;
  59. const auto inputGaussianWidth = std::sqrt (-std::log (inputGaussianValueAtEnds)) / inputGaussianMidpoint;
  60. createGaussian (input, 1.0f, inputGaussianMidpoint, inputGaussianWidth);
  61. for (auto speedRatio : { 0.4, 0.8263, 1.0, 1.05, 1.2384, 1.6 })
  62. {
  63. const auto expectedGaussianMidpoint = (inputGaussianMidpoint + InterpolatorType::getBaseLatency()) / (float) speedRatio;
  64. const auto expectedGaussianWidth = inputGaussianWidth * (float) speedRatio;
  65. const auto outputBufferSize = (size_t) std::floor ((float) input.size() / speedRatio);
  66. for (int numBlocks : { 1, 5 })
  67. {
  68. const auto inputBlockSize = (float) input.size() / (float) numBlocks;
  69. const auto outputBlockSize = (int) std::floor (inputBlockSize / speedRatio);
  70. std::vector<float> output (outputBufferSize, std::numeric_limits<float>::min());
  71. beginTest (interpolatorName + " process " + String (numBlocks) + " blocks ratio " + String (speedRatio));
  72. interpolator.reset();
  73. {
  74. auto* inputPtr = input.data();
  75. auto* outputPtr = output.data();
  76. for (int i = 0; i < numBlocks; ++i)
  77. {
  78. auto numInputSamplesRead = interpolator.process (speedRatio, inputPtr, outputPtr, outputBlockSize);
  79. inputPtr += numInputSamplesRead;
  80. outputPtr += outputBlockSize;
  81. }
  82. }
  83. expectWithinAbsoluteError (findGaussianPeak (output), expectedGaussianMidpoint, 0.1f);
  84. std::vector<float> expectedOutput (output.size());
  85. createGaussian (expectedOutput, 1.0f, expectedGaussianMidpoint, expectedGaussianWidth);
  86. expectAllElementsWithin (output, expectedOutput, 0.02f);
  87. beginTest (interpolatorName + " process adding " + String (numBlocks) + " blocks ratio " + String (speedRatio));
  88. interpolator.reset();
  89. constexpr float addingGain = 0.7384f;
  90. {
  91. auto* inputPtr = input.data();
  92. auto* outputPtr = output.data();
  93. for (int i = 0; i < numBlocks; ++i)
  94. {
  95. auto numInputSamplesRead = interpolator.processAdding (speedRatio, inputPtr, outputPtr, outputBlockSize, addingGain);
  96. inputPtr += numInputSamplesRead;
  97. outputPtr += outputBlockSize;
  98. }
  99. }
  100. expectWithinAbsoluteError (findGaussianPeak (output), expectedGaussianMidpoint, 0.1f);
  101. std::vector<float> additionalOutput (output.size());
  102. createGaussian (additionalOutput, addingGain, expectedGaussianMidpoint, expectedGaussianWidth);
  103. FloatVectorOperations::add (expectedOutput.data(), additionalOutput.data(), (int) additionalOutput.size());
  104. expectAllElementsWithin (output, expectedOutput, 0.02f);
  105. }
  106. beginTest (interpolatorName + " process wrap 0 ratio " + String (speedRatio));
  107. std::vector<float> doubleLengthOutput (2 * outputBufferSize, std::numeric_limits<float>::min());
  108. interpolator.reset();
  109. interpolator.process (speedRatio, input.data(), doubleLengthOutput.data(), (int) doubleLengthOutput.size(),
  110. (int) input.size(), 0);
  111. std::vector<float> expectedDoubleLengthOutput (doubleLengthOutput.size());
  112. createGaussian (expectedDoubleLengthOutput, 1.0f, expectedGaussianMidpoint, expectedGaussianWidth);
  113. expectAllElementsWithin (doubleLengthOutput, expectedDoubleLengthOutput, 0.02f);
  114. beginTest (interpolatorName + " process wrap double ratio " + String (speedRatio));
  115. interpolator.reset();
  116. interpolator.process (speedRatio, input.data(), doubleLengthOutput.data(), (int) doubleLengthOutput.size(),
  117. (int) input.size(), (int) input.size());
  118. std::vector<float> secondGaussian (doubleLengthOutput.size());
  119. createGaussian (secondGaussian, 1.0f, expectedGaussianMidpoint + outputBufferSize, expectedGaussianWidth);
  120. FloatVectorOperations::add (expectedDoubleLengthOutput.data(), secondGaussian.data(), (int) expectedDoubleLengthOutput.size());
  121. expectAllElementsWithin (doubleLengthOutput, expectedDoubleLengthOutput, 0.02f);
  122. }
  123. }
  124. public:
  125. void runTest() override
  126. {
  127. runInterplatorTests<WindowedSincInterpolator> ("WindowedSincInterpolator");
  128. runInterplatorTests<LagrangeInterpolator> ("LagrangeInterpolator");
  129. runInterplatorTests<CatmullRomInterpolator> ("CatmullRomInterpolator");
  130. runInterplatorTests<LinearInterpolator> ("LinearInterpolator");
  131. }
  132. };
  133. static InterpolatorTests interpolatorTests;
  134. #endif
  135. } // namespace juce