jlind456 commited on
Commit
8d85c43
·
verified ·
1 Parent(s): 18037f4

Update AI Twin TTS with cloned voice support

Browse files
Files changed (1) hide show
  1. chat_twin.py +36 -2
chat_twin.py CHANGED
@@ -20,6 +20,7 @@ import subprocess
20
  import threading
21
  import queue
22
  import time
 
23
 
24
  # --- ANSI Terminal Colors ---
25
  C_BLUE = "\033[94m"
@@ -89,8 +90,11 @@ def clean_markdown(text):
89
  text = re.sub(r'\s+', ' ', text)
90
  return text.strip()
91
 
 
 
 
92
  def speak_text(text):
93
- """Executes the TTS system commands to say the text."""
94
  if not tts_config["enabled"]:
95
  return
96
 
@@ -98,6 +102,34 @@ def speak_text(text):
98
  if not clean_text:
99
  return
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Prepare command for spd-say
102
  if not tts_config["fallback_espeak"]:
103
  cmd = ["spd-say", "-w"] # -w waits until speaking is finished
@@ -293,7 +325,9 @@ def main():
293
 
294
  # If input is empty, enter Voice Input Mode
295
  if not user_input:
296
- print(f"{C_YELLOW}[Recording... Press ENTER to stop recording]{C_RESET}", end="", flush=True)
 
 
297
 
298
  # Start recording
299
  record_proc = record_audio()
 
20
  import threading
21
  import queue
22
  import time
23
+ import tempfile
24
 
25
  # --- ANSI Terminal Colors ---
26
  C_BLUE = "\033[94m"
 
90
  text = re.sub(r'\s+', ' ', text)
91
  return text.strip()
92
 
93
+ CLONED_SPEAKER_WAV = "/home/jason/local-tts/cloned_output.wav"
94
+ TTS_CMD = "/home/jason/anaconda3/envs/tts-backend/bin/tts"
95
+
96
  def speak_text(text):
97
+ """Executes the TTS system commands to say the text using cloned voice."""
98
  if not tts_config["enabled"]:
99
  return
100
 
 
102
  if not clean_text:
103
  return
104
 
105
+ # Attempt voice-cloned TTS using XTTS v2 and cloned_output.wav
106
+ if os.path.exists(TTS_CMD) and os.path.exists(CLONED_SPEAKER_WAV) and not tts_config.get("fallback_espeak", False):
107
+ try:
108
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
109
+ tmp_wav = tmp_file.name
110
+
111
+ cmd = [
112
+ TTS_CMD,
113
+ "--model_name", "tts_models/multilingual/multi-dataset/xtts_v2",
114
+ "--text", clean_text,
115
+ "--speaker_wav", CLONED_SPEAKER_WAV,
116
+ "--language_idx", "en",
117
+ "--out_path", tmp_wav,
118
+ "--use_cuda", "true"
119
+ ]
120
+ res = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=45)
121
+ if res.returncode == 0 and os.path.exists(tmp_wav):
122
+ play_res = subprocess.run(["paplay", tmp_wav], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
123
+ if play_res.returncode != 0:
124
+ subprocess.run(["aplay", tmp_wav], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
125
+ try:
126
+ os.remove(tmp_wav)
127
+ except Exception:
128
+ pass
129
+ return
130
+ except Exception:
131
+ pass
132
+
133
  # Prepare command for spd-say
134
  if not tts_config["fallback_espeak"]:
135
  cmd = ["spd-say", "-w"] # -w waits until speaking is finished
 
325
 
326
  # If input is empty, enter Voice Input Mode
327
  if not user_input:
328
+ if os.path.exists("/home/jason/coral/mic_active.wav"):
329
+ subprocess.run(["aplay", "-q", "/home/jason/coral/mic_active.wav"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
330
+ print(f"{C_YELLOW}[🎙️ Mic Active - Recording... Press ENTER to stop recording]{C_RESET}", end="", flush=True)
331
 
332
  # Start recording
333
  record_proc = record_audio()