Skip to main content
Unmute provides several utility classes for timing, queuing, service discovery, and caching.

Quest and QuestManager

Async task lifecycle management for services with init/run/close phases.

Class: Quest[T]

Represents an async unit of work with initialization, execution, and cleanup phases.

Methods

method
Wait for initialization to complete and return the initialized resource.
method
Get the initialized resource immediately, or None if not ready.
method
Cancel the quest and run cleanup.

Class: QuestManager

Manages multiple quests and ensures proper cleanup.

Timing Utilities

Class: Stopwatch

Simple timer for measuring elapsed time.

Properties and Methods

property
Whether the stopwatch has been started.
method
Get elapsed time in seconds. Returns 0.0 if not started.
method
Stop the timer and return elapsed time, or None if already stopped.

Class: PhasesStopwatch

Track timing for multiple sequential phases.

Queue Utilities

Class: RealtimeQueue[T]

Priority queue that releases items based on timestamps for real-time processing.
Used by TTS for buffering audio chunks to ensure smooth playback timing.

Service Discovery

Functions for finding and connecting to backend services with failover support.

get_instances

function
Get all available instances of a service.Parameters:
  • service_name (str) - Service name (e.g., “stt”, “tts”, “llm”)

find_instance

function
Find a working instance of a service by trying connections.Parameters:
  • service_name (str) - Service name
  • client_factory (Callable) - Function to create client from URL
  • timeout_sec (float) - Connection timeout per attempt (default: 0.5)
  • max_trials (int) - Maximum connection attempts (default: 3)
Raises:
  • MissingServiceAtCapacity - All instances are at capacity
  • MissingServiceTimeout - Connection attempts timed out

async_ttl_cached

function
Decorator to cache async function results with TTL.Parameters:
  • func (Callable) - Async function to cache
  • ttl_sec (float) - Cache TTL in seconds (default: 0.1)

Caching

get_cache

Get a cache instance (local or Redis-backed).
Cache Implementation:
  • If KYUTAI_REDIS_URL is set, uses RedisCache for distributed caching
  • Otherwise uses LocalCache for single-instance in-memory caching

Recording

Class: Recorder

Records WebSocket session events to disk for replay and analysis.
EventSender: "client" or "server" Recordings are saved as JSON files in the specified directory, one file per session.

Audio Processing

ExponentialMovingAverage

Smooth signal processing with configurable attack and release times.
Used by the STT component for pause detection and VAD.

WebSocket Utilities

URL Conversion

Convert between HTTP and WebSocket URLs.

Usage Example