speex庫音頻降噪(含代碼)


speex庫中音頻降噪效果不錯,應該是應用最廣泛的吧,speex庫下載地址https://www.speex.org/downloads/,可以直接下載二進制代碼使用,像配置OpenCV一樣配置speex庫就可以了。speex庫的API參考文檔下載:http://download.csdn.net/detail/yizhaoyanbo/9856894

 

貼出C語言實現的音頻降噪代碼如下。

代碼中采樣率、音頻幀大小需要根據實際情況設置,HEADLEN是WAV格式的文件頭,占44個字節,這44個字節是不需要處理的,不然文件頭會損壞,導致得到的結果無法播放。

noiseSuppress的值可以控制減除的噪聲強度,負值越小,噪聲去除的強度越大,同時會造成原聲的失真,需要作出權衡。

#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <assert.h> #include <string.h> #include <speex/speex_preprocess.h> #include <speex/speex.h>

#define HEADLEN 44
#define SAMPLE_RATE   (48000)  
#define SAMPLES_PER_FRAME  (1024)
#define FRAME_SIZE   (SAMPLES_PER_FRAME * 1000/ SAMPLE_RATE)
#define FRAME_BYTES  (SAMPLES_PER_FRAME)
int main() { size_t n = 0; FILE *inFile, *outFile; fopen_s(&inFile, "./audio/input01L.wav", "rb"); fopen_s(&outFile, "./audio/output01L.wav", "wb"); char *headBuf = (char*)malloc(HEADLEN); char *dataBuf = (char*)malloc(FRAME_BYTES * 2 ); memset(headBuf, 0, HEADLEN); memset(dataBuf, 0, FRAME_BYTES); assert(headBuf != NULL); assert(dataBuf != NULL); SpeexPreprocessState *state = speex_preprocess_state_init(1024, SAMPLE_RATE); int denoise = 1; int noiseSuppress = -25; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DENOISE, &denoise); speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress); int i; i = 0; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC, &i); i = 80000; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &i); i = 0; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DEREVERB, &i); float f = 0; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &f); f = 0; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &f); //靜音檢測
    /*int vad = 1; int vadProbStart = 80; int vadProbContinue = 65; speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_VAD, &vad); speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_PROB_START, &vadProbStart); speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue);*/

    bool flag = true; while (1) { if (flag == true) { flag = false; n = fread(headBuf, 1, HEADLEN, inFile); if (n == 0) break; fwrite(headBuf, 1, HEADLEN, outFile); } else { n = fread(dataBuf, 1, SAMPLES_PER_FRAME, inFile); if (n == 0) break; speex_preprocess_run(state, (spx_int16_t*)(dataBuf)); fwrite(dataBuf, 1, SAMPLES_PER_FRAME, outFile); } } free(headBuf); free(dataBuf); fclose(inFile); fclose(outFile); speex_preprocess_state_destroy(state); return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM