Cascading plugin effects tutorial fault in webpage

Hi there

I’m following the cascading plugin effects tutorial on the AudioProcessorGraph. But there is a part missing, it sais :

In the next part, we iterate over the three available processor slots and check the options that were selected for each of the AudioParameterChoice objects as follows:

Then it shows the code for the prepareToPlay of the filterprocessor

void prepareToPlay (double sampleRate, int samplesPerBlock) override
{
    juce::dsp::ProcessSpec spec { sampleRate, static_cast<juce::uint32> (samplesPerBlock) };
    oscillator.prepare (spec);
}

Instead of the correct code:

for (int i = 0; i < 3; ++i)
{
    auto& choice = choices.getReference (i);
    auto  slot   = slots  .getUnchecked (i);

    if (choice->getIndex() == 0)            // [1]
    {
        if (slot != nullptr)
        {
            mainProcessor->removeNode (slot.get());
            slots.set (i, nullptr);
            hasChanged = true;
        }
    }
    else if (choice->getIndex() == 1)       // [2]
    {
        if (slot != nullptr)
        {
            if (slot->getProcessor()->getName() == "Oscillator")
                continue;

            mainProcessor->removeNode (slot.get());
        }

        slots.set (i, mainProcessor->addNode (std::make_unique<OscillatorProcessor>()));
        hasChanged = true;
    }
    else if (choice->getIndex() == 2)       // [3]
    {
        if (slot != nullptr)
        {
            if (slot->getProcessor()->getName() == "Gain")
                continue;

            mainProcessor->removeNode (slot.get());
        }

        slots.set (i, mainProcessor->addNode (std::make_unique<GainProcessor>()));
        hasChanged = true;
    }
    else if (choice->getIndex() == 3)       // [4]
    {
        if (slot != nullptr)
        {
            if (slot->getProcessor()->getName() == "Filter")
                continue;

            mainProcessor->removeNode (slot.get());
        }

        slots.set (i, mainProcessor->addNode (std::make_unique<FilterProcessor>()));
        hasChanged = true;
    }
}

Just a heads up

Other errors are juce::ReferenceCountedArray<Node> slots; is supposed to be a member variable not a local one, and updateGraph() is not supposed to be called from inside the processBlock.

Thanks, we’ve updated the incorrect code snippet and it should appear on the website shortly.

The other things that you mentioned aren’t errors though, just how the project is structured. In the second part of the tutorial the updateGraph() method is moved into the timer callback.

Oh okay.
But with me having juce::ReferenceCountedArray<Node> slots; in the updateGraph() caused the nullptr checks to be useless, they always contained a nullptr since they were freshly initialized. Wich caused the constructors of the individual processors to be called and reset the parameters on the already initialized processors

The local slots array is populated with the slotXNode Ptrs after construction:

juce::ReferenceCountedArray<Node> slots;
slots.add (slot1Node);
slots.add (slot2Node);
slots.add (slot3Node);

which are set at the bottom of updateGraph() and will be pointing to one of the processors if chosen:

slot1Node = slots.getUnchecked (0);
slot2Node = slots.getUnchecked (1);
slot3Node = slots.getUnchecked (2);

These are members of the main TutorialProcessor class so will persist between updateGraph() calls.