Skip to content

Commit

Permalink
refactor: Stop tts service in WakeWordDetector when cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSco committed Aug 23, 2024
1 parent a5c44ad commit 00404ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ def run(self):
def cleanup(self):
print("Cleaning up detector...")
self.is_running = False
if self.speech is not None:
self.speech.stop()
current_thread = threading.current_thread()
if self.consumer_thread.is_alive() and self.consumer_thread != current_thread:
self.consumer_thread.join()
Expand Down
14 changes: 11 additions & 3 deletions tts_service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
import time
from elevenlabs import VoiceSettings
from elevenlabs import stream
from elevenlabs import stream, play
from elevenlabs.client import ElevenLabs
import pyttsx3
from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play
from pydub.playback import play as pyDubPlay
import io

class TextToSpeechService:
Expand All @@ -21,10 +21,16 @@ def __init__(self, config):
self.language = config["language"]
self.accent = config["assistant_dict"]["accent"]
self.sound_effect = None
self.is_running = True

def remove_non_ascii(self, text):
return re.sub(r'[^\x00-\x7F]+', '', text)

def stop(self):
self.is_running = False
if self.sound_effect is not None:
self.sound_effect.stop_sound()

def speak(self, text):
textToSpeak = text
try:
Expand Down Expand Up @@ -64,6 +70,8 @@ def speech_stream(self, text):
)
)
for chunk in response:
if not self.is_running:
break
yield chunk

except Exception as e:
Expand Down Expand Up @@ -92,7 +100,7 @@ def speak_with_gtts(self, text):
print(f"{self.assistant_name}: {text}")

# Play the audio
play(audio)
pyDubPlay(audio)
except Exception as e:
print(f"Failed to use gTTS for speech: {e}")

Expand Down

0 comments on commit 00404ae

Please sign in to comment.