Search code examples
caudiohardwareaudio-recordingnios

Saving memory address content in C Array


I'm implementing a delay in C code for audio. I have a memory address where I receive the audio sample. And another memory address which indicates where a new sample appears.

What I am trying to do is record the first audio second (48000 samples). In order to do so I'm declaring an array where I save the audio samples.

In the main loop I make a sum of the actual sample and the delayed (1 second ago) sample, it's quite similar to an echo what I want to achieve.

The problem is that my code reproduces the actual sound, but not the delayed, in fact, I can heard a little noise every second, so I guess it only reads the first sample of the array, and the rest is empty.

I have analyzed my code, but I don't know where my failure is. I think it may be related with memory allocation, but I'm not very familiarized with C language. Could you please help me?

#define au_in (volatile short *) 0x0081050  //Input memory address
#define au_out (volatile short *) 0x0081040
#define samp_rdy (volatile int *) 0x0081030 //Indicates if a new sample is ready

/* REPLAY */
void main(){
    short *buff[48000];
    int i=0;
    while(i<48000){
        if((*(samp_rdy + 0x3)==0x1)){ //If there's a new sample
            *buff[i]= *au_in;
            i++;
            *(samp_rdy + 0x3)=0x0;
        }
    }

    while (1){
        i=0;
        while(i<48000){
            if((*(samp_rdy + 0x3)==0x1)){
                *au_out = *buff[i]+(*au_in); //Reproduces actual sample + delayed sample
                *buff[i]=*au_in; //replaces the sample in the array for the new one
                i++;
                *(samp_rdy + 0x3)=0x0;
            }
        }
    }
}

Thanks.


Solution

  • Try to run with C99 mode:

    #define au_in (volatile short *) 0x0081050  //Input memory address
    #define au_out (volatile short *) 0x0081040
    #define samp_rdy (volatile int *) 0x0081030 //Indicates if a new sample is ready
    #define SAMPLE_BUF_SIZE     (48000)
    
    
    void main()
    {
        static short buff[SAMPLE_BUF_SIZE];
        for(int i = 0; i < SAMPLE_BUF_SIZE; )
        {
            if(*(samp_rdy + 0x3)) //If there's a new sample
            {
                buff[i]= *au_in;
                *(samp_rdy + 0x3)= 0;
                i++;
            }
        }
    
        while (1)
        {
            for(int i = 0; i < SAMPLE_BUF_SIZE; )
            {
                if(*(samp_rdy + 0x3)) //If there's a new sample
                {
                    *au_out = buff[i] + (*au_in);   //Reproduces actual sample + delayed sample
                    buff[i] = *au_in;               //replaces the sample in the array for the new one
                    *(samp_rdy + 0x3)= 0;
                    i++;
                }
            }
        }
    }