Is this acceptable code? my intuition says functions are bad for real-time

This is intended to grow up to be a distortion tool but I was getting glitches, so I wanted neater code.

void ClipperAudio::applyFX(float* databuff, int currentSamp) {

    databuff[currentSamp] *= mGain;
   
}

void ClipperAudio::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages)
{
    const int totalInputs = getTotalNumInputChannels();
    const int totalOutputs = getTotalNumOutputChannels();
    
    for (int channel = 0; channel < totalInputs; ++channel){
        //point to the audio in the buffer
        float* channelData= buffer.getWritePointer(channel);

        for (int sample = 0; sample < buffer.getNumSamples(); ++sample){

            applyFX(channelData, sample);
            
        }

    }
}

This code should not really be a performance bottleneck. The compiler will probably inline that function call anyway, so if you encounter glitches and the snippet above is really all you do, you should search somewhere else for the reason… Does mGain perform crazy jumps while processing the block?

However, there might be neater and cleaner ways to implement it.

  • As I assume that you try to do more than simply applying gain in the end (The class name ClipperAudio lets me assume you want to do some clipping, distortion, whatever…?) you should consider chaining up your processing blocks in a dsp:: ProcessorChain.
  • If applying gain to your buffer is all you need, your processBlock implementation can become a one-liner: buffer.applyGain (mGain);
  • Instead of looping over the samples, you can replace the inner loop with FloatVectorOperations::multiply (channelData, mGain, buffer.getNumSamples());