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.

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