在C Array中保存内存地址内容

时间:2017-07-04 14:47:17

标签: c audio hardware audio-recording nios

我正在实现音频C代码的延迟。 我有一个内存地址,我收到音频样本。另一个内存地址指示新样本出现的位置。

我要做的是记录第一个音频秒(48000个样本)。为了做到这一点,我正在声明一个保存音频样本的数组。

在主循环中,我将实际样本和延迟(1秒前)样本相加,它与我想要实现的回声非常相似。

问题是我的代码重现了实际的声音,但没有延迟,事实上,我每秒都能听到一点噪音,所以我猜它只读取数组的第一个样本,其余的都是空的。

我已经分析了我的代码,但我不知道我的失败在哪里。我认为这可能与内存分配有关,但我对C语言并不熟悉。你能帮我吗?

#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;
            }
        }
    }
}

感谢。

1 个答案:

答案 0 :(得分:-1)

尝试使用C99模式运行:

#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++;
            }
        }
    }
}