140 lines
4.4 KiB
Python
140 lines
4.4 KiB
Python
import os
|
|
import sys
|
|
import subprocess
|
|
import threading
|
|
from elevenlabs import VoiceSettings
|
|
|
|
from .config import AUDIO_SINK, AGENTS, log
|
|
|
|
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY", "")
|
|
ELEVENLABS_MODEL = os.getenv("ELEVENLABS_MODEL", "eleven_flash_v2_5")
|
|
|
|
_elevenlabs_client = None
|
|
_current_process: subprocess.Popen | None = None
|
|
_process_lock = threading.Lock()
|
|
|
|
|
|
def _get_elevenlabs():
|
|
global _elevenlabs_client
|
|
if _elevenlabs_client is None:
|
|
from elevenlabs.client import ElevenLabs
|
|
_elevenlabs_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
|
return _elevenlabs_client
|
|
|
|
|
|
def stop_speaking():
|
|
"""Прерывает текущее воспроизведение (barge-in)"""
|
|
global _current_process
|
|
with _process_lock:
|
|
if _current_process and _current_process.poll() is None:
|
|
_current_process.terminate()
|
|
try:
|
|
_current_process.wait(timeout=1)
|
|
except subprocess.TimeoutExpired:
|
|
_current_process.kill()
|
|
_current_process = None
|
|
|
|
|
|
def is_speaking() -> bool:
|
|
with _process_lock:
|
|
return _current_process is not None and _current_process.poll() is None
|
|
|
|
|
|
def _mpv_cmd() -> list[str]:
|
|
"""Команда mpv для воспроизведения из stdin"""
|
|
mpv_bin = os.getenv("MPV_PATH", "mpv")
|
|
cmd = [mpv_bin, "--no-video", "--really-quiet", "--no-terminal"]
|
|
if AUDIO_SINK:
|
|
cmd.append(f"--audio-device=pulse/{AUDIO_SINK}")
|
|
cmd.append("-")
|
|
return cmd
|
|
|
|
|
|
def speak(text: str, agent_id: str = "cosmo"):
|
|
try:
|
|
_speak_elevenlabs(text, agent_id)
|
|
except Exception as e:
|
|
log.exception("TTS ошибка")
|
|
print(f"⚠️ Ошибка воспроизведения: {e}")
|
|
play_error_sound()
|
|
|
|
|
|
def _speak_elevenlabs(text: str, agent_id: str):
|
|
global _current_process
|
|
client = _get_elevenlabs()
|
|
voice_id = AGENTS.get(agent_id, AGENTS["cosmo"]).get("tts_voice", "")
|
|
|
|
if not voice_id:
|
|
log.error(f"tts_voice не задан для {agent_id}")
|
|
print(f"⚠️ tts_voice не задан для {agent_id}")
|
|
return
|
|
|
|
voice_settings = VoiceSettings(
|
|
stability=0.65, # ниже = живее интонация (для multilingual_v2)
|
|
similarity_boost=0.6,
|
|
style=0.45, # выше = эмоциональнее
|
|
use_speaker_boost=True,
|
|
speed=1.05
|
|
)
|
|
|
|
audio_stream = client.text_to_speech.convert(
|
|
text=text,
|
|
voice_id=voice_id,
|
|
model_id=ELEVENLABS_MODEL,
|
|
output_format="mp3_44100_128",
|
|
voice_settings=voice_settings
|
|
)
|
|
|
|
with _process_lock:
|
|
_current_process = subprocess.Popen(
|
|
_mpv_cmd(), stdin=subprocess.PIPE,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
)
|
|
proc = _current_process
|
|
|
|
try:
|
|
for chunk in audio_stream:
|
|
if proc.poll() is not None:
|
|
break
|
|
try:
|
|
proc.stdin.write(chunk)
|
|
except BrokenPipeError:
|
|
break
|
|
proc.stdin.close()
|
|
proc.wait()
|
|
except Exception:
|
|
proc.kill()
|
|
finally:
|
|
with _process_lock:
|
|
if _current_process is proc:
|
|
_current_process = None
|
|
|
|
|
|
def _play_sound_file(filename: str, wait: bool = False):
|
|
"""Воспроизводит файл из папки sounds/ через mpv.
|
|
wait=True — блокирует до конца воспроизведения."""
|
|
sounds_dir = os.path.join(os.path.dirname(__file__), "..", "sounds")
|
|
path = os.path.normpath(os.path.join(sounds_dir, filename))
|
|
mpv_bin = os.getenv("MPV_PATH", "mpv")
|
|
cmd = [mpv_bin, "--no-video", "--really-quiet", "--no-terminal", path]
|
|
if wait:
|
|
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
else:
|
|
subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
|
|
def play_activation_sound():
|
|
"""Звук активации — неблокирующий"""
|
|
try:
|
|
_play_sound_file("Success_Cosmo.mp3", wait=False)
|
|
except Exception as e:
|
|
log.warning(f"Ошибка звука активации: {e}")
|
|
|
|
|
|
def play_error_sound():
|
|
"""Звук ошибки — 'не получилось'"""
|
|
try:
|
|
_play_sound_file("Error_Cosmo.mp3")
|
|
except Exception as e:
|
|
log.warning(f"Ошибка звука ошибки: {e}")
|