/* ============================================================================== This file is part of the JUCE framework. Copyright (c) Raw Material Software Limited JUCE is an open source framework subject to commercial or open source licensing. By downloading, installing, or using the JUCE framework, or combining the JUCE framework with any other source code, object code, content or any other copyrightable work, you agree to the terms of the JUCE End User Licence Agreement, and all incorporated terms including the JUCE Privacy Policy and the JUCE Website Terms of Service, as applicable, which will bind you. If you do not agree to the terms of these agreements, we will not license the JUCE framework to you, and you must discontinue the installation or download process and cease use of the JUCE framework. JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/ JUCE Privacy Policy: https://juce.com/juce-privacy-policy JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/ Or: You may also use this code under the terms of the AGPLv3: https://www.gnu.org/licenses/agpl-3.0.en.html THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. ============================================================================== */ namespace juce::dsp { /** @cond */ //============================================================================== /** 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) { (fn (std::get (tuple), std::integral_constant()), ...); } template using TupleIndexSequence = std::make_index_sequence>>>; template constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple) { forEachInTuple (std::forward (fn), std::forward (tuple), TupleIndexSequence{}); } template inline constexpr auto useContextDirectly = ! Context::usesSeparateInputAndOutputBlocks() || Ix == 0; } /** @endcond */ /** 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 void processOne (const Context& context, Proc& proc, std::integral_constant) noexcept { if constexpr (detail::useContextDirectly) { auto contextCopy = context; contextCopy.isBypassed = (bypassed[Ix] || context.isBypassed); proc.process (contextCopy); } else { jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels()); ProcessContextReplacing replacingContext (context.getOutputBlock()); replacingContext.isBypassed = (bypassed[Ix] || context.isBypassed); proc.process (replacingContext); } } 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 juce::dsp /** @cond */ 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 /** @endcond */