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.

183 lines
6.2KB

  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
  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. {
  32. (fn (std::get<Ix> (tuple), std::integral_constant<size_t, Ix>()), ...);
  33. }
  34. template <typename T>
  35. using TupleIndexSequence = std::make_index_sequence<std::tuple_size_v<std::remove_cv_t<std::remove_reference_t<T>>>>;
  36. template <typename Fn, typename Tuple>
  37. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple)
  38. {
  39. forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{});
  40. }
  41. template <typename Context, size_t Ix>
  42. inline constexpr auto useContextDirectly = ! Context::usesSeparateInputAndOutputBlocks() || Ix == 0;
  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, auto) { proc.prepare (spec); }, processors);
  67. }
  68. /** Reset all inner processors. */
  69. void reset()
  70. {
  71. detail::forEachInTuple ([] (auto& proc, auto) { 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 ([this, &context] (auto& proc, auto index) noexcept { this->processOne (context, proc, index); },
  78. processors);
  79. }
  80. private:
  81. template <typename Context, typename Proc, size_t Ix>
  82. void processOne (const Context& context, Proc& proc, std::integral_constant<size_t, Ix>) noexcept
  83. {
  84. if constexpr (detail::useContextDirectly<Context, Ix>)
  85. {
  86. auto contextCopy = context;
  87. contextCopy.isBypassed = (bypassed[Ix] || context.isBypassed);
  88. proc.process (contextCopy);
  89. }
  90. else
  91. {
  92. jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels());
  93. ProcessContextReplacing<typename Context::SampleType> replacingContext (context.getOutputBlock());
  94. replacingContext.isBypassed = (bypassed[Ix] || context.isBypassed);
  95. proc.process (replacingContext);
  96. }
  97. }
  98. std::tuple<Processors...> processors;
  99. std::array<bool, sizeof...(Processors)> bypassed { {} };
  100. };
  101. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  102. member template syntax.
  103. */
  104. template <int Index, typename... Processors>
  105. inline auto& get (ProcessorChain<Processors...>& chain) noexcept
  106. {
  107. return chain.template get<Index>();
  108. }
  109. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  110. member template syntax.
  111. */
  112. template <int Index, typename... Processors>
  113. inline auto& get (const ProcessorChain<Processors...>& chain) noexcept
  114. {
  115. return chain.template get<Index>();
  116. }
  117. /** Non-member equivalent of ProcessorChain::setBypassed which avoids awkward
  118. member template syntax.
  119. */
  120. template <int Index, typename... Processors>
  121. inline void setBypassed (ProcessorChain<Processors...>& chain, bool bypassed) noexcept
  122. {
  123. chain.template setBypassed<Index> (bypassed);
  124. }
  125. /** Non-member equivalent of ProcessorChain::isBypassed which avoids awkward
  126. member template syntax.
  127. */
  128. template <int Index, typename... Processors>
  129. inline bool isBypassed (const ProcessorChain<Processors...>& chain) noexcept
  130. {
  131. return chain.template isBypassed<Index>();
  132. }
  133. } // namespace dsp
  134. } // namespace juce
  135. #ifndef DOXYGEN
  136. namespace std
  137. {
  138. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wmismatched-tags")
  139. /** Adds support for C++17 structured bindings. */
  140. template <typename... Processors>
  141. struct tuple_size<::juce::dsp::ProcessorChain<Processors...>> : integral_constant<size_t, sizeof... (Processors)> {};
  142. /** Adds support for C++17 structured bindings. */
  143. template <size_t I, typename... Processors>
  144. struct tuple_element<I, ::juce::dsp::ProcessorChain<Processors...>> : tuple_element<I, tuple<Processors...>> {};
  145. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  146. } // namespace std
  147. #endif