# Unexpected behaviour with dsp::ProcessorChain and ProcessContextNonReplacing

**URL:** <https://forum.juce.com/t/unexpected-behaviour-with-dsp-processorchain-and-processcontextnonreplacing/48929>\
**Category:** General JUCE discussion\
**Created:** [November 24, 2021, 11:51am UTC](https://forum.juce.com/t/unexpected-behaviour-with-dsp-processorchain-and-processcontextnonreplacing/48929 "2021-11-24T11:51:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [November 24, 2021, 11:51am UTC](https://forum.juce.com/t/unexpected-behaviour-with-dsp-processorchain-and-processcontextnonreplacing/48929/1 "2021-11-24T11:51:28Z")

</div>

We have wrapped quite a few of our legacy dsp algorithm codebase into interfaces suitable to be used with the juce dsp module. Since a lot of them are only designed for in-place processing of float buffers, their `process` function are not templated but instead take a `const dsp::ProcessContextReplacing<float>&`. Chaining those classes, mixed with classes that have a templated processing function works well when a `ProcessContextReplacing` is passed to the process function of the chain.

When the dsp::ProcessorChain processes a non-replacing context, internally the first processor processes the samples non-replacing and all subsequent processors process the output buffer of that non-replacing context with a replacing one, which totally makes sense.

Now I had the situation where the first element in the chain was capable of non-replacing processing, but the second was not and I wanted to pass in a non-replacing context and I ran into quite cryptic compile errors. It took me some time to figure out that even if only the first processor in the chain will ever be called with a non-replacing process function, the compiler attempts to compile a non-replacing process version for all chained elements, because the `if` condition that is used in the processor chain internally to figure out if the first processor or a subsequent one is processed will be evaluated at runtime. To me this was unexpected and made an unnecessary ugly rework to one of our in-house processor classes necessary. This could be solved in the processor chain by pulling the first processor out of the tuple iterator, processing it with the context type passed in and only processing the subsequent processors in that “loop” with a hard-coded replacing context. Would you consider something like that?

Here is some example code to trigger the error:

```auto
    struct OnlyReplacing
    {
        void prepare (juce::dsp::ProcessSpec&) {}
        void process (const juce::dsp::ProcessContextReplacing<float>&) {}
        void reset() {}
    };

    juce::dsp::ProcessorChain<juce::dsp::Gain<float>, OnlyReplacing> chain;

    juce::dsp::AudioBlock<float> in, out;
    juce::dsp::ProcessContextNonReplacing<float> context (in, out);

    chain.process (context);

```

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [November 24, 2021, 1:25pm UTC](https://forum.juce.com/t/unexpected-behaviour-with-dsp-processorchain-and-processcontextnonreplacing/48929/2 "2021-11-24T13:25:54Z")

</div>

Nice idea, I think this will have the side-effect of unrolling the ‘loop body’ completely so there won’t be any runtime branching inside the process call.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [December 2, 2021, 11:35am UTC](https://forum.juce.com/t/unexpected-behaviour-with-dsp-processorchain-and-processcontextnonreplacing/48929/3 "2021-12-02T11:35:15Z")

</div>

I’ve added that here:

> <https://github.com/juce-framework/JUCE/commit/8407b5ea74f70f0d0631e77c80553c7738e78617>

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [December 3, 2021, 9:47am UTC](https://forum.juce.com/t/unexpected-behaviour-with-dsp-processorchain-and-processcontextnonreplacing/48929/4 "2021-12-03T09:47:03Z")

</div>

Great! Will give it a try
