File size: 3,111 Bytes
429023a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""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)