harp-dev commited on
Commit
19cd1f0
·
verified ·
1 Parent(s): 234c9a9

Restore the input's sample rate and channel count

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -19,10 +19,15 @@ model_card = ModelCard(
19
  name="DeepFilterNet",
20
  description=(
21
  (
22
- "Background noise suppression / speech enhancement using DeepFilterNet3. "
23
- "Input is converted to mono 48kHz. Slider controls strength through "
24
- "wet/dry blend. Repository: https://github.com/Rikorose/DeepFilterNet "
 
 
 
 
25
  "Paper: https://arxiv.org/abs/2305.08227"
 
26
  )
27
  ),
28
  author="Hendrik Schröter, Alberto N. Escalante-B., Tobias Rosenkranz, Andreas Maier",
@@ -87,6 +92,12 @@ def process_fn(input_audio_path: str, noise_atten_db: float) -> str:
87
  if not input_audio_path:
88
  raise ValueError("No input audio provided.")
89
 
 
 
 
 
 
 
90
  # Load and normalize
91
  noisy, sr = load_audio_mono_48k(input_audio_path)
92
 
@@ -105,7 +116,15 @@ def process_fn(input_audio_path: str, noise_atten_db: float) -> str:
105
  out_dir.mkdir(parents=True, exist_ok=True)
106
  out_path = out_dir / "denoised.wav"
107
 
108
- sf.write(str(out_path), out, sr)
 
 
 
 
 
 
 
 
109
  return str(out_path)
110
 
111
 
 
19
  name="DeepFilterNet",
20
  description=(
21
  (
22
+ (
23
+ "Background noise suppression / speech enhancement using "
24
+ "DeepFilterNet3. Input is converted to mono 48kHz. Slider controls "
25
+ "strength through wet/dry blend. Denoising runs at 48 kHz in mono; the "
26
+ "result is resampled back to the input's rate and copied across its "
27
+ "channels, so the output matches the format it came in with but carries "
28
+ "no stereo image. Repository: https://github.com/Rikorose/DeepFilterNet "
29
  "Paper: https://arxiv.org/abs/2305.08227"
30
+ )
31
  )
32
  ),
33
  author="Hendrik Schröter, Alberto N. Escalante-B., Tobias Rosenkranz, Andreas Maier",
 
92
  if not input_audio_path:
93
  raise ValueError("No input audio provided.")
94
 
95
+ # The session's format, read before the downmix and resample that
96
+ # DeepFilterNet needs, and restored before saving
97
+ info = sf.info(input_audio_path)
98
+ original_sr = info.samplerate
99
+ original_channels = info.channels
100
+
101
  # Load and normalize
102
  noisy, sr = load_audio_mono_48k(input_audio_path)
103
 
 
116
  out_dir.mkdir(parents=True, exist_ok=True)
117
  out_path = out_dir / "denoised.wav"
118
 
119
+ if original_sr != sr:
120
+ out = librosa.resample(out, orig_sr=sr, target_sr=original_sr)
121
+
122
+ # Denoising is mono here, so the channels are copies rather than a
123
+ # recovered image, which the model card states
124
+ if original_channels > 1:
125
+ out = np.tile(out[:, None], (1, original_channels))
126
+
127
+ sf.write(str(out_path), out, original_sr)
128
  return str(out_path)
129
 
130