Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/chirp/audio_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ def __init__(
else:
self._logger.debug("Audio feedback initialized using %s", backend)

def preload(self, asset_name: str, override_path: Optional[str] = None) -> None:
"""Preload a sound file into the cache to avoid latency on first play."""
if not self._enabled:
return

cache_key = override_path or asset_name
if cache_key in self._cache:
return

try:
with self._get_sound_path(asset_name, override_path) as path:
self._load_and_cache(path, cache_key)
except (FileNotFoundError, Exception) as exc:
self._logger.warning(
"Failed to preload sound %s: %s", override_path or asset_name, exc
)

def play_start(self, override_path: Optional[str] = None) -> None:
self._play_sound("ping-up.wav", override_path)

Expand Down
9 changes: 9 additions & 0 deletions src/chirp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def __init__(self, *, verbose: bool = False) -> None:
volume=self.config.audio_feedback_volume,
)

if self.config.audio_feedback:
# Preload sounds to minimize latency on first use
self.audio_feedback.preload("ping-up.wav", self.config.start_sound_path)
self.audio_feedback.preload("ping-down.wav", self.config.stop_sound_path)
if self.config.error_sound_path:
self.audio_feedback.preload(
"error-placeholder", self.config.error_sound_path
)

console = None
for handler in self.logger.handlers:
if isinstance(handler, RichHandler):
Expand Down