前几天无意间看到一个项目rnnoise。
项目地址: https://github.com/xiph/rnnoise
基于RNN的音频降噪算法。
采用的是 GRU/LSTM 模型。
阅读下训练代码,可惜的是作者没有提供数据训练集。
不过基本可以断定他采用的数据集里,肯定有urbansound8k。
urbansound8k 数据集地址:
https://serv.cusp.nyu.edu/projects/urbansounddataset/urbansound8k.html
也可以考虑采用用作者训练的模型来构建数据集的做法,不过即费事,也麻烦。
经过实测,降噪效果很不错,特别是在背景噪声比较严重的情况下。
不过作者仅仅提供 pcm 的代码示例,并且还只支持48K采样率,
( 明显是为了兼容其另一个 项目 opus)
在很多应用场景下,这很不方便。
尽管稍微有点麻烦,但是事在人为,花了点时间,稍作修改。
具体修改如下:
1.支持wav格式
采用dr_wav(https://github.com/mackron/dr_libs/blob/master/dr_wav.h )
2.支持全部采样率
采样率的处理问题,采用简单粗暴法,
详情请移步博主另一篇小文《简洁明了的插值音频重采样算法例子 (附完整C代码)》
3.增加CMake文件
4.增加测试用 示例音频sample.wav
取自(https://github.com/orctom/rnnoise-java)
贴上完整示例代码 :
/* Copyright (c) 2017 Mozilla */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include "rnnoise.h" #include <stdlib.h> #include <stdint.h> #define DR_WAV_IMPLEMENTATION #include "dr_wav.h" void wavWrite_int16(char *filename, int16_t *buffer, int sampleRate, uint32_t totalSampleCount) { drwav_data_format format; format.container = drwav_container_riff; format.format = DR_WAVE_FORMAT_PCM; format.channels = 1; format.sampleRate = (drwav_uint32) sampleRate; format.bitsPerSample = 16; drwav *pWav = drwav_open_file_write(filename, &format); if (pWav) { drwav_uint64 samplesWritten = drwav_write(pWav, totalSampleCount, buffer); drwav_uninit(pWav); if (samplesWritten != totalSampleCount) { fprintf(stderr, "ERROR "); exit(1); } } } int16_t *wavRead_int16(char *filename, uint32_t *sampleRate, uint64_t *totalSampleCount) { unsigned int channels; int16_t *buffer = drwav_open_and_read_file_s16(filename, &channels, sampleRate, totalSampleCount); if (buffer == NULL) { fprintf(stderr, "ERROR "); exit(1); } if (channels != 1) { drwav_free(buffer); buffer = NULL; *sampleRate = 0; *totalSampleCount = 0; } return buffer; } void splitpath(const char *path, char *drv, char *dir, char *name, char *ext) { const char *end; const char *p; const char *s; if (path[0] && path[1] == ':') { if (drv) { *drv++ = *path++; *drv++ = *path++; *drv = '