gpt4 book ai didi

c - 将内存地址内容保存在 C 数组中

转载 作者:行者123 更新时间:2023-11-30 18:11:39 25 4
gpt4 key购买 nike

我正在用 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;
}
}
}
}

谢谢。

最佳答案

尝试以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++;
}
}
}
}

关于c - 将内存地址内容保存在 C 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com