zoukankan      html  css  js  c++  java
  • Speex Acoustic Echo Cancellation (AEC) 回声消除模块的使用

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

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

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


    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include "speex/speex_echo.h"
    #include "speex/speex_preprocess.h"
    
    
    #define NN 128
    #define TAIL 1024
    
    int main(int argc, char **argv)
    {
       FILE *echo_fd, *ref_fd, *e_fd;
       short echo_buf[NN], ref_buf[NN], e_buf[NN];
       SpeexEchoState *st;
       SpeexPreprocessState *den;
       int sampleRate = 8000;
    
       if (argc != 4)
       {
          fprintf(stderr, "testecho mic_signal.sw speaker_signal.sw output.sw
    ");
          exit(1);
       }
       echo_fd = fopen(argv[2], "rb");
       ref_fd  = fopen(argv[1],  "rb");
       e_fd    = fopen(argv[3], "wb");
       // Step1: 初始化结构
         st = speex_echo_state_init(NN, TAIL);
       den = speex_preprocess_state_init(NN, sampleRate);
    
       //Step2: 设置相关参数
       speex_echo_ctl(st, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);
       speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, st);
    
       while (!feof(ref_fd) && !feof(echo_fd))
       {
          fread(ref_buf, sizeof(short), NN, ref_fd);
          fread(echo_buf, sizeof(short), NN, echo_fd);
          
          //Step3: 调用Api回声消除,ref_buf是麦克采集到的数据
          // echo_buf:是从speaker处获取到的数据
          // e_buf: 是回声消除后的数据
          speex_echo_cancellation(st, ref_buf, echo_buf, e_buf);
          speex_preprocess_run(den, e_buf);
          fwrite(e_buf, sizeof(short), NN, e_fd);
       }
    
       //Step4: 销毁结构 释放资源
       speex_echo_state_destroy(st);
       speex_preprocess_state_destroy(den);
       fclose(e_fd);
       fclose(echo_fd);
       fclose(ref_fd);
       return 0;
    }
    


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

    write_to_soundcard(echo_frame, frame_size);     //播放音频数据,并从声卡获得播放的数据echo_frame.
    read_from_soundcard(input_frame, frame_size);   //在数据播放后,从声卡麦克获取采集到的数据input_frame.
    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 传输放到另外一个线程。


  • 相关阅读:
    Qt 3D教程(二)初步显示3D的内容
    linux关于ftp查看不到文件列表的问题
    Mahout推荐算法API具体解释【一起学Mahout】
    GBK编码具体解析(附GBK码位分布图)
    HTML5实战与剖析之媒体元素(3、媒体元素的事件及方法)
    CentOS添加swap分区
    Transaction: atomicity, consistency, separability, persistence
    redis ins 调试
    jemalloc/jemalloc.h: No such file or directory
    MySQL表设计基础
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3172118.html
Copyright © 2011-2022 走看看