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.

143 lines
4.5KB

  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. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. class ProcessorChainTest : public UnitTest
  24. {
  25. template <int AddValue>
  26. struct MockProcessor
  27. {
  28. void prepare (const ProcessSpec&) noexcept { isPrepared = true; }
  29. void reset() noexcept { isReset = true; }
  30. template <typename Context>
  31. void process (const Context& context) noexcept
  32. {
  33. bufferWasClear = context.getInputBlock().getSample (0, 0) == 0;
  34. if (! context.isBypassed)
  35. context.getOutputBlock().add (AddValue);
  36. }
  37. bool isPrepared = false;
  38. bool isReset = false;
  39. bool bufferWasClear = false;
  40. };
  41. public:
  42. ProcessorChainTest()
  43. : UnitTest ("ProcessorChain", UnitTestCategories::dsp) {}
  44. void runTest() override
  45. {
  46. beginTest ("After calling setBypass, processor is bypassed");
  47. {
  48. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  49. setBypassed<0> (chain, true);
  50. expect (isBypassed<0> (chain));
  51. setBypassed<0> (chain, false);
  52. expect (! isBypassed<0> (chain));
  53. setBypassed<1> (chain, true);
  54. expect (isBypassed<1> (chain));
  55. setBypassed<1> (chain, false);
  56. expect (! isBypassed<1> (chain));
  57. }
  58. beginTest ("After calling prepare, all processors are prepared");
  59. {
  60. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  61. expect (! get<0> (chain).isPrepared);
  62. expect (! get<1> (chain).isPrepared);
  63. chain.prepare (ProcessSpec{});
  64. expect (get<0> (chain).isPrepared);
  65. expect (get<1> (chain).isPrepared);
  66. }
  67. beginTest ("After calling reset, all processors are reset");
  68. {
  69. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  70. expect (! get<0> (chain).isReset);
  71. expect (! get<1> (chain).isReset);
  72. chain.reset();
  73. expect (get<0> (chain).isReset);
  74. expect (get<1> (chain).isReset);
  75. }
  76. beginTest ("After calling process, all processors contribute to processing");
  77. {
  78. ProcessorChain<MockProcessor<1>, MockProcessor<2>> chain;
  79. AudioBuffer<float> buffer (1, 1);
  80. AudioBlock<float> block (buffer);
  81. ProcessContextReplacing<float> context (block);
  82. block.clear();
  83. chain.process (context);
  84. expectEquals (buffer.getSample (0, 0), 3.0f);
  85. expect (get<0> (chain).bufferWasClear);
  86. expect (! get<1> (chain).bufferWasClear);
  87. setBypassed<0> (chain, true);
  88. block.clear();
  89. chain.process (context);
  90. expectEquals (buffer.getSample (0, 0), 2.0f);
  91. expect (get<0> (chain).bufferWasClear);
  92. expect (get<1> (chain).bufferWasClear);
  93. setBypassed<1> (chain, true);
  94. block.clear();
  95. chain.process (context);
  96. expectEquals (buffer.getSample (0, 0), 0.0f);
  97. expect (get<0> (chain).bufferWasClear);
  98. expect (get<1> (chain).bufferWasClear);
  99. setBypassed<0> (chain, false);
  100. block.clear();
  101. chain.process (context);
  102. expectEquals (buffer.getSample (0, 0), 1.0f);
  103. expect (get<0> (chain).bufferWasClear);
  104. expect (! get<1> (chain).bufferWasClear);
  105. }
  106. }
  107. };
  108. static ProcessorChainTest processorChainUnitTest;
  109. } // namespace dsp
  110. } // namespace juce