ichad.c
ichad.c

Reputation: 131

How to get this audio delay to work?

I'm tying to implement a basic audio delay - but all I'm getting is garbage, probably something very obvious - but I can't seem to spot it...

Audio is processed via buffers that are determined at runtime.

I think I'm doing something horribly wrong with the pointers, tried looking at some other code - but they all seem "incomplete" always something rudimentary is missing - probably what's miss in my code as well.

// Process audio
// 1
void Gain::subProcessSimpleDelay( int bufferOffset, int sampleFrames )
{
    // Assign pointers to your in/output buffers. 
    // Each buffer is an array of float samples.
    float* in1  = bufferOffset + pinInput1.getBuffer();
    float* in2  = bufferOffset + pinInput2.getBuffer();
    float* out1 = bufferOffset + pinOutput1.getBuffer();

    // SampleFrames = how many samples to process (can vary). 
    // Repeat (loop) that many times
    for( int s = sampleFrames; s > 0; --s ) 
    {
        // get the sample 'POINTED TO' by in1.
        float input1 = *in1;    

        float feedback = *in2;
        float output;
        unsigned short int p, r;
        unsigned short int len;
        len =  600;

        // check at delay length calculation
        if (len > 65535) 
            len = 65535; 
        // otherwise, a length of 0 will output the input from 
        // 65536 samples ago
        else if (len < 1) 
            len = 1; 

        r = p - len; // loop
        output = buffer[r];
        buffer[p] = input1 + output * feedback;
        p++;

        *out1 = output;

        // store the result in the output buffer.
        // increment the pointers (move to next sample in buffers).
        in1++;
        in2++;
        out1++;
    }
}

Could anyone tell me what's wrong?

Upvotes: 3

Views: 1389

Answers (1)

Dan Hulme
Dan Hulme

Reputation: 15280

You haven't initialized p. Other things to be careful of in this code:-

  • Are you sure that sampleFrames + bufferOffset is less than the size of your input and output buffers? You could really do with a way to check that.
  • It's not clear where buffer comes from, or what else might be writing to it. If it's garbage before your code runs, you're going to end up with garbage everywhere, because the first thing you do is read from it.
  • You don't say what types pinInput1.getBuffer() etc. return. If they return a char*, and you just know that it happens to point to an array of floats, you need to cast the result to float* before you do any pointer arithmetic, to make sure you're advancing to the next float in the array, not the next byte of the array.

Upvotes: 2

Related Questions