Speex Acoustic Echo Cancellation (AEC) 回声消除模块的使用

转自:http://blog.csdn.net/u011202336/article/details/9238699


背景:回声与啸叫的产生  http://blog.csdn.net/u011202336/article/details/9238397

参考资料:  http://www.speex.org/docs/manual   

从代码分析,下边是Speex test demo


[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6. #include "speex/speex_echo.h"  
  7. #include "speex/speex_preprocess.h"  
  8.   
  9.   
  10. #define NN 128  
  11. #define TAIL 1024  
  12.   
  13. int main(int argc, char **argv)  
  14. {  
  15.    FILE *echo_fd, *ref_fd, *e_fd;  
  16.    short echo_buf[NN], ref_buf[NN], e_buf[NN];  
  17.    SpeexEchoState *st;  
  18.    SpeexPreprocessState *den;  
  19.    int sampleRate = 8000;  
  20.   
  21.    if (argc != 4)  
  22.    {  
  23.       fprintf(stderr, "testecho mic_signal.sw speaker_signal.sw output.sw\n");  
  24.       exit(1);  
  25.    }  
  26.    echo_fd = fopen(argv[2], "rb");  
  27.    ref_fd  = fopen(argv[1],  "rb");  
  28.    e_fd    = fopen(argv[3], "wb");  
  29.    // Step1: 初始化结构  
  30.      st = speex_echo_state_init(NN, TAIL);  
  31.    den = speex_preprocess_state_init(NN, sampleRate);  
  32.   
  33.    //Step2: 设置相关参数  
  34.    speex_echo_ctl(st, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);  
  35.    speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, st);  
  36.   
  37.    while (!feof(ref_fd) && !feof(echo_fd))  
  38.    {  
  39.       fread(ref_buf, sizeof(short), NN, ref_fd);  
  40.       fread(echo_buf, sizeof(short), NN, echo_fd);  
  41.         
  42.       //Step3: 调用Api回声消除,ref_buf是麦克采集到的数据  
  43.       // echo_buf:是从speaker处获取到的数据  
  44.       // e_buf: 是回声消除后的数据  
  45.       speex_echo_cancellation(st, ref_buf, echo_buf, e_buf);  
  46.       speex_preprocess_run(den, e_buf);  
  47.       fwrite(e_buf, sizeof(short), NN, e_fd);  
  48.    }  
  49.   
  50.    //Step4: 销毁结构 释放资源  
  51.    speex_echo_state_destroy(st);  
  52.    speex_preprocess_state_destroy(den);  
  53.    fclose(e_fd);  
  54.    fclose(echo_fd);  
  55.    fclose(ref_fd);  
  56.    return 0;  
  57. }  

Speex 源码中附带的这个例子,只适合于串行的链式媒体流,当媒体播放、媒体采集、媒体网络数据接口分属在不同现成时,就会存在同步问题,异步线程会导致信号延迟加大,回声消除收敛效果不好。其中Speex 回声消除必须按照建议的流程:

[cpp]  view plain  copy
  1. write_to_soundcard(echo_frame, frame_size);     //播放音频数据,并从声卡获得播放的数据echo_frame.  
  2. read_from_soundcard(input_frame, frame_size);   //在数据播放后,从声卡麦克获取采集到的数据input_frame.  
  3. speex_echo_cancellation(echo_state, input_frame, echo_frame, output_frame); //调用Api消除噪声,输入input_frame,echo_frame,输出out_frame  

在典型的VOIP类型应用中:

echo_frame: 从RTP接收的数据包解码后,送入声卡播放,获取的数据。

input_frame: 本地麦克采集到的数据

output_frame: 回声消除后的数据,送入encodec,并构造rtp数据包,传输到远端。


典型的应用模式: 

Thread A:  接收audio rtp -> decodec -----> sound card 

                                                                    |__> echo_frame queue


Thread B: 获取麦克数据input_frame --> speex_echo_cancellation( speex_state, input_frame, echo_frame,out_frame ) -> rtp packet -> network 

也可以将rtp packet 与network 传输放到另外一个线程。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值