diff --git a/ipin_vr/tts.py b/ipin_vr/tts.py index 89f41f6..c45edd2 100644 --- a/ipin_vr/tts.py +++ b/ipin_vr/tts.py @@ -1,8 +1,11 @@ import os +import shutil import subprocess import sys import tempfile +_LINUX_PLAYERS = ["pw-play", "paplay", "aplay"] + def speak(text: str, voice_path: str) -> None: with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tf: @@ -35,7 +38,11 @@ def speak(text: str, voice_path: str) -> None: def _play(wav_path: str) -> None: if sys.platform.startswith("linux"): - _run_player("aplay", wav_path) + player = next((p for p in _LINUX_PLAYERS if shutil.which(p)), None) + if player is None: + print("[TTS: brak odtwarzacza audio]") + return + _run_player(player, wav_path) elif sys.platform == "darwin": _run_player("afplay", wav_path) elif sys.platform == "win32": @@ -50,7 +57,10 @@ def _play(wav_path: str) -> None: def _run_player(player: str, wav_path: str) -> None: try: - subprocess.run([player, wav_path], capture_output=True, timeout=30) + proc = subprocess.run([player, wav_path], capture_output=True, timeout=30) + if proc.returncode != 0: + stderr = proc.stderr.decode(errors="replace").strip() + print(f"[TTS: {player} błąd {proc.returncode}: {stderr}]") except FileNotFoundError: print(f"[TTS off: {player} nie znaleziony]") except Exception as exc: