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.

150 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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. noexcept (noexcept (std::initializer_list<int> { (fn (std::get<Ix> (tuple), Ix), 0)... }))
  27. {
  28. (void) std::initializer_list<int> { ((void) fn (std::get<Ix> (tuple), Ix), 0)... };
  29. }
  30. template <typename T>
  31. using TupleIndexSequence = std::make_index_sequence<std::tuple_size<std::remove_cv_t<std::remove_reference_t<T>>>::value>;
  32. template <typename Fn, typename Tuple>
  33. constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple)
  34. noexcept (noexcept (forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{})))
  35. {
  36. forEachInTuple (std::forward<Fn> (fn), std::forward<Tuple> (tuple), TupleIndexSequence<Tuple>{});
  37. }
  38. }
  39. #endif
  40. /** This variadically-templated class lets you join together any number of processor
  41. classes into a single processor which will call process() on them all in sequence.
  42. */
  43. template <typename... Processors>
  44. class ProcessorChain
  45. {
  46. public:
  47. /** Get a reference to the processor at index `Index`. */
  48. template <int Index> auto& get() noexcept { return std::get<Index> (processors); }
  49. /** Get a reference to the processor at index `Index`. */
  50. template <int Index> const auto& get() const noexcept { return std::get<Index> (processors); }
  51. /** Set the processor at index `Index` to be bypassed or enabled. */
  52. template <int Index>
  53. void setBypassed (bool b) noexcept { bypassed[(size_t) Index] = b; }
  54. /** Query whether the processor at index `Index` is bypassed. */
  55. template <int Index>
  56. bool isBypassed() const noexcept { return bypassed[(size_t) Index]; }
  57. /** Prepare all inner processors with the provided `ProcessSpec`. */
  58. void prepare (const ProcessSpec& spec)
  59. {
  60. detail::forEachInTuple ([&] (auto& proc, size_t) { proc.prepare (spec); }, processors);
  61. }
  62. /** Reset all inner processors. */
  63. void reset()
  64. {
  65. detail::forEachInTuple ([] (auto& proc, size_t) { proc.reset(); }, processors);
  66. }
  67. /** Process `context` through all inner processors in sequence. */
  68. template <typename ProcessContext>
  69. void process (const ProcessContext& context) noexcept
  70. {
  71. detail::forEachInTuple ([&] (auto& proc, size_t index) noexcept
  72. {
  73. if (context.usesSeparateInputAndOutputBlocks() && index != 0)
  74. {
  75. jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels());
  76. ProcessContextReplacing<typename ProcessContext::SampleType> replacingContext (context.getOutputBlock());
  77. replacingContext.isBypassed = (bypassed[index] || context.isBypassed);
  78. proc.process (replacingContext);
  79. }
  80. else
  81. {
  82. ProcessContext contextCopy (context);
  83. contextCopy.isBypassed = (bypassed[index] || context.isBypassed);
  84. proc.process (contextCopy);
  85. }
  86. }, processors);
  87. }
  88. private:
  89. std::tuple<Processors...> processors;
  90. std::array<bool, sizeof...(Processors)> bypassed { {} };
  91. };
  92. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  93. member template syntax.
  94. */
  95. template <int Index, typename... Processors>
  96. inline auto& get (ProcessorChain<Processors...>& chain) noexcept
  97. {
  98. return chain.template get<Index>();
  99. }
  100. /** Non-member equivalent of ProcessorChain::get which avoids awkward
  101. member template syntax.
  102. */
  103. template <int Index, typename... Processors>
  104. inline auto& get (const ProcessorChain<Processors...>& chain) noexcept
  105. {
  106. return chain.template get<Index>();
  107. }
  108. /** Non-member equivalent of ProcessorChain::setBypassed which avoids awkward
  109. member template syntax.
  110. */
  111. template <int Index, typename... Processors>
  112. inline void setBypassed (ProcessorChain<Processors...>& chain, bool bypassed) noexcept
  113. {
  114. chain.template setBypassed<Index> (bypassed);
  115. }
  116. /** Non-member equivalent of ProcessorChain::isBypassed which avoids awkward
  117. member template syntax.
  118. */
  119. template <int Index, typename... Processors>
  120. inline bool isBypassed (const ProcessorChain<Processors...>& chain) noexcept
  121. {
  122. return chain.template isBypassed<Index>();
  123. }
  124. } // namespace dsp
  125. } // namespace juce