1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2024-12-09 09:27:56 +02:00
STARK/majordom_va/VoiceAssistant.py

59 lines
2.0 KiB
Python
Raw Normal View History

import asyncio
2023-02-05 14:00:48 +02:00
from VICore import CommandsContext, CommandsContextDelegate
2023-02-23 02:14:34 +02:00
from IO.protocols import SpeechRecognizer, SpeechRecognizerDelegate, SpeechSynthesizer, SpeechSynthesizerResult
2021-08-21 01:09:37 +02:00
2023-02-05 14:00:48 +02:00
class VoiceAssistant(SpeechRecognizerDelegate, CommandsContextDelegate):
2021-08-21 01:09:37 +02:00
2023-02-05 14:00:48 +02:00
speech_recognizer: SpeechRecognizer
2023-02-23 02:14:34 +02:00
speech_synthesizer: SpeechSynthesizer
2023-02-05 14:00:48 +02:00
commands_context: CommandsContext
voids: int = 0
2023-02-05 14:00:48 +02:00
last_clap_time: float = 0
double_clap: bool = False
2021-08-21 01:09:37 +02:00
# Control
2023-02-23 02:14:34 +02:00
def __init__(self, speech_recognizer: SpeechRecognizer, speech_synthesizer: SpeechSynthesizer, commands_context: CommandsContext):
self.speech_recognizer = speech_recognizer
self.speech_synthesizer = speech_synthesizer
2023-02-05 14:00:48 +02:00
self.commands_context = commands_context
commands_context.delegate = self
2021-08-21 01:09:37 +02:00
def start(self):
2023-02-05 14:00:48 +02:00
self.speech_recognizer.delegate = self
print('Listen...')
asyncio.get_event_loop().run_until_complete(
2023-02-23 02:14:34 +02:00
self.speech_recognizer.start_listening()
)
def stop(self):
2023-02-23 02:14:34 +02:00
self.speech_recognizer.stop_listening()
# SpeechRecognizerDelegate
2023-02-12 19:33:23 +02:00
def speech_recognizer_did_receive_final_result(self, result: str):
self.voids = 0
2023-02-05 14:00:48 +02:00
# self.commands_context.lastInteractTime = VITime()
print(f'\rYou: {result}')
2023-02-05 14:00:48 +02:00
self.commands_context.process_string(result)
2023-02-12 19:33:23 +02:00
def speech_recognizer_did_receive_partial_result(self, result: str):
print(f'\rYou: \x1B[3m{result}\x1B[0m', end = '')
2023-02-12 19:33:23 +02:00
def speech_recognizer_did_receive_empty_result(self):
self.voids += 1
2021-08-21 01:09:37 +02:00
2023-02-05 14:00:48 +02:00
# CommandsContextDelegate
2021-08-21 01:09:37 +02:00
2023-02-05 14:00:48 +02:00
def commands_context_did_receive_response(self, response):
if response.text:
print(f'Archie: {response.text}')
if response.voice:
2023-02-05 14:00:48 +02:00
was_recognizing = self.speech_recognizer.is_recognizing
self.speech_recognizer.is_recognizing = False
2023-02-23 02:14:34 +02:00
self.voice.synthesize(response.voice).play()
2023-02-12 23:33:24 +02:00
self.speech_recognizer.is_recognizing = was_recognizing