bumpy-florist-37019
09/15/2025, 10:35 PMtall-airline-16756
09/15/2025, 10:35 PMtall-airline-16756
09/15/2025, 10:36 PMbefore_tts_cb hook is handy for preprocessing, or you might prefer creating a custom TTS wrapper to manage the caching logic smoothly.
• Cache Invalidation: It’s important to have policies to invalidate outdated cache entries, whether based on configuration changes or a time-to-live (TTL) setting.
If you'd like, I can provide a simple code example for a minimal caching wrapper using an in-memory dictionary. Just let me know! Sources: livekit-plugins/livekit-plugins-hume/livekit/plugins/hume/tts.py | chain-of-thought-tts/agent.py | Working with plugins | LiveKit Docsbumpy-florist-37019
09/15/2025, 10:37 PMtall-airline-16756
09/15/2025, 10:37 PMclass CachingTTS(TTS):
def __init__(self, tts: TTS):
super().__init__(
capabilities=tts.capabilities,
sample_rate=tts.sample_rate,
num_channels=tts.num_channels
)
self._tts = tts
self._cache = {}
def _generate_key(self, text: str, **kwargs) -> str:
# Create a unique key based on text and TTS settings
import hashlib
key_data = f"{text}|{kwargs}"
return hashlib.sha256(key_data.encode()).hexdigest()
def synthesize(self, text: str, **kwargs):
# Generate cache key
key = self._generate_key(text, **kwargs)
# Check if audio is already cached
if key in self._cache:
return self._cache[key]
# If not cached, synthesize and store in cache
audio = self._tts.synthesize(text, **kwargs)
self._cache[key] = audio
return audio
How it works:
• The _generate_key method creates a unique hash for each TTS request based on the input text and settings.
• The synthesize method checks the cache for existing audio before invoking the TTS engine.
• Newly synthesized audio is stored in the cache for future use.
Feel free to build on this by using a more robust caching backend like Redis or disk storage. If you have any questions or want to explore this further, just let me know—I’m here to help!