Audio plugin host https://kx.studio/carla
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.

juce_ProcessorChain.h 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. (void) std::initializer_list<int> { ((void) fn (std::get<Ix> (tuple), std::integral_constant<size_t, Ix>()), 0)... };
  33. }
  34. template <typename T>
  35. using TupleIndexSequence = std::make_index_sequence<std::tuple_size<std::remove_cv_t<std::remove_reference_t<T>>>::value>;
  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. // This could be a template variable, but that code causes an internal compiler error in MSVC 19.00.24215
  42. template <typename Context, size_t Ix>
  43. struct UseContextDirectly
  44. {
  45. static constexpr auto value = ! Context::usesSeparateInputAndOutputBlocks() || Ix == 0;
  46. };
  47. }
  48. #endif
  49. /** This variadically-templated class lets you join together any number of processor
  50. classes into a single processor which will call process() on them all in sequence.
  51. @tags{DSP}
  52. */
  53. template <typename... Processors>
  54. class ProcessorChain
  55. {
  56. public:
  57. /** Get a reference to the processor at index `Index`. */
  58. template <int Index> auto& get() noexcept { return std::get<Index> (processors); }
  59. /** Get a reference to the processor at index `Index`. */
  60. template <int Index> const auto& get() const noexcept { return std::get<Index> (processors); }
  61. /** Set the processor at index `Index` to be bypassed or enabled. */
  62. template <int Index>
  63. void setBypassed (bool b) noexcept { bypassed[(size_t) Index] = b; }
  64. /** Query whether the processor at index `Index` is bypassed. */
  65. template <int Index>
  66. bool isBypassed() const noexcept { return bypassed[(size_t) Index]; }
  67. /** Prepare all inner processors with the provided `ProcessSpec`. */
  68. void prepare (const ProcessSpec& spec)
  69. {
  70. detail::forEachInTuple ([&] (auto& proc, auto) { proc.prepare (spec); }, processors);
  71. }
  72. /** Reset all inner processors. */
  73. void reset()
  74. {
  75. detail::forEachInTuple ([] (auto& proc, auto) { proc.reset(); }, processors);
  76. }
  77. /** Process `context` through all inner processors in sequence. */
  78. template <typename ProcessContext>
  79. void process (const ProcessContext& context) noexcept
  80. {
  81. detail::forEachInTuple ([this, &context] (auto& proc, auto index) noexcept { this->processOne (context, proc, index); },
  82. processors);
  83. }
  84. private:
  85. template <typename Context, typename Proc, size_t Ix, std::enable_if_t<! detail::UseContextDirectly<Context, Ix>::value, int> = 0>
  86. void processOne (const Context& context, Proc& proc, std::integral_constant<size_t, Ix>) noexcept
  87. {
  88. jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels());
  89. ProcessContextReplacing<typename Context::SampleType> replacingContext (context.getOutputBlock());
  90. replacingContext.isBypassed = (bypassed[Ix] || context.isBypassed);
  91. proc.process (replacingContext);
  92. }
  93. template <typename Context, typename Proc, size_t Ix, std::enable_if_t<detail::UseContextDirectly<Context, Ix>::value, int> = 0>
  94. void processOne (const Context& context, Proc& proc, std::integral_constant<size_t, Ix>) noexcept
  95. {
  96. auto contextCopy = context;
  97. contextCopy.isBypassed = (bypassed[Ix] || context.isBypassed);
  98. proc.process (contextCopy);
  99. }
  100. std::tuple<Processors...> processors;
  101. std::array<bool, sizeof...(Processors)> bypassed { {} };
  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
  137. #ifndef DOXYGEN
  138. namespace std
  139. {
  140. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wmismatched-tags")
  141. /** Adds support for C++17 structured bindings. */
  142. template <typename... Processors>
  143. struct tuple_size<::juce::dsp::ProcessorChain<Processors...>> : integral_constant<size_t, sizeof... (Processors)> {};
  144. /** Adds support for C++17 structured bindings. */
  145. template <size_t I, typename... Processors>
  146. struct tuple_element<I, ::juce::dsp::ProcessorChain<Processors...>> : tuple_element<I, tuple<Processors...>> {};
  147. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  148. } // namespace std
  149. #endif