我正在用Python为Raspberry Pi写一个声码器,以使声音无法识别。我录制音频并使用回调功能实时播放-它可以工作。现在,我需要对输入进行去噪,以Numpy数组表示(像大多数教程和SO上的文章一样,没有.wav文件!)。我尝试使用几种音频去噪器,但是特别要注意的是:https://pypi.org/project/noisereduce/(教程:https://timsainburg.com/noise-reduction-python.html,来源:https://github.com/timsainb/noisereduce)。它对我不起作用。
我的main.py:
from copy import copy
import numpy as np
import pyaudio as pa
import time
import noisereduce as nr
p = pa.PyAudio()
FORMAT = pa.paFloat32
NP_FORMAT = np.float32
CHANNELS = 2
CHUNK = 1024
RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
NOISE_SAMPLE = None
# function for manipulating input data
def callback(in_data,frame_count,time_info,flag):
data = copy(np.frombuffer(in_data)) # copy,since original is read-only
# here everything is ok for first time
# print("X")
out_data = nr.reduce_noise(audio_clip=data,noise_clip=NOISE_SAMPLE)
# print("Y")
# I get here only once
return out_data,pa.paContinue
def get_noise():
audio = pa.PyAudio()
stream = audio.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)
frames = []
RECORD_SECONDS = 1
for i in range(0,int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(np.frombuffer(data))
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
return np.array(frames,dtype=NP_FORMAT).flatten()
def main():
global NOISE_SAMPLE
NOISE_SAMPLE = get_noise()
stream = p.open(format=FORMAT,output=True,frames_per_buffer=CHUNK,stream_callback=callback)
stream.start_stream()
# main input loop
while True:
time.sleep(1)
stream.stop_stream()
stream.close()
p.terminate()
if __name__ == "__main__":
main()
我记录噪声样本1秒钟,然后进入无限的记录播放循环,在这里我想对输入进行降噪并使其输出。由于某种原因,它只能运行一次:长时间(为什么?)后,我会打印一次“ X”和“ Y”,然后程序继续运行,但是不再输入回调函数。有什么想法吗?任何建议,其他易于在这里使用的库,等等,都欢迎。