voice-isolator / audio_processing.py
fffiloni's picture
Build Voice Isolator ZeroGPU listening studio
429023a verified
Raw History Blame Contribute Delete
3.11 kB
"""Decode source audio and export aligned model inputs and outputs."""
import subprocess
from pathlib import Path
import numpy as np
import soundfile as sf
import soxr
RATE = 48_000
MAX_SECONDS = 180
MAX_BYTES = 64_000_000
FORMATS = "wav,mp3,mov,ogg,flac,aac,matroska,webm,aiff"
class AudioInputError(ValueError):
"""A readable explanation for an invalid upload."""
def read_audio(path: str) -> np.ndarray:
if not path or not Path(path).is_file():
raise AudioInputError("Choose an audio file first.")
if Path(path).stat().st_size > MAX_BYTES:
raise AudioInputError("This recording is over the 64 MB limit.")
try:
with sf.SoundFile(path) as source:
# Bound allocations for files with unusual channel counts or sample rates.
if source.frames <= 0 or source.samplerate <= 0:
raise AudioInputError("This recording is empty or unreadable.")
if source.frames > source.samplerate * MAX_SECONDS:
raise AudioInputError("Choose a recording of 3 minutes or less.")
if source.frames * source.channels > 36_000_000:
return _read_with_ffmpeg(path)
rate = source.samplerate
audio = source.read(dtype="float32", always_2d=True).mean(axis=1)
if rate != RATE:
audio = soxr.resample(audio, rate, RATE)
except (sf.LibsndfileError, RuntimeError):
return _read_with_ffmpeg(path)
return _validate(audio)
def _read_with_ffmpeg(path: str) -> np.ndarray:
command = [
"ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin",
"-protocol_whitelist", "file,pipe", "-format_whitelist", FORMATS,
"-i", path, "-map", "0:a:0", "-vn", "-sn", "-dn",
"-t", str(MAX_SECONDS + 0.1), "-ac", "1", "-ar", str(RATE),
"-c:a", "pcm_f32le", "-f", "f32le", "pipe:1",
]
try:
result = subprocess.run(command, capture_output=True, check=True, timeout=50)
except (subprocess.SubprocessError, OSError) as exc:
raise AudioInputError("This audio format could not be read. Try WAV, MP3, or M4A.") from exc
return _validate(np.frombuffer(result.stdout, dtype="<f4"))
def _validate(audio: np.ndarray) -> np.ndarray:
if audio.size > RATE * MAX_SECONDS:
raise AudioInputError("Choose a recording of 3 minutes or less.")
if not audio.size or not np.isfinite(audio).all():
raise AudioInputError("This recording is empty or contains invalid audio.")
return np.array(audio, dtype=np.float32, order="C", copy=True)
def export_tracks(directory: Path, original: np.ndarray, isolated: np.ndarray) -> tuple[str, str, str]:
if isolated.shape != original.shape or not np.isfinite(isolated).all():
raise RuntimeError("The model returned invalid or misaligned audio.")
paths = []
for name, audio in (("original", original), ("isolated", isolated), ("removed", original - isolated)):
path = directory / f"{name}.wav"
sf.write(path, audio, RATE, format="WAV", subtype="FLOAT")
paths.append(str(path))
return tuple(paths)