这里用speex进行音频去噪,主要用如下几个关键点:
创新互联建站是一家专业提供淇滨企业网站建设,专注与网站设计、做网站、HTML5、小程序制作等业务。10年已为淇滨众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。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
#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.... ");
if(argn != 3){
printf("please input 2 parameters
");
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
", szInFilename);
return -2;
}
pOutFileHandle = fopen(szOutFilename, "wb");
if(!pOutFileHandle){
printf("open file %s error
", 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.
");
fwrite(in, sizeof(short), FRAME_SIZE, pOutFileHandle);
}else{
printf("slience############################
");
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