Noise Reduction using Spectral Gating
NoiseReduce is a Python library for reducing noise in audio signals using a spectral gating algorithm. It offers both a traditional NumPy/SciPy implementation and a more performant PyTorch-based backend for advanced use cases. Currently at version 3.0.3, it is under active development with occasional major updates introducing new features and performance improvements.
Warnings
- breaking The API for `noisereduce` underwent a significant breaking change in version 2.0.0. The primary `reduce_noise` function's signature and behavior changed, and the old API was moved to `noisereduce.noisereducev1.reduce_noise`.
- breaking Version 3.0.0 introduced a new PyTorch-based implementation for `noisereduce`, offering significant performance improvements and the ability to integrate into neural network architectures. While the original `reduce_noise` function still exists, new PyTorch-specific functionality requires `noisereduce.nn.NoiseReduce`.
- gotcha The `stationary` parameter in `nr.reduce_noise` (default: `True`) significantly impacts results. While `True` works well for constant background hums, `False` is crucial for non-stationary noise sources like speech or music, though it can be computationally more intensive.
- gotcha As of version 3.0.3, `librosa` is no longer a direct dependency of `noisereduce`. If your existing code relies on `librosa` for audio loading, resampling, or other utilities in conjunction with `noisereduce`, you will need to explicitly install `librosa`.
Install
-
pip install noisereduce -
pip install noisereduce[torch]
Imports
- reduce_noise
from noisereduce import reduce_noise
- NoiseReduce (PyTorch module)
from noisereduce.nn import NoiseReduce
- reduce_noise (legacy v1)
from noisereduce.noisereducev1 import reduce_noise
Quickstart
import noisereduce as nr
import numpy as np
# --- 1. Generate dummy noisy audio ---
rate = 44100 # sampling rate
duration = 5 # seconds
t = np.linspace(0, duration, int(rate * duration), endpoint=False)
# Clean signal (e.g., a sine wave)
clean_audio = 0.5 * np.sin(2 * np.pi * 440 * t) # A4 note
# Add some random noise
noise = 0.2 * np.random.randn(len(t))
noisy_audio = clean_audio + noise
# --- 2. Reduce noise ---
# For stationary noise (default and generally faster)
reduced_noise_stationary = nr.reduce_noise(
y=noisy_audio,
sr=rate,
stationary=True
)
# For non-stationary noise (e.g., speech with varying background noise)
# This is often more effective but can be slower.
reduced_noise_non_stationary = nr.reduce_noise(
y=noisy_audio,
sr=rate,
stationary=False
)
print(f"Original audio shape: {noisy_audio.shape}")
print(f"Reduced audio (stationary) shape: {reduced_noise_stationary.shape}")
print(f"Reduced audio (non-stationary) shape: {reduced_noise_non_stationary.shape}")
# --- Optional: Using the PyTorch backend (requires `pip install noisereduce[torch]`) ---
# try:
# import torch
# model = nr.nn.NoiseReduce(sr=rate, nonstationary=False)
# audio_tensor = torch.from_numpy(noisy_audio).float().unsqueeze(0) # Add batch dim
# reduced_audio_tensor = model(audio_tensor)
# reduced_audio_pytorch = reduced_audio_tensor.squeeze(0).numpy()
# print(f"Reduced audio (PyTorch) shape: {reduced_audio_pytorch.shape}")
# except ImportError:
# print("PyTorch not installed, skipping PyTorch example.")