gilliaan commited on
Commit
dae9457
·
verified ·
1 Parent(s): 69249b8

Updated by Bas Curtiz. Seems to work better this way!

Browse files
Files changed (1) hide show
  1. demucs_stem_organizer_ui.py +25 -0
demucs_stem_organizer_ui.py CHANGED
@@ -106,6 +106,31 @@ def next_sequence_number(out_dir: Path, manifest: dict) -> int:
106
 
107
 
108
  def load_audio(path: str, sr: int, ch: int = 2) -> np.ndarray:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  return AudioFile(path).read(streams=0, samplerate=sr, channels=ch).numpy().astype(np.float32)
110
 
111
 
 
106
 
107
 
108
  def load_audio(path: str, sr: int, ch: int = 2) -> np.ndarray:
109
+ p = Path(path)
110
+ sf_exts = {'.wav', '.flac', '.aif', '.aiff', '.ogg'}
111
+ if p.suffix.lower() in sf_exts:
112
+ try:
113
+ data, file_sr = sf.read(str(p), dtype='float32', always_2d=True)
114
+ # sf.read returns (samples, channels); convert to (channels, samples)
115
+ audio = data.T
116
+ # Normalise channel count
117
+ if audio.shape[0] == 1:
118
+ audio = np.repeat(audio, ch, axis=0)
119
+ elif audio.shape[0] > ch:
120
+ audio = audio[:ch]
121
+ # Resample if needed
122
+ if file_sr != sr:
123
+ try:
124
+ import resampy
125
+ audio = resampy.resample(audio, file_sr, sr, axis=1)
126
+ except ImportError:
127
+ raise RuntimeError(
128
+ f"Sample rate mismatch ({file_sr} Hz vs expected {sr} Hz) "
129
+ "and resampy is not installed. Install it with: pip install resampy"
130
+ )
131
+ return audio.astype(np.float32)
132
+ except Exception:
133
+ pass # fall through to AudioFile / ffmpeg
134
  return AudioFile(path).read(streams=0, samplerate=sr, channels=ch).numpy().astype(np.float32)
135
 
136