1

I'm getting some odd results with some code I'm working on. Help me with the implementation of this delay effect?

The code takes an input, adds delayed input to it and then passes it to the output whilst also copying it back to the delay line. Control of the delay level and feedback are provided to control respectively the amount of delayed signal added to the output and returning to the delay line.

Here's the process of my delay effect.

void delay::processReplacing(float** inputs, float** outputs, VstInt32 sampleFrames)
{
        float *in = inputs[0];
        float *out = outputs[0];
        float delayed_sample;
        int delayed_sample_index;
        for (int frame_idx=0;frame_idx< sampleFrames;frame_idx++)
        {
                delayed_sample_index = (((int)(cBuf.index-delay_samples) + CIRCULAR_BUFFER_LENGTH) % (int)CIRCULAR_BUFFER_LENGTH);
                delayed_sample = (cBuf.buffer)[delayed_sample_index];
                //write current sample to buffer
                (cBuf.buffer)[cBuf.index] = in[frame_idx] + (delayed_sample*feedback);
                //Calculate + write output sample
                out[frame_idx] = in[frame_idx] + (delay_level*delayed_sample);
                //Increment buffer index
                cBuf.index = ((cBuf.index+1)) % CIRCULAR_BUFFER_LENGTH;
        }
}

On the whole it works pretty well and functions as a delay effect should. However, the output I'm getting is slightly distorted. I'm not sure if this is an error in the arithmetic of sample times and values or something else. Any ideas?

enter image description here

tobassist
  • 816
  • 5
  • 18
  • delay_samples looks suspicious. It is used in index arithmetic, but has not corresponding suffix in the name. – Gluttton Mar 26 '15 at 21:57
  • delay_samples is constant. It defines the number of samples between the delay buffer index (write point) and the delayed sample (read point). – tobassist Mar 26 '15 at 22:03
  • what is the value of sampleFrames and how might that be related to the spacing between your glitches? – robert bristow-johnson Mar 26 '15 at 23:52
  • It's suspicious subtract delay_samples after that cast to int and after add CIRCULAR_BUFFER_LENGTH, I'd suggest first add CIRCULAR_BUFFER_LENGTH and only after that subtract delay_samples. 2. Share types of variables where this is possible, it makes analysing of the code easy. 3. Can you put on input of your function straight line instead of sine, I hope it helps. 4. I strongly recommended if it is possible check your code with some validator (static or runtime, valgrind, cppcheck, etc) it can helps realise problems in index arithmetic.
  • – Gluttton Mar 27 '15 at 07:27