Problem with storing values/ 1 sample delay NoobQuestion

Hi there,


i ve just sarted using Juce and tried to implement a really simple filter. It seems to work but there is also some weird distortion going on and i just cant figure out where the problem is. Here is a file that demonstrates that behavior:


I ve started with using the introjucer and its Audio Plug-In preset. lastSample is declared as float in the PluginProcessor.h and initialised in its constructor.


I would be grateful for any help.

void FilterAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
    for (int i = getNumInputChannels(); i < getNumOutputChannels(); ++i)
        buffer.clear (i, 0, buffer.getNumSamples());

    for (int channel = 0; channel < getNumInputChannels(); ++channel)
        float* leftData = buffer.getWritePointer(0);

        for (long i = 0; i < buffer.getNumSamples();i++)
            leftData[i] = (leftData[i] + lastSample) / 2.0f;
            lastSample = leftData[i];    

You’re using lastSample for the left and right channel.

Something like this would work:

This will break when using more than two channels, so for “real” code It’d be better to dynamically resize lastSample (e.g. in prepareToPlay using a HeapBlock).

Yep, that's exactly it. You have to use a different unit delay for each channel. If you're processing audio that's exactly the same on both channels (like an oscillator), you would actually only hear the cracking on one of the channels. I usually just use an array of 2 elements like shown above, but that's a good idea to make it dynamically sized. I'll have to change my code to do that. :)

Ouch i feel so dumb right now xD.......thanks to both of you!