fix: waveform cache
This commit is contained in:
1 parent
669967eed3
commit
05d6c20691
1 file changed
+43
@@ -0,0 +1,43 @@
|
||||
"""File-based cache for loaded/resampled WAV data.
|
||||
|
||||
Stores .npy files in `cache/waveforms/` keyed by source path + target_sr + mono.
|
||||
Invalidated when source file mtime changes.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import hashlib
|
||||
import os
|
||||
import numpy as np
|
||||
|
||||
CACHE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cache", "waveforms")
|
||||
|
||||
|
||||
def _cache_key(path: str, target_sr: int, mono: bool) -> str:
|
||||
real = os.path.realpath(path)
|
||||
mtime = os.path.getmtime(real)
|
||||
raw = f"{real}|{target_sr}|{mono}|{mtime:.6f}"
|
||||
return hashlib.sha256(raw.encode()).hexdigest()[:16]
|
||||
|
||||
|
||||
def cached_load_wav(path: str, target_sr: int = 48000, mono: bool = True,
|
||||
_loader=None) -> tuple[np.ndarray, int]:
|
||||
"""Load WAV with disk cache. Falls back to _loader(data, sr) if cache miss."""
|
||||
key = _cache_key(path, target_sr, mono)
|
||||
npy_path = os.path.join(CACHE_DIR, f"{key}.npy")
|
||||
sr_path = os.path.join(CACHE_DIR, f"{key}.sr")
|
||||
|
||||
if os.path.exists(npy_path) and os.path.exists(sr_path):
|
||||
try:
|
||||
data = np.load(npy_path, allow_pickle=False)
|
||||
sr = int(np.load(sr_path, allow_pickle=False).item())
|
||||
return data, sr
|
||||
except Exception:
|
||||
pass # corrupted cache, rebuild
|
||||
|
||||
if _loader is None:
|
||||
from sync_audio import load_wav as _loader
|
||||
|
||||
data, sr = _loader(path, target_sr=target_sr, mono=mono)
|
||||
os.makedirs(CACHE_DIR, exist_ok=True)
|
||||
np.save(npy_path, data, allow_pickle=False)
|
||||
np.save(sr_path, np.array(sr), allow_pickle=False)
|
||||
return data, sr
|
||||
Reference in new issue
Block a user