예제 #1
0
def listen_keyword():
    """ Passively listens for the WAKE_UP_WORD string """
    with ignore_stderr():
        global decoder, p
        stream = p.open(format=pyaudio.paInt16,
                        channels=1,
                        rate=16000,
                        input=True,
                        frames_per_buffer=1024)
        stream.start_stream()

        log.info("Waiting to be woken up... ")
        # Process audio chunk by chunk. On keyword detected perform action and restart search
        decoder.start_utt()
        waiting = False
        wait_count = 0
        while True:
            buf = stream.read(1024, exception_on_overflow=False)
            decoder.process_raw(buf, False, False)
            if decoder.hyp():
                if decoder.hyp().hypstr[:13] == "orion cancel" or decoder.hyp(
                ).hypstr[:11] == "orion stop":
                    decoder.end_utt()
                    return "orion stop"
                else:
                    if waiting:
                        if wait_count >= 8:
                            decoder.end_utt()
                            return "orion"
                        else:
                            wait_count += 1
                    else:
                        waiting = True
예제 #2
0
def enable_mod(name):
    """ Attempts to enable the specified mod """
    global mod_lib
    for mod in mod_lib:
        if name in mod.name:
            log.info('Enabling: ' + name + '\n')
            mod.enabled = True
예제 #3
0
 def action(self, text):
     mod_name = self.module.lower().strip().replace(' ', '')
     if 'disable' in self.enable.lower() or 'remove' in self.enable.lower():
         log.info("Attempting to disable '" + mod_name + "'")
         brain.inst.disable_mod(mod_name)
     else:
         log.info("Attempting to enable '" + mod_name + "'")
         brain.inst.enable_mod(mod_name)
예제 #4
0
    def mod_select(self, mods):
        """ Prompt user to specify which module to use to respond """
        print('\n~ Which module (greedy) would you like me to use to respond?')
        print('~ Choices: ' + str([mod.name for mod in mods])[1:-1] + '\n')
        mod_select = input('> ')

        for mod in mods:
            if re.search('^.*\\b' + mod.name + '\\b.*$', mod_select,
                         re.IGNORECASE):
                return mod
        log.info('No module name found.')
예제 #5
0
    def run(self):
        """ Listen for input, match the modules and respond """
        while True:
            if self.quit_flag:
                break
            try:
                if settings.USE_STT:
                    stt.listen_keyword()
                    text = stt.active_listen()
                else:
                    text = input('> ')
                if not text:
                    log.info('No text input received.')
                    continue
                else:
                    log.info("'" + text + "'")

                self.match_mods(text)
                self.execute_mods(text)
            except OSError as e:
                if 'Invalid input device' in str(e):
                    log.error(settings.NO_MIC + '\n')
                    settings.USE_STT = False
                    continue
                else:
                    raise Exception
            except (EOFError, KeyboardInterrupt):
                log.info('Shutting down...')
                break
            except:
                log.error("(runtime error)")
                self.error()

        log.info('Arrivederci.')
예제 #6
0
 def open(self, url=None, new_tab=False):
     if not self.driver:
         try:
             # print(settings.CHROME_DRIVER)
             # print(os.path.isfile(settings.CHROME_DRIVER))
             if not os.path.isfile(settings.CHROME_DRIVER):
                 raise Exception
             self.driver = webdriver.Chrome(settings.CHROME_DRIVER)
         except:
             print(traceback.format_exc())
             self.driver = webdriver.Firefox()
     else:
         if new_tab:
             log.info('Opening new tab...')
             self.driver.find_element_by_tag_name('body').send_keys(NW_TAB)
     if url:
         if not url[0:4] == 'http':
             url = 'https://' + url.replace(' ', '')
         self.driver.get(url)
예제 #7
0
def speak(phrase,
          cache=False,
          filename='default',
          show_text=True,
          log_text=True):
    """Speaks a given text phrase

    :param phrase: text string to speak
    :param cache: if True, store .mp3 in 'media/responses'
    :param filename: filename if cached
    :param show_text: if True, store .mp3 in 'media/responses'
    :param cache: if True, store .mp3 in 'media/responses'
    """
    if show_text:
        log.info(phrase)
    if not settings.USE_TTS:
        log.info('SPOKEN: ' + phrase)
        return

    try:
        phrase = phrase[:settings.MAX_CHAR]
        tts = gTTS(text=phrase, lang=settings.LANG_CODE)

        if not cache:
            with tempfile.NamedTemporaryFile(mode='wb',
                                             suffix='.mp3',
                                             delete=False) as f:
                (temp_path, temp_name) = os.path.split(f.name)
                tts.write_to_fp(f)

            play_mp3(temp_name, temp_path)
            os.remove(os.path.join(temp_path, temp_name))
        else:
            filename = os.path.join(settings.RESPONSES_DIR, filename + '.mp3')
            tts.save(filename)
            log.info('Saved to: ' + filename)

    except HTTPError as e:
        log.error('Google TTS might not be updated: ' + str(e))
    except Exception as e:
        log.error('Unknown Google TTS issue: ' + str(e))
예제 #8
0
 def action(self, text):
     log.info('\n~ Searching Google...\n')
     api_lib['voice_browse_api'].search(text)
예제 #9
0
 def search(self, q):
     log.info('Answering with Google...')
     self.open(GOOGLE_URL + quote_plus(q), new_tab=False)
예제 #10
0
def list_mods():
    """ Print modules in order """
    global mod_lib
    log.info('Module Order: ' + str([mod.name
                                     for mod in mod_lib])[1:-1] + '\n')
예제 #11
0
def active_listen():
    """
    Actively listens for speech to translate into text
    :return: speech input as a text string
    """
    global r
    # use the default microphone as the audio source
    with ignore_stderr():
        with speech_recognition.Microphone() as src:
            # listen for 1 second to adjust energy threshold for ambient noise
            # r.adjust_for_ambient_noise(src)
            log.info("Active listening... ")
            tts.play_mp3('double-beep.mp3')

            # listen for the first phrase and extract it into audio data
            audio = r.listen(src)

    msg = ''
    try:
        if settings.ACTIVE_ENGINE == "google":
            msg = r.recognize_google(audio, language=settings.LANG_4CODE
                                     )  # recognize speech using Google STT
        elif settings.ACTIVE_ENGINE == "sphinx":
            msg = r.recognize_sphinx(audio)
    except speech_recognition.UnknownValueError:
        log.info("Google Speech Recognition could not understand audio")
    except speech_recognition.RequestError as e:
        log.info("Could not request results from Google STT; {0}".format(e))
        log.info(
            "Perhaps you need to update the 'SpeechRecognition' python package"
        )
    except:
        log.info("Unknown exception occurred!")
        log.info(traceback.format_exc())
    finally:
        return msg