• 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.... ");

    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
  • 相关阅读:
    HTML新解
    关于EF4.1更新数据后的显示问题PagedList
    SQL2008中的XML字段操作,与命名空间相关
    64位Win7+iis7下发布MVC3 web项目
    蓝屏、异常关机操成的 未能加载文件或程序集“....”或它的某一个依赖项。参数错误。
    FreeWriting_12
    FreeWriting_13
    【转】ACE的构建(VC++6.0环境)
    Freewriting_10
    FreeWriting_16
  • 原文地址:https://www.cnblogs.com/runner42/p/5102524.html
Copyright © 2020-2023  润新知