feat: improve audio stream selection and caching

This commit is contained in:
Honney OC committed 2026-09-14 02:02:26 +00:00
1 parent 6050e81be1
commit 8e3db0d7f4
4 files changed
+86 -29

No files matched your search

+5 -4
View File
@@ -11,7 +11,7 @@ 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:
def _cache_key_path(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}"
@@ -19,9 +19,10 @@ def _cache_key(path: str, target_sr: int, mono: bool) -> str:
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)
_loader=None, _cache_key: str | None = None) -> tuple[np.ndarray, int]:
"""Load WAV with disk cache. Falls back to _loader(data, sr) if cache miss.
_cache_key: override cache key (e.g. source_mkv + stream_idx) for temp files."""
key = _cache_key if _cache_key else _cache_key_path(path, target_sr, mono)
npy_path = os.path.join(CACHE_DIR, f"{key}.npy")
sr_path = os.path.join(CACHE_DIR, f"{key}.sr")