How to correctly use the AudioVisualiserComponent class to visualise an LFO


Ive not used the AudioVisualiserComponent class before and Im trying to visualise the waveform of an LFO rather than the over all signal from the buffer in the process block. Ive gone about this by creating a separate audio buffer and while looping through the individual samples setting them to the value of the LFO. Outside the sample loop Im then passing the LFO buffer to my instance of the AudioVisualiserComponent class (lfoScope) using the pushBuffer() method. To begin with this displays the waveform but then it being to glitch before ceasing to draw the waveform entirely.

would anyone be able to point out where exactly Im going wrong?

Heres the code from my process block

void Lfo_visulizerAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
    juce::ScopedNoDenormals noDenormals;
    auto totalNumInputChannels  = getTotalNumInputChannels();
    auto totalNumOutputChannels = getTotalNumOutputChannels();
    auto lfoBufferSampleLen = lfoBuffer.getNumSamples();

    for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
        buffer.clear (i, 0, buffer.getNumSamples());

        for (int sample = 0; sample < lfoBufferSampleLen; ++sample)
            step += rate / getSampleRate();
            float lfoVal = sin(2 * M_PI * step);
            lfoBuffer.setSample(0, sample, lfoVal);

Ive set the the number of channels of lfoScope to 1, setRepaintRate to 30 and setBufferSize to 512.
Rate is currently set to 1.

Thanks in advance.

Where is step getting initialized/reset?

Is your “step” variable a float and not a double? If it’s a float, you should try changing it to a double, as accumulating into floats can lead to some surprising issues. Also, as railjonrogut mentioned, it should be initialized/resetted to zero somewhere before the processing starts, so you don’t begin the phase at some random garbage value.

Also, you should ensure the buffer sizes for the processBlock method and your LFO buffer match, which might not be the case.

Step is initialised to zero in the processor header. its currently not being reset anywhere.

It was indeed a float, I have now changed it to a double which improved the situation.

I think the problem was that I misunderstood the size of the AudioBuffer in the process block, I set my LFOBuffer buffer size to 44100 thinking that was the size of the process block buffer, after seeing it was actually 512 I brought down the LFOBuffer buffer size and match the two. It now works as I expected it to.

Thanks so much for the help!

Can you reset step to zero when you get back to the start of the buffer? Otherwise you’re going to get a roll over on your variable (or it’s going to get reduced to nothing )when it reaches its limit… since you have no idea how long the user will keep your plugin running.