diff --git a/backend/konduktor/core/grid_detect.py b/backend/konduktor/core/grid_detect.py index 7c852f7..87efe2b 100644 --- a/backend/konduktor/core/grid_detect.py +++ b/backend/konduktor/core/grid_detect.py @@ -235,8 +235,8 @@ def detect_grid( if bpm is not None and not (20.0 <= bpm <= 400.0): raise ValueError(f"Not a usable BPM: {bpm}") if y is None: - import librosa - y, sr = librosa.load(audio_path, sr=sr, mono=True) + from . import waveform + y, sr = waveform.load(audio_path, sr=sr) y = np.asarray(y, dtype=np.float32) duration = len(y) / sr if duration < 1.5: diff --git a/backend/konduktor/core/key_detect.py b/backend/konduktor/core/key_detect.py index 02173a8..f429a6e 100644 --- a/backend/konduktor/core/key_detect.py +++ b/backend/konduktor/core/key_detect.py @@ -178,7 +178,7 @@ def with_detected_key(track, y: np.ndarray | None, sr: int): def detect_key(audio_path: str) -> KeyResult | None: - import librosa + from . import waveform - y, sr = librosa.load(audio_path, sr=SR, mono=True) + y, sr = waveform.load(audio_path, sr=SR) return detect_key_samples(y, sr) diff --git a/backend/konduktor/core/structure.py b/backend/konduktor/core/structure.py index 70222af..4535644 100644 --- a/backend/konduktor/core/structure.py +++ b/backend/konduktor/core/structure.py @@ -273,8 +273,8 @@ def analyse( if not markers: raise ValueError("Set a beatgrid first") if y is None: - import librosa - y, _ = librosa.load(audio_path, sr=_SR, mono=True) + from . import waveform + y, _ = waveform.load(audio_path, sr=_SR) y = np.asarray(y, dtype=np.float32) duration = len(y) / _SR beats, bar0 = grid_beats(markers, duration) diff --git a/backend/konduktor/core/waveform.py b/backend/konduktor/core/waveform.py index 9c00067..2e97c55 100644 --- a/backend/konduktor/core/waveform.py +++ b/backend/konduktor/core/waveform.py @@ -84,8 +84,8 @@ class Analysis: return self.columns.duration -def _decode_pyav(path: Path) -> np.ndarray: - """Mono float32 at `SR`, via PyAV — FFmpeg's decoders, bundled in its wheel. +def _decode_pyav(path: Path, sr: int = SR) -> np.ndarray: + """Mono float32 at `sr`, via PyAV — FFmpeg's decoders, bundled in its wheel. The reason it is first: it decodes AAC (every Traktor `.stem.m4a`) on EVERY OS, where librosa needs CoreAudio (macOS only) or a system ffmpeg. Measured @@ -98,7 +98,7 @@ def _decode_pyav(path: Path) -> np.ndarray: with pyav.open_input(path) as container: stream = container.streams.audio[0] - resampler = av.AudioResampler(format="flt", layout="mono", rate=SR) + resampler = av.AudioResampler(format="flt", layout="mono", rate=sr) chunks = [] for frame in container.decode(stream): for out in resampler.resample(frame): @@ -122,16 +122,24 @@ def even_slices(n: int, k: int) -> list[slice]: return [slice(int(a), int(max(b, a + 1)) if n else 0) for a, b in zip(edges[:-1], edges[1:])] -def _decode(path: Path) -> np.ndarray | None: +def load(path: Path | str, sr: int = SR) -> tuple[np.ndarray, int]: + """Mono float32 at `sr` as `(y, sr)` — `librosa.load`'s shape, raising as it + does. EVERY analysis decode goes through here, never `librosa.load` itself: + librosa reads AAC only through CoreAudio or a system ffmpeg, so on Windows + a `.stem.m4a` failed with "Format not recognised".""" try: - return _decode_pyav(path) + return _decode_pyav(Path(path), sr), sr except Exception as ex: # PyAV missing, or a file FFmpeg cannot open log.debug("PyAV could not decode %s (%s); trying librosa", path, ex) - try: - import librosa + import librosa - y, _ = librosa.load(str(path), sr=SR, mono=True) - return y + y, _ = librosa.load(str(path), sr=sr, mono=True) + return y, sr + + +def _decode(path: Path) -> np.ndarray | None: + try: + return load(path)[0] except Exception as ex: # any decoder failure: no waveform, not a failed export log.warning("could not decode %s for a waveform: %s", path, ex) return None diff --git a/backend/konduktor/main.py b/backend/konduktor/main.py index a62c56b..9ea0388 100644 --- a/backend/konduktor/main.py +++ b/backend/konduktor/main.py @@ -1666,10 +1666,10 @@ def analyze_preview(body: AnalyzeRequest) -> AnalyzePreview: def _decode_for_analysis(path: Path): """Mono samples at the rate BOTH detectors work at (they share one decode).""" - import librosa + from .core import waveform assert grid_detect._SR == key_detect.SR, "grid and key detection must share a sample rate" - return librosa.load(str(path), sr=key_detect.SR, mono=True) + return waveform.load(path, sr=key_detect.SR) def _analyse_one(a, track_id: str, body: AnalyzeRequest, result: dict) -> None: