Speex 回声消除
为什么需要声学回声消除呢?在一般的VOIP软件或视频会议系统中,假设我们只有A和B两个人在通话,首先,A的声音传给B,B然后用喇叭放出来,而这时B的MIC呢则会采集到喇叭放出来的声音,然后传回给A,如果这个传输的过程中时延足够大,A就会听到一个和自己刚才说过的话一样的声音,这就是回声,声学回声消除器的作用就是在B端对B采集到的声音进行处理,把采集到声音包含的A的声音去掉再传给A,这样,A就不会听到自己说过的话了。
声学回声消除的原理我就不说了,这在网上有很多文档,网上缺少的是实现,所以,我在这把一个开源的声学回声消除器介绍一下,希望对有些有人用,如果有人知道怎么把这消除器用的基于实时流的VOIP软件中,希望能一起分享一下。
这个声学回声消除器是一个著名的音频编解码器speex中的一部分,1.1.9版本后的回声消除器才起作用,以前版本的都不行,我用的也是这个版本,测试表明,用同一个模拟文件,它有效果比INTEL IPP库4.1版中的声学回声消除器的还要好。
先说编译。首先,从www.speex.org上下载speex1.1.9的源代码,解压,打开speex\win32\libspeex中的libspeex.dsw,这个工作区里有两个工程,一个是 libspeex,另一个是libspeex_dynamic。然后,将libspeex中的mdf.c文件添加到工程libspeex中,编译即可。
以下是我根据文档封装的一个类,里面有一个测试程序: //file name: speexEC.h
#ifndef SPEEX_EC_H
#define SPEEX_EC_H
#include <stdio.h>
#include <stdlib.h>
#include "speex/speex_echo.h"
#include "speex/speex_preprocess.h"
class CSpeexEC
{
public:
CSpeexEC();
~CSpeexEC();
void Init(int frame_size=160, int filter_length=1280, int sampling_rate=8000);
void DoAEC(short *mic, short *ref, short *out);
protected:
void Reset();
private:
bool m_bHasInit;
SpeexEchoState* m_pState;
SpeexPreprocessState* m_pPreprocessorState;
int m_nFrameSize;
int m_nFilterLen;
int m_nSampleRate;
float* m_pfNoise;
};
#endif
//fine name:speexEC.cpp
#include "SpeexEC.h"
CSpeexEC::CSpeexEC()
{
m_bHasInit = false;
m_pState = NULL;
m_pPreprocessorState = NULL;
m_nFrameSize = 160;
m_nFilterLen = 160*8;
m_nSampleRate = 8000;
m_pfNoise = NULL;
}
CSpeexEC::~CSpeexEC()
{
Reset();
}
void CSpeexEC::Init(int frame_size, int filter_length, int sampling_rate)
{
Reset();
if (frame_size<=0 || filter_length<=0 || sampling_rate<=0)
{
m_nFrameSize =160;
m_nFilterLen = 160*8;
m_nSampleRate = 8000;
}
else
{
m_nFrameSize =frame_size;
m_nFilterLen = filter_length;
m_nSampleRate = sampling_rate;
}
m_pState = speex_echo_state_init(m_nFrameSize, m_nFilterLen);
m_pPreprocessorState = speex_preprocess_state_init(m_nFrameSize, m_nSampleRate);
m_pfNoise = new float[m_nFrameSize+1];
m_bHasInit = true;
}
void CSpeexEC::Reset()
{
if (m_pState != NULL)
{
speex_echo_state_destroy(m_pState);
m_pState = NULL;
}
if (m_pPreprocessorState != NULL)
{
speex_preprocess_state_destroy(m_pPreprocessorState);
m_pPreprocessorState = NULL;
}
if (m_pfNoise != NULL)
{
delete []m_pfNoise;
m_pfNoise = NULL;
}
m_bHasInit = false;
}
void CSpeexEC::DoAEC(short* mic, short* ref, short* out)
{
if (!m_bHasInit)
return;
speex_echo_cancel(m_pState, (const spx_int16_t *)mic, (const spx_int16_t *)ref, (spx_int16_t *)out, (spx_int32_t *)m_pfNoise);
speex_preprocess(m_pPreprocessorState, (__int16 *)out, (spx_int32_t *)m_pfNoise);
}
可以看出,这个回声消除器类很简单,只要初始化一下就可以调用了。但是,要注意的是,传给回声消除器的两个声音信号,必须同步得非常的好,就是说,在B端,接收到A说的话以后,要把这些话音数据传给回声消除器做参考,然后再传给声卡,声卡再放出来,这有一段延时,这时,B再采集,然后传给回声消除器,与那个参考数据比较,从采集到的数据中把频域和参考数据相同的部分消除掉。如果传给消除器的两个信号同步得不好,即两个信号找不到频域相同的部分,就没有办法进行消除了。
测试程序:
#define NN 160
void main()
{
FILE* ref_fd, *mic_fd, *out_fd;
short ref[NN], mic[NN], out[NN];
ref_fd = fopen ("ref.pcm", "rb"); //打开参考文件,即要消除的声音
mic_fd = fopen ("mic.pcm", "rb");//打开mic采集到的声音文件,包含回声在里面
out_fd = fopen ("echo.pcm", "wb");//消除了回声以后的文件
CSpeexEC ec;
ec.Init();
while (fread(mic, 1, NN*2, mic_fd))
{
fread(ref, 1, NN*2, ref_fd);
ec.DoAEC(mic, ref, out);
fwrite(out, 1, NN*2, out_fd);
}
fclose(ref_fd);
fclose(mic_fd);
fclose(out_fd);
}
以上的程序是用文件来模拟回声和MIC,但在实时流中是大不一样的,在一般的VOIP软件中,接收对方的声音并传到声卡中播放是在一个线程中进行的,而采集本地的声音并传送到对方又是在另一个线程中进行的,而声学回声消除器在对采集到的声音进行回声消除的同时,还需要播放线程中的数据作为参考,而要同步这两个线程中的数据是非常困难的,因为稍稍有些不同步,声学回声消除器中的自适应滤波器就会发散,不但消除不了回声,还会破坏原始采集到的声音,使被破坏的声音难以分辨。我做过好多尝试,始终无法用软件来实现对这两个线程中的数据进行同步,导致实现失败,希望有经验的网友们一起分享一下这方面的经验。
示例代码:
Sample code
This section shows sample code for encoding and decoding speech using the Speex API. The commands can be used to encode and decode a file by calling:
% sampleenc in_file.sw | sampledec out_file.sw
where both files are raw (no header) files encoded at 16 bits per sample (in the machine natural endianness).
sampleenc.c
sampleenc takes a raw 16 bits/sample file, encodes it and outputs a Speex stream to stdout. Note that the packing used is NOT compatible with that of speexenc/speexdec.
#include <speex/speex.h>
#include <stdio.h>
#define FRAME_SIZE 160
int main(int argc, char **argv)
{
char *inFile;
FILE *fin;
short in[FRAME_SIZE];
float input[FRAME_SIZE];
char cbits[200];
int nbBytes;
void *state;
SpeexBits bits;
int i, tmp;
state = speex_encoder_init(&speex_nb_mode);
tmp=8;
speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
inFile = argv[1];
fin = fopen(inFile, "r");
speex_bits_init(&bits);
while (1)
{
fread(in, sizeof(short), FRAME_SIZE, fin);
if (feof(fin))
break;
for (i=0;i<FRAME_SIZE;i++)
input[i]=in[i];
speex_bits_reset(&bits);
speex_encode(state, input, &bits);
nbBytes = speex_bits_write(&bits, cbits, 200);
fwrite(&nbBytes, sizeof(int), 1, stdout);
fwrite(cbits, 1, nbBytes, stdout);
}
speex_encoder_destroy(state);
speex_bits_destroy(&bits);
fclose(fin);
return 0;
}
sampledec.c
sampledec reads a Speex stream from stdin, decodes it and outputs it to a raw 16 bits/sample file. Note that the packing used is NOT compatible with that of speexenc/speexdec.
#include <speex/speex.h>
#include <stdio.h>
#define FRAME_SIZE 160
int main(int argc, char **argv)
{
char *outFile;
FILE *fout;
short out[FRAME_SIZE];
float output[FRAME_SIZE];
char cbits[200];
int nbBytes;
void *state;
SpeexBits bits;
int i, tmp;
state = speex_decoder_init(&speex_nb_mode);
tmp=1;
speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
outFile = argv[1];
fout = fopen(outFile, "w");
speex_bits_init(&bits);
while (1)
{
fread(&nbBytes, sizeof(int), 1, stdin);
fprintf (stderr, "nbBytes: %d\n", nbBytes);
if (feof(stdin))
break;
fread(cbits, 1, nbBytes, stdin);
speex_bits_read_from(&bits, cbits, nbBytes);
speex_decode(state, &bits, output);
for (i=0;i<FRAME_SIZE;i++)
out[i]=output[i];
fwrite(out, sizeof(short), FRAME_SIZE, fout);
}
speex_decoder_destroy(state);
speex_bits_destroy(&bits);
fclose(fout);
return 0;
}
在Speex(www.speex.org)的最新版本中,开始集成了回音消除的模块,而回音消除一直是Voip之中亟待解决的主要问题。
很多朋友和我说speex的aec模块的效能并不好,我们先来看一下speex的aec的api调用方式。
/*
*创建AEC对象
*/
SpeexEchoState *echo_state = speex_echo_state_init(frame_size, filter_length);
frame_size 的取值最好是一个编码的frame大小, 在低带宽条件下,一般延迟20ms,而大小为160
filter_length,最好是房间内反射时间的1/3
如: 一个房间的反射时延为300ms
那么这个filter_length就最好是100ms(这个长度又被称为tail length).
而其中filter_length的设定是一个关键。
/*
*执行AEC
*/
speex_echo_cancel(echo_state, input_frame, echo_frame, output_frame, residue);
其中:
input_frame: 就是被声卡捕捉到的声音
echo_frame: 是由扬声器播放出的声音,这个声音是需要从 input_frame中抵消的声音.
output_frame 是处理完以后输出的声音
residue是一个可选参数,如果不使用可以将之设置为NULL, 也可以通过preprocessor 来控制
问题的关键是 处理input和echo 之间的关系,
也就是说在捕捉到的信号和播放的信号之间的延迟必须足够的小,才可以提高效率.
writetosndcard(echo_frame, frame_size)
readfromsndcard(input_frame, frame_size)
speex_echo_cancel(echo_state, input_frame, echo_frame, output_frame, residue)
如果你想要尽可能的减小信号中的回音,那么可以将residue这个参数设置为噪音参数.
我相信在大多数情况下,都是因为声音捕捉和声音播放之间的同步问题没有处理好,导致的音频质量下降。
/*
*销毁和复位
*/
speex_echo_state_destroy(echo_state);
speex_echo_state_reset(echo_state);
不再复述了!
说明:
据说在Speex的最新的1.2beta版本上,Speex提供了可选择的,简化的API,来提高echo执行过程中的同步问题。
speex的窄带编码算法是基于 8k 16bit 单声道,每帧数据160个采样
speex内部将160个采样点,分成了4个子帧,每个子帧40个(…)
Speex 回声消除相关推荐
- speex回声消除功能测试
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 今天测试了speex中带回声消除模块,效果还可以. 测试环境:网络双工通话,16K采样16位编码,spe ...
- java speex回声消除_speex 回声消除的用法
speex的回声消息 就是speex_echo_cancellation函数的正确用法 回声消息的原理: 对参考声音(解码的对端原始语音包)做延迟(会有多个延迟,如麦克风直接采集到音箱的声音,经墙壁反 ...
- Speex回声消除原理深度解析
这里假设读者具有自适应滤波器的基础知识.Speex的AEC是以NLMS为基础,用MDF频域实现,最终推导出最优步长估计:残余回声与误差之比.最优步长等于残余回声方差与误差信号方差之比,这个结论可以记下 ...
- [投稿]Speex回声消除代码分析
本文是音频处理的朋友icoolmedia(QQ:314138065)的投稿.对音频处理有兴趣的朋友可以通过下面的方式与他交流: 作者:icoolmedia QQ:314138065 音视频算法讨论 ...
- [投稿] Speex回声消除原理深度解析
本文是音频处理的朋友icoolmedia(QQ:314138065)的投稿.对音频处理有兴趣的朋友可以通过下面的方式与他交流: 作者:icoolmedia QQ:314138065 音视频算法讨论 ...
- Speex Acoustic Echo Cancellation (AEC) 回声消除模块的使用
背景:回声与啸叫的产生 http://blog.csdn.net/u011202336/article/details/9238397 参考资料: http://www.speex.org/doc ...
- Speex Acoustic Echo Cancellation (AEC) 回声消除模块的使用
转自:http://blog.csdn.net/u011202336/article/details/9238699 背景:回声与啸叫的产生 http://blog.csdn.net/u011202 ...
- speex与webrtc回声消除小结
回声消除AEC包含: 延时估计对齐+线性自适应滤波器+NLP(双讲检测.处理)+舒适噪声CNG 一.speex aec 1.没有NLP 2.只考虑实时DSP系统,即是没有延时对齐等 3.自适应滤波 ...
- 音频应用(如sip与Voip),编解码API(Ringtone,SoundPool,MediaPlayer),回声消除等(Lame Speex等),OpenSL ES
3套音频播放API:MediaPlayer,SoundPool,AudioTrack. android录音项目,用单例模式集成了record,并实时转码mp3- https://github.com/ ...
最新文章
- android realm删除对象,Android Realm-从服务访问Realm对象
- word邮件合并一页8个_办公软件小课堂||word邮件合并
- 【学习笔记】node.js基础介绍
- keepalived 服务器内存持续升高问题
- itchat 道歉_人类的“道歉”
- windows:(1)xmind常用快捷键
- Linux Mint 17 搭建 JSP 环境
- node命令错误--nodemon : 无法将“nodemon”项识别
- asmcmd:Connected to an idle instance.
- 艺街开放平台开源计划
- Atitit 信息检索 之音乐检索实践 艾提拉注 目录 1. 常规检索 歌手 歌名	1 1.1. 年代检索	1 1.2. 歌词检索(可以依靠web	1 1.3. 哼唱检索 原曲检索(可以使用酷
- win7计算机怎么录屏,win7电脑怎么录屏?这个实用工具给你答案!
- 幻境网盾3.40 (skiller)
- 如何有效的屏蔽百度蜘蛛
- FCW前车碰撞预警的两个重要指标——THW、TTC
- 个人博客系统的设计与实现
- Spring Boot(一):概述(5)——Spring Boot项目推荐工程结构
- 【UV打印机】理光喷头组合说明(16H)
- python解一元二次方程虚根_怎么用python解一元二次方程
- php 星盘代码,爱星盘SDK开发接口说明
热门文章
- 天平学院计算机系,2017年3月苏州科技大学天平学院计算机等级考试报名时间(江苏)...
- python作业习题
- 「数字化生产」PDM和PIM:有什么区别?
- GWAS学习 | 02-表型数据清洗
- 第十七届智能车竞赛--极速越野组总结报告
- 做Vue项目时点击按钮页面会刷新问题
- 题目描述:把下面数组的首尾两个元素互换 <br>var arr = [“鹿晗“,“王俊凯“,“蔡徐坤“,“彭于晏“,“周杰伦“,“刘德华“,“赵本山“];
- 帮我写一个批量取消抖音喜欢的Python代码
- 他人经受的,我必经受
- 学会这几招,让你的项目顺利通过验收