77from ffmpeg_normalize import FFmpegNormalize
88from tqdm import tqdm
99
10- from openlrc .defaults import LOUDNORM_SUFFIX , NOISE_SUPPRESSED_SUFFIX , PREPROCESSED_DIR , default_preprocess_options
10+ from openlrc .defaults import (
11+ DEFAULT_DPDFNET_MODEL ,
12+ LOUDNORM_SUFFIX ,
13+ NOISE_SUPPRESSED_SUFFIX ,
14+ PREPROCESSED_DIR ,
15+ default_preprocess_options ,
16+ )
1117from openlrc .logger import logger
12- from openlrc .media_utils import release_memory
1318from openlrc .utils import get_preprocessed_path
1419
1520
@@ -63,17 +68,15 @@ def noise_suppression(self, audio_paths: list[Path], atten_lim_db: int = 15):
6368 return []
6469
6570 try :
66- import torch
67- from df .enhance import enhance , init_df , load_audio , save_audio
71+ import dpdfnet
72+ import librosa
73+ import soundfile
6874 except ImportError :
69- raise ImportError (
70- "Noise suppression requires torch and deepfilternet. Install them with: pip install 'openlrc[full]'"
71- )
75+ raise ImportError ("Noise suppression requires dpdfnet. Install it with: pip install 'openlrc[full]'" )
7276
7377 if "atten_lim_db" in self .options :
7478 atten_lim_db = self .options ["atten_lim_db" ]
75-
76- model , df_state , _ = init_df ()
79+ model = self .options .get ("dpdfnet_model" , DEFAULT_DPDFNET_MODEL )
7780 chunk_size = 180 # 3 min
7881
7982 ns_audio_paths = []
@@ -82,31 +85,33 @@ def noise_suppression(self, audio_paths: list[Path], atten_lim_db: int = 15):
8285 ns_path = output_path / f"{ audio_name } { NOISE_SUPPRESSED_SUFFIX } .wav"
8386
8487 if not ns_path .exists ():
85- audio , info = load_audio (str (audio_path ), sr = df_state .sr ())
88+ waveform , sr = librosa .load (str (audio_path ), sr = None , mono = False )
89+ sr = int (sr )
8690
8791 # Split audio into 3 min chunks
92+ chunk_samples = chunk_size * sr
8893 audio_chunks = [
89- audio [:, i : i + chunk_size * info .sample_rate ]
90- for i in range (0 , audio .shape [1 ], chunk_size * info .sample_rate )
94+ waveform [..., i : i + chunk_samples ] for i in range (0 , waveform .shape [- 1 ], chunk_samples )
9195 ]
9296
93- enhanced_chunks = []
94- for ac in tqdm (audio_chunks , desc = f"Noise suppressing for { audio_name } " ):
95- enhanced_chunks .append (enhance (model , df_state , ac , atten_lim_db = atten_lim_db ))
96-
97- enhanced = torch .cat (enhanced_chunks , dim = 1 )
97+ try :
98+ with soundfile .SoundFile (str (ns_path ), mode = "w" , samplerate = sr , channels = 1 , subtype = "PCM_16" ) as f :
99+ for ac in tqdm (audio_chunks , desc = f"Noise suppressing for { audio_name } " ):
100+ enhanced = dpdfnet .enhance (ac , sr , model = model , attn_limit_db = atten_lim_db )
98101
99- if enhanced .shape != audio .shape :
100- raise ValueError (
101- f"Enhanced audio shape does not match original audio shape: { enhanced .shape } != { audio .shape } "
102- )
102+ if enhanced .shape [- 1 ] != ac .shape [- 1 ]:
103+ raise ValueError (
104+ f"Enhanced audio shape does not match original audio shape: "
105+ f"{ enhanced .shape } != { ac .shape } "
106+ )
103107
104- save_audio (str (ns_path ), enhanced , sr = df_state .sr ())
108+ f .write (enhanced )
109+ except Exception :
110+ ns_path .unlink (missing_ok = True )
111+ raise
105112
106113 ns_audio_paths .append (ns_path )
107114
108- release_memory (model )
109-
110115 return ns_audio_paths
111116
112117 def loudness_normalization (self , audio_paths : list [Path ]):
0 commit comments