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.

179 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. //==============================================================================
  18. #ifndef DOXYGEN
  19. /** The contents of this namespace are used to implement ProcessorChain and should
  20. not be used elsewhere. Their interfaces (and existence) are liable to change!
  21. */
  22. namespace detail
  23. {
  24. template <typename Fn, typename Tuple, size_t... Ix>
  25. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple, std::index_sequence<Ix...>)
  26. {
  27. (void) std::initializer_list<int> { ((void) fn (std::get<Ix> (tuple), std::integral_constant<size_t, Ix>()), 0)... };
  28. }
  29. template <typename T>
  30. using TupleIndexSequence = std::make_index_sequence<std::tuple_size<std::remove_cv_t<std::remove_reference_t<T>>>::value>;
  31. template <typename Fn, typename Tuple>
  32. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple)
  33. {
  34. forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{});
  35. }
  36. // This could be a template variable, but that code causes an internal compiler error in MSVC 19.00.24215
  37. template <typename Context, size_t Ix>
  38. struct UseContextDirectly
  39. {
  40. static constexpr auto value = ! Context::usesSeparateInputAndOutputBlocks() || Ix == 0;
  41. };
  42. }
  43. #endif
  44. /** This variadically-templated class lets you join together any number of processor
  45. classes into a single processor which will call process() on them all in sequence.
  46. @tags{DSP}
  47. */
  48. template <typename... Processors>
  49. class ProcessorChain
  50. {
  51. public:
  52. /** Get a reference to the processor at index `Index`. */
  53. template <int Index> auto& get() noexcept { return std::get<Index> (processors); }
  54. /** Get a reference to the processor at index `Index`. */
  55. template <int Index> const auto& get() const noexcept { return std::get<Index> (processors); }
  56. /** Set the processor at index `Index` to be bypassed or enabled. */
  57. template <int Index>
  58. void setBypassed (bool b) noexcept { bypassed[(size_t) Index] = b; }
  59. /** Query whether the processor at index `Index` is bypassed. */
  60. template <int Index>
  61. bool isBypassed() const noexcept { return bypassed[(size_t) Index]; }
  62. /** Prepare all inner processors with the provided `ProcessSpec`. */
  63. void prepare (const ProcessSpec& spec)
  64. {
  65. detail::forEachInTuple ([&] (auto& proc, auto) { proc.prepare (spec); }, processors);
  66. }
  67. /** Reset all inner processors. */
  68. void reset()
  69. {
  70. detail::forEachInTuple ([] (auto& proc, auto) { proc.reset(); }, processors);
  71. }
  72. /** Process `context` through all inner processors in sequence. */
  73. template <typename ProcessContext>
  74. void process (const ProcessContext& context) noexcept
  75. {
  76. detail::forEachInTuple ([this, &context] (auto& proc, auto index) noexcept { this->processOne (context, proc, index); },
  77. processors);
  78. }
  79. private:
  80. template <typename Context, typename Proc, size_t Ix, std::enable_if_t<! detail::UseContextDirectly<Context, Ix>::value, int> = 0>
  81. void processOne (const Context& context, Proc& proc, std::integral_constant<size_t, Ix>) noexcept
  82. {
  83. jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels());
  84. ProcessContextReplacing<typename Context::SampleType> replacingContext (context.getOutputBlock());
  85. replacingContext.isBypassed = (bypassed[Ix] || context.isBypassed);
  86. proc.process (replacingContext);
  87. }
  88. template <typename Context, typename Proc, size_t Ix, std::enable_if_t<detail::UseContextDirectly<Context, Ix>::value, int> = 0>
  89. void processOne (const Context& context, Proc& proc, std::integral_constant<size_t, Ix>) noexcept
  90. {
  91. auto contextCopy = context;
  92. contextCopy.isBypassed = (bypassed[Ix] || context.isBypassed);
  93. proc.process (contextCopy);
  94. }
  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
  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