2

I've been trying to use noisereduce pypi algorithm to reduce noise for an audio file but it is giving me an error :

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:\Users\Seif Koretum\Desktop\noise_reduce\venv\lib\site-packages\noisereduce\noisereduce.py", line 594, in reduce_noise
    return sg.get_traces()
  File "C:\Users\Seif Koretum\Desktop\noise_reduce\venv\lib\site-packages\noisereduce\noisereduce.py", line 232, in get_traces
    filtered_chunk = self.filter_chunk(start_frame=0, end_frame=end_frame)
  File "C:\Users\Seif Koretum\Desktop\noise_reduce\venv\lib\site-packages\noisereduce\noisereduce.py", line 162, in filter_chunk
    padded_chunk = self._read_chunk(i1, i2)
  File "C:\Users\Seif Koretum\Desktop\noise_reduce\venv\lib\site-packages\noisereduce\noisereduce.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

and my code is based on recording an audio file then remove noise then play this audio :

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)
Seif Koretum
  • 27
  • 1
  • 3
  • 1
    The problem is that the algorithm is trying to allocate an array which is way to large. It seems that - for one reason or another - the amount of channels is assumes to be 220500, which actually seems to be the amount of data for a single channel. Maybe try `nr.reduce_noise(y=data, shape=(2,), sr=rate)`? – Schottky Jan 16 '22 at 14:40
  • it gets an error reduce_noise() got an unexpected keyword argument 'shape' – Seif Koretum Jan 16 '22 at 17:41
  • Ah, I misread the documentation. Can you check what shape your incoming data has and update your post? – Schottky Jan 16 '22 at 19:00
  • I actually don't know the shape .. can you tell me how can I find out or just simply copy my code and try to make it work on your IDE and tell me what is wrong with it. – Seif Koretum Jan 16 '22 at 22:25
  • I can't replicate it since I don't have access to two channels. Try `data = np.reshape(data, (2, -1))` and see if that works. Using only one channel does not raise the exception. – Schottky Jan 16 '22 at 22:39
  • Thank you this worked .. but one last question how do I write the data and rate into a wav file after noise cancellation this : write('without noise.wav',rate,data) not working – Seif Koretum Jan 17 '22 at 14:48
  • maybe you need an underscore? ` write('without_noise.wav',rate,data)` – Schottky Jan 17 '22 at 14:57

2 Answers2

1

You have to use the reshaping suggested in the comments and then reshape back before saving:

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))
Caridorc
  • 6,222
  • 2
  • 31
  • 46
0

You can change the audio to mono

aud_seg = AudioSegment.from_wav(wav_filename)
aud_seg = aud_seg.set_channels(1)
aud_seg.export(wav_filename, format="wav")
njho
  • 2,023
  • 4
  • 22
  • 37