timsainb / noisereduce Goto Github PK
View Code? Open in Web Editor NEWNoise reduction in python using spectral gating (speech, bioacoustics, audio, time-domain signals)
License: MIT License
Noise reduction in python using spectral gating (speech, bioacoustics, audio, time-domain signals)
License: MIT License
Hi, I have been trying to reduce the background stationary noise from a 4 second audio signal but the RAM crashes on Google Colab.
It gives the user warning as n_fft=1024 is too small for input signal of length=2 n_fft, y.shape[-1]
The sampling frequency of the audio signal is 44100 Hz, file type is wav file and is a 16 bit file.
I have attached the audio signal here by converting it into mp4 format because github wasn't allowing to upload .wav file.
Thanks
I tested this on multiple audio files (16kHz telephony audio) with prop_decrease ranging from (0,0.1,0.5,0.75,1)
My 3 observations were:
Could you help me identify if there's something wrong with my use of it or if there are any improvements that can be made.
(Also, I got floating point expected error so instead of using wavfile.read(), I used librosa.load(filepath,sr=None))
float32_to_int16(data): data * 32767 should be data * 32768
Hello, I have suspicion that the TQDM disabling does not work properly when another tqdm
progress bar is already in use
and the used function is:
import torchaudio
import noisereduce as nr
def create_spectrogram(fname, reduce_noise: bool = False):
waveform, sample_rate = torchaudio.load(fname, normalize=True)
waveform = waveform[0]
if reduce_noise:
waveform = torch.tensor(nr.reduce_noise(y=waveform, sr=sample_rate, win_length=256, use_tqdm=False, n_jobs=-1))
transform = torchaudio.transforms.Spectrogram(n_fft=3600, win_length=256)
spectrogram = transform(waveform)
return torch.log(spectrogram).numpy()
With tqdm 4.11.2, the import of noisereduce.py failed due to tqdm.autonotebook
being missing. I fixed it by updating to 4.31.1. A minimum version should be specified in the requirements.
I have to files ,i want to compare the noise floor,which is beetter?
Are there any indicators that reflect the background noise?
tks.
I've trawled through the C++ source for audacity's noise reduction effect, but I'm still unsure
in what way noisereduce.py differs from it (algorithmically) - and I'm also unsure
how the 'Sensitivity' control in the effect relates to the std_threshold paramater in noicereduce.py.
Can you explain?
Hi, What time unit are you using in this part of the code?
noisy_part = data[10000:15000]
Regards.
For the nonstationary algorithm, options to return sig_stft_denoised
and sig_mask
could be super useful for a few use cases
sig_stft_denoised
could be used for further spectral methods, rather than risking loss of info converting from spectrum to signal to spectrum again.sig_mask
would allow for visual inspection of the effect of each parameter change.If prop_decrease=0 the recovered_signal returned in reduce_noise function should be the same as the input signal to the function. This doesn't happen.
The reason it doesn't happen is because of a bug in mask_signal function. Inline 113 sig_stft_db_masked is calculated and later treated as it only contains the masked real part of the stft. This is wrong since sig_stft_db contains the magnitude of the stft (Real and Imaginary) which means that sig_stft_db_masked cannot contain only the real part.
This bug causes the real part of sig_stft_amp to be too large, which causes some sort of distortion.
I m on windows 10 and jupyter environment, the audio file lasted 30 minutes, so I cut the file in 10 seconds each and then continue, on the first chunk0 file came across MemoryError.
Is the file still large for the situation? I followed the link :
https://colab.research.google.com/github/timsainb/noisereduce/blob/master/notebooks/1.0-test-noise-reduction.ipynb#scrollTo=E5UkLtmT3xy3, the sample only lasted four seconds.
Or the paramter tuning would help for this ?
myaudio = AudioSegment.from_file(('myaudio.wav') , "wav")
chunk_length_ms = 10000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec
#Export all of the individual chunks as wav files
for i, chunk in enumerate(chunks):
chunk_name = "chunk{0}.wav".format(i)
print ("exporting", chunk_name)
chunk.export(chunk_name, format="wav")
data, rate = sf.read('chunk0.wav')
data = data
reduced_noise = nr.reduce_noise(y = data, sr=rate, n_std_thresh_stationary=1.5,stationary=True)
MemoryError: Unable to allocate 197. GiB for an array with shape (441000, 60002) and data type float64
It would be very interesting if we could have this project in a VST plugin, because in this case it would be possible to filter in real time, not just for a specific file. Have you guys thought about it?
First thanks @timsainb for porting this into python!
Unfortunately I have issues on some files for which the noise is actually amplified.
With the default parameters, this is an example of result that I get https://drive.google.com/open?id=1By4_l1kM9s6K013j6iRwQNsVx3Rvu6Nv
I have tried to play with the parameters but it didn't help. Even setting the noise to zero or prop_decrease
to zero produces similar additional noise.
Please let me know if it is expected, otherwise I will dig into the code. Thanks!
For reference, this is my code:
import librosa
from noisereduce import reduce_noise
file_path = "raw.wav"
s, e = 33.99000000000053, 35.520000000000586
a, sr = librosa.load(file_path, sr=None)
i_s = int(sr*s)
i_e = int(sr*e)
a_noise = a[i_s:i_e]
a_clean = reduce_noise(a, a_noise, prop_decrease=0)
librosa.output.write_wav("noise.wav", a_noise, sr)
librosa.output.write_wav("denoised.wav", a_clean, sr)
Hi @timsainb thanks for making the repo public.
I was trying to separate a given audio recording into speech and residual noise in the non-stationary case. I couldn't find a clean way to obtain the residual noise. Any suggestions?
According to the code the denoised version is obtained as sig_stft_denoised = sig_stft * sig_mask
. I tried to obtain the residual as sig_stft_residual = sig_stft * (1-sig_mask)
but this doesn't seem to behave as expected since sig_mask
values are not always bound to [0, 1]
When I try to use the pip package method and run the program, an error shows in \lib\site-packages\librosa\util\utils.py in valid_audio(y, mono)
data must be floating-point
Please help.
Code:
import noisereduce as nr
import scipy.io.wavfile as RE
rate, data = RE.read("sample.wav")
data = data/1.0
noisy_part = data[0:37526917]
noisy_part = noisy_part.sum(axis=1) / 2
reduced_noise = nr.reduce_noise(audio_clip=data, noise_clip=noisy_part, verbose=True)
RE.write('clean_audio.wav', rate, reduced_noise)
Code fails during STFT and prints "Killed: 9"
After installing this library for python 2.7, i found the following issues with file imports.
2)In the noisereduce.py file the 4th line i.e the from noisereduce.plotting simply needs to be from plotting import plot_reduction_steps.
I cannot create a new branch and push the code for fixing this issue.If you get the time can you please fix these 2 import issues?
Thanks.
Hi, it seems use_tqdm=False leads to the use of tqdm, while use_tqdm=True leads to no use of tqdm. In the source code:
tqdm(pos_list, disable=self.use_tqdm)
So use_tqdm=False means disable=False so enable is True...
Do you have a method for creating a common background noise file? I have a bunch of wav files that I would like to extract the common background noise from them instead of guessing. If not have you thought about adding this into your package?
Thanks for any help. I'm not experienced at GitHub posting so I hope this is an ok way to send ideas or requests.
Several people have asked for noisereduce to work for multiple channels.
Hello!,
I am trying to use noisereduce's reduce_noise function to clean some audio data. When I use a GPU (V100), some of the cleaned audio arrays come back as all NaN. This does not happen when I run the code on CPU. Any idea of how I might be able to fix this? Thanks!
When I import the noisereduce function in a Jupyter notebook I get the following warning with Tqdm :
It looks like the same issue from swifter issue #66
Hello! I will tell you my problem and please help me.
I am using Bluetooth headphones which I wear inside of a helmet. There is usually noise when recording any wav file inside of python. I thought about your library as I was thinking about removing the noise from the recorded wave file. The noise is caused by Bluetooth connection and the noise created by the car. I did the following, I recorded a 2-second wav file without any speech. This file mainly used to record the noise in the environment I am sitting in. Then, I recorded another file with me talking in the same environment for 2 seconds.
`rate, data = read("noise.wav")#the 2 second noise without any speaking
rate2, data2 = read("testing3.wav")#the 2 second noise + me talking
noisy_part = data[:]
reduced_noise = nr.reduce_noise(audio_clip=data, noise_clip=noisy_part, verbose=False)
write("lili.wav", frequency, reduced_noise)#saving the data into a new wav file'
I keep getting this error. I have a deadline tomorrow and I hope you could help me find out the problem in this error. I was wondering if you might have any ideas whether the method I am doing to remove the noise from the 2-second wave file will work or not. If you have any better methods I would be very thankful. I tried using high pass low pass bandwidth mean filter separately but the results are not that promising. It would great if you could give me your advice.
This is a link to the code and the files
https://drive.google.com/open?id=1J9abNuhtJpI01pm-LIKFg8zvS2JBR3rK
The setting of the wav file is
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 2
File "test.py", line 101, in
reduced_noise = nr.reduce_noise(audio_clip=data, noise_clip=noisy_part, verbose=False)
File "/home/mostafahaggag/.local/lib/python3.5/site-packages/noisereduce/noisereduce.py", line 123, in reduce_noise
noise_stft = _stft(noise_clip, n_fft, hop_length, win_length)
File "/home/mostafahaggag/.local/lib/python3.5/site-packages/noisereduce/noisereduce.py", line 9, in _stft
return librosa.stft(y=y, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
File "/home/mostafahaggag/.local/lib/python3.5/site-packages/librosa/core/spectrum.py", line 161, in stft
util.valid_audio(y)
File "/home/mostafahaggag/.local/lib/python3.5/site-packages/librosa/util/utils.py", line 159, in valid_audio
raise ParameterError('data must be floating-point')
librosa.util.exceptions.ParameterError: data must be floating-point
i am highly interesting in this project and currently stuck on my academic research. i am looking for a model to distinguish my specific machine in good or bad conditions. and the working sound of my machines are the important feature i need to use(easy to get), i am wondering could i tune this model for my work? thanks for your attension.
I listen and plot the reduced noise, but how can I save the reduced noise in a wav file??
I save like this:
librosa.output.write_wav('./prueba/audio_reduce1.wav', reduced_noise, sr)
But the wav file is noise.
Help me please!
Parameter Error : Invalid shape for monophonic audio : ndim=2
There is a typo on the noisereduce documentation page at https://pypi.org/project/noisereduce/
The mask is appled to the spectrogram of the signal, and is inverted If the noise signal is not provided, the algorithm will treat the signal as the noise clip, which tends to work pretty well
appled should say "applied"
Hi, What time unit are you using in this part of the code?
noisy_part = data[10000:15000]
Thanks.
can we check there is noise or not in wav file ?
I would like to congratulate on your great work, the library works as promised.
But there is a small issue which i found.
When i load the file with librosa.load and give that array as a input to reduce_noise, length of the array gets changed, what is the reason behind this. For a certain project of mine i am trying to use this library on small clips hardly of 1secs, but if such frames keep on vanishing i will loose a lot of important data. What is the workaround for this, moreover why are no of frames getting reduced?
I am a beginner of this project and curious of how to figure the value of the unit of x,y axis。
No package named soundfile
Not very disturbing, but when I use the function nr.reduce_noise(), by default I see the progress bar, and if I set use_tqdm to True I don't see it.
I want to remove environmental sounds from multiple files but of different lengths. Does this process require noise.wav to be of the exact same length as that of the original .wav file?
Hello,
First of all, thank you for your great module.
I'm trying to apply real-time noise reduction on incoming audio stream
Settings on stream opening:
Settings on stream reading:
The problem that I'm facing is that a periodical "fan spinning" sound appears, after actively applying noise reduction (while loop), but this sound does not appear on a normal 5 second recording with noise reduction on the np.int16 array afterwards.
What is different is that in the first case (active-ish noise reduction), I append the sound data for each iteration,after noise reduction, whereas in the second case I record for 5 seconds and THEN apply the noise reduction on the whole set of data.
I'm uploading example wavs to give you a better perspective:
normal_case_wavs.zip
active_case_wavs.zip
P.S I noticed that by changing the number of frames I read from the stream buffer, the frequency of this sound changes too. Could this be some kind of edge case where this sound indicates the change of "Sound CHUNK" I am processing (appending to the list for future .wav write) ?
Crucial part of code is here:
stream= p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=16000)
for i in range(0, int(16000 / 16000 * 5)):
data = stream.read(16000)
sound_data_npint16 = np.hstack(np.fromstring(data, dtype=np.int16))
noisy_frames.append(sound_data_npint16)
sound_data_float = np.ndarray.astype(sound_data_npint16,float)/32768
reduced_noise_float = nr.reduce_noise(audio_clip=sound_data_float, noise_clip=noise, verbose=False, n_fft=4096, n_std_thresh=1, pad_clipping=True) #Tried both pad_clipping=True/False
reduced_noise_npint16 = np.ndarray.astype(np.iinfo(np.int16).max*reduced_noise_float,dtype=np.int16)
denoised_active_frames.append(reduced_noise_npint16)
total_noisy_frames = np.hstack(noisy_frames) # noisy frames gathered
total_noisy_frames_float = np.ndarray.astype(total_noisy_frames,float)/32768
reduced_total_noisy_frames_float = nr.reduce_noise(audio_clip=total_noisy_frames_float, noise_clip=noise, verbose=False, n_fft=4096, n_std_thresh=1, pad_clipping=True)
reduced_total_noisy_frames_npint16 = np.ndarray.astype(np.iinfo(np.int16).max*reduced_total_noisy_frames_float,dtype=np.int16)
# noise wav comes from "noisy_frames"
# actively denoised wav comes from "denoised_active_frames"
# denoised wav after 5seconds recording comes from "reduced_total_noisy_frames_npint16"
Convolving time and frequency independently should speed up filtering
HI. Now it seems that new module is only GPU based???
or I am making any mistake?? It is giving this with only CPU based machine.
Please see error.. and help
----> 1 import noisereduce as nr
~/anaconda3/envs/server/lib/python3.6/site-packages/noisereduce/init.py in
----> 1 from noisereduce.noisereduce import reduce_noise
~/anaconda3/envs/server/lib/python3.6/site-packages/noisereduce/noisereduce.py in
10
11 print(
---> 12 "GPUs available: {}".format(tf.config.experimental.list_physical_devices("GPU"))
13 )
14 if int(tf.version[0]) < 2:
AttributeError: module 'tensorflow' has no attribute 'config'
Hi -
Even when using the "Simplest usage" code, it never returns, until it throws a signal 10+ minutes later, on a twenty-second wav file.
Does this require linux? (I'm on a mac), or some particular version of python? (I'm using 3.10).
Currently, it does not work for me, at all.
If I add hop_length parameter to the noise_reduce function, it gives an error:
`
import numpy as np
import noisereduce as nr
nfft = 512
fs = 8000
signal = np.loadtxt('signal.csv')
nr.reduce_noise(y=signal, sr=fs, n_fft=nfft, win_length=nfft)
nfft = 512
fs = 8000
signal = np.loadtxt('signal.csv')
nr.reduce_noise(y=signal, sr=fs, n_fft=nfft, win_length=nfft, hop_length=100)
`
I tried with different values and it always gives the same error:
AttributeError: 'SpectralGateNonStationary' object has no attribute '_hop_length'
Observed loss of volume when used with thresh_n_mult_nonstationary .
Hi, it's a great contribution but i have a question, if you only have one audio, so you don't have a extra file to the noise?
This is a typical problem, that you want to remove noise of a real audio.
In the document string of the y_noise
argument, it said:
y_noise : np.ndarray [shape=(# frames,) or (# channels, # frames)], real-valued noise signal to compute statistics over (only for non-stationary noise reduction).
But actually, an array of stationary noise can be passed to and well processed by the function(when stationary==True
). Am I right?
Hi, I've been using the noise reduction algorithm to standardize the noise before input the signal into a deep learning model. The issue I found while doing the noise reduction is a slightly frequency shift to lower values on the signal.
The shift becomes evident when looking the colors near the 4096 frequencies on the mel spectogram.
My Question is, How can I avoid this shift to lower frequencies? (It happens when reducing Stationary or Non-stationary noise)
Thank you
import pydub
from pydub import AudioSegment as am
import noisereduce as nr
from scipy.io import wavfile
import numpy as np
rate, sound1 = wavfile.read("test with noise.wav")
sound1 = sound1.astype(np.float32)
noisy_part = sound1[10000:15000]
reduced_noise = nr.reduce_noise(audio_clip=sound1, noise_clip=noisy_part, verbose=True)
The Above is the simply code I used, and when I run it, I get the following error
ParameterError: Invalid shape for monophonic audio: ndim=2, shape=(5000, 2)
I changed sound1
to a float because I encountered an error that asked me to change the variable to float.
The size of sound1
is (660672, 2)
The size of noisy_part
is (5000, 2)
Hi all,
I tried the reduce_noise function on a noisy clip with a sample of noisy part from the original clip, the output from the function include echo and the voice sound different than the one in original clip.
any help/fix for this issue
thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.