From 55d10b73e97e56b045e867f072bb3a8df217737e Mon Sep 17 00:00:00 2001 From: Joel Mathew Thomas <90510078+joelmathewthomas@users.noreply.github.com> Date: Mon, 24 Feb 2025 20:16:36 +0530 Subject: [PATCH] create deepfilternet_wrapper --- src/refinement/__init__.py | 12 +++++++++ src/refinement/deepfilternet_wrapper.py | 35 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/refinement/__init__.py create mode 100644 src/refinement/deepfilternet_wrapper.py diff --git a/src/refinement/__init__.py b/src/refinement/__init__.py new file mode 100644 index 0000000..b02a5df --- /dev/null +++ b/src/refinement/__init__.py @@ -0,0 +1,12 @@ +# __init__.py + +import logging +from datetime import datetime + +# Configure logging +logging.basicConfig( + format='%(asctime)s : %(message)s', + level = logging.INFO +) + +logging.info("freq-split-enhance/refinement package has been imported.") \ No newline at end of file diff --git a/src/refinement/deepfilternet_wrapper.py b/src/refinement/deepfilternet_wrapper.py new file mode 100644 index 0000000..6140a28 --- /dev/null +++ b/src/refinement/deepfilternet_wrapper.py @@ -0,0 +1,35 @@ +import os +import torch +from df.enhance import enhance, init_df, load_audio, save_audio + +def noisereduce(input_audio_path, output_audio_path, model_path=None): + """ + Apply noise reduction using DeepFilterNet. + + Args: + input_audio_path (str): Path to the input noisy audio file. + output_audio_path (str): Path to save the enhanced audio file. + model_path (str, optional): Path to a custom DeepFilterNet model. Defaults to None (uses the pre-trained model). + + Returns: + str: Path to the enhanced audio file. + """ + if not os.path.exists(input_audio_path): + raise FileNotFoundError(f"Input file {input_audio_path} not found") + + # Initialize DeepFilterNet model + model, df_state, _ = init_df(model_path) + + # Load audio + audio, _ = load_audio(input_audio_path, sr=df_state.sr()) + + # Ensure output path exists + os.makedirs(os.path.dirname(output_audio_path), exist_ok=True) + + # Apply noise reduction + enhanced_audio = enhance(model, df_state, audio) + + # Save the enhanced audio + save_audio(output_audio_path, enhanced_audio, df_state.sr()) + + return output_audio_path