speex进行音频去噪

应用speex进行音频去噪,speex功能很强大,因为opus的出现,用speex进行编码/解码的人几乎没有了,但是用speex来进行降噪,去除回声,增益还是很多。

这里用speex进行音频去噪,主要用如下几个关键点:

1,准确设置pcm音频的音频采样率,和帧长度,

st = speex_preprocess_state_init(FRAME_SIZE, FRAME_SAMPLERATE);//初始化

2,设置降噪参数,其中DENOISE_DB默认是-25,单位是dB(分贝);

int denoise = 1;
int noiseSuppress = DENOISE_DB;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DENOISE, &denoise); //降噪
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress); //设置噪声的dB

 

源代码如下,main函数参数一是原始pcm文件名,参数二是去噪后的pcm文件名

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include "speex/speex_preprocess.h"
#include <stdio.h>

#define FRAME_SIZE 1152
#define FRAME_SAMPLERATE 32000
#define DENOISE_DB (-90)

int main(int argn, char* argv[]) {
char* szInFilename = NULL;
char* szOutFilename = NULL;
FILE* pInFileHandle = NULL;
FILE* pOutFileHandle = NULL;

short in[FRAME_SAMPLERATE];

int i;
SpeexPreprocessState *st;
int count=0;
float f;

printf("starting....\r\n");

if(argn != 3){
printf("please input 2 parameters\r\n");
return -1;
}

memset((void*)empty, 0, sizeof(empty));

szInFilename = argv[1];
szOutFilename = argv[2];

pInFileHandle = fopen(szInFilename, "rb");
if(!pInFileHandle){
printf("open file %s error\r\n", szInFilename);
return -2;
}

pOutFileHandle = fopen(szOutFilename, "wb");
if(!pOutFileHandle){
printf("open file %s error\r\n", szOutFilename);
fclose(pInFileHandle);
return -3;
}

st = speex_preprocess_state_init(FRAME_SIZE, FRAME_SAMPLERATE);

int denoise = 1;
int noiseSuppress = DENOISE_DB;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DENOISE, &denoise); //降噪
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress); //设置噪声的dB
i=0;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_AGC, &i);
i=8000;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_AGC_LEVEL, &i);
i=0;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DEREVERB, &i);
f=.0;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &f);
f=.0;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &f);

int vad = 1;
int vadProbStart = 80;
int vadProbContinue = 65;
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_VAD, &vad); //静音检测
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_PROB_START , &vadProbStart); //Set probability required for the VAD to go from silence to voice
speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue); //Set probability required for the VAD to stay in the voice state (integer percent)

while (1)
{
int vad;
int iLen = fread(in, sizeof(short), FRAME_SIZE, pInFileHandle);
if(iLen <= 0){
break;
}

if (feof(pInFileHandle))
break;

vad = speex_preprocess_run(st, in);

if(vad != 0){
printf("speech.\r\n");
fwrite(in, sizeof(short), FRAME_SIZE, pOutFileHandle);
}else{
printf("slience############################\r\n");
fwrite(in, sizeof(short), FRAME_SIZE, pOutFileHandle);
}
count++;
}
speex_preprocess_state_destroy(st);

fclose(pInFileHandle);
fclose(pOutFileHandle);

return 0;
}

编译的Makefile如下:

OBJS = test.o
CC = gcc
CFLAGS = -Wall -O -g -D HAVE_CONFIG_H
LD = /usr/local/lib/libspeexdsp.a
INCLUDE_PATH = /usr/local/include/speex

test : $(OBJS)
$(CC) $(OBJS) $(LD) -lm -I$(INCLUDE_PATH) -o test

test.o : test.c
$(CC) $(CFLAGS) -I$(INCLUDE_PATH) -c test.c

clean:
rm -rf *.o test

 

而编译speexdsp-1.2rc3库文件的脚本如下:

./configure --prefix=/usr/local --enable-shared --enable-static --enable-sse

make
make install

 

去噪后的效果,感觉不是很理想,原因应该是speex的去噪功能比较简单,基本上是以来声音分贝来进行去噪,所以不是很准确。

接下来去研究一下Audacity的源码,看看是否能提高降噪效果。

相关网址:

https://github.com/audacity/audacity

https://github.com/audacity/audacity/blob/ae5d29a04815cc41ccf36ef765a9f75636b8bd21/src/effects/NoiseRemoval.cpp

转载于:https://www.cnblogs.com/runner42/p/5102524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值