Question about dsp::ProcessorChain and enum indices

So I’ve been working through the Introduction to DSP Tutorial and had a question about how it uses enums as the indices for the DSP processes.

Basically, it creates the enums and ProcessorChain

    enum
    {
        oscIndex,
        gainIndex   // [2]
    };
    juce::dsp::ProcessorChain<juce::dsp::Oscillator<Type>, juce::dsp::Gain<Type>> processorChain;

Then calls the prepare function to prepare the chain.

Then in the constructor does this:

    CustomOscillator()
    {
        auto& osc = processorChain.template get<oscIndex>();        // [5]
        osc.initialise ([] (Type x) { return std::sin (x); }, 128); // [6]
    }

My question is, how does the code know that oscIndex corresponds to the juce::DSP::Oscillator? More importantly, when/how is oscIndex connected to processorChain in the first place?

To me it just seems like these enums are setup, then the processorChain is initialized independently of that, but somehow the processorChain knows that oscIndex should return the DSP::Oscillator.

Any information greatly appreciated!

processorChain put those processors into a std::tuple. When you call get with template oscIndex (i.e., 0), it returns the first processor (i.e., the Oscillator):

template <int Index>       auto& get()       noexcept { return std::get<Index> (processors); }

https://en.cppreference.com/w/cpp/utility/tuple/get

Ah, so when you first create the enums, they get numbered behind the scenes to be 0, and 1 based on the order of creation?

Exactly.

https://en.cppreference.com/w/cpp/language/enum

Each enumerator is associated with a value of the underlying type. When = are provided in an enumerator-list, the values of enumerators are defined by those associated constant-expressions. If the first enumerator does not have = , the associated value is zero. For any other enumerator whose definition does not have an = , the associated value is the value of the previous enumerator plus one.

2 Likes

Awesome, thanks!