我一直在尝试使用noisereduce-pypi算法来降低音频文件的噪声,但它给了我一个错误:
Traceback (most recent call last):
File "C:/Users/Seif Koretum/Desktop/noise_reduce/main.py", line 22, in <module>
reduced_noise = nr.reduce_noise(y=data, sr=rate)
File "C:UsersSeif KoretumDesktopnoise_reducevenvlibsite-packagesnoisereducenoisereduce.py", line 594, in reduce_noise
return sg.get_traces()
File "C:UsersSeif KoretumDesktopnoise_reducevenvlibsite-packagesnoisereducenoisereduce.py", line 232, in get_traces
filtered_chunk = self.filter_chunk(start_frame=0, end_frame=end_frame)
File "C:UsersSeif KoretumDesktopnoise_reducevenvlibsite-packagesnoisereducenoisereduce.py", line 162, in filter_chunk
padded_chunk = self._read_chunk(i1, i2)
File "C:UsersSeif KoretumDesktopnoise_reducevenvlibsite-packagesnoisereducenoisereduce.py", line 154, in _read_chunk
chunk = np.zeros((self.n_channels, i2 - i1))
numpy.core._exceptions.MemoryError: Unable to allocate 98.6 GiB for an array with shape (220500, 60002) and data type float64
我的代码是基于录制一个音频文件,然后去除噪音,然后播放这个音频:
from scipy.io import wavfile as wav
from scipy.io.wavfile import write
import sounddevice as sd
from playsound import playsound
import noisereduce as nr
fs = 44100 # Sample rate
seconds = 5 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('sample2.wav', fs, myrecording)
#SEARCH FOR NOISEREDUCE PYPI
# # load data
rate, data = wav.read("sample2.wav")
# perform noise reduction
reduced_noise = nr.reduce_noise(y=data, sr=rate)
filename = 'sample2.wav'
playsound(filename)
您必须使用注释中建议的整形,然后在保存前整形:
from scipy.io import wavfile
import noisereduce as nr
import numpy as np
# load data
rate, data = wavfile.read("Why Rust (online-audio-converter.com) (1).wav")
orig_shape = data.shape
data = np.reshape(data, (2, -1))
# perform noise reduction
# optimized for speech
reduced_noise = nr.reduce_noise(
y=data,
sr=rate,
stationary=True
)
wavfile.write("mywav_reduced_noise.wav", rate, reduced_noise.reshape(orig_shape))
您可以将音频更改为单声道
aud_seg = AudioSegment.from_wav(wav_filename)
aud_seg = aud_seg.set_channels(1)
aud_seg.export(wav_filename, format="wav")