/* ============================================================================== This file is part of the JUCE 6 technical preview. Copyright (c) 2020 - Raw Material Software Limited You may use this code under the terms of the GPL v3 (see www.gnu.org/licenses). For this technical preview, this file is not subject to commercial licensing. 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) noexcept (noexcept (std::initializer_list { (fn (std::get (tuple), Ix), 0)... })) { (void) std::initializer_list { ((void) fn (std::get (tuple), Ix), 0)... }; } template using TupleIndexSequence = std::make_index_sequence>>::value>; template constexpr void forEachInTuple (Fn&& fn, Tuple&& tuple) noexcept (noexcept (forEachInTuple (std::forward (fn), std::forward (tuple), TupleIndexSequence{}))) { forEachInTuple (std::forward (fn), std::forward (tuple), TupleIndexSequence{}); } } #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. */ 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, size_t) { proc.prepare (spec); }, processors); } /** Reset all inner processors. */ void reset() { detail::forEachInTuple ([] (auto& proc, size_t) { proc.reset(); }, processors); } /** Process `context` through all inner processors in sequence. */ template void process (const ProcessContext& context) noexcept { detail::forEachInTuple ([&] (auto& proc, size_t index) noexcept { if (context.usesSeparateInputAndOutputBlocks() && index != 0) { jassert (context.getOutputBlock().getNumChannels() == context.getInputBlock().getNumChannels()); ProcessContextReplacing replacingContext (context.getOutputBlock()); replacingContext.isBypassed = (bypassed[index] || context.isBypassed); proc.process (replacingContext); } else { ProcessContext contextCopy (context); contextCopy.isBypassed = (bypassed[index] || context.isBypassed); proc.process (contextCopy); } }, processors); } private: 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