def quit(self): log.debug("Quitting...") # set quit to True self.stop = True self.clean() log.info("Bye!")
def say(text): if settings.TTS_SUBTITLE: log.info(text) # Windows if platform.system() == "Windows" and settings.TTS_AUTODETECT: engine.Speak(text) # other OS else: with named_temp_file() as f: # google server request output = engine(text=text, lang=settings.LANGUAGE_SHORT) # save received file output.save(f.name + ".mp3") # play play_mp3(f.name + ".mp3")
def listen_for_yn(): # get yes and no replies yes = replying.get_reply(["stt", "yn_y"], system=True, module=True) no = replying.get_reply(["stt", "yn_n"], system=True, module=True) log.info("Waiting for {0} or {1}".format(yes, no)) while True: audio = listen() input = recognize(audio) if input: if yes in input.lower(): log.debug("'{}' detected".format(yes)) return True elif no in input.lower(): log.debug("'{}' detected".format(no)) return False else: log.debug("Not detected in '{}'".format(input))
def listen_for_keyword(): log.debug("Keyword loop...") # global processes global keyword_detected global new_process keyword_detected = False new_process = True log.info("Waiting for '{}'...".format(settings.KEYWORD)) while True: # quit when keyword is detected if keyword_detected: break # if new process is requested if new_process: new_process = False # start async keyword recognizing thread (start new process) threading.Thread(target=recognize_for_keyword).start() # play activation sound tts.play_mp3(settings.ACTIVATION_SOUND_PATH) return True
def setup(): global engine # tts engine setup # detect OS if platform.system() == "Windows" and settings.TTS_AUTODETECT: try: # import module import win32com.client as win32com engine = win32com.Dispatch("SAPI.SpVoice") except ModuleNotFoundError: log.error("Couldn't find a module named 'win32com.client'") log.error( "Check installation or install via 'pip install pypiwin32'") else: try: # import module from gtts import gTTS engine = gTTS except ModuleNotFoundError: log.error("Couldn't find a module named 'gtts'") log.error("Check installation or install via 'pip install gtts'") log.info("(!) Using slow TTS engine on your OS")
def run(self): # greeting self.greet() # global loop while True: # check if quit if self.stop: break try: # listen for keyword # wake up on recognized keyword if stt.listen_for_keyword(): log.debug("Back in loop...") # listen for text input audio = stt.listen() # try resolving input audio_input = stt.recognize(audio) # check if text input received if not audio_input: log.info("Couldn't resolve audio...") continue else: log.info("Catched input '{}'...".format(audio_input)) # find match cmd = matching.get_match(audio_input) # execute match matching.execute_match(cmd) # user interrupted program except KeyboardInterrupt: log.info("Detected keyboard interruption...") self.quit() break except: log.error("Unexpected error") traceback.print_exc() break