-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathaudio_client.py
More file actions
439 lines (379 loc) · 18.1 KB
/
Copy pathaudio_client.py
File metadata and controls
439 lines (379 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved.
# SPDX-License-Identifier: MIT
import asyncio
import queue
import threading
import time
from gaia.llm import create_client
from gaia.logger import get_logger
class AudioClient:
"""Handles all audio-related functionality including TTS, ASR, and voice chat."""
def __init__(
self,
whisper_model_size="base",
audio_device_index=None, # Use default input device
silence_threshold=0.5,
enable_tts=True,
logging_level="INFO",
use_claude=False,
use_chatgpt=False,
system_prompt=None,
):
self.log = get_logger(__name__)
self.log.setLevel(getattr(__import__("logging"), logging_level))
# Audio configuration
self.whisper_model_size = whisper_model_size
self.audio_device_index = audio_device_index
self.silence_threshold = silence_threshold
self.enable_tts = enable_tts
# Audio state
self.is_speaking = False
self.tts_thread = None
self.whisper_asr = None
self.transcription_queue = queue.Queue()
self.tts = None
# Initialize LLM client - factory auto-detects provider from flags
self.llm_client = create_client(
use_claude=use_claude,
use_openai=use_chatgpt,
system_prompt=system_prompt,
)
self.log.info("Audio client initialized.")
async def start_voice_chat(self, message_processor_callback):
"""Start a voice-based chat session."""
try:
self.log.debug("Initializing voice chat...")
print(
"Starting voice chat.\n"
"Say 'stop' to quit application "
"or 'restart' to clear the chat history.\n"
"Press Enter key to stop during audio playback."
)
# Initialize TTS before starting voice chat
self.initialize_tts()
from gaia.audio.whisper_asr import WhisperAsr
# Create WhisperAsr with custom thresholds
# Your audio shows energy levels of 0.02-0.03 when speaking
self.whisper_asr = WhisperAsr(
model_size=self.whisper_model_size,
device_index=self.audio_device_index,
transcription_queue=self.transcription_queue,
silence_threshold=0.01, # Set higher to ensure detection (your levels are 0.01-0.2+)
min_audio_length=16000 * 1.0, # 1 second minimum at 16kHz
)
# Log the thresholds being used (reduce verbosity)
self.log.debug(
f"Audio settings: SILENCE_THRESHOLD={self.whisper_asr.SILENCE_THRESHOLD}, "
f"MIN_LENGTH={self.whisper_asr.MIN_AUDIO_LENGTH/self.whisper_asr.RATE:.1f}s"
)
device_name = self.whisper_asr.get_device_name()
self.log.debug(f"Using audio device: {device_name}")
# Start recording
self.log.debug("Starting audio recording...")
self.whisper_asr.start_recording()
# Start the processing thread after recording is initialized
self.log.debug("Starting audio processing thread...")
process_thread = threading.Thread(
target=self._process_audio_wrapper, args=(message_processor_callback,)
)
process_thread.daemon = True
process_thread.start()
# Keep the main thread alive while processing
self.log.debug("Listening for voice input...")
try:
while True:
if not process_thread.is_alive():
self.log.debug("Process thread stopped unexpectedly")
break
if not self.whisper_asr or not self.whisper_asr.is_recording:
self.log.warning("Recording stopped unexpectedly")
break
await asyncio.sleep(0.1)
except KeyboardInterrupt:
self.log.info("Received keyboard interrupt")
print("\nStopping voice chat...")
except Exception as e:
self.log.error(f"Error in main processing loop: {str(e)}")
raise
finally:
if self.whisper_asr:
self.log.debug("Stopping recording...")
self.whisper_asr.stop_recording()
self.log.debug("Waiting for process thread to finish...")
process_thread.join(timeout=2.0)
except ImportError:
self.log.error(
'WhisperAsr not found. Please install voice support with: uv pip install ".[talk]"'
)
raise
except Exception as e:
self.log.error(f"Failed to initialize voice chat: {str(e)}")
raise
finally:
if self.whisper_asr:
self.whisper_asr.stop_recording()
self.log.info("Voice recording stopped")
async def process_voice_input(self, text, get_stats_callback=None):
"""Process transcribed voice input and get AI response"""
# Initialize TTS streaming
text_queue = None
tts_finished = threading.Event() # Add event to track TTS completion
interrupt_event = threading.Event() # Add event for keyboard interrupts
try:
# Check if we're currently generating and halt if needed
if self.llm_client.is_generating():
self.log.debug("Generation in progress, halting...")
if self.llm_client.halt_generation():
print("\nGeneration interrupted.")
await asyncio.sleep(0.5)
# Pause audio recording before sending query
if self.whisper_asr:
self.whisper_asr.pause_recording()
self.log.debug("Recording paused before generation")
self.log.debug(f"Sending message to LLM: {text[:50]}...")
print("\nGaia: ", end="", flush=True)
# Keyboard listener thread for both generation and playback
def keyboard_listener():
input() # Wait for any input
# Use LLMClient to halt generation
if self.llm_client.halt_generation():
print("\nGeneration interrupted.")
else:
print("\nInterrupt requested.")
interrupt_event.set()
if text_queue:
text_queue.put("__HALT__") # Signal TTS to stop immediately
# Start keyboard listener thread
keyboard_thread = threading.Thread(target=keyboard_listener)
keyboard_thread.daemon = True
keyboard_thread.start()
if self.enable_tts:
text_queue = queue.Queue(maxsize=100)
# Define status callback to update speaking state
def tts_status_callback(is_speaking):
self.is_speaking = is_speaking
if not is_speaking: # When TTS finishes speaking
tts_finished.set()
if self.whisper_asr:
self.whisper_asr.resume_recording()
else: # When TTS starts speaking
if self.whisper_asr:
self.whisper_asr.pause_recording()
self.log.debug(f"TTS speaking state: {is_speaking}")
self.tts_thread = threading.Thread(
target=self.tts.generate_speech_streaming,
args=(text_queue,),
kwargs={
"status_callback": tts_status_callback,
"interrupt_event": interrupt_event,
},
daemon=True,
)
self.tts_thread.start()
# Use LLMClient streaming instead of WebSocket
accumulated_response = ""
initial_buffer = "" # Buffer for the start of response
initial_buffer_sent = False
try:
# Start LLM generation with streaming
response_stream = self.llm_client.generate(text, stream=True)
# Process streaming response
for chunk in response_stream:
if interrupt_event.is_set():
self.log.debug("Keyboard interrupt detected, stopping...")
if text_queue:
text_queue.put("__END__")
break
if self.transcription_queue.qsize() > 0:
self.log.debug(
"New input detected during generation, stopping..."
)
if text_queue:
text_queue.put("__END__")
# Use LLMClient to halt generation
if self.llm_client.halt_generation():
self.log.debug("Generation interrupted for new input.")
return
if chunk:
print(chunk, end="", flush=True)
if text_queue:
if not initial_buffer_sent:
initial_buffer += chunk
# Send if we've reached 20 chars or if we get a clear end marker
if len(initial_buffer) >= 20 or chunk.endswith(
("\n", ". ", "! ", "? ")
):
text_queue.put(initial_buffer)
initial_buffer_sent = True
else:
text_queue.put(chunk)
accumulated_response += chunk
# Send any remaining buffered content
if text_queue:
if not initial_buffer_sent and initial_buffer:
# Small delay for very short responses
if len(initial_buffer) <= 20:
await asyncio.sleep(0.1)
text_queue.put(initial_buffer)
text_queue.put("__END__")
except Exception as e:
if text_queue:
text_queue.put("__END__")
raise e
finally:
if self.tts_thread and self.tts_thread.is_alive():
self.tts_thread.join(timeout=1.0) # Add timeout to thread join
keyboard_thread.join(timeout=1.0) # Add timeout to keyboard thread join
print("\n")
# Get performance stats from LLMClient
if get_stats_callback:
# First try the provided callback for backward compatibility
stats = get_stats_callback()
else:
# Use LLMClient stats
stats = self.llm_client.get_performance_stats()
if stats:
from pprint import pprint
formatted_stats = {
k: round(v, 1) if isinstance(v, float) else v
for k, v in stats.items()
}
pprint(formatted_stats)
except Exception as e:
if text_queue:
text_queue.put("__END__")
raise e
finally:
if self.tts_thread and self.tts_thread.is_alive():
# Wait for TTS to finish before resuming recording
tts_finished.wait(timeout=2.0) # Add reasonable timeout
self.tts_thread.join(timeout=1.0)
# Only resume recording after TTS is completely finished
if self.whisper_asr:
self.whisper_asr.resume_recording()
def initialize_tts(self):
"""Initialize TTS if enabled."""
if self.enable_tts:
try:
from gaia.audio.kokoro_tts import KokoroTTS
self.tts = KokoroTTS()
self.log.debug("TTS initialized successfully")
except Exception as e:
raise RuntimeError(
f'Failed to initialize TTS:\n{e}\nInstall talk dependencies with: uv pip install ".[talk]"\nYou can also use --no-tts option to disable TTS'
)
async def speak_text(self, text: str) -> None:
"""Speak text using initialized TTS, if available."""
if not self.enable_tts:
return
if not getattr(self, "tts", None):
self.log.debug("TTS is not initialized; skipping speak_text")
return
# Reuse the streaming path used in process_voice_input
text_queue = queue.Queue(maxsize=100)
interrupt_event = threading.Event()
tts_thread = threading.Thread(
target=self.tts.generate_speech_streaming,
args=(text_queue,),
kwargs={"interrupt_event": interrupt_event},
daemon=True,
)
tts_thread.start()
# Send the whole text and end
text_queue.put(text)
text_queue.put("__END__")
tts_thread.join(timeout=5.0)
def _process_audio_wrapper(self, message_processor_callback):
"""Wrapper method to process audio and handle transcriptions"""
try:
accumulated_text = []
current_display = ""
last_transcription_time = time.time()
spinner_chars = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
dots_animation = [" ", ". ", ".. ", "..."]
spinner_idx = 0
dots_idx = 0
animation_counter = 0
self.is_speaking = False # Initialize speaking state
while self.whisper_asr and self.whisper_asr.is_recording:
try:
text = self.transcription_queue.get(timeout=0.1)
current_time = time.time()
time_since_last = current_time - last_transcription_time
cleaned_text = text.lower().strip().rstrip(".!?")
# Handle special commands
if cleaned_text in ["stop"]:
print("\nStopping voice chat...")
self.whisper_asr.stop_recording()
break
# Update animations
spinner_idx = (spinner_idx + 1) % len(spinner_chars)
animation_counter += 1
if animation_counter % 4 == 0: # Update dots every fourth cycle
dots_idx = (dots_idx + 1) % len(dots_animation)
spinner = spinner_chars[spinner_idx]
dots = dots_animation[dots_idx]
# Normal text processing - only if it's not a system message
if text != current_display:
# Clear the current line and display updated text with spinner
print(f"\r\033[K{spinner} {text}", end="", flush=True)
current_display = text
# Only add new text if it's significantly different
if not any(text in existing for existing in accumulated_text):
accumulated_text = [text] # Replace instead of append
last_transcription_time = current_time
# Process accumulated text after silence threshold
if time_since_last > self.silence_threshold:
if accumulated_text:
complete_text = accumulated_text[
-1
] # Use only the last transcription
print() # Add a newline before agent response
asyncio.run(message_processor_callback(complete_text))
accumulated_text = []
current_display = ""
except queue.Empty:
# Update animations
spinner_idx = (spinner_idx + 1) % len(spinner_chars)
animation_counter += 1
if animation_counter % 4 == 0:
dots_idx = (dots_idx + 1) % len(dots_animation)
spinner = spinner_chars[spinner_idx]
dots = dots_animation[dots_idx]
if current_display:
print(
f"\r\033[K{spinner} {current_display}", end="", flush=True
)
else:
# Access the class-level speaking state
status = (
"Speaking"
if getattr(self, "is_speaking", False)
else "Listening"
)
print(f"\r\033[K{spinner} {status}{dots}", end="", flush=True)
if (
accumulated_text
and (time.time() - last_transcription_time)
> self.silence_threshold
):
complete_text = accumulated_text[-1]
print() # Add a newline before agent response
asyncio.run(message_processor_callback(complete_text))
accumulated_text = []
current_display = ""
except Exception as e:
self.log.error(f"Error in process_audio_wrapper: {str(e)}")
finally:
if self.whisper_asr:
self.whisper_asr.stop_recording()
if self.tts_thread and self.tts_thread.is_alive():
self.tts_thread.join(timeout=1.0) # Add timeout to thread join
async def halt_generation(self):
"""Send a request to halt the current generation."""
if self.llm_client.halt_generation():
self.log.debug("Successfully halted generation via LLMClient")
print("\nGeneration interrupted.")
else:
self.log.debug("Halt requested - generation will stop on next iteration")
print("\nInterrupt requested.")