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.

159 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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
  19. {
  20. namespace dsp
  21. {
  22. //==============================================================================
  23. #ifndef DOXYGEN
  24. /** The contents of this namespace are used to implement ProcessorChain and should
  25. not be used elsewhere. Their interfaces (and existence) are liable to change!
  26. */
  27. namespace detail
  28. {
  29. template <typename Fn, typename Tuple, size_t... Ix>
  30. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple, std::index_sequence<Ix...>)
  31. noexcept (noexcept (std::initializer_list<int> { (fn (std::get<Ix> (tuple), Ix), 0)... }))
  32. {
  33. (void) std::initializer_list<int> { ((void) fn (std::get<Ix> (tuple), Ix), 0)... };
  34. }
  35. template <typename T>
  36. using TupleIndexSequence = std::make_index_sequence<std::tuple_size<std::remove_cv_t<std::remove_reference_t<T>>>::value>;
  37. template <typename Fn, typename Tuple>
  38. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple)
  39. noexcept (noexcept (forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{})))
  40. {
  41. forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{});
  42. }
  43. }
  44. #endif
  45. /** This variadically-templated class lets you join together any number of processor
  46. classes into a single processor which will call process() on them all in sequence.
  47. @tags{DSP}
  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); }
  55. /** Get a reference to the processor at index `Index`. */
  56. template <int Index> const auto& get() const noexcept { return std::get<Index> (processors); }
  57. /** Set the processor at index `Index` to be bypassed or enabled. */
  58. template <int Index>
  59. void setBypassed (bool b) noexcept { bypassed[(size_t) Index] = b; }
  60. /** Query whether the processor at index `Index` is bypassed. */
  61. template <int Index>
  62. bool isBypassed() const noexcept { return bypassed[(size_t) Index]; }
  63. /** Prepare all inner processors with the provided `ProcessSpec`. */
  64. void prepare (const ProcessSpec& spec)
  65. {
  66. detail::forEachInTuple ([&] (auto& proc, size_t) { proc.prepare (spec); }, processors);
  67. }
  68. /** Reset all inner processors. */
  69. void reset()
  70. {
  71. detail::forEachInTuple ([] (auto& proc, size_t) { proc.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& proc, 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 = (bypassed[index] || context.isBypassed);
  84. proc.process (replacingContext);
  85. }
  86. else
  87. {
  88. ProcessContext contextCopy (context);
  89. contextCopy.isBypassed = (bypassed[index] || context.isBypassed);
  90. proc.process (contextCopy);
  91. }
  92. }, processors);
  93. }
  94. private:
  95. std::tuple<Processors...> processors;
  96. std::array<bool, sizeof...(Processors)> bypassed { {} };
  97. };
  98. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  99. member template syntax.
  100. */
  101. template <int Index, typename... Processors>
  102. inline auto& get (ProcessorChain<Processors...>& chain) noexcept
  103. {
  104. return chain.template get<Index>();
  105. }
  106. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  107. member template syntax.
  108. */
  109. template <int Index, typename... Processors>
  110. inline auto& get (const ProcessorChain<Processors...>& chain) noexcept
  111. {
  112. return chain.template get<Index>();
  113. }
  114. /** Non-member equivalent of ProcessorChain::setBypassed which avoids awkward
  115. member template syntax.
  116. */
  117. template <int Index, typename... Processors>
  118. inline void setBypassed (ProcessorChain<Processors...>& chain, bool bypassed) noexcept
  119. {
  120. chain.template setBypassed<Index> (bypassed);
  121. }
  122. /** Non-member equivalent of ProcessorChain::isBypassed which avoids awkward
  123. member template syntax.
  124. */
  125. template <int Index, typename... Processors>
  126. inline bool isBypassed (const ProcessorChain<Processors...>& chain) noexcept
  127. {
  128. return chain.template isBypassed<Index>();
  129. }
  130. } // namespace dsp
  131. } // namespace juce