The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

165 lines
5.5KB

  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. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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::dsp
  19. {
  20. class ProcessorChainTest final : public UnitTest
  21. {
  22. template <int AddValue>
  23. struct MockProcessor
  24. {
  25. void prepare (const ProcessSpec&) noexcept { isPrepared = true; }
  26. void reset() noexcept { isReset = true; }
  27. template <typename Context>
  28. void process (const Context& context) noexcept
  29. {
  30. bufferWasClear = approximatelyEqual (context.getInputBlock().getSample (0, 0), 0.0f);
  31. if (! context.isBypassed)
  32. context.getOutputBlock().add (AddValue);
  33. }
  34. bool isPrepared = false;
  35. bool isReset = false;
  36. bool bufferWasClear = false;
  37. };
  38. public:
  39. ProcessorChainTest()
  40. : UnitTest ("ProcessorChain", UnitTestCategories::dsp) {}
  41. void runTest() override
  42. {
  43. beginTest ("After calling setBypass, processor is bypassed");
  44. {
  45. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  46. setBypassed<0> (chain, true);
  47. expect (isBypassed<0> (chain));
  48. setBypassed<0> (chain, false);
  49. expect (! isBypassed<0> (chain));
  50. setBypassed<1> (chain, true);
  51. expect (isBypassed<1> (chain));
  52. setBypassed<1> (chain, false);
  53. expect (! isBypassed<1> (chain));
  54. }
  55. beginTest ("After calling prepare, all processors are prepared");
  56. {
  57. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  58. expect (! get<0> (chain).isPrepared);
  59. expect (! get<1> (chain).isPrepared);
  60. chain.prepare (ProcessSpec{});
  61. expect (get<0> (chain).isPrepared);
  62. expect (get<1> (chain).isPrepared);
  63. }
  64. beginTest ("After calling reset, all processors are reset");
  65. {
  66. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  67. expect (! get<0> (chain).isReset);
  68. expect (! get<1> (chain).isReset);
  69. chain.reset();
  70. expect (get<0> (chain).isReset);
  71. expect (get<1> (chain).isReset);
  72. }
  73. beginTest ("After calling process, all processors contribute to processing");
  74. {
  75. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  76. AudioBuffer<float> buffer (1, 1);
  77. AudioBlock<float> block (buffer);
  78. ProcessContextReplacing<float> context (block);
  79. block.clear();
  80. chain.process (context);
  81. expectEquals (buffer.getSample (0, 0), 3.0f);
  82. expect (get<0> (chain).bufferWasClear);
  83. expect (! get<1> (chain).bufferWasClear);
  84. setBypassed<0> (chain, true);
  85. block.clear();
  86. chain.process (context);
  87. expectEquals (buffer.getSample (0, 0), 2.0f);
  88. expect (get<0> (chain).bufferWasClear);
  89. expect (get<1> (chain).bufferWasClear);
  90. setBypassed<1> (chain, true);
  91. block.clear();
  92. chain.process (context);
  93. expectEquals (buffer.getSample (0, 0), 0.0f);
  94. expect (get<0> (chain).bufferWasClear);
  95. expect (get<1> (chain).bufferWasClear);
  96. setBypassed<0> (chain, false);
  97. block.clear();
  98. chain.process (context);
  99. expectEquals (buffer.getSample (0, 0), 1.0f);
  100. expect (get<0> (chain).bufferWasClear);
  101. expect (! get<1> (chain).bufferWasClear);
  102. }
  103. beginTest ("Chains with trailing items that only support replacing contexts can be built");
  104. {
  105. AudioBuffer<float> inBuf (1, 1), outBuf (1, 1);
  106. juce::dsp::AudioBlock<float> in (inBuf), out (outBuf);
  107. struct OnlyReplacing
  108. {
  109. void prepare (const juce::dsp::ProcessSpec&) {}
  110. void process (const juce::dsp::ProcessContextReplacing<float>& c)
  111. {
  112. c.getOutputBlock().multiplyBy (2.0f);
  113. }
  114. void reset() {}
  115. };
  116. {
  117. juce::dsp::ProcessorChain<juce::dsp::Gain<float>, OnlyReplacing, OnlyReplacing> c;
  118. juce::dsp::ProcessContextNonReplacing<float> context (in, out);
  119. get<0> (c).setGainLinear (1.0f);
  120. c.prepare (ProcessSpec{});
  121. inBuf.setSample (0, 0, 1.0f);
  122. c.process (context);
  123. expectEquals (outBuf.getSample (0, 0), 4.0f);
  124. }
  125. }
  126. }
  127. };
  128. static ProcessorChainTest processorChainUnitTest;
  129. } // namespace juce::dsp