From: p15692025051 <846603105@qq.com> Date: Wed, 23 Apr 2025 03:37:52 +0000 (+0800) Subject: 添加aec前备份 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=410a36042371ac2bd4e444bb4e7cbc7f8ec41b3a;p=mirrors%2FFX01.git 添加aec前备份 --- diff --git a/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c b/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c index b4895da..c0f1708 100644 --- a/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c +++ b/2.code/aicos/app/AI_AMP_2_5/AI_AMP_2_5.c @@ -50,7 +50,47 @@ void VC_record_Start(void); //连接websocket static int websocket_connect_VC(void); static void websocket_deconnect_VC(void); +#include +#include +#define SCALE_FACTOR 10000 // 用于缩放的因子 +// 高级声学回声消除算法(基于NLMS),不使用浮点运算 +void aec(short *input_signal, short *reference_signal, short *output_signal, int num_samples, int filter_length) { + int *filter_weights = (int *)calloc(filter_length, sizeof(int)); + if (filter_weights == NULL) { + fprintf(stderr, "内存分配失败\n"); + return; + } + + int mu = 5000; // 步长因子,相当于浮点的0.5 * SCALE_FACTOR + int delta = 1; // 正则化参数,避免分母为零 + + for (int n = filter_length; n < num_samples; n++) { + int echo_estimate = 0; + // 计算估计的回声 + for (int i = 0; i < filter_length; i++) { + echo_estimate += filter_weights[i] * reference_signal[n - i - 1]; + } + echo_estimate /= SCALE_FACTOR; + + // 计算误差信号(消除回声后的信号) + output_signal[n] = input_signal[n] - (echo_estimate); + + // 计算参考信号的能量 + int energy = 0; + for (int i = 0; i < filter_length; i++) { + energy += reference_signal[n - i - 1] * reference_signal[n - i - 1]; + } + + // 使用NLMS算法更新滤波器权重 + int step = (mu * SCALE_FACTOR) / (energy + delta); + for (int i = 0; i < filter_length; i++) { + filter_weights[i] += (step * output_signal[n] * reference_signal[n - i - 1]) / SCALE_FACTOR; + } + } + + free(filter_weights); +} int play_data_len = 0; //关闭音频播放 void VC_Play_End(void); @@ -601,11 +641,11 @@ static void AI_VC_play_task(void *thread_input)//音频播放线程 // int audio_play_time = AI_VC_play_queue_data.len*1000/32000+1; ret = app_audio_write(APP_AUDIO_TYPE_STREAM_DUPLEX, - AI_VC_play_queue_data.data, AI_VC_play_queue_data.len, AOS_WAIT_FOREVER); + AI_VC_play_queue_data.data, AI_VC_play_queue_data.len, 2000); //audio_data(AI_VC_play_queue_data.data,AI_VC_play_queue_data.len,20); if (ret < 0) bk_printf("app_audio_write error, ret=%d\n", ret); - // for(int i = 0;i<=AI_VC_play_queue_data.len/sizeof(play_tmp_d);i++)//分段写 + // for(int i = 0;i<=AI_VC_play_queue_data.len/sizeof(play_tmp_d);i++)//分段写,一次写入小于640会出问题 // { // int play_tmp_d_len = sizeof(play_tmp_d); // memset(play_tmp_d,0,play_tmp_d_len); @@ -948,7 +988,7 @@ void AI_VC_start(void) void AI_VC_Close(void) { bk_printf("AI_VC_Close!!!!!!!!!!!!"); - app_audio_stop(APP_AUDIO_TYPE_STREAM_DUPLEX, NULL); + //销毁发送线程 if(AI_VC_record_task_id != 0) { @@ -1018,6 +1058,7 @@ void AI_VC_Close(void) AI_VC_1S_timer = NULL; } close_timerout = 0; + app_audio_stop(APP_AUDIO_TYPE_STREAM_DUPLEX, NULL);//要在销毁线程之后 } aos_status AI_AMP_MSG_put(const char *data,u32 len,AI_AMP_MSG_Type MSG_Type) {