{"id":6011,"library":"noisereduce","title":"Noise Reduction using Spectral Gating","description":"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.","status":"active","version":"3.0.3","language":"en","source_language":"en","source_url":"https://github.com/timsainb/noisereduce","tags":["audio","signal processing","noise reduction","spectral gating","pytorch","machine learning","sound"],"install":[{"cmd":"pip install noisereduce","lang":"bash","label":"Core library"},{"cmd":"pip install noisereduce[torch]","lang":"bash","label":"With PyTorch backend (optional)"}],"dependencies":[{"reason":"Core numerical operations for audio processing.","package":"numpy"},{"reason":"Scientific computing tools, especially signal processing functions.","package":"scipy"},{"reason":"Progress bar for long-running operations.","package":"tqdm"},{"reason":"Required for the PyTorch-based noise reduction module (noisereduce.nn.NoiseReduce).","package":"torch","optional":true}],"imports":[{"symbol":"reduce_noise","correct":"from noisereduce import reduce_noise"},{"symbol":"NoiseReduce (PyTorch module)","correct":"from noisereduce.nn import NoiseReduce"},{"note":"Only use this import for the legacy (v1.x) API after upgrading to v2.0.0+ if compatibility is required.","wrong":"from noisereduce import reduce_noise","symbol":"reduce_noise (legacy v1)","correct":"from noisereduce.noisereducev1 import reduce_noise"}],"quickstart":{"code":"import noisereduce as nr\nimport numpy as np\n\n# --- 1. Generate dummy noisy audio ---\nrate = 44100  # sampling rate\nduration = 5  # seconds\nt = np.linspace(0, duration, int(rate * duration), endpoint=False)\n\n# Clean signal (e.g., a sine wave)\nclean_audio = 0.5 * np.sin(2 * np.pi * 440 * t) # A4 note\n\n# Add some random noise\nnoise = 0.2 * np.random.randn(len(t))\nnoisy_audio = clean_audio + noise\n\n# --- 2. Reduce noise ---\n# For stationary noise (default and generally faster)\nreduced_noise_stationary = nr.reduce_noise(\n    y=noisy_audio, \n    sr=rate, \n    stationary=True\n)\n\n# For non-stationary noise (e.g., speech with varying background noise)\n# This is often more effective but can be slower.\nreduced_noise_non_stationary = nr.reduce_noise(\n    y=noisy_audio, \n    sr=rate, \n    stationary=False\n)\n\nprint(f\"Original audio shape: {noisy_audio.shape}\")\nprint(f\"Reduced audio (stationary) shape: {reduced_noise_stationary.shape}\")\nprint(f\"Reduced audio (non-stationary) shape: {reduced_noise_non_stationary.shape}\")\n\n# --- Optional: Using the PyTorch backend (requires `pip install noisereduce[torch]`) ---\n# try:\n#     import torch\n#     model = nr.nn.NoiseReduce(sr=rate, nonstationary=False)\n#     audio_tensor = torch.from_numpy(noisy_audio).float().unsqueeze(0) # Add batch dim\n#     reduced_audio_tensor = model(audio_tensor)\n#     reduced_audio_pytorch = reduced_audio_tensor.squeeze(0).numpy()\n#     print(f\"Reduced audio (PyTorch) shape: {reduced_audio_pytorch.shape}\")\n# except ImportError:\n#     print(\"PyTorch not installed, skipping PyTorch example.\")","lang":"python","description":"This quickstart demonstrates how to generate a simple noisy audio signal and apply noise reduction using both the default stationary and the more robust non-stationary modes of the `noisereduce.reduce_noise` function. It also includes comments on how to use the optional PyTorch backend for higher performance."},"warnings":[{"fix":"Update your code to use the new `noisereduce.reduce_noise` function. If you need to maintain compatibility with older code, import `reduce_noise` from `noisereduce.noisereducev1`.","message":"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`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For performance-critical applications or integration into deep learning pipelines, consider migrating to the `noisereduce.nn.NoiseReduce` module and installing `noisereduce[torch]`.","message":"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`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Experiment with `stationary=True` and `stationary=False` based on the characteristics of your noise. For voice or complex environmental sounds, `stationary=False` is generally recommended.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"If your project requires `librosa`, ensure it is explicitly listed in your project's dependencies (e.g., `pip install librosa`). For basic audio file I/O, `soundfile` is a common alternative.","message":"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`.","severity":"gotcha","affected_versions":">=3.0.3"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}