Example #1
0
    def run(self):
        """
        Recorder main loop
        """
        CONTENT_TYPE = 'audio/mpeg3'
        result = ""
        retry = 1

        # Thread loop
        while not self._stopevent.isSet():
            # Wait record order
            if self.running_state == False:
                sleep(.1)
                continue

            # Activate capture, wait for 2s of silence before cancelling
            wit_e = None
            self.record_time_start = 0
            self.record_time_end = time.time() + 2
            self.capture_buffers.clear()
            result = ""
            print '\n [Recording]' + ' ' * 20 + '[Recording]'

            # Send captured voice to wit
            try:
                result = self.wit.post_speech(data = self._read_audio_buffer(), content_type=CONTENT_TYPE)
            except Exception as e:
                wit_e = e

            # If record was stopped during recording
            if self.running_state == True:
                # If Wit did not succeeded
                if len(result) == 0 or result.has_key('outcome') == False or result['outcome'].has_key('confidence') == False or result['outcome']['confidence'] < self.wit_confidence:
                    if wit_e is not None:
                        log.err("Wit exception : " + str(e))

                    # If retry is available and vader detected an utterance
                    if self.record_time_start != 0 and retry > 0:
                        Speaker.speak('please_repeat')

                        # Decrement retries
                        retry = retry - 1
                        continue

                    # No more retry
                    Speaker.speak('not_understood')

                # Send recognized intent to the server
                else:
                    self.lisa_client.sendMessage(message=result['msg_body'], type='chat', dict=result['outcome'])

            # Reset running state
            self.running_state = False
            retry = 1

            # Reconnect pocketsphinx to pipeline
            print ""
            print "> Ready Recognize Voice"
            self.asr_tee.link(self.asr)
            self.asr_tee.unlink(self.asr_sink)
Example #2
0
    def _asr_result(self, asr, text, uttid):
        """
        Result from pocketsphinx : checking keyword recognition
        """
        # Check keyword detection
        if text.lower() == self.botname and self.recorder.get_running_state() == False:
            # Get scrore from decoder
            dec_text, dec_uttid, dec_score = self.ps.get_hyp()

            # Detection must have a minimal score to be valid
            if dec_score < self.keyword_score:
                log.msg("I recognized the %s keyword but I think it's a false positive according the %s score" % (self.botname, dec_score))
                return

            # Logs
            self.scores.append(dec_score)
            log.msg("======================")
            log.msg("%s keyword detected" % self.botname)
            log.msg("score: {} (min {}, moy {}, max {})".format(dec_score, min(self.scores), sum(self.scores) / len(self.scores), max(self.scores)))

            # Start voice recording
            Speaker.speak('yes')

            # Start recorder
            self.recorder.set_running_state(True)
Example #3
0
    def run(self):
        """
        Listener main loop
        """
        Speaker.speak("ready")
        self.pipeline.set_state(gst.STATE_PLAYING)

        # Thread loop
        self.loop = gobject.MainLoop()
        self.loop.run()
Example #4
0
    def stop(self):
        """
        Stop listener.
        """
        Speaker.speak('lost_server')

        # Stop everything
        self.pipeline.set_state(gst.STATE_NULL)
        self.recorder.stop()
        if self.loop is not None:
            self.loop.quit()