Problem with delay effect at getNextAudioBlock()

 i'm using delay algorithm at getNextAudioBlock , It doesnt give a delay sound but something like robot sound ...

void MyApplication::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)


    const int numSamples = bufferToFill.buffer->getNumSamples();

    int channel, dp = 0;

    AudioSampleBuffer delayBuffer = *new AudioSampleBuffer(2, 48000);

    int delayPosition=0;

    float delay = 0.8;

        for (channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)


            float* channelData =  bufferToFill.buffer->getWritePointer (channel);

            float* delayData = delayBuffer.getWritePointer (jmin (channel, delayBuffer.getNumChannels() - 1));

            dp = delayPosition;

            for (int i = 0; i < numSamples; ++i)


                const float in = channelData[i];

                channelData[i] += delayData[dp];

                delayData[dp] = (delayData[dp] + in) * delay;

                if (++dp >= delayBuffer.getNumSamples())

                    dp = 0;




any idea pls : (

Thanks in advance 

You need to learn the difference between local and member variables. Without understanding the basic principles of variable scope (this isn't even C++ specific, all OO languages are the same), you're not going to be able to get very far towards making anything work!

AudioSampleBuffer delayBuffer = *new AudioSampleBuffer(2, 48000);


The callback you're working in,  getNextAudioBlock(), gets called by the audio system repeatedly based on the sample rate and buffer size. That means whatever happens between those brackets probably happens 44100 or more times a second in the resulting audio. 

So just take a step back and think about why this doesn't work. For example, you're creating the AudioSampleBuffer (the basic "memory" of your delay effect) 44 thousand times a second! (edit: not exactly, there's buffering, but it's useful to think of this as a continous stream of audio samples that's happening really fast). You only need to do that once when you first create your delay.  

So as Jules said, you need to think about the scope of these variables and where they belong. The only stuff that belongs in getNextAudioBlock() is the writing into and reading out of your delay buffer. All of the set up should already be done when you create an instance of your class, or when the audio system is preparing to start. 

Audio programming is a bit of a scary introduction to OOP but it's where I started too. Good luck :)


Well, it's not exactly that bad. The callback isn't made at the sample rate frequency, but more like every 64-1024 samples. (It will depend on the audio driver and audio system used etc.)

Still, the original poster's code is completely wrong and misguided and doesn't have any chances of working as it is.

Thanks everyone :D .. by the way do you know some existing project that use AudioProcessor on iOS ? it would be great if i have some source to start with :))

oh , i've tried to look at many parts of Juce .. and try to learn more about OOP concept ... and i just got my first REVERB effect :D




yes, thanks for pointing this out Xenakios. I was oversimplifying :)