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.

164 lines
5.8KB

  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. //==============================================================================
  24. #ifndef DOXYGEN
  25. /** The contents of this namespace are used to implement ProcessorChain and should
  26. not be used elsewhere. Their interfaces (and existence) are liable to change!
  27. */
  28. namespace detail
  29. {
  30. template <typename Fn, typename Tuple, size_t... Ix>
  31. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple, std::index_sequence<Ix...>)
  32. noexcept (noexcept (std::initializer_list<int> { (fn (std::get<Ix> (tuple), Ix), 0)... }))
  33. {
  34. (void) std::initializer_list<int> { ((void) fn (std::get<Ix> (tuple), Ix), 0)... };
  35. }
  36. template <typename T>
  37. using TupleIndexSequence = std::make_index_sequence<std::tuple_size<std::remove_cv_t<std::remove_reference_t<T>>>::value>;
  38. template <typename Fn, typename Tuple>
  39. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple)
  40. noexcept (noexcept (forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{})))
  41. {
  42. forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{});
  43. }
  44. }
  45. #endif
  46. /** This variadically-templated class lets you join together any number of processor
  47. classes into a single processor which will call process() on them all in sequence.
  48. */
  49. template <typename... Processors>
  50. class ProcessorChain
  51. {
  52. public:
  53. /** Get a reference to the processor at index `Index`. */
  54. template <int Index> auto& get() noexcept { return std::get<Index> (processors).processor; }
  55. /** Get a reference to the processor at index `Index`. */
  56. template <int Index> const auto& get() const noexcept { return std::get<Index> (processors).processor; }
  57. /** Set the processor at index `Index` to be bypassed or enabled. */
  58. template <int Index>
  59. void setBypassed (bool b) noexcept { std::get<Index> (processors).isBypassed = b; }
  60. /** Query whether the processor at index `Index` is bypassed. */
  61. template <int Index>
  62. bool isBypassed() const noexcept { return std::get<Index> (processors).isBypassed; }
  63. /** Prepare all inner processors with the provided `ProcessSpec`. */
  64. void prepare (const ProcessSpec& spec)
  65. {
  66. detail::forEachInTuple ([&] (auto& item, size_t) { item.processor.prepare (spec); }, processors);
  67. }
  68. /** Reset all inner processors. */
  69. void reset()
  70. {
  71. detail::forEachInTuple ([] (auto& item, size_t) { item.processor.reset(); }, processors);
  72. }
  73. /** Process `context` through all inner processors in sequence. */
  74. template <typename ProcessContext>
  75. void process (const ProcessContext& context) noexcept
  76. {
  77. detail::forEachInTuple ([&] (auto& item, size_t index) noexcept
  78. {
  79. if (context.usesSeparateInputAndOutputBlocks() && index != 0)
  80. {
  81. jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels());
  82. ProcessContextReplacing<typename ProcessContext::SampleType> replacingContext (context.getOutputBlock());
  83. replacingContext.isBypassed = (item.isBypassed || context.isBypassed);
  84. item.processor.process (replacingContext);
  85. }
  86. else
  87. {
  88. ProcessContext contextCopy (context);
  89. contextCopy.isBypassed = (item.isBypassed || context.isBypassed);
  90. item.processor.process (contextCopy);
  91. }
  92. }, processors);
  93. }
  94. private:
  95. template <typename Processor>
  96. struct ProcessorWithBypass
  97. {
  98. Processor processor;
  99. bool isBypassed = false;
  100. };
  101. std::tuple<ProcessorWithBypass<Processors>...> processors;
  102. };
  103. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  104. member template syntax.
  105. */
  106. template <int Index, typename... Processors>
  107. inline auto& get (ProcessorChain<Processors...>& chain) noexcept
  108. {
  109. return chain.template get<Index>();
  110. }
  111. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  112. member template syntax.
  113. */
  114. template <int Index, typename... Processors>
  115. inline auto& get (const ProcessorChain<Processors...>& chain) noexcept
  116. {
  117. return chain.template get<Index>();
  118. }
  119. /** Non-member equivalent of ProcessorChain::setBypassed which avoids awkward
  120. member template syntax.
  121. */
  122. template <int Index, typename... Processors>
  123. inline void setBypassed (ProcessorChain<Processors...>& chain, bool bypassed) noexcept
  124. {
  125. chain.template setBypassed<Index> (bypassed);
  126. }
  127. /** Non-member equivalent of ProcessorChain::isBypassed which avoids awkward
  128. member template syntax.
  129. */
  130. template <int Index, typename... Processors>
  131. inline bool isBypassed (const ProcessorChain<Processors...>& chain) noexcept
  132. {
  133. return chain.template isBypassed<Index>();
  134. }
  135. } // namespace dsp
  136. } // namespace juce