RNNoise是一种用于语音信号降噪的开源库,它使用递归神经网络(RNN)来实现噪声抑制。由Jean-Marc Valin开发,RNNoise结合了传统数字信号处理(DSP)和深度学习技术,旨在实时去除语音通信中的背景噪声。
通过本地ssh ubuntu@服务器IP地址 ,远程登录到腾讯云云服务器上。在服务器中,执行如下指令:
sudo apt update
sudo apt-get update
sudo apt-get install git
sudo apt-get install autoconf
sudo apt-get install libtool
git clone https://github.com/xiph/rnnoise
cd rnnoise
./autogen.sh
./configure
make
sudo make install
configure:
------------------------------------------------------------------------
rnnoise 0.2-22-g70f1d25: Automatic configuration OK.
Assertions ................... no
Hidden visibility ............ yes
API code examples ............ yes
API documentation ............ yes
------------------------------------------------------------------------
make会有很多warning,这里仅演示流程,可以忽略。如果需要提升性能,可以使用:
make CFLAGS="-march=native"
由于rnnoise编译出来是.so库,这里给一个python3的wrapper程序供python工程师使用。
首先需要安装依赖库
pip3 install numpy
pip3 install scipy
下面是pydenoise.py的源代码。
import numpy as np
import ctypes
from scipy.signal import resample
class RNNoiseWrapper:
def __init__(self, lib_path):
self.rnnoise_lib = ctypes.CDLL(lib_path)
# RNNOISE_EXPORT DenoiseState *rnnoise_create(RNNModel *model);
self.rnnoise_lib.rnnoise_create.argtypes = [ctypes.c_void_p]
self.rnnoise_lib.rnnoise_create.restype = ctypes.c_void_p
# RNNOISE_EXPORT float rnnoise_process_frame(DenoiseState *st, float *out, const float *in);
self.rnnoise_lib.rnnoise_process_frame.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_float),
ctypes.POINTER(ctypes.c_float)
]
self.rnnoise_lib.rnnoise_process_frame.restype = ctypes.c_float
# RNNOISE_EXPORT void rnnoise_destroy(DenoiseState *st);
self.rnnoise_lib.rnnoise_destroy.argtypes = [ctypes.c_void_p]
self.state = self.rnnoise_lib.rnnoise_create(None)
def detect_sample_rate_from_frame(self, frame):
"""Detect the sample rate based on the length of the frame."""
frame_length = len(frame)
if frame_length == 160:
return 16000
elif frame_length == 240:
return 24000
elif frame_length == 480:
return 48000
else:
raise ValueError("Unsupported frame length. Expected 160, 240, or 480 for 10ms at 16000Hz, 24000Hz or 48000Hz.")
def process_frame(self, input_frame):
""" Process a frame of audio data with RNNoise, supporting multiple sample rates. """
input_frame = np.asarray(input_frame, dtype=np.float32)
# Detect the sample rate of the input frame
input_sample_rate = self.detect_sample_rate_from_frame(input_frame)
# Resample the input frame to 48000Hz if necessary
original_length = len(input_frame)
if input_sample_rate != 48000:
input_frame = resample(input_frame, 480)
# Allocate output buffer
output_frame = np.zeros(480, dtype=np.float32)
# Convert numpy array to ctypes pointers
input_ptr = input_frame.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
output_ptr = output_frame.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
# Process the frame
self.rnnoise_lib.rnnoise_process_frame(self.state, output_ptr, input_ptr)
if input_sample_rate != 48000:
output_frame = resample(output_frame, original_length)
return output_frame
def __del__(self):
if self.state:
self.rnnoise_lib.rnnoise_destroy(self.state)
# Usage Example
if __name__ == "__main__":
lib_path = "/usr/local/lib/librnnoise.so"
# Create RNNoise wrapper instance
rnnoise_wrapper = RNNoiseWrapper(lib_path)
# Create example input frames for different sample rates
frame_16000Hz = np.random.randn(160).astype(np.float32)
frame_24000Hz = np.random.randn(240).astype(np.float32)
frame_48000Hz = np.random.randn(480).astype(np.float32)
# Process frames
processed_frame_16000 = rnnoise_wrapper.process_frame(frame_16000Hz)
processed_frame_24000 = rnnoise_wrapper.process_frame(frame_24000Hz)
processed_frame_48000 = rnnoise_wrapper.process_frame(frame_48000Hz)
# Output the processed frames size
print(f"Processed frame (16000Hz frame) length: {len(processed_frame_16000)}")
print(f"Processed frame (24000Hz frame) length: {len(processed_frame_24000)}")
print(f"Processed frame (48000Hz frame) length: {len(processed_frame_48000)}")
运行上述代码,结果见下图。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有