Migrate to Deepgram STT, Silero v5 TTS, and fix wake word mic handling
This commit is contained in:
36
wakeword.py
36
wakeword.py
@@ -40,6 +40,24 @@ class WakeWordDetector:
|
||||
"""
|
||||
if not self.porcupine:
|
||||
self.initialize()
|
||||
|
||||
# Ensure stream is open and active
|
||||
if self.audio_stream is None or not self.audio_stream.is_active():
|
||||
# If closed or None, we might need to recreate it.
|
||||
# PyAudio streams once closed cannot be reopened usually?
|
||||
# We should probably recreate it.
|
||||
if self.audio_stream:
|
||||
try:
|
||||
self.audio_stream.close()
|
||||
except: pass
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
while True:
|
||||
pcm = self.audio_stream.read(self.porcupine.frame_length, exception_on_overflow=False)
|
||||
@@ -48,6 +66,9 @@ class WakeWordDetector:
|
||||
keyword_index = self.porcupine.process(pcm)
|
||||
if keyword_index >= 0:
|
||||
print("✅ Wake word обнаружен!")
|
||||
# Stop and CLOSE stream to release mic for STT
|
||||
self.audio_stream.stop_stream()
|
||||
self.audio_stream.close()
|
||||
return True
|
||||
|
||||
def check_wakeword_once(self) -> bool:
|
||||
@@ -59,6 +80,21 @@ class WakeWordDetector:
|
||||
self.initialize()
|
||||
|
||||
try:
|
||||
# Ensure stream is open/active
|
||||
if self.audio_stream is None or not self.audio_stream.is_active():
|
||||
# Re-open if needed (similar to wait_for_wakeword logic)
|
||||
if self.audio_stream:
|
||||
try:
|
||||
self.audio_stream.close()
|
||||
except: pass
|
||||
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
|
||||
)
|
||||
|
||||
pcm = self.audio_stream.read(self.porcupine.frame_length, exception_on_overflow=False)
|
||||
pcm = struct.unpack_from("h" * self.porcupine.frame_length, pcm)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user