首先感谢大佬整理的关于WebRTC的ANS噪声抑制的源码,我们只需要其中noise_suppression.c
和noise_suppression.h
就可以实现噪声抑制功能了
https://github.com/cpuimage/WebRTC_NS
1、将noise_suppression.c
和noise_suppression.h
这两个文件添加到工程中
2、需定义NDEBUG以屏蔽assert宏定义
3、初始化代码
typedef enum{
kLow,
kModerate,
kHigh,
kVeryHigh
}nsLevel;
/**
* @brief: 初始化WebRTC噪声抑制器
* @param NsHandles: 噪声抑制器句柄
* @param sampleRate: 采样率(Hz),只能为8000、16000
* @param level: 噪声抑制等级(0~3 0:低, 1:中, 2:高, 3:极高)
* @return: -1: 失败, 1: 成功
*/
int webrtcAnsInit_self(NoiseSuppressionC *NsHandles, uint32_t sampleRate, nsLevel level)
{
uint16_t samples = MIN(160, sampleRate / 100);
if (samples == 0) return -1;
if (NsHandles == NULL) {
printf("NsHandles NULL\n");
return -1;
}
int status = WebRtcNs_InitCore(NsHandles, sampleRate);
if (status != 0) {
printf("WebRtcNs_Init fail\n");
if (NsHandles){ myfree(SRAMIN, NsHandles);}
} else {
status = WebRtcNs_set_policy_core(NsHandles, level);
if (status != 0) {
printf("WebRtcNs_set_policy fail\n");
if (NsHandles){ myfree(SRAMIN, NsHandles);}
}
}
return 1;
}
4、运行降噪程序
/**
* @brief: 运行WebRTC噪声抑制器
* @param NsHandles: 噪声抑制器句柄
* @param buffer: 待处理的PCM数据,长度必须为(sampleRate/100)的整数倍
* @param frameBuffer: 用于临时存储单帧音频数据
* @param sampleRate: 采样率(Hz)
* @param samplesCount: 待处理的PCM数据个数(单位:采样点)
* @return: -1: 失败, 1: 成功
*/
int webrtcAnsRun_self( NoiseSuppressionC *NsHandles,
int16_t *buffer,
int16_t *frameBuffer,
uint32_t sampleRate,
uint32_t samplesCount)
{
uint16_t samples = MIN(160, sampleRate / 100);
uint32_t frames = samplesCount / samples;
if (frames == 0) return -1;
for (int i = 0; i < frames; i++) {
for (int k = 0; k < samples; k++)
{ frameBuffer[k] = buffer[k + i*samples];}
int16_t *nsIn[1] = {frameBuffer}; //ns input[band][data]
int16_t *nsOut[1] = {frameBuffer}; //ns output[band][data]
WebRtcNs_AnalyzeCore(NsHandles, nsIn[0]);
WebRtcNs_ProcessCore(NsHandles, (const int16_t *const *) nsIn, 1, nsOut);
for (int k = 0; k < samples; k++)
{ buffer[k + i*samples] = frameBuffer[k];}
}
return 1;
}