/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2020 - Raw Material Software Limited JUCE is an open source library subject to commercial or open-source licensing. By using JUCE, you agree to the terms of both the JUCE 6 End-User License Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020). End User License Agreement: www.juce.com/juce-6-licence Privacy Policy: www.juce.com/juce-privacy-policy Or: You may also use this code under the terms of the GPL v3 (see www.gnu.org/licenses). 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. @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, 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 #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