/* ============================================================================== This file is part of the JUCE 7 technical preview. Copyright (c) 2022 - Raw Material Software Limited You may use this code under the terms of the GPL v3 (see www.gnu.org/licenses). For the technical preview this file cannot be licensed commercially. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE DISCLAIMED. ============================================================================== */ namespace juce { namespace dsp { //============================================================================== #ifndef DOXYGEN /** The contents of this namespace are used to implement ProcessorChain and should not be used elsewhere. Their interfaces (and existence) are liable to change! */ namespace detail { template constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple, std::index_sequence) { (void) std::initializer_list { ((void) fn (std::get (tuple), std::integral_constant()), 0)... }; } template using TupleIndexSequence = std::make_index_sequence>>::value>; template constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple) { forEachInTuple (std::forward (fn), std::forward (tuple), TupleIndexSequence{}); } // This could be a template variable, but that code causes an internal compiler error in MSVC 19.00.24215 template struct UseContextDirectly { static constexpr auto value = ! Context::usesSeparateInputAndOutputBlocks() || Ix == 0; }; } #endif /** This variadically-templated class lets you join together any number of processor classes into a single processor which will call process() on them all in sequence. @tags{DSP} */ template class ProcessorChain { public: /** Get a reference to the processor at index `Index`. */ template auto& get() noexcept { return std::get (processors); } /** Get a reference to the processor at index `Index`. */ template const auto& get() const noexcept { return std::get (processors); } /** Set the processor at index `Index` to be bypassed or enabled. */ template void setBypassed (bool b) noexcept { bypassed[(size_t) Index] = b; } /** Query whether the processor at index `Index` is bypassed. */ template bool isBypassed() const noexcept { return bypassed[(size_t) Index]; } /** Prepare all inner processors with the provided `ProcessSpec`. */ void prepare (const ProcessSpec& spec) { detail::forEachInTuple ([&] (auto& proc, auto) { proc.prepare (spec); }, processors); } /** Reset all inner processors. */ void reset() { detail::forEachInTuple ([] (auto& proc, auto) { proc.reset(); }, processors); } /** Process `context` through all inner processors in sequence. */ template void process (const ProcessContext& context) noexcept { detail::forEachInTuple ([this, &context] (auto& proc, auto index) noexcept { this->processOne (context, proc, index); }, processors); } private: template ::value, int> = 0> void processOne (const Context& context, Proc& proc, std::integral_constant) noexcept { jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels()); ProcessContextReplacing replacingContext (context.getOutputBlock()); replacingContext.isBypassed = (bypassed[Ix] || context.isBypassed); proc.process (replacingContext); } template ::value, int> = 0> void processOne (const Context& context, Proc& proc, std::integral_constant) noexcept { auto contextCopy = context; contextCopy.isBypassed = (bypassed[Ix] || context.isBypassed); proc.process (contextCopy); } std::tuple processors; std::array bypassed { {} }; }; /** Non-member equivalent of ProcessorChain::get which avoids awkward member template syntax. */ template inline auto& get (ProcessorChain& chain) noexcept { return chain.template get(); } /** Non-member equivalent of ProcessorChain::get which avoids awkward member template syntax. */ template inline auto& get (const ProcessorChain& chain) noexcept { return chain.template get(); } /** Non-member equivalent of ProcessorChain::setBypassed which avoids awkward member template syntax. */ template inline void setBypassed (ProcessorChain& chain, bool bypassed) noexcept { chain.template setBypassed (bypassed); } /** Non-member equivalent of ProcessorChain::isBypassed which avoids awkward member template syntax. */ template inline bool isBypassed (const ProcessorChain& chain) noexcept { return chain.template isBypassed(); } } // namespace dsp } // namespace juce #ifndef DOXYGEN namespace std { JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wmismatched-tags") /** Adds support for C++17 structured bindings. */ template struct tuple_size<::juce::dsp::ProcessorChain> : integral_constant {}; /** Adds support for C++17 structured bindings. */ template struct tuple_element> : tuple_element> {}; JUCE_END_IGNORE_WARNINGS_GCC_LIKE } // namespace std #endif