Spaces:
Sleeping
Sleeping
Update js/app.py
Browse files163 unmodified lines164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201121 unmodified lines323324325326327328329163 unmodified linesdef load_audio_via_ffmpeg(input_path: Path, target_sr: int) -> torch.Tensor:"""Decodes any audio format to a temporary stereo WAV file at the targetsample rate using FFmpeg, then loads it with torchaudio.Uses pcm_s24le (universally supported 24-bit integer PCM) to preservedynamic range, rather than pcm_f32le whose IEEE-float WAV header supportvaries across library versions on the HF Docker image."""temp_wav = input_path.with_suffix(".temp.wav")n_threads = "2"try:cmd = ["ffmpeg", "-y","-threads", n_threads,"-i", str(input_path),"-vn","-acodec", "pcm_s24le", # high-fidelity 24-bit PCM"-ac", "2","-ar", str(target_sr),str(temp_wav)]result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)if result.returncode != 0:error_msg = result.stderr.decode("utf-8", errors="ignore")raise RuntimeError(f"FFmpeg conversion failed: {error_msg}")wav, _ = torchaudio.load(str(temp_wav))return wavfinally:if temp_wav.exists():temp_wav.unlink()def save_mp3_directly(tensor: torch.Tensor, out_path: Path, samplerate: int) -> None:121 unmodified linesstems_to_write = [("vocals", vocals), ("instrumental", instrumental)]job.stems = ["vocals", "instrumental"]n_workers = min(len(stems_to_write), _N_THREADS)with ThreadPoolExecutor(max_workers=n_workers) as pool:futures = [pool.submit(_save_stem, name, tensor, out_dir, model.samplerate)163 unmodified lines164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199121 unmodified lines321322323324325326327163 unmodified linesdef load_audio_via_ffmpeg(input_path: Path, target_sr: int) -> torch.Tensor:"""Decodes any audio format directly to memory using FFmpeg and loads it into a torch Tensor,avoiding intermediate disk I/O."""# -f f32le specifies raw 32-bit floating point PCM output on stdout.cmd = ["ffmpeg", "-y","-threads", "2","-i", str(input_path),"-vn","-f", "f32le","-ac", "2","-ar", str(target_sr),"-"]process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)stdout, stderr = process.communicate()if process.returncode != 0:error_msg = stderr.decode("utf-8", errors="ignore")raise RuntimeError(f"FFmpeg decoding failed: {error_msg}")import numpy as np# Convert raw bytes back to PyTorch tensor.# FFmpeg outputs interleaved float32 samples.raw_audio = np.frombuffer(stdout, dtype=np.float32)# Reshape to (samples, 2) and transpose to (2, samples)raw_audio = raw_audio.reshape(-1, 2)wav = torch.from_numpy(raw_audio).t().contiguous()return wavdef save_mp3_directly(tensor: torch.Tensor, out_path: Path, samplerate: int) -> None:121 unmodified linesstems_to_write = [("vocals", vocals), ("instrumental", instrumental)]job.stems = ["vocals", "instrumental"]n_workers = len(stems_to_write)with ThreadPoolExecutor(max_workers=n_workers) as pool:futures = [pool.submit(_save_stem, name, tensor, out_dir, model.samplerate)
js/app.py+29-31