harp-dev commited on
Commit
3cedd40
·
verified ·
1 Parent(s): 0cccef3

Restore the input's sample rate and channel count

Browse files
Files changed (1) hide show
  1. app.py +13 -1
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import math
 
2
  import tempfile
3
  from pathlib import Path
4
 
@@ -301,13 +302,24 @@ MODEL = LarsNet(wiener_filter=False, device="cpu", config="config.yaml")
301
 
302
  @torch.inference_mode()
303
  def process_fn(audio_path: str):
 
 
 
 
304
  stems = MODEL(audio_path)
305
  output_dir = Path("outputs")
306
  output_dir.mkdir(exist_ok=True)
307
  output_paths = []
308
  for stem_name in ["kick", "snare", "toms", "hihat", "cymbals"]:
309
  out_path = output_dir / f"{stem_name}.wav"
310
- sf.write(out_path, stems[stem_name].cpu().numpy().T, MODEL.sr)
 
 
 
 
 
 
 
311
  output_paths.append(str(out_path))
312
  return tuple(output_paths)
313
 
 
1
  import math
2
+ import librosa
3
  import tempfile
4
  from pathlib import Path
5
 
 
302
 
303
  @torch.inference_mode()
304
  def process_fn(audio_path: str):
305
+ # The model works at its own rate, so the session's rate is read before
306
+ # anything touches the file and the stems are resampled back to it
307
+ original_sr = sf.info(audio_path).samplerate
308
+
309
  stems = MODEL(audio_path)
310
  output_dir = Path("outputs")
311
  output_dir.mkdir(exist_ok=True)
312
  output_paths = []
313
  for stem_name in ["kick", "snare", "toms", "hihat", "cymbals"]:
314
  out_path = output_dir / f"{stem_name}.wav"
315
+ stem = stems[stem_name].cpu().numpy().T
316
+
317
+ if original_sr != MODEL.sr:
318
+ stem = librosa.resample(
319
+ stem.T, orig_sr=MODEL.sr, target_sr=original_sr
320
+ ).T
321
+
322
+ sf.write(out_path, stem, original_sr)
323
  output_paths.append(str(out_path))
324
  return tuple(output_paths)
325