pythonaudioscipysignal-processinglibrosa

Python: How to separate out noise from human speech in audio file?


I have an audio file in which I attempted to filter out the noise:

frequency, array = read('sample/OSR_us_000_0014_8k.wav')
b, a = signal.butter(5, 1000/(frequency/2), btype='highpass')
filteredSignal = signal.lfilter(b, a, newSound)

This highpass filter doesn't appear to be that effective though. Are there better ways to accomplish what I am attempting to do? Additionally, I would prefer to have the extracted background noise. Most algorithms available filter out the noise, but I'd like to extract the noise out as a numpy array.


Solution

  • There is a deep learning-based neural network pretrained model available in Python for noise removal from audio files. I've used it, and it provides very high accuracy.

    However, it only supports WAV audio files with a sampling rate of 48k. If your WAV file has a different sampling rate, you can convert it to 48k using the librosa library..

    Steps to Use DeepFilterNet for Enhancing Noisy Audio Files

    If your audio file has a different sampling rate, follow these steps:

    Install librosa & soundfile library:

    pip install librosa soundfile
    

    Use the following function to resample the audio:

    import librosa
    import soundfile as sf
    
    def re_sample_audio(audio_path, sr):
        print("Resampling audio")
        audio, _ = librosa.load(audio_path, sr=16000)
        audio_resampled = librosa.resample(audio, target_sr=sr, orig_sr=16000)
        sf.write(audio_path, audio_resampled, 48000)
        print("Resampling done")
    
    re_sample_audio(audio_path, 16000)
    

    Installing Necessary Libraries for DeepFilterNet

    Install the required PyTorch version (>=1.9) with CPU or CUDA support from pytorch.org, e.g.:

    pip install torch torchaudio -f https://download.pytorch.org/whl/cpu/torch_stable.html
    

    Install DeepFilterNet:

    pip install deepfilternet
    

    Enhancing the Noisy Audio File

    Use the following code to enhance a noisy audio file:

    from df.enhance import enhance, init_df, load_audio, save_audio
    from df.utils import download_file
    
    if __name__ == "__main__":
        # Load the default model
        model, df_state, _ = init_df()
    
        # Path to the noisy audio
        audio_path = "noisy_audio_path.wav"
    
        audio, _ = load_audio(audio_path, sr=df_state.sr())
    
        # Denoise the audio
        enhanced = enhance(model, df_state, audio)
    
        # Save the enhanced audio
        save_audio("enhanced.wav", enhanced, df_state.sr())
    

    Alternatively, You Can Use the Command Line to Enhance Noisy Audio Files

    # Specify an output directory with --output-dir [OUTPUT_DIR]
    deepFilter path/to/noisy_audio.wav