feat: add waveform cache to avoid re-reading WAV files

This commit is contained in:
Honney OC committed 2026-09-13 23:32:36 +00:00
1 parent 37f19c5fd3
commit 4404dda926
3 files changed
+35 -4

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
"""
Shared waveform cache — avoids re-reading and re-decoding large WAV files
when the same file is loaded multiple times (e.g. sync_audio_test → verify).
"""
from __future__ import annotations
import os
import numpy as np
_cache: dict[tuple[str, int], np.ndarray] = {}
def cached_load_wav(path: str, target_sr: int = 48000) -> tuple[np.ndarray, int]:
"""Load a WAV file with caching. Returns (audio_array, sample_rate)."""
real_path = os.path.realpath(path)
key = (real_path, target_sr)
if key in _cache:
print(f" (cached: {os.path.basename(path)})")
return _cache[key].copy(), target_sr
# Import here to avoid circular imports
from sync_audio import load_wav
data, sr = load_wav(path, target_sr=target_sr)
_cache[key] = data
return data, sr
def clear_cache():
_cache.clear()