2021-12-05 03:07:17 +02:00
|
|
|
import asyncio
|
2021-12-04 01:27:10 +02:00
|
|
|
|
2023-02-05 14:00:48 +02:00
|
|
|
from VICore import CommandsContext, CommandsContextDelegate
|
2022-05-18 14:06:33 +02:00
|
|
|
from IO.SpeechRecognition import SpeechRecognizer, SpeechRecognizerDelegate
|
|
|
|
from IO import Text2Speech
|
2021-08-21 01:09:37 +02:00
|
|
|
|
2022-05-18 14:06:33 +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
|
|
|
|
commands_context: CommandsContext
|
2021-12-05 06:00:03 +02:00
|
|
|
voice = Text2Speech.Engine()
|
2021-12-04 01:27:10 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-12-05 06:00:03 +02:00
|
|
|
# Control
|
|
|
|
|
2023-02-05 14:00:48 +02:00
|
|
|
def __init__(self, commands_context: CommandsContext):
|
|
|
|
self.speech_recognizer = SpeechRecognizer(delegate = self)
|
|
|
|
self.commands_context = commands_context
|
|
|
|
commands_context.delegate = self
|
2021-11-28 20:01:53 +02:00
|
|
|
|
2021-08-21 01:09:37 +02:00
|
|
|
def start(self):
|
2023-02-05 14:00:48 +02:00
|
|
|
self.speech_recognizer.delegate = self
|
2021-12-05 06:00:03 +02:00
|
|
|
print('Listen...')
|
2021-12-05 03:07:17 +02:00
|
|
|
asyncio.get_event_loop().run_until_complete(
|
2023-02-05 14:00:48 +02:00
|
|
|
self.speech_recognizer.startListening()
|
2021-12-05 03:07:17 +02:00
|
|
|
)
|
2021-12-04 01:27:10 +02:00
|
|
|
|
|
|
|
def stop(self):
|
2023-02-05 14:00:48 +02:00
|
|
|
self.speech_recognizer.stopListening()
|
2021-12-04 01:27:10 +02:00
|
|
|
|
2021-12-05 06:00:03 +02:00
|
|
|
# SpeechRecognizerDelegate
|
2021-12-04 01:27:10 +02:00
|
|
|
|
2023-02-12 19:33:23 +02:00
|
|
|
def speech_recognizer_did_receive_final_result(self, result: str):
|
2021-12-05 03:07:17 +02:00
|
|
|
self.voids = 0
|
2023-02-05 14:00:48 +02:00
|
|
|
# self.commands_context.lastInteractTime = VITime()
|
2021-12-04 01:27:10 +02:00
|
|
|
print(f'\rYou: {result}')
|
|
|
|
|
2023-02-05 14:00:48 +02:00
|
|
|
self.commands_context.process_string(result)
|
2021-12-04 01:27:10 +02:00
|
|
|
|
2023-02-12 19:33:23 +02:00
|
|
|
def speech_recognizer_did_receive_partial_result(self, result: str):
|
2021-12-05 06:00:03 +02:00
|
|
|
print(f'\rYou: \x1B[3m{result}\x1B[0m', end = '')
|
2021-12-05 03:07:17 +02:00
|
|
|
|
2023-02-12 19:33:23 +02:00
|
|
|
def speech_recognizer_did_receive_empty_result(self):
|
2021-12-05 06:00:03 +02:00
|
|
|
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):
|
2021-12-05 03:07:17 +02:00
|
|
|
if response.text:
|
2021-12-05 06:00:03 +02:00
|
|
|
print(f'Archie: {response.text}')
|
2021-12-05 03:07:17 +02:00
|
|
|
if response.voice:
|
2023-02-05 14:00:48 +02:00
|
|
|
was_recognizing = self.speech_recognizer.is_recognizing
|
|
|
|
self.speech_recognizer.is_recognizing = False
|
2021-12-05 03:07:17 +02:00
|
|
|
self.voice.generate(response.voice).speak()
|
2023-02-05 14:00:48 +02:00
|
|
|
self.speech_recognizer.is_recognizing = was_recognizing
|
2021-08-21 01:09:37 +02:00
|
|
|
|
|
|
|
# check double clap from arduino microphone module
|
|
|
|
def checkClap(self, channel):
|
|
|
|
now = time.time()
|
2023-02-05 14:00:48 +02:00
|
|
|
delta = now - self.last_clap_time
|
2021-08-21 01:09:37 +02:00
|
|
|
if 0.1 < delta < 0.6:
|
2023-02-05 14:00:48 +02:00
|
|
|
self.speech_recognizer.is_recognizing = True
|
2021-08-21 01:09:37 +02:00
|
|
|
else:
|
2023-02-05 14:00:48 +02:00
|
|
|
self.last_clap_time = now
|
2021-08-21 01:09:37 +02:00
|
|
|
|
2022-05-18 14:06:33 +02:00
|
|
|
# if config.double_clap_activation:
|
|
|
|
# import RPi.GPIO as GPIO
|
|
|
|
#
|
|
|
|
# GPIO.setmode(GPIO.BCM)
|
|
|
|
# GPIO.setup(12, GPIO.IN)
|
|
|
|
# GPIO.add_event_detect(12, GPIO.RISING, callback = VoiceAssistant().checkClap)
|