Fixed STEM analysis not working on Windows

This commit is contained in:
patben8 committed 2026-10-13 10:59:22 +02:00
1 parent 591f2bc791
commit 87cbd3720e
5 files changed
+24 -16

No files matched your search

+2 -2
View File
@@ -235,8 +235,8 @@ def detect_grid(
if bpm is not None and not (20.0 <= bpm <= 400.0): if bpm is not None and not (20.0 <= bpm <= 400.0):
raise ValueError(f"Not a usable BPM: {bpm}") raise ValueError(f"Not a usable BPM: {bpm}")
if y is None: if y is None:
import librosa from . import waveform
y, sr = librosa.load(audio_path, sr=sr, mono=True) y, sr = waveform.load(audio_path, sr=sr)
y = np.asarray(y, dtype=np.float32) y = np.asarray(y, dtype=np.float32)
duration = len(y) / sr duration = len(y) / sr
if duration < 1.5: if duration < 1.5:
+2 -2
View File
@@ -178,7 +178,7 @@ def with_detected_key(track, y: np.ndarray | None, sr: int):
def detect_key(audio_path: str) -> KeyResult | None: 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) return detect_key_samples(y, sr)
+2 -2
View File
@@ -273,8 +273,8 @@ def analyse(
if not markers: if not markers:
raise ValueError("Set a beatgrid first") raise ValueError("Set a beatgrid first")
if y is None: if y is None:
import librosa from . import waveform
y, _ = librosa.load(audio_path, sr=_SR, mono=True) y, _ = waveform.load(audio_path, sr=_SR)
y = np.asarray(y, dtype=np.float32) y = np.asarray(y, dtype=np.float32)
duration = len(y) / _SR duration = len(y) / _SR
beats, bar0 = grid_beats(markers, duration) beats, bar0 = grid_beats(markers, duration)
+16 -8
View File
@@ -84,8 +84,8 @@ class Analysis:
return self.columns.duration return self.columns.duration
def _decode_pyav(path: Path) -> np.ndarray: def _decode_pyav(path: Path, sr: int = SR) -> np.ndarray:
"""Mono float32 at `SR`, via PyAV — FFmpeg's decoders, bundled in its wheel. """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 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 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: with pyav.open_input(path) as container:
stream = container.streams.audio[0] stream = container.streams.audio[0]
resampler = av.AudioResampler(format="flt", layout="mono", rate=SR) resampler = av.AudioResampler(format="flt", layout="mono", rate=sr)
chunks = [] chunks = []
for frame in container.decode(stream): for frame in container.decode(stream):
for out in resampler.resample(frame): 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:])] 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: try:
return _decode_pyav(path) return _decode_pyav(Path(path), sr), sr
except Exception as ex: # PyAV missing, or a file FFmpeg cannot open except Exception as ex: # PyAV missing, or a file FFmpeg cannot open
log.debug("PyAV could not decode %s (%s); trying librosa", path, ex) 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) y, _ = librosa.load(str(path), sr=sr, mono=True)
return y 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 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) log.warning("could not decode %s for a waveform: %s", path, ex)
return None return None
+2 -2
View File
@@ -1666,10 +1666,10 @@ def analyze_preview(body: AnalyzeRequest) -> AnalyzePreview:
def _decode_for_analysis(path: Path): def _decode_for_analysis(path: Path):
"""Mono samples at the rate BOTH detectors work at (they share one decode).""" """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" 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: def _analyse_one(a, track_id: str, body: AnalyzeRequest, result: dict) -> None: