fix: select audio input device via env

This commit is contained in:
2026-03-12 13:03:50 +03:00
parent 6769486e83
commit e9f26f8050
4 changed files with 128 additions and 2 deletions

View File

@@ -86,6 +86,7 @@ class SpeechRecognizer:
self.stream = None
self.transcript = ""
self.last_successful_operation = datetime.now()
self._input_device_index = None
def initialize(self):
"""Инициализация клиента Deepgram и PyAudio."""
@@ -102,7 +103,9 @@ class SpeechRecognizer:
print(f"❌ Ошибка при создании клиента Deepgram: {e}")
raise
self.pa = get_audio_manager().get_pyaudio()
audio_manager = get_audio_manager()
self.pa = audio_manager.get_pyaudio()
self._input_device_index = audio_manager.get_input_device_index()
print("✅ Deepgram клиент готов")
# Обновляем время последней успешной операции
self.last_successful_operation = datetime.now()
@@ -128,12 +131,17 @@ class SpeechRecognizer:
def _get_stream(self):
"""Открывает аудиопоток PyAudio, если он еще не открыт."""
if self.stream is None:
kwargs = {}
if self._input_device_index is not None:
kwargs["input_device_index"] = self._input_device_index
self.stream = self.pa.open(
rate=SAMPLE_RATE,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=4096,
**kwargs,
)
return self.stream

View File

@@ -37,7 +37,9 @@ class WakeWordDetector:
)
# Используем общий экземпляр PyAudio
self.pa = get_audio_manager().get_pyaudio()
audio_manager = get_audio_manager()
self.pa = audio_manager.get_pyaudio()
self._input_device_index = audio_manager.get_input_device_index()
self._open_stream()
print(f"🎤 Ожидание wake word 'Alexandr' (sens={PORCUPINE_SENSITIVITY:.2f})...")
@@ -54,12 +56,17 @@ class WakeWordDetector:
pass
# Открываем поток с параметрами, которые требует Porcupine
kwargs = {}
if getattr(self, "_input_device_index", None) is not None:
kwargs["input_device_index"] = self._input_device_index
self.audio_stream = self.pa.open(
rate=self.porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=self.porcupine.frame_length,
**kwargs,
)
self._stream_closed = False