Process call in vst wrapper seems broken


#1

I've customised the vst wrapper quite a bit and I'm trying to change it to be as similar as possible to the current one to allow some additions I've made to be merged more easily with Juce. One particularaly disturbing function in the file juce_VST_Wrapper.cpp is this:

    void process (float** inputs, float** outputs, VstInt32 numSamples)
    {
        const int numIn = numInChans;
        const int numOut = numOutChans;
        AudioSampleBuffer temp (numIn, numSamples);
        for (int i = numIn; --i >= 0;)
            memcpy (temp.getSampleData (i), outputs[i], sizeof (float) * (size_t) numSamples);
        processReplacing (inputs, outputs, numSamples);
        AudioSampleBuffer dest (outputs, numOut, numSamples);
        for (int i = jmin (numIn, numOut); --i >= 0;)
            dest.addFrom (i, 0, temp, i, 0, numSamples);
    }

The first two lines are fine. The next line will cause memory allocation in the audio thread, so that's no good at all. The loop after that is done on "numIn" but then the outputs are accessed, so that will break if numIn > numOut, so this will possibly crash.

Juce uses a single max (numInChans, numOutChans) sest of io pointers and processes in place by copying the inputs over the top of the output, and if there are more inputs than outputs then just pointing to the rest. At the end of a call to "processReplacing" all numOutChans of data will be overwritten, so to be able to accumulate to this data in "process" then all numOutChans of data needs to be stored prior to calling "processReplacing". This would make the code look like:

    void resume()
    {
        if (filter != nullptr)
        {
            ...
            processTemp.setSize (numOutChans, blockSize);
            ...
        }
    }

    void process (float** inputs, float** outputs, VstInt32 numSamples)
    {
        for (int i = numOutChans; --i >= 0;)
            memcpy (processTemp.getSampleData (i), outputs[i], sizeof (float) * (size_t) numSamples);
        processReplacing (inputs, outputs, numSamples);
        AudioSampleBuffer dest (outputs, numOutChans, numSamples);
        for (int i = numOutChans; --i >= 0;)
            dest.addFrom (i, 0, processTemp, i, 0, numSamples);
    }

#2

Damn, good point! TBH I doubt whether many hosts would ever call process(), but that's certainly an overhead that shouldn't be there.. Have tidied it up now, cheers!