Files
movie_sync/waveform_cache.py
T

30 lines
845 B
Python

"""
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()