Synthesiser local buffer for voices

Hi everyone,

I’m trying to use the JUCE Synthesiser class and was wondering if it is possible to allocate a local buffer (with the same size as the current audiobuffer size set in the plugin settings) for each SynthesiserVoice so I can make use of vector operations. This made me wonder why Synthesiser does not inherit from AudioProcessor as this would give access to the prepareToPlay function as well as processBlock which passes a MidiBuffer in a very explicit way. It would seem much more elegant to me so I’m not sure why this was not implemented this way? Any tips on how I should approach this problem with the current implementation would be very helpful to me.

Thanks!

The general advice is to prefer composition over inheritance, which is why Synthesizer doesn’t inherit AudioProcessor.
This makes it possible to use Synthesizer in different contexts.

What you actually need is a prepareToPlay in the SynthesizerVoice. But for that it doesn’t need to inherit AudioProcessor either.
When you create the subclass of SynthesiserVoice you can add a setBlockSize method. Even better would be to wrap the renderNextBlock into an internal method that you can call in case the block size is not sufficient.

processBlock() btw. would be not suitable, because it processes only full blocks, but the renderNextBlock() needs to render sub blocks, e.g. when noteStart starts midway in a buffer.

I agree with everything else, but actually AudioBuffer can also be used to reference sub blocks:

void renderNextBlock (AudioBuffer<float>& buffer, int start, int length)
{
    auto subBlock = AudioBuffer<float>(
        buffer.getArrayOfWritePointers(), buffer.getNumChannels(), start, length);

    //You can even re-use AudioProcessors here if you have them:
    someFuncThatTakesBuffer(subBlock);
}

That’s why I never understood the calling convention in Synthesiser. Also I never understood why does each voice need to take care of creating a tempBuffer instead of the synth already doing it for each voice.

I do agree with @daniel that inheritance isn’t the right way to fix it, and you should be able to use any vector operations even with the existing calling style, as those usually take a pointer to the start + length which you do get on the voice level.

I guess you’re right, I only need to implement the setBlockSize() in the SynthesiserVoice without inheritance in the Synthesiser. Thanks for the reply!