0

I'm wondering if anyone would possibly be able to give me some advice on how to implement a cross-correlation function within two simple delay lines that I have set up. My problem is that I have two hard coded delay lines that I can manually change to align two signals going in. I'm using a DI signal and a microphone signal from a bass amp. If I use this code in its current state it will delay the DI signal, but what I want it to do, is take the two signals and align them within the DSP for it to output them in phase with one and other. My current code can be seen below:

#include <Bela.h>
#define DELAY_BUFFER_SIZE 44100
// Buffer holding previous samples per channel
float gDelayBuffer_l[DELAY_BUFFER_SIZE] = {0};
float gDelayBuffer_r[DELAY_BUFFER_SIZE] = {0};
// Write pointer
int gDelayBufWritePtr = 0;
// Amount of delay
float gDelayAmount = 1;
// Amount of feedback
float gDelayFeedbackAmount = 0;
// Level of pre-delay input
float gDelayAmountPre = 1;
// Amount of delay in samples 
int gDelayInSamples = 22050;

// Buffer holding previous samples per channel
float hDelayBuffer_l[DELAY_BUFFER_SIZE] = {0};
float hDelayBuffer_r[DELAY_BUFFER_SIZE] = {0};
// Write pointer
int hDelayBufWritePtr = 0;
// Amount of delay
float hDelayAmount = 1;
// Amount of feedback
float hDelayFeedbackAmount = 0;
// Level of pre-delay input
float hDelayAmountPre = 1;
// Amount of delay in samples 
int hDelayInSamples = 44100;

bool setup(BelaContext *context, void *userData)
{

    return true;
}

void render(BelaContext *context, void *userData)
{

    for(unsigned int n = 0; n < context->analogFrames; n++) {

        float out_l = 0;
        float out_r = 0;

        // Read audio inputs
        out_l = analogRead(context,n,0);
        out_r = analogRead(context,n,1);

        // Increment delay buffer write pointer
        if(++gDelayBufWritePtr>DELAY_BUFFER_SIZE)
            gDelayBufWritePtr = 0;


               // Increment delay buffer write pointer

        // Calculate the sample that will be written into the delay buffer...
        // 1. Multiply the current (dry) sample by the pre-delay gain level (set above)
        // 2. Get the previously delayed sample from the buffer, multiply it by the feedback gain and add it to the current sample
        float del_input_l = (gDelayAmountPre * out_l + gDelayBuffer_l[(gDelayBufWritePtr-gDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * gDelayFeedbackAmount);
        float del_input_r = (gDelayAmountPre * out_r + gDelayBuffer_r[(gDelayBufWritePtr-gDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * gDelayFeedbackAmount);


        //  Now we can write it into the delay buffer
        gDelayBuffer_l[gDelayBufWritePtr] = del_input_l;
        gDelayBuffer_r[gDelayBufWritePtr] = del_input_r;



        // Get the delayed sample (by reading `gDelayInSamples` many samples behind our current write pointer) and add it to our output sample
        out_l = gDelayBuffer_l[(gDelayBufWritePtr-gDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * gDelayAmount;
        out_r = gDelayBuffer_r[(gDelayBufWritePtr-gDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * gDelayAmount;

       // Write the sample into the output buffer
        analogWrite(context, n, 0, out_l);
        analogWrite(context, n, 1, out_r);

    }
    for(unsigned int n = 0; n < context->analogFrames; n++) {

        float out_l = 0;
        float out_r = 0;

        // Read audio inputs
       out_l = analogRead(context,n,2);
        out_r = analogRead(context,n,3);
        // Increment delay buffer write pointer
        if(++hDelayBufWritePtr>DELAY_BUFFER_SIZE)
            hDelayBufWritePtr = 0;

               // Increment delay buffer write pointer
        if(++hDelayBufWritePtr>DELAY_BUFFER_SIZE)
            hDelayBufWritePtr = 0;

        // Calculate the sample that will be written into the delay buffer...
        // 1. Multiply the current (dry) sample by the pre-delay gain level (set above)
        // 2. Get the previously delayed sample from the buffer, multiply it by the feedback gain and add it to the current sample
        float del_input_l = (hDelayAmountPre * out_l + hDelayBuffer_l[(hDelayBufWritePtr-hDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * hDelayFeedbackAmount);
        float del_input_r = (hDelayAmountPre * out_r + hDelayBuffer_r[(hDelayBufWritePtr-hDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * hDelayFeedbackAmount);



        //  Now we can write it into the delay buffer
        hDelayBuffer_l[hDelayBufWritePtr] = del_input_l;
        hDelayBuffer_r[hDelayBufWritePtr] = del_input_r;

        // Get the delayed sample (by reading `gDelayInSamples` many samples behind our current write pointer) and add it to our output sample
        out_l = hDelayBuffer_l[(hDelayBufWritePtr-hDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * hDelayAmount;
        out_r = hDelayBuffer_r[(hDelayBufWritePtr-hDelayInSamples+DELAY_BUFFER_SIZE)%DELAY_BUFFER_SIZE] * hDelayAmount;

        // Write the sample into the output buffer

        analogWrite(context, n, 2, out_l);
        analogWrite(context, n, 3, out_r);



}
}
void cleanup(BelaContext *context, void *userData)
{
}
  • HI, welcome to StackOverflow. Can you please add more specific information to your question - current results, expected results, what is your exact problem etc. In current state this question is too broad and will probably be closed soon. – Paweł Rumian Mar 01 '17 at 14:53
  • Thank you for your reply. My problem is that I have two hard coded delay lines that I can manually change to align two signals going in. I'm using a DI signal and a microphone signal from a bass amp. If I use this code in its current state it will delay the DI signal, but what I want it to do, is take the two signals and align them within the DSP for it to output them in phase with one and other. – Edward James Mar 01 '17 at 15:03
  • That's a bit better - if only you could edit your original question to include these information, as finding crucial questions in comments is not always easy ;) – Paweł Rumian Mar 01 '17 at 15:10
  • Perhaps, [this implementation](https://forum.kde.org/viewtopic.php?f=74&t=118619) using Eigen may be usefull for you. As an alternative, you can calculate the [cross correlation](https://en.wikipedia.org/wiki/Cross-correlation#Time_delay_analysis) yourself based on [this post](http://stackoverflow.com/questions/15138634/eigen-is-there-an-inbuilt-way-to-calculate-sample-covariance) – m7913d Mar 08 '17 at 21:40

0 Answers0